rrichards
8/22/2013 - 5:33 AM

bundle-to-ebs.sh

#!/bin/bash -xe

EBS_DEVICE='/dev/sdh'
INSTANCE_ID=$1
AKI=${2:-'aki-5f15f636'}
ARI=${3:-'ari-0915f660'}
ARCH=${4:-'i386'}
SIZE=${5:-10}
AZ=${6:-'us-east-1d'}
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

ec2-register --snapshot $SNAP --kernel $AKI --ramdisk $ARI --description="$DESCRIPTION" --name="$NAME" --architecture $ARCH --root-device-name /dev/sda1
#sample output => IMAGE	ami-5007exxx
#!/bin/bash
# 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 -p $IMAGE_DIR
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

#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