Last active
August 29, 2015 13:57
-
-
Save greenmang0/9680907 to your computer and use it in GitHub Desktop.
Create a custom AMI using Canonical provided images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## Refer: http://alestic.com/2010/01/ec2-ebs-boot-ubuntu ## | |
# create directory structure | |
mkdir -p $HOME/image | |
# download, extract and mount image | |
cd $HOME | |
wget "http://cloud-images.ubuntu.com/releases/precise/release/ubuntu-12.04-server-cloudimg-amd64.tar.gz" | |
tar xf ubuntu-12.04-server-cloudimg-amd64.tar.gz | |
sudo mount -o loop precise-server-cloudimg-amd64.img $HOME/image | |
# define some variables | |
IMGDIR="$HOME/image" | |
ARCH="x86_64" | |
SIZE=8 # root disk size in gigs | |
NOW=$(date +%Y%m%d-%H%M) | |
PREFIX="ubuntu-12.04-precise-server-amd64-$NOW" | |
DESCRIPTION="Ubuntu 12.04 Precise Server amd64 $NOW" | |
EBSOPTS="--block-device-mapping /dev/sdb=ephemeral0 --kernel=aki-88aa75e1" | |
# export ec2 creds | |
export EC2_CERT=$(echo /home/ubuntu/.ec2/cert-*.pem) | |
export EC2_PRIVATE_KEY=$(echo /home/ubuntu/.ec2/pk-*.pem) | |
# create & attach volume with ext4 fs | |
VOLUMEID=$(ec2-create-volume --size $SIZE --availability-zone us-east-1b | cut -f2) | |
INSTANCEID=$(wget -qO- http://instance-data/latest/meta-data/instance-id) | |
ec2-attach-volume --device /dev/sdc --instance "$INSTANCEID" "$VOLUMEID" | |
while [ ! -e /dev/xvdc ]; do echo -n .; sleep 1; done | |
sudo mkfs.ext4 -F /dev/xvdc # 12.04 FS image is ext4 | |
# mount newly created volume and put 12.04 image on it | |
EBSIMAGE=$IMGDIR-ebs | |
mkdir -p $EBSIMAGE | |
sudo mount /dev/xvdc $EBSIMAGE | |
sudo tar -Svcf - -C $IMGDIR . | sudo tar xvf - -C $EBSIMAGE | |
sudo umount $EBSIMAGE | |
# detach volume and create a snapshot | |
ec2-detach-volume "$VOLUMEID" | |
SNAPID=$(ec2-create-snapshot "$VOLUMEID" | cut -f2) | |
ec2-delete-volume "$VOLUMEID" | |
while ec2-describe-snapshots "$SNAPID" | grep -q pending | |
do | |
echo -n . | |
sleep 1 | |
done | |
# create ami using snapshot | |
ec2-register --architecture $ARCH --name "$PREFIX" --description "$DESCRIPTION" $EBSOPTS --snapshot "$SNAPID" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment