Skip to content

Instantly share code, notes, and snippets.

@neilellis
Last active December 19, 2015 15:38
Show Gist options
  • Save neilellis/5977553 to your computer and use it in GitHub Desktop.
Save neilellis/5977553 to your computer and use it in GitHub Desktop.
Builds a new Eucalyptus image based on an existing *CentOS* image combined with the contents of running modified instances. Then adds it to the images in Eucalyptus. This is really useful if you want to constantly make changes to your base images. Also includes support for a custome run_once.sh script which will be run on first boot. Please read…
#!/bin/sh
################# CHANGE THESE! ######################
IMAGE=centos-ops
RAMDISK_IMAGE=eri-A3493391
KERNEL_IMAGE=eki-E7E73878
INSTANCE_KEY=~/credentials/admin/admin.private
. ~/credentials/admin/eucarc
######################################################
if [ -f ${IMAGE}.img ]
then
echo
else
echo "Could not find ${IMAGE}.img, this should be your base filesystem image."
exit -1
fi
if [ -z "$1" ]
then
echo "Usage: rebase.sh <running-instance-ip> [ <image-flavor> ] "
exit -1
fi
if [ -z "$2" ]
then
FLAVOR=default
else
FLAVOR=$2
fi
BUCKET=${IMAGE}-images
MOUNT_DIR=img-mount
DIR=$(pwd)
IP=$(hostname -i)
TIME=$(date +%F-%H-%M-%S)
FINAL_IMAGE_NAME=${IMAGE}-${FLAVOR}-${TIME}
echo "Simply make sure you have a file called ${IMAGE}.img edit the scripts variables (especially the ramdisk and kernel images), remove the exit -1 line from the script and away you go."
exit -1
mkdir -p $MOUNT_DIR
mkdir tmp
rm -rf tmp/*
cp ${IMAGE}.img tmp/${IMAGE}-${TIME}.img
mount -o loop tmp/${IMAGE}-${TIME}.img $MOUNT_DIR
mkdir -p $MOUNT_DIR/proc
mkdir -p $MOUNT_DIR/sys
mkdir -p $MOUNT_DIR/dev
mount -o bind /proc $MOUNT_DIR/proc
mount -o bind /sys $MOUNT_DIR/sys
mount -o bind /dev $MOUNT_DIR/dev
scp -i ${INSTANCE_KEY} root@$1:~/.ssh/id_rsa.pub /tmp/remote.pub
if grep "$(cat /tmp/remote.pub)" ~/.ssh/authorized_keys
then
echo "Remote already has access to local."
else
cat /tmp/remote.pub >> ~/.ssh/authorized_keys
fi
ssh -i ${INSTANCE_KEY} root@$1 <<EOF
if [ run_once.log ]
then
chkconfig runonce on
rm run_once.log
fi
cd /
tar -zcvpf /media/ephemeral0/all.tar.gz --preserve-permissions --preserve-order --directory / --exclude=mnt --exclude=proc --exclude=tmp --exclude=dev --exclude=sys --exclude=media /
scp /media/ephemeral0/all.tar.gz root@${IP}:${DIR}/tmp
EOF
cd $MOUNT_DIR
cp ../tmp/all.tar.gz .
if [ -f root/run_once.sh ]
then
cp root/run_once.sh ../tmp
fi
rm -rf boot etc home lib lib64 opt bin sbin root selinux srv usr var
tar -zxpv --preserve-permissions --same-owner -f all.tar.gz
rm -f all.tar.gz
cat > etc/init.d/runonce << 'EOF'
#!/bin/bash
#
# runonce run once
#
# chkconfig: 3 98 98
# description: Run on first boot
case "$1" in
start)
cd /root
bash /root/run_once.sh 2>&1 > /root/run_once.log
chkconfig runonce off
;;
stop|status|restart|reload|force-reload)
# do nothing
;;
esac
EOF
cd etc/rc3.d
ln -s ../init.d/runonce S98runonce
cd -
#Force networking init on next reboot
sed -i 's/HWADDR=".*"//' etc/sysconfig/network-scripts/ifcfg-eth0
rm -f etc/udev/rules.d/70-persistent-net.rules
if [ -f ../tmp/run_once.sh ]
then
cp ../tmp/run_once.sh root
fi
cd ..
umount $MOUNT_DIR/proc
umount $MOUNT_DIR/sys
umount $MOUNT_DIR/dev
umount $MOUNT_DIR
rmdir $MOUNT_DIR
MANIFEST_TMP=$(euca-bundle-image -r x86_64 -i $(ls tmp/*.img) --ramdisk ${RAMDISK_IMAGE} --kernel ${KERNEL_IMAGE} | tail -1 | cut -c15-)
MANIFEST_NAME=`echo $(euca-upload-bundle -b ${BUCKET} -m $MANIFEST_TMP | tail -1 | cut -c9-)`
ID=$(euca-register -n ${FINAL_IMAGE_NAME} $MANIFEST_NAME | tail -1 | cut -c7-)
euca-modify-image-attribute -l $ID -a all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment