Skip to content

Instantly share code, notes, and snippets.

@krisr
Forked from fairchild/bundle-to-ebs.sh
Created December 9, 2009 02:12
Show Gist options
  • Save krisr/252194 to your computer and use it in GitHub Desktop.
Save krisr/252194 to your computer and use it in GitHub Desktop.
#!/bin/bash -xe
# Run this script from machine with the credintials and the latest api tools installed
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:-`date "+%Y-%m-%d-%H-%M"`}
DESCRIPTION=${8:-''}
VOL_ID=`ec2-create-volume --size $SIZE -z $AZ |cut -f2|grep vol`
echo $VOL_ID
# 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
#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
# ATTACHMENT vol-0500fb6c i-c72f 6aaf /dev/sdh detaching 2009-12-05T08:14:17+0000
SNAP=`ec2-create-snapshot $VOL_ID --description $NAME|cut -f2|grep snap`
echo $SNAP
# 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
#IMAGE ami-5007exxx
#!/bin/bash
# Run this script on the instance to be bundled
EBS_DEVICE=${1:-'/dev/sdh'}
mkdir /mnt/ebs
mkfs.ext3 ${EBS_DEVICE}
mount ${EBS_DEVICE} /mnt/ebs
#make a local working copy
mkdir /mnt/tmp
rsync -av --exclude=/mnt/* --exclude=/proc/* / /mnt/tmp/
#remove files that should be instance specific
rm /mnt/tmp/root/.bash_history
rm /mnt/tmp/home/*/.bash_history
rm /mnt/tmp/etc/ssh/ssh_host_*
rm /mnt/tmp/etc/ssh/moduli
rm /mnt/tmp/etc/udev/rules.d/*persistent-net.rules
#ensure that ami init scripts will be run
chmod u+x /mnt/ebs/etc/init.d/ec2-init-user-data
rm /mnt/ebs/var/lib/ec2
#clear out log files
cd /mnt/tmp/var/log
for i in `ls ./**/*`; do
echo $i && echo -n> $i
done
cd /mnt/tmp
tar -cSf - -C ./ . | tar xvf - -C /mnt/ebs/
#NOTE, rsync could be used, but this tar trickery saves some space in the snapshot
umount /mnt/ebs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment