Creating HVM AMI from existing PV AMI

aws-ami

Hi everyone, it has been long time since my first post, anyway I will try to post more frequently 🙂

I’m currently working on migrating some AWS EC2 instances to T2 instances type.

The issue is that T2 instances must be launched into an Amazon Virtual Private Cloud (VPC); they are not supported on the EC2-Classic platform. Amazon VPC enables you to launch AWS resources into a virtual network that you’ve defined. You cannot change the instance type of an existing EC2-Classic instance to a T2 instance type.

So actually I created AMIs from old instances I have to be used to launch T2 instances, but the problem here was that the AMIs I created from old EC2 instances was Paravirtualization based AMIs (PV AMI) which isn’t applicable here as T2 instances required HVM based AMIs to be launched from.

So let’s start create HVM AMIs from our previously created PV AMIs

First we need to start an EC2 instance from PV AMI we previously created as following

aws ec2 run-instances –image-id <previously created PV AMI iD> –key-name <ssh key pair> –security-groups default –instance-type <any instance type of your choose that works with PV AMIs>  –count 1

Then install the following packages on EC2 instance you launched from previously created PV AMI as following “Assuming Ubuntu Instance”

sudo apt-get install -y grub-pc grub-pc-bin grub-legacy-ec2 grub-gfxpayload-lists

Then stop the instance and detach the root disk (/dev/sda1) from the instance

aws ec2 stop-instances –instance-ids  <EC2 instance we launched before ID>

// make sure instance is stopped

aws ec2 describe-instance-status –instance-ids <EC2 instance we launched before ID>

aws ec2 detach-volume –volume-id <root partition “/dev/sda1” volume id>

Launch another EC2 instance, but this time let it be a HVM besed EC2 instance, and here I prefer to choose the same OS version to avoid kernel upgrade headache, for my case it will be Ubuntu 12.04, here you can find a list of different Ubuntu AMIs

aws ec2 run-instances –image-id ami-8e7ab6e6 –key-name <ssh key pair> –security-groups default –instance-type <any instance type of your choose that works with PV AMIs>  –count 1

Attach root partition of PV based EC2 instance to HVM based EC2 instance

aws ec2 attach-volume –volume-id <root partition of PV based EC2 instance Volume ID> –instance-id <HVM based EC2 instance ID> –device /dev/sdf

SSH to HVM based EC2 instance and

mkdir -p /mnt/xvdf && mount /dev/xvdf /mnt/xvdf

rsync -avzXA /boot/ /mnt/xvdf/boot/

mount -o bind /dev /mnt/xvdf/dev && mount -o bind /dev/pts /mnt/xvdf/dev/pts && mount -o bind /proc /mnt/xvdf/proc && mount -o bind /sys /mnt/xvdf/sys

chroot /mnt/xvdf

grub-install –no-floppy –recheck –force /dev/xvdf

update-grub2

// Just in case of PV instance kernel version isn’t the same as HVM instance

sudo apt-get install -y linux-virtual linux-image-virtual

Stop the HVM instance, detach both original root of HVM instance “/dev/sda1” and PV root partition from HVM instance “/dev/sdf”

aws ec2 stop-instances –instance-ids  <EC2 instance we launched before ID>

// make sure instance is stopped

aws ec2 describe-instance-status –instance-ids <EC2 instance we launched before ID>

aws ec2 detach-volume –volume-id <original root partiion of HVM instance “/dev/sda1”>

aws ec2 detach-volume –volume-id <PV instance root partition “/dev/sdf”>

Attach PV instance’s root partition to HVM instance as a root partition to the HVM instance i.e. “/dev/sda1”

aws ec2 attach-volume –volume-id <root partition of PV instance Volume ID> –instance-id <HVM instance ID> –device /dev/sda1

And if you have any other Disks attached to PV instance detach them from PV instance and attach them to the HVM instance with the same device mapping so as not to corrupt mount points you’ve created for those disk and not that it seems there is some device mapping in PV instances won’t be applicable in HVM ones “e.g. /dev/sdh1” that require a modification to mount points used in PV instance, i.e. edit /etc/fstab to allign with new device mapping.

It’s time now to start the HVM instance with newly root partition “that partition was used in PV instance”  and make sure everything is working fine 🙂

Finally bundle your newly HVM instance into a HVM AMI to be used whenever you want

aws ec2 create-image –instance-id <HVM instance ID> –name “My HVM AMI” –description “An HVM AMI for my server from an old PV AMI”

That’s it 🙂

References :

Advertisements