プライベートクラウドのVMWareの仮想マシンをパブリッククラウドのAWSに移行したときのメモ
環境
- Hypervisor : VMwareESXi5
- OS : Ubuntu 12.04 LTS
手順
1.VMware上の仮想マシンをova形式でエクスポート
移行対象の仮想マシンは複数ディスクなのでova形式でエクスポート
2.エクスポートしたファイルをs3へアップロード
ova形式のファイルをAWSのAMIとしてインポートするためにs3へアップロード
aws s3 cp sample.ova s3://sample-bucket/
3.s3へアップロードしたファイルをAMIとしてインポート
s3へアップロードしたova形式のファイルをAMIとしてインポート
aws ec2 import-image --cli-input-json "{ \"Description\": \"SAMPLE\", \"DiskContainers\": [ { \"Description\": \"SAMPLE\", \"UserBucket\": { \"S3Bucket\": \"sample-bucket\", \"S3Key\" : \"sample.ova\" } } ]}"
4.インポートしたAMIからEC2インスタンスをとりあえず起動
インポートしたAMIは「import-ami-XXXXXXXX」というAMI名で作成されるので、そのAMIを指定してとりえず起動
5.起動したEC2インスタンスを一旦停止
そのままでは起動しなかった、「Get System Log」を確認しても何も出力されていなかったので、一旦停止
6.EC2インスタンスのルートデバイスをデタッチして別インスタンスのボリュームとしてアタッチ
ルートデバイスの設定を変更しないと起動しなさそうなので、一旦デタッチして別のEC2インスタンスの「/dev/sdf」にアタッチ
7.アタッチしたボリュームで設定変更
ルートデバイスをマウントしたEC2インスタンスにログインしてマウント
sudo su - mount -t ext4 -o defaults /dev/xvdf1 /mnt
chroot /mnt
resolv.confの設定をVPCに合わせる
vi /run/resolvconf/resolv.conf -------------------------------------------------------------------------------- # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8) # DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN nameserver 172.30.0.2 search ap-northeast-1.compute.internal --------------------------------------------------------------------------------
パッケージ情報を一旦リセット
rm -r /var/lib/apt/lists mkdir /var/lib/apt/lists mkdir /var/lib/apt/lists/partial apt-get autoclean apt-get clean apt-get update
grub再設定のための準備作業
apt-get install grub ln -s /boot/grub/menu.lst /boot/grub/grub.conf ln -s /boot/grub/grub.conf /etc/grub.conf rm -f /boot/grub/*stage* cp /usr/*/grub/*/*stage* /boot/grub/ rm -f /boot/grub/device.map
chrootを一旦終了
exit
grub再設定
cat <<EOF | grub --batch device (hd0) /dev/xvdf root (hd0,0) setup (hd0) EOF メッセージはこんな感じ -------------------------------------------------------------------------------- Probing devices to guess BIOS drives. This may take a long time. [ Minimal BASH-like line editing is supported. For the first word, TAB lists possible command completions. Anywhere else TAB lists the possible completions of a device/filename. ] grub> root (hd0,0) /dev/xvdf grub> setup (hd0) Checking if "/boot/grub/stage1" exists... yes Checking if "/boot/grub/stage2" exists... yes Checking if "/boot/grub/e2fs_stage1_5" exists... yes Running "embed /boot/grub/e2fs_stage1_5 (hd0)"... 21 sectors are embedded. succeeded Running "install /boot/grub/stage1 (hd0) (hd0)1+21 p (hd0,0)/boot/grub/stage2 /boot/grub/menu.lst"... succeeded Done. --------------------------------------------------------------------------------
menu.lstをマウントしたEC2インスタンスからコピーして編集
※カーネルとかは適宜環境に合わせて設定
cp -p /boot/grub/menu.lst /mnt/boot/grub/menu.lst vi /mnt/boot/grub/menu.lst ---------------------------------------- title Ubuntu 12.04.4 LTS, kernel 3.11.0-15-generic root (hd0,0) kernel /boot/vmlinuz-3.11.0-15-generic root=/dev/xvda1 console=ttyS0 xen_pv_hvm=enable initrd /boot/initrd.img-3.11.0-15-generic title Ubuntu 12.04.4 LTS, kernel 3.11.0-15-generic (recovery mode) root (hd0,0) kernel /boot/vmlinuz-3.11.0-15-generic root=/dev/xvda1 single initrd /boot/initrd.img-3.11.0-15-generic title Ubuntu 12.04.4 LTS, memtest86+ root (hd0) kernel /boot/memtest86+.bin ----------------------------------------
fstabを下記のように設定
vi /mnt/etc/fstab ---------------------------------------- /dev/xvda1/ / ext4 defaults,noatime 1 1 /dev/xvdf /opt ext4 defaults,nobootwait,noatime,comment=cloudconfig 0 2 none /dev/pts devpts git=5 mode=620 0 0 none /dev/shm tmpfs defaults 0 0 none /proc proc defaults 0 0 none /sys sysfs defaults 0 0 ----------------------------------------
マウントしたボリュームに対してgrub-install
grub-install --root-directory=/mnt /dev/xvdf1 ---------------------------------------- Probing devices to guess BIOS drives. This may take a long time. Installing GRUB to /dev/xvdf1 as (hd0,0)... Installation finished. No error reported. This is the contents of the device map /mnt/boot/grub/device.map. Check if this is correct or not. If any of the lines is incorrect, fix it and re-run the script `grub-install'. (fd0) /dev/fd0 (hd0) /dev/xvdf ----------------------------------------
chroot /mnt update-grub
8.設定変更したボリュームを再度インスタンスにマウントしてEC2インスタンスを起動
問題なく起動できることを確認
やってみた感想
手順はざっとこんな感じだったけど、grubの設定関連ではかなり悩まされてしまったのが反省
起動デバイスが認識できずに、起動できなくて何度もトライアンドエラーで時間がかかってしまった
起動してしまえばその後は問題なく動作しているというのは、仮想化技術はすごい!
参考

AWSで仮想化方式 PV(paravirtual) の既存 CentOS AMIを HVMにする - Qiita
t2.microが出た!
t1.microに比べて高性能なのにお安い。
さっそく使おうと思ったら、「あなたのAMIはHVMじゃないから無理です」
的なエラー文でt2.microを選択できなかった。
調べてみるとXenの仮想化に...