Skip to content

Instantly share code, notes, and snippets.

@fredrik
Forked from fairchild/bundle-to-ebs.sh
Created December 14, 2009 07:16
Show Gist options
  • Save fredrik/255858 to your computer and use it in GitHub Desktop.
Save fredrik/255858 to your computer and use it in GitHub Desktop.
#!/bin/bash -xe
# from http://gist.github.com/249915 by fairchild
if [ -z $1 ]; then
echo "usage: $0 INSTANCE_ID"
exit
fi
EBS_DEVICE='/dev/sdh'
INSTANCE_ID=$1
AKI=${2:-'aki-02486376'}
ARI=${3:-'ari-fa4d668e'}
ARCH=${4:-'i386'}
SIZE=${5:-10}
AZ=${6:-'eu-west-1b'}
NAME=${7:-"ami-from-$INSTANCE_ID"}
DESCRIPTION=${8:-''}
VOL_ID=`ec2-create-volume --size $SIZE -z $AZ |cut -f2|grep vol`
echo $VOL_ID
#sample output => VOLUME vol-0500xxx 10 us-east-1d creating 2009-12-05T08:07:51+0000
ec2-attach-volume $VOL_ID -i $INSTANCE_ID -d $EBS_DEVICE
#sample output => ATTACHMENT vol-0500fxxx i-c72f6aaf /dev/sdh attaching 2009-12-05T08:14:17+0000
echo "run the server side script on $INSTANCE_ID, and click enter when done:"
read waiting
ec2-detach-volume $VOL_ID
#sample output => ATTACHMENT vol-0500fb6c i-c72f 6aaf /dev/sdh detaching 2009-12-05T08:14:17+0000
SNAP=`ec2-create-snapshot $VOL_ID|cut -f2|grep snap`
echo $SNAP
#sample output => SNAPSHOT snap-415b3xxx vol-0500xxx pending 2009-12-05T09:04:27+0000 424024621003 10 Ubuntu 9.10 base 32bit server image
# TODO: while grep 'pending', sleep 1 (or while not grep 'completed')
ec2-register --snapshot $SNAP --kernel $AKI --ramdisk $ARI --description="$DESCRIPTION" --name="$NAME" --architecture $ARCH --root-device-name /dev/sda1
#sample output => IMAGE ami-5007exxx
# getting error:
# Client.IncorrectInstanceState: Snapshot 'snap-78df3711 is not 'completed'
# TODO: delete volume.
#!/bin/bash
# from by http://gist.github.com/249915 by fairchild
# Run this script on the instance to be bundled
# tested with Canonical Ubuntu 9.10 base ami
EBS_DEVICE=${1:-'/dev/sdh'}
IMAGE_DIR=${2:-'/mnt/tmp'}
EBS_MOUNT_POINT=${3:-'/mnt/ebs'}
mkdir -p $EBS_MOUNT_POINT
mkfs.ext3 ${EBS_DEVICE}
mount ${EBS_DEVICE} $EBS_MOUNT_POINT
#make a local working copy
mkdir /mnt/tmp
rsync --stats -av --exclude /root/.bash_history --exclude /home/*/.bash_history --exclude /etc/ssh/ssh_host_* --exclude /etc/ssh/moduli --exclude /etc/udev/rules.d/*persistent-net.rules --exclude /var/lib/ec2/* --exclude=/mnt/* --exclude=/proc/* --exclude=/tmp/* / $IMAGE_DIR
# ? => also exclude /sys, /dev
#ensure that ami init scripts will be run
chmod u+x $IMAGE_DIR/etc/init.d/ec2-init-user-data
#clear out log files
cd $IMAGE_DIR/var/log
for i in `ls ./**/*`; do
echo $i && echo -n> $i
done
cd $IMAGE_DIR
tar -cSf - -C ./ . | tar xvf - -C $EBS_MOUNT_POINT
#NOTE, You could rsync / directly to EBS_MOUNT_POINT, but this tar trickery saves some space in the snapshot
umount $EBS_MOUNT_POINT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment