A Process of Making a Custom HVM Virtualized AMI for EC2
Jan 7th , 2012
Forewarning: This post is about a process; it is not meant to give you a complete working basis to create a hvm virtualization AMI. It is here to give you the information and or tools to build your own.
Required Reading Material.
Requirements.
Step 1
booting an instance. 1
2
3
4
ec2-run-instances ami-0da96764 -k ssh_key -t cc1.4xlarge -H
Type ReservationID Owner Groups Platform
RESERVATION r-f5743394 000000000000 default
INSTANCE i-de453ebc ami-0da96764 pending ssh_key 0 cc1.4xlarge 2012-01-07T23:01:54+0000 us-east-1c monitoring-disabled ebs hvm xen sg-7baf4812 default
create your EBS Root volume 1
2
ec2-create-volume -z us-east-1c -s 8
VOLUME vol-03aff26e 8 us-east-1c creating 2012-01-07T23:03:25+0000
attach the volume to the instance as /dev/sdp 1
2
ec2-attach-volume vol-03aff26e -d /dev/sdp -i i-de453ebc
ATTACHMENT vol-03aff26e i-de453ebc /dev/sdp attaching 2012-01-07T23:05:26+0000
SSH to the instance in question as ec2-user@$hostname
obtain root priviledges and install screen 1
2
3
sudo -s
yum install screen
screen -S ami
install the amazon api and ami tools 1
2
3
4
5
mkdir -p /mnt/work && cd /mnt/work
wget http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools.zip
unzip ec2-ami-tools.zip
wget http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip
unzip ec2-api-tools.zip*
configure your environment 1
2
3
4
5
6
7
8
9
10
11
12
export EC2_BASE = "/mnt/work"
export EC2_AMIHOME = "${EC2_BASE}/ec2-ami-tools-1.4.0.5"
export EC2_HOME = "${EC2_BASE}/ec2-api-tools-1.5.2.3"
export PATH = ${ EC2_AMIHOME } /bin:${ EC2_HOME } /bin:${ PATH }
export EC2_CERT = "/mnt/work/.ec2/cert-example.pem"
export EC2_PRIVATE_KEY = "/mnt/work/.ec2/pk-example.pem"
export EC2_SECRET_KEY = "secret_key"
export EC2_ACCESS_KEY = "access_key"
export SOURCE_AMI_NAME = "example_ami_name"
export SOURCE_AMI_MANIFEST = "${SOURCE_AMI_NAME}.manifest.xml"
export EC2_AMIS = "${EC2_BASE}/amis"
export EBS_DEV = "/dev/xvdp1"
download your s3 ami locally 1
2
3
4
5
ln -sf $EC2_AMIHOME /lib/ec2 $EC2_HOME /lib/ec2
temp_dir = $( mktemp -d)
mkdir -p ${ EC2_AMIS }
cd ${ EC2_AMIS }
ec2-download-bundle -b example_bucket -a ${ EC2_ACCESS_KEY } -s ${ EC2_SECRET_KEY } -k ${ EC2_CERT } -m ${ SOURCE_AMI_MANIFEST }
unbundle your s3 ami and mount it 1
2
3
4
5
6
ec2-unbundle -k ${ EC2_PRIVATE_KEY } -m ${ SOURCE_AMI_MANIFEST } -d ${ temp_dir }
cd ${ EC2_BASE }
export EC2_AMI_IMAGE = "${temp_dir}/${SOURCE_AMI_NAME}"
export EC2_SOURCE_MOUNT = $( mktemp -d)
export EC2_DEST_MOUNT = $( mktemp -d)
mount -o loop ${ EC2_AMI_IMAGE } ${ EC2_SOURCE_MOUNT }
Please send suggestions on how to improve this to /dev/null .
partition out your ebs volume 1
2
3
4
5
6
7
8
9
10
fdisk /dev/xvdp
n
p
1
<enter>
<enter>
a
1
w
q
format the EBS volume and mount it. No filesystem checks desired. 1
2
3
4
blockdev --rereadpt /dev/xvdp
mkfs -t ext4 -L rootfs ${ EBS_DEV }
tune2fs -i 0 -c 0 ${ EBS_DEV }
mount ${ EBS_DEV } ${ EC2_DEST_MOUNT }
copy your local S3 AMI to the EBS volume 1
rsync -aHAS ${ EC2_SOURCE_MOUNT } / ${ EC2_DEST_MOUNT } /
chroot into your EBS root and download the linux kernel 1
2
3
4
5
cp /boot/config* ${ EC2_DEST_MOUNT }
mount -o bind /dev ${ EC2_DEST_MOUNT } /dev
chroot ${ EC2_DEST_MOUNT }
cd /usr/src && curl http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.39.4.tar.bz2 | tar jxf -
ln -s /usr/src/linux-2.6.39.4 linux
configure your kernel 1
2
3
4
5
6
cd /usr/src/linux
cp /config* .config
make bzImage -j16
make modules -j16
make modules_install
cp arch/x86/boot/bzImage /boot/vmlinuz-hvm
Configure /boot/grub/menu.lst similar to how you see provided by amazon; but replace the kernel and initrd with your own.
Use your favorite package manager to install grub. The following should work provided no ephemeral disks are attached.
install grub on the disk 1
2
3
4
grub
root ( hd1,0)
setup ( hd1)
quit
Exit out of your chroot and unmount everything
unmount your ebs root 1
2
umount ${ EC2_DEST_MOUNT } /dev
umount ${ EC2_DEST_MOUNT }
Now locally create a snapshot of your EBS root in case something goes wrong and you want to re-attach it; fix it; re-register.
snapshot your ebs-root 1
2
ec2-create-snapshot vol-03aff26e
SNAPSHOT snap-9239e6f6 vol-03aff26e pending 2012-01-07T23:37:08+0000 621517282122 8
stop your instance while it’s snapshotting 1
2
ec2-stop-instances i-de453ebc
INSTANCE i-de453ebc running stopping
Verify the instance is stopped
list your instance stopping 1
2
3
4
5
6
ec2-describe-instances i-de453ebc -H
Type ReservationID Owner Groups Platform
RESERVATION r-f5743394 621517282122 default
INSTANCE i-de453ebc ami-0da96764 stopped ssh_key 0 cc1.4xlarge 2012-01-07T23:01:54+0000 us-east-1c monitoring-disabled ebs hvm xen sg-7baf4812 default
BLOCKDEVICE /dev/sda1 vol-a9acf1c4 2012-01-07T23:39:34.000Z
BLOCKDEVICE /dev/sdp vol-03aff26e 2012-01-07T23:39:34.000Z
detach the old root volume provided by amazon and your ebs root volume you made 1
2
3
4
ec2-detach-volume vol-a9acf1c4
ATTACHMENT vol-a9acf1c4 i-de453ebc /dev/sda1 detaching 2012-01-07T23:39:34+0000
ec2-detach-volume vol-03aff26e
ATTACHMENT vol-03aff26e i-de453ebc /dev/sdp detaching 2012-01-07T23:39:34+0000
attach volume 1
2
ec2-attach-volume vol-03aff26e -i i-de453ebc -d /dev/sda1
ATTACHMENT vol-03aff26e i-de453ebc /dev/sda1 attaching 2012-01-07T23:42:38+0000
create your AMI 1
2
ec2-create-image i-de453ebc -n "fail"
IMAGE ami-ed9d4a84
Once it’s done preparing the AMI; boot it and hope for success. If you have a problem review the Console log and research. I suggest reviewing Amazon’s Kernel configuration and Ubuntu’s Kernel configuration for the HVM instance types if you have any problems; most of all good luck!