24 October 2011

Create Lucid Lynx EC2 micro ami from Canonical Images with XFS root filesystem and ec2-consistent-snapshot

I read Shlomo Swidler's excellent post Creating Consistent Snapshots of a Live Instance with XFS but couldn't get it working for Ubuntu 10.04 (Lucid Lynx) on a micro EC2 instance. This guide details the steps that I used to create my AMI.

As a starting point I used the latest 64 bit official Canonical Ubuntu 10.04 Lucid Lynx AMI which is currently ami-ad36fbc4 in the us-east-1 zone.

Launch an Ubuntu 10.04 Lucid Lynx instance and note the instance id.
ubuntu@localhost:~$ ami=ami-ad36fbc4
ubuntu@localhost:~$ security_groups=default
ubuntu@localhost:~$ keypair=mykeypair
ubuntu@localhost:~$ instance_type=t1.micro
ubuntu@localhost:~$ zone=us-east-1c
ubuntu@localhost:~$ ec2-run-instances $ami --availability-zone $zone -t $instance_type -g $security_groups -k $keypair
ubuntu@localhost:~$ instance_id=i-XXXXXXXX

Create an EBS volume and attach it to the running instance.
ubuntu@localhost:~$ ec2-create-volume --size 10 -z $zone
ubuntu@localhost:~$ xfs_volume=vol-XXXXXXXX
ubuntu@localhost:~$ ec2-attach-volume $xfs_volume --instance $instance_id --device /dev/sdm

Wait until the instance is running then ssh to it.
ubuntu@localhost:~$ host=$(ec2-describe-instances $instance_id | awk '-F\t' '$2 == instance_id { print $4 }' instance_id=${instance_id} )
ubuntu@localhost:~$ ssh -i mykeypair ubuntu@${host}

Update and upgrade existing packages and install ec2-consistent-snapshot.
ubuntu@ec2:~$ sudo apt-get update && sudo apt-get -y upgrade
ubuntu@ec2:~$ sudo add-apt-repository ppa:alestic
ubuntu@ec2:~$ sudo apt-get update
ubuntu@ec2:~$ sudo apt-get install -y ec2-consistent-snapshot

Create and mount the xfs filesystem.
ubuntu@ec2:~$ sudo apt-get install -y xfsprogs
ubuntu@ec2:~$ sudo mkfs.xfs /dev/sdm
ubuntu@ec2:~$ sudo mkdir -p -m 000 /vol/xfs
ubuntu@ec2:~$ sudo mount -t xfs /dev/sdm /vol/xfs

Copy the contents of the instance’s root filesystem to the xfs EBS volume.
ubuntu@ec2:~$ sudo rsync -avx --exclude /vol / /vol/xfs

Ubuntu looks for a disk labelled 'cloudimg-rootfs' when booting but xfs labels have a 12 character limit. So modify fstab and grub to work using partition names.
ubuntu@ec2:~$ sudo vi /vol/xfs/etc/fstab
replace
LABEL=cloudimg-rootfs / ext3 defaults 0 0
with
/dev/sda1 / xfs defaults 0 0
ubuntu@ec2:~$ sudo vi /vol/xfs//boot/grub/menu.lst
replace all occurrences of LABEL=cloudimg-rootfs with /dev/sda1

Unmount the xfs volume.
ubuntu@ec2:~$ sudo umount /vol/xfs

Snapshot the xfs volume and register as an AMI.
ubuntu@localhost:~$ ec2-create-snapshot $xfs_volume
ubuntu@localhost:~$ snapshot=snap-XXXXXXXX
ubuntu@localhost:~$ kernel=aki-427d952b
ubuntu@localhost:~$ description="Ubuntu 10.04 Lucid formatted with XFS"
ubuntu@localhost:~$ ami_name=ubuntu-10.04-64-bit-ami-ad36fbc4-xfs
ubuntu@localhost:~$ ec2-register --snapshot $snapshot --kernel $kernel --description="$description" --name=$ami_name --architecture x86_64 --root-device-name /dev/sda1
ubuntu@localhost:~$ ami=ami-XXXXXXXX

Launch an instance.
ubuntu@localhost:~$ ec2-run-instances $ami -t $instance_type -g $security_groups -k $keypair