How to Convert EC2 AMI to VMDK for Vagrant

No items found.

First launch an EC2 instance with the desired AMI. Now we’ll need to enable root ssh access.

$ sudo perl -i -pe 's/#PermitRootLogin .*/PermitRootLogin without-password/' /etc/ssh/sshd_config$ sudo perl -i -pe 's/.*(ssh-rsa .*)/\1/' /root/.ssh/authorized_keys$ sudo /etc/init.d/sshd reload # optional command<br>(Thanks to Gurjeet Singh)Now we need to copy the running system to a local disk image:$ ssh -i ~/.ec2/your_key root@ec2-XX-XX-XX-X.compute-1.amazonaws.com 'dd if=/dev/xvda1 bs=1M | gzip' | gunzip | dd of=./ec2-image.raw    Now to prepare a filesystem on a new image file:$ dd if=/dev/zero of=vmdk-image.raw bs=1M count=10240 # create a 10gb image file$ losetup -fv vmdk-image.raw # mount as loopback device$ cfdisk /dev/loop0 # create a bootable partition, write, and quit$ losetup -fv -o 32256 vmdk-image.raw # mount the partition with an offset$ fdisk -l -u /dev/loop0 # get the size of the partition$ mkfs.ext4 -b 4096 /dev/loop1 $(((20971519 - 63)*512/4096)) # format using the END number<br>Now we need to copy everything from the EC2 image to the empty image:$ losetup -fv ec2-image.raw$ mkdir -p /mnt/loop/1 /mnt/loop/2 # create mount points$ mount -t ext4 /dev/loop1 /mnt/loop/1 # mount vmdk-image$ mount -t ext4 /dev/loop2 /mnt/loop/2 # mount ami-image$ cp -a /mnt/loop/2/* /mnt/loop/1/    Now install grub:$ cp /usr/lib/grub/x86_64-pc/stage* /mnt/loop/1/boot/grub/<br>Check /boot/grub/menu.lst and ensure everything is correct (you will probably need to use root (hd0,0) to make it work with grub).Now we can unmount the device (umount /dev/loop1) and convert the raw disk image to a vmdk image.$ qemu-img convert -f raw -O vmdk vmdk-image.raw final.vmdk<br>Now just create a VirtualBox VM with the vmdk image mounted as the primary boot device.Unfortunately at this point I could not get the Amazon Linux kernel to boot inside VirtualBox. I tried recompiling without Xen support, but still kept running into one problem or another. The only way that I was able to boot into a working system was to copy over my Ubuntu 11.10 kernel and initrd images. If anyone is able to make the Amazon Linux kernel run inside VirtualBox, please leave a comment here or on my ServerFault question.Did this post make sense to you? Then check out our technical job openings here. Cover photo by Mario Calvo

Subscribe to the Smashing Boxes Blog today!

Smashing Boxes is a creative technology lab that partners with clients, taking an integrated approach to solving complex business problems. We fuse strategy, design, and engineering with a steady dose of entrepreneurial acumen and just the right amount of disruptive zeal. Simply put, no matter what we do, we strive to do it boldly. Let's talk today!

Related Posts

No items found.