Introduction
Well there is already an update to Chef’s Ohai library. At first I thought, “Oh no, I have to generate another EC2 image”. But then I remember reading that you can update and clone a running EBS boot image.
One of the cool features of using an Amazon EC2 instance that boots from an EBS Snapshot is that its easy to create new boot images from an existing running EC2 instance, assuming that you are running an EC2 instance that is itself bootable from an EBS Image.
Prerequisites
The following expects that you have a recent copy of the Amazon ec2-api-tools on the instance and that you have recent version of the ec2-api-tools on your host development system.
Start up an instance, make changes
Start up an instance you can use as a base, for instance the one we created in Using the Official Opscode 0.8.x Gems to build EC2 AMI Chef Client and Server
Get the name of the instance
First you will need the instance name of your instance you want to copy. You can use Elasticfox or other tool. Or run the following command on the instance:
wget -qO- http://instance-data/latest/meta-data/instance-id
On another host
The rest of the instructions will be run on your host development system (not the system you are copying). This makes it so you don’t have to put your Amazon Certs onto the machine you are cloning (you don’t want those keys to end up on the cloned image)
Create some shell defines
To make the instructions easier make some defines we’ll use in commands. Tune them for your environment.
# This will be the instance id of the running instance you want to clone instanceid=i-07202042 # Some info for creating the name and description codename=karmic release=9.10 tag=server region=us-west-1 availability_zone=us-west-1a # Make sure you set this as appropriate # 64bit arch=x86_64 arch2=amd64 #32bit arch=i386 arch2=i386 now=$(date +%Y%m%d-%H%M) # Make this specific to what you are making prefix=runa-chef-0.8.4-ubuntu-$release-$codename-$tag-$arch-$now description="Runa Chef 0.8.4 Ubuntu $release $codename $tag $arch $now"
Get the info about your running instance
Use Elasticfox or your favorite tool or the following command to get the volume id of the instance
ec2-describe-instances --region $region "$instanceid" > /tmp/instance_info volumeid=$(egrep ^BLOCKDEVICE /tmp/instance_info | cut -f3); echo $volumeid kernel=$(egrep ^INSTANCE /tmp/instance_info | cut -f13); echo $kernel ramdisk=$(egrep ^INSTANCE /tmp/instance_info | cut -f14) ;echo $ramdisk
Shutdown the instance
Its not clear if you really need to do this. But when I first tried doing it without shuting down the instance, the snapshots took forever.
Create a new snapshot
snapshotid=$(ec2-create-snapshot -region $region -d "$description" $volumeid | cut -f2)
Register the new image
ec2reg --region $region -s $snapshotid -a $arch --kernel $kernel --ramdisk $ramdisk -d "$description" -n "$prefix"
The result of this command will be the ami image name. After this completes, the image and snapshot can be used to create new instances.
Cool.
You can create bootable EBS volumes that are formatted with the XFS filesystem, allowing you to “freeze” the filesystem, call the ec2-create-snapshot command, and the “unfreeze” the filesystem. This way you can create a consistent, bootable snapshot of the live-running instance.
Here’s my article explaining how to do this:
http://www.shlomoswidler.com/2010/01/creating-consistent-snapshots-of-live.html