Skip to content

Instantly share code, notes, and snippets.

@jsianes
Last active October 15, 2015 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsianes/77c8b61ca20d399114dd to your computer and use it in GitHub Desktop.
Save jsianes/77c8b61ca20d399114dd to your computer and use it in GitHub Desktop.
Shell script to register a new AMI from Mirage Unikernel into AWS (N.Virginia region)
#!/bin/bash
#
# Shell script to register a new AMI from Mirage Unikernel into AWS (N.Virginia region)
# Developed by: Javier Sianes - jsianes@gmail.com
#
usage() {
echo "Incorrect parameters."
echo "Usage:"
echo ""
echo "$0 KERNEL-FILE AMI-NAME DEVICE-ID [AKI]"
echo ""
echo "Example: $0 www-mirage.xen MIRAGE-WWW-SERVER /dev/xvdf aki-4e7d9527"
echo ""
exit 1
}
check_exit() {
if [ $? -eq 0 ];then echo "OK"; else echo "FAIL"; sanity_check; exit 1; fi
}
sanity_check() {
sudo umount $TMPDIR >/dev/null 2>/dev/null
sudo rm -f $TMPFILE >/dev/null 2>/dev/null
if [ -d $TMPDIR ];then sudo rm -rf $TMPDIR >/dev/null 2>/dev/null; fi
if [ "$VOLUMEID" != "" ]
then
ec2-detach-volume $VOLUMEID >/dev/null 2>/dev/null; sleep 10
ec2-delete-volume $VOLUMEID >/dev/null 2>/dev/null; sleep 5
VOLUMEID=""
fi
if [ "$SNAPSHOTID" != "" ]
then
if [ $AMIREGISTERED -eq 0 ]
then
ec2-delete-snapshot $SNAPSHOTID >/dev/null 2>/dev/null; sleep 5
SNAPSHOTID=""
fi
fi
}
trap sanity_check EXIT
if [ $# -lt 3 ];then usage; fi
KERNEL=$1
AMINAME=$2
DEVICEID=$3
if [ $# -lt 4 ];then AKI="aki-4e7d9527";else AKI=$4; fi
VOLUMEID=""
SNAPSHOTID=""
AMIREGISTERED=0
TMPFILE=`mktemp`
TMPDIR=`mktemp -d`
if [ ! -f $KERNEL ];then echo "Error: kernel file not found"; sanity_check; exit 1; fi
echo -n "-- creating volume ... "
ec2-create-volume --size 1 -z $(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone) >$TMPFILE 2>/dev/null; check_exit
VOLUMEID=`cat $TMPFILE | awk '{ print $2; }'`; sleep 15;
echo -n "-- attaching $VOLUMEID to the instance ... "
ec2-attach-volume $VOLUMEID -i $(curl -s http://169.254.169.254/latest/meta-data/instance-id) -d $DEVICEID >$TMPFILE 2>/dev/null; check_exit; sleep 10
echo -n "-- creating a partition inside $VOLUMEID volume ... "
echo -e "n\np\n1\n\n\nt\n83\na\n1\nw" | sudo fdisk $DEVICEID >/dev/null 2>/dev/null; check_exit
echo -n "-- formating ${DEVICEID}1 partition ... "
sudo mkfs.ext3 ${DEVICEID}1 >/dev/null 2>/dev/null; check_exit
echo -n "-- mounting ${DEVICEID}1 partition temporary inside the instance ... "
sudo mount ${DEVICEID}1 $TMPDIR >/dev/null 2>/dev/null; check_exit
echo -n "-- creating directory structure inside volume ... "
sudo mkdir -p $TMPDIR/boot/grub >/dev/null 2>/dev/null; check_exit
echo -n "-- copy $KERNEL inside $VOLUMEID volume ... "
sudo cp -p $KERNEL $TMPDIR/boot/ >/dev/null 2>/dev/null; check_exit
echo -n "-- generating menu.lst file ... "
FILENAMEKERNEL=`filename $KERNEL`
echo "default 0" | sudo tee $TMPDIR/boot/grub/menu.lst >/dev/null; echo "timeout 1" | sudo tee -a $TMPDIR/boot/grub/menu.lst >/dev/null; echo "title Mirage-Test" | sudo tee -a $TMPDIR/boot/grub/menu.lst >/dev/null; echo " root (hd0,0)" | sudo tee -a $TMPDIR/boot/grub/menu.lst >/dev/null; echo " kernel /boot/$FILENAMEKERNEL" | sudo tee -a $TMPDIR/boot/grub/menu.lst >/dev/null; check_exit; sleep 5
echo -n "-- umounting $VOLUMEID ... "
sudo umount $TMPDIR 2>/dev/null; check_exit; sleep 5
echo -n "-- creating snapshot from $VOLUMEID volume ... "
ec2-create-snapshot $VOLUMEID >$TMPFILE 2>/dev/null; check_exit
SNAPSHOTID=`cat $TMPFILE | awk '{ print $2; }'`; sleep 30
echo -n "-- registering new AMI from $SNAPSHOTID snapshot ... "
ec2-register --snapshot $SNAPSHOTID --kernel $AKI --architecture x86_64 -n ${AMINAME} >$TMPFILE 2>/dev/null; check_exit; AMIREGISTERED=1
AMIID=`cat $TMPFILE | awk '{ print $2; }'`; sleep 10;
echo -n "-- AMI registered: $AMIID"; echo ""
sanity_check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment