Skip to content

Instantly share code, notes, and snippets.

@sideshow
Created August 14, 2011 02:44
Show Gist options
  • Save sideshow/1144502 to your computer and use it in GitHub Desktop.
Save sideshow/1144502 to your computer and use it in GitHub Desktop.
Mount EBS Volume (see: http://bit.ly/EsrqQ)
#!/bin/sh
#
# /etc/init.d/mountebsvol
#
# chkconfig: 234 20 50
# description: Assigns an EC2 EBS Volume to a device and mounts the device
#
# To add this as a service run:
# /sbin/chkconfig --add mountec2vol
#
# VARS
#
VOL="vol-XXXXXXX"
SCRIPT_NAME="mount_ebs_vol"
DEV="/dev/sdh"
MOUNT_POINT="/vol/www"
export JAVA_HOME=/usr/java/latest
export EC2_PRIVATE_KEY=/root/.ec2/pk-XXXXXXXXXXX.pem
export EC2_CERT=/root/.ec2/cert-XXXXXXXXXXXX.pem
export EC2_HOME=/usr/local/ec2/apitools
PATH=$PATH:$HOME/bin:$EC2_HOME/bin
MAX_TRIES=60
# start/stop functions for OS
start(){
INSTANCE=`curl http://169.254.169.254/latest/meta-data/instance-id 2> /dev/null`
CTR=0
#/bin/echo "Mounting Elastic Block Store Volumes."
echo -n $"Mounting Amazon EBS Volume $MOUNT_POINT: "
ec2-attach-volume $VOL -i $INSTANCE -d $DEV
while [ ! -e "$DEV" ]; do
/bin/sleep 1
CTR=`expr $CTR + 1`
if [ $CTR -eq $MAX_TRIES ]
then
/bin/echo "WARNING: Cannot attach volume $VOL to $DEV -- Giving up after $MAX_TRIES attempts"
exit 1
fi
done
if [ ! -d $MOUNT_POINT ]; then
mkdir $MOUNT_POINT
fi
/bin/mount $DEV $MOUNT_POINT
touch "/var/lock/subsys/$SCRIPT_NAME"
}
stop(){
#/bin/echo "Unmounting Elastic Block Store Volumes."
echo -n $"UnMounting Amazon EBS Volume: "
/bin/umount $MOUNT_POINT
ec2-detach-volume $VOL
rm -f "/var/lock/subsys/$SCRIPT_NAME"
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 5
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment