EC2 launch script

  • From: Andrew Stuart <andrew.stuart@xxxxxxxxxxxxxxxxxx>
  • To: rumpkernel-users <rumpkernel-users@xxxxxxxxxxxxx>
  • Date: Thu, 23 Jul 2015 23:17:53 +1000

Hi Antti,

I’m afraid I’m out of steam for tonight. The script is written but it hasn’t
been run even once yet - I need to run it and debug it and make it work. It
definitely won’t work right now as it hasn’t been run even once.

It’s copied from the MirageOS guys and hacked severely to suit our purposes.

Importantly, it must be run from an EC2 instance - it won’t work from outside
EC2 and there’s no way to do so unfortunately.

If you’re interested, here is what it currently is:



#!/usr/bin/env bash
# this script is copied from the Mirage-OS project and modified for rump
kernels.
set -x
SUDO=sudo
MOUNTPOINT=/mnt/ebs
REGION=us-west-1
ZONE=us-west-1c
# KERNELID is ec2-describe-images -o amazon --region ${REGION} -F
"manifest-location=*pv-grub-hd0*" -F "architecture=x86_64" | tail -1 | cut -f2
# Also obtained from
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/UserProvidedKernels.html
#KERNELID=aki-fc8f11cc #us-west-2
KERNELID=aki-880531cd #us-west1

NAME=unikernelfilename
BUCKET=tempunikerneldeployment
while getopts "hn:b:r:k:" arg; do
case $arg in
h)
echo "usage: $0 [-h] [-n <name>] [-r <region>] -k <unikernel> "
echo ""
echo "<path>: Directory path to copy to block file system and attach to
unikernel"
echo "<unikernel>: Name of the kernel file (e.g. rump.xen)"
echo "<name>: the application name to use (default: ${NAME})"
echo "<region>: the EC2 region to register AMI in (default: ${REGION})"

echo To run this script you will need the Amazon command line tools
installed from here:
echo
http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/set-up-ec2-cli-linux.html
echo ""
echo Remember to set each of the following environment variables in your
echo environment before running this script:
echo AWS_ACCESS_KEY
echo AWS_SECRET_KEY
exit 1 ;;
p) PATH=$OPTARG ;;
n) NAME=$OPTARG ;;
b) BUCKET=$OPTARG ;;
r) REGION=$OPTARG ;;
k) APP=$OPTARG ;;
esac
done

if [ ! -e "$APP" ]; then
echo "Must specify a unikernel file with the [-k] flag."
echo "Run '$0 -h' for full option list."
exit 1
fi


##########################################################################################
###### prepare block device
###### this block device will contain the unikernel code, and is booted by EC2
##########################################################################################

# create a 1 GB EBS volume using the AWS console
#
http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-CreateVolume.html
VOLUMEID=""
VOLUMEID=`ec2-create-volume --availability-zone ${ZONE} --region ${REGION} -s 1
| awk '{print $2}'`

# attach the EBS volume
#
http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-AttachVolume.html
ec2-attach-volume ${VOLUME-ID} --instance INSTANCE-ID --device /dev/sdf

# unmount any existing volume at the mountpoint
${SUDO} umount ${MOUNTPOINT}

# remove the mountpoint
${SUDO} rm -rf ${MOUNTPOINT}

# create the mountpoint
${SUDO} mkdir -p ${MOUNTPOINT}

# format the EBS volume as ext4
${SUDO} mkfs.ext4 /dev/sdf

# mount the device
${SUDO} mount /dev/xvdf ${MOUNTPOINT}

5: copy into the EBS volume the nginx directory tree that contains the nginx
configuration and website files

# unmount any existing volume at the mountpoint
${SUDO} umount ${MOUNTPOINT}

# detach the EBS volume
#
http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-DetachVolume.html
ec2-detach-volume ${VOLUME-ID}

##########################################################################################
###### prepare block device
###### this will be the root file system for the unikernel
##########################################################################################

# create a 1 GB EBS volume using the AWS console
#
http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-CreateVolume.html
VOLUMEID=""
VOLUMEID=`ec2-create-volume --availability-zone ${ZONE} --region ${REGION} -s 1
| awk '{print $2}'`

# attach the EBS volume
#
http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-AttachVolume.html
ec2-attach-volume ${VOLUMEID} --instance INSTANCE-ID --device /dev/sdf

# unmount any existing volume at the mountpoint
${SUDO} umount ${MOUNTPOINT}

# remove the mountpoint
${SUDO} rm -rf ${MOUNTPOINT}

# create the mountpoint
${SUDO} mkdir -p ${MOUNTPOINT}

# format the EBS volume as ext2
${SUDO} mke2fs.ext4 /dev/sdf

# mount the device
${SUDO} mount /dev/xvdf ${MOUNTPOINT}

# Make name unique to avoid registration clashes
NAME=${NAME}-`date +%s`
IMG=${NAME}.img

echo Name : ${NAME}
echo Bucket: ${BUCKET}
echo Region: ${REGION}

set -e

${SUDO} mkdir -p ${MOUNTPOINT}/boot/grub
echo default 0 > menu.lst
echo timeout 1 >> menu.lst
echo title Rump >> menu.lst
echo " root (hd0)" >> menu.lst
echo " kernel /boot/rump-os.gz" >> menu.lst
${SUDO} mv menu.lst ${MOUNTPOINT}/boot/grub/menu.lst
${SUDO} sh -c "gzip -c $APP > ${MOUNTPOINT}/boot/rump-os.gz"
${SUDO} ls ${MOUNTPOINT}
${SUDO} umount -d ${MOUNTPOINT}

#Label the disk. AWS has an unofficial tutorial that does not include this step.
${SUDO} tune2fs -L '/' /dev/xvdf

# unmount any existing volume at the mountpoint
${SUDO} umount ${MOUNTPOINT}

# detach the EBS volume
#
http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-DetachVolume.html
ec2-detach-volume ${VOLUMEID}


##########################################################################################
###### prepare the unikernel for booting on EC2
##########################################################################################

# make a snapshot of the nginx kernel root block volume (AMI’s cannot be
created from volumes, only from snapshots)
SNAPSHOTID=""
SNAPSHOTID=`ec2-create-snapshot --region ${REGION} ${VOLUMEID} | awk '{print
$2}'`

# Create image/AMI from the snapshot
http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-CreateImage.html
## HAVING TROUBLE? COULD IT BE [--root-device-name name]
SNAPSHOTID=`ec2-register --name 'my_unikernel' \
--description 'my_unikernel description' \
-a x86_64 \
-s ${SNAPSHOTID} \
-b "/dev/sdc=${SNAPSHOTID}" \
--kernel ${KERNELID} \
--virtualization-type paravirtual \
| awk '{print $2}'`


unset -x

echo You can now start this instance via:
echo ec2-run-instances --region ${REGION} $id
echo ""
echo Don\'t forget to customise this with a security group, as the
echo default one won\'t let any inbound traffic in.




Other related posts: