All posts All posts by this author Paper color Change page color Announcements

Migration of amazon EC2 t1.micro instance to t2.micro instance

Amazon EC2 has been phasing out their t1.micro instances. All new micro instances need to be started as t2.micro instance. Due to the fact that t1.micro type was xen-PV based virtualization, while t2.micro is based on xen HVM virtualization, disks attached to t1.micro cannot be easily converted to t2.micro disks by simple snapshotting and AMI creation. Here are the steps I followed to convert one of my amazon linux t1.micro to t2.micro instance. It will also work for Centos and RHEL linux instances. I did not test converting ubuntu and debian t1.micro, but if you change the appropriate commands, it should work there too.

Before you start, install aws cli tools to your computer. Installation instructions for your operating system can be found here - http://docs.aws.amazon.com/cli/latest/userguide/installing.html

After aws cli tools are installed, configure it following the quick configuration instructions - http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html

Now the actual conversion process:

Step 1:

Install grub in the source ec2 instance:

yum install grub -y

If you are on ubuntu/debian, run

apt-get -y install grub

Step2:

Stop the source instance and take a snapshot of the instance. We will call it snap-abcd.

Step3:

Launch a new ec2 instance named 'worker' in the same availability zone of original instance.

Step4:

Create a new EBS volume called 'pv disk' from the snapshot created in step2(snap-abcd) as /dev/sdm to worker instance.

Step5:

Create a blank EBS volume, let us call it 'hvm disk' and attach it as /dev/sdo to worker instance.

Step6:

Partition the hvm disk:

parted /dev/xvdo --script 'mklabel msdos mkpart primary 1M -1s print quit'
partprobe /dev/xvdo
udevadm settle

Step7:

Check the 'pv disk' volume and reduce the size of original filesystem to speed up net steps

e2fsck -f /dev/xvdm
resize2fs -M /dev/xvdm

Resizing should output something along the lines of: Resizing the filesystem on /dev/xvdm to 288040 (4k) blocks. The filesystem on /dev/xvdm is now 288040 blocks long.

Step8:

Now we will copy the pv disk data to hvm disk byte by byte: dd if=/dev/xvdm of=/dev/xvdo1 bs=4K count=288040

The values of bs and count above should be same as the values of block size and count in Step7.

Step9:

Resize the pv disk:

resize2fs /dev/xvdo1

Step10:

Prepare the pv disk for grub bootloader installation;

mount /dev/xvdo1 /mnt
cp -a /dev/xvdo /dev/xvdo1 /mnt/dev/
rm -f /mnt/boot/grub/*stage*
cp /mnt/usr/*/grub/*/*stage* /mnt/boot/grub/
rm -f /mnt/boot/grub/device.map

Step11:

Install grub in pv disk:

cat <<EOF | chroot /mnt grub --batch
device (hd0) /dev/xvdo
root (hd0,0)
setup (hd0)
EOF

Step12:

Remove the temporary device from pv disk:

rm -f /mnt/dev/xvdo /mnt/dev/xvdo1

Step13:

Configure grub by editing /mnt/boot/grub/menu.lst: * change root (hd0) to root (hd0,0) * add or replace console=* console=ttyS0 to kernel line * Replace root=* with root=LABEL=/ in the kernel line * Add xen_pv_hvm=enable to kernel line

Example /mnt/boot/grub/menu.lst - *ensure the filenames match the files in /mnt/boot:

default=0
timeout=0

title CentOS 6.5
root (hd0,0)
kernel /boot/vmlinuz-2.6.32-431.11.2.el6.x86_64 o root=LABEL=/
console=ttyS0 xen_pv_hvm=enable
initrd /boot/initramfs-2.6.32-431.11.2.el6.x86_64.img

If you are on ubuntu, adjust the title. Your kernel names will be different of course.

Step14:

Update the root (/) entry in /mnt/etc/fstab, as per the sample fstab below:

LABEL=/ / ext4 defaults,noatime 1 1
none /dev/pts devpts gid=5,mode=620 0 0
none /dev/shm tmpfs defaults 0 0
none /proc proc defaults 0 0
none /sys sysfs defaults 0 0

Step15:

Create label on /dev/xvdo1 and unmount it.

e2label /dev/xvdo1 /
sync
umount /mnt

Step16:

create a snapshot from the 'pv disk' EBS volume using command line

    aws ec2 create-snapshot --volume-id vol-xxxxxxxx --description "CentOS 6.5 HVM snapshot"

It will create a new snapshot, let us call it 'snap-mnop'

Step17:

Register an AMI from the snapshot created in step17

    aws ec2 register-image --name "myt2micro hvm" --description "my hvm ami" --architecture x86_64 --root-device-name "/dev/sda1" 
    --virtualization-type hvm --block-device-mappings "[{\"DeviceName\":\"/dev/sda1\",\"Ebs\":{\"SnapshotId\":\"snap-mnop\"}}]"

Replace snap-mnop with your actual snapshot ID.

Final Step:

Launch an ec2 instance from the AMI 'myt2micro hvm', and be happy :)