To convert ec2 AMI to VMDK for vagrant
Follow these steps and you will able to convert ec2
So basically you need to enable root SSH access
For example:
$ 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>
After this copy the running system to a local disk image:
$ ssh -i ~/.ec2/your_key [email protected] 'dd if=/dev/xvda1 bs=1M | gzip' | gunzip | dd of=./ec2-image.raw
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
Now you 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/
and install Grub:
$ cp /usr/lib/grub/x86_64-pc/stage* /mnt/loop/1/boot/grub/
and 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
Now just create a VirtualBox VM with the vmdk image mounted as the primary boot device. Then it boots inside QEMU and copies the path /dev/xvda then it will boot in QEMU without any problem.