2014-02-20 54 views
29

我想複製正在運行的EC2實例(抓取AMI)的磁盤映像並將其導入到虛擬框中或最終使用Vagrant運行。我看到該打包程序(http://www.packer.io/)允許您創建AMI和相應的Vagrant框一起工作,但是當前運行的實例已經運行了兩年多,並且難以複製。將Amazon EC2 AMI轉換爲Virtual或Vagrant框

我想這個問題在devops社區中很常見,但是在我的在線研究中還沒有找到解決方案。有沒有可以讓你完成這項任務的工具?

+0

如果可能,我正在尋找一個易於重複/自動化的過程。 –

+0

對於未來的讀者,也許嘗試https://serverfault.com/questions/374861/converting-an-ec2-ami-to-vmdk-image# –

回答

7

您應該export the instance

有關更多詳細信息,請檢查:How to export a VM from Amazon EC2 to VMware On-Premise

就我個人而言,我在Windows上通過在實例上安裝VMWare轉換器並將本地系統轉換爲VMDK來完成此操作。然後我將VMDK發佈到S3。

+0

導出只有在您最初導入時纔可用,不是嗎? 「如果您以前使用VM Import將虛擬機導入Amazon EC2 ...」 –

6

我只是想指出@Drewness在對原始問題的第一個評論中回答了此問題。我只是添加這個答案,使其更加清晰,因爲答案也鏈接到了錨標籤。鏈接指向以下頁面:How to convert EC2 AMI to VMDK for Vagrant

所以基本上你需要啓用root SSH訪問,例如

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

然後運行的系統複製到本地磁盤映像:

$ ssh -i ~/.ec2/your_key [email protected] 'dd if=/dev/xvda1 bs=1M | gzip' | gunzip | dd of=./ec2-image.raw 

之後,一個新的圖像文件準備文件系統:

$ 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 

現在你需要從複製一切EC2圖像爲空圖像:

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

和Grub安裝:

$ cp /usr/lib/grub/x86_64-pc/stage* /mnt/loop/1/boot/grub/ 

和卸載設備(umount /dev/loop1)和原始磁盤映像轉換爲VMDK圖像:

$ qemu-img convert -f raw -O vmdk vmdk-image.raw final.vmdk 

現在只是創建一個VirtualBox的VM與VMDK圖像裝載在主引導設備。

不幸的是,在這一點上,我無法讓亞馬遜Linux內核在VirtualBox內啓動。