Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Amazon EC2 AMI to Virtual or Vagrant box

I'd like to copy the disk image of a running EC2 instance (grab the AMI) and import it into virtual box or eventually have it run using Vagrant. I saw that packer (http://www.packer.io/) allows you to create AMI's and corresponding Vagrant boxes to work together, however the running instance I currently have has been running for over two years and would be difficult to replicate.

I imagine that this issue is common in the devops community however have not found a solution in my research online. Are there any tools out there that let you accomplish this task?

like image 733
Justin W. Avatar asked Feb 20 '14 22:02

Justin W.


People also ask

Can I run virtual box on EC2 instance?

You will have to either use an emulator as David suggests, or somehow convert your VirtualBox image into a format supported by Amazon's VM Import tool — essentially convert your VM to run directly as its own EC2 instance.

Is an AMI a virtual machine?

An Amazon Machine Image (AMI) is a special type of virtual appliance that is used to create a virtual machine within the Amazon Elastic Compute Cloud ("EC2"). It serves as the basic unit of deployment for services delivered using EC2.

Can I change the AMI of EC2 instance?

The answer is that you cannot replace the AMI for an existing EC2 instance. However, you can replace the root volume with a new volume which is basically the same thing. That new root volume can come from another EC2 instance.


2 Answers

I just wanted to note that @Drewness answered this question in the first comment to the original question. I'm just adding this answer to make it more clear because the answer is link to in an anchor tag too. The link points toward the following page: How to convert EC2 AMI to VMDK for Vagrant.

So basically you need to enable root SSH access, e.g.

$ 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> 

Then 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 

After that 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.

Unfortunately at this point I could not get the Amazon Linux kernel to boot inside VirtualBox.

like image 51
BillMan Avatar answered Sep 22 '22 14:09

BillMan


You should export the instance.

For more details, check: How to export a VM from Amazon EC2 to VMware On-Premise.

Personally I've done this on a Windows box by installing VMWare converter on the instance and converting the local system to a VMDK. Then I posted the VMDK to S3.

like image 24
John Avatar answered Sep 21 '22 14:09

John