Skip to content

Instantly share code, notes, and snippets.

@brillozon
Created April 30, 2015 02:39
Show Gist options
  • Save brillozon/93d07c8ca842108f3608 to your computer and use it in GitHub Desktop.
Save brillozon/93d07c8ca842108f3608 to your computer and use it in GitHub Desktop.
SysV initscript for mounting EC2 volumes, including creating a swap partition on the first available ephemeral drive.
# Default configuration values for nimbus startup.
region=us-west-2
role=nimbus-dev
# Ephemeral mounting
localmount=/project
# EBS mounting
mount=/ebs
# S3 mounting
s3user=ubuntu
s3fs=/usr/local/bin/s3fs
nimbusbucket=com.example.nimbus
clientbucket=com.example.nimbus-demo-client
s3mountbase=/s3
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre
# Endpoint for us-west-2
EC2_URL=https://ec2.us-west-2.amazonaws.com
# Settings for the cluster resources.
#!/bin/bash
### BEGIN INIT INFO
# Provides: nimbus-volume-mounting
# Required-Start: $network $local_fs
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: nimbus volume mounts
# Description: Nimbus single-host pseudo-cluster instance provisioning
### END INIT INFO
#
# Nimbus - a cloudy day cluster
#
# Access authorization is from the IAM role the instance is started with.
# Include nimbus defaults if available.
# Includes: region, mnt
if [ -f /etc/default/nimbus ]
then
. /etc/default/nimbus
fi
export JAVA_HOME
# Default region to Oregon.
if [ -z "$region" ] ; then
region="us-west-2"
fi
# Default role to nimbus-dev
if [ -z "$role" ] ; then
role="nimbus-dev"
fi
# Prefer system commands.
PATH=$PATH:/usr/bin:$HOME/bin
# Seconds to wait for volume to attach.
# MAX_TRIES=60
prog=$(basename $0)
logger="logger -t $prog "
curl="curl --retry 3 --silent --show-error --fail"
# Information about the current instance from here.
instance_data_url=http://169.254.169.254/latest
# Wait until networking is up on the instance.
perl -MIO::Socket::INET -e '
until(new IO::Socket::INET("169.254.169.254:80"))
{print "Waiting for network...\n";sleep 1}
' | $logger
# start/stop functions
start() {
# Get the instance ID to work with.
instance_id=$($curl $instance_data_url/meta-data/instance-id)
$logger "starting on instance $instance_id"
# Mount the ephemeral drives.
count=1
for device in $($curl $instance_data_url/meta-data/block-device-mapping/ | grep ephemeral)
do
local_dev=$($curl $instance_data_url/meta-data/block-device-mapping/$device/ | sed 's/^s/xv/')
if ! /bin/lsblk "/dev/$local_dev" ; then
continue
fi
# Partition the first ephemeral drive to include an 8G swap section.
if [ "$count" == 1 ] ; then
has_swap=$(/bin/lsblk /dev/${local_dev}1 2>/dev/null | /bin/grep -ic swap)
if [ "$has_swap" == 0 ] ; then
$logger "creating a swap partition on /dev/${local_dev}"
sudo /bin/umount -f /dev/$local_dev
# Partition the disk
# new, primary 1, start at 2048, end at +8G, type, 82, new, primary 2, start at lowest, end at highest, write
(echo n; echo p; echo 1; echo 2048; echo +8G; echo t; echo 82; echo n; echo p; echo 2; echo; echo; echo w) | sudo /sbin/fdisk /dev/$local_dev
fi
# Create and enable the swap partition.
sudo /sbin/mkswap /dev/${local_dev}1
sudo /sbin/swapon /dev/${local_dev}1
$logger "swap partition enabled on /dev/${local_dev}1"
# Adjust the device mounted as data.
local_dev=${local_dev}2
fi
# Modify the formatting and mounting if TRIMS is supported on the instance drives.
mkfs_opts=
mnt_opts=
discard_bytes=$(cat /sys/block/xvdb/queue/discard_max_bytes)
if [ "$discard_bytes" != 0 ] ; then
mkfs_opts=-K
mnt_opts="-o discard"
fi
# Put a filesystem on the disk.
$logger "creating filesystem on $local_dev"
sudo /sbin/mkfs -t xfs $mkfs_opts /dev/$local_dev
if [ ! -d "$localmount$count" ] ; then
sudo /bin/mkdir -p "$localmount$count"
fi
$logger "mounting /dev/$local_dev"
# Mount the ephemeral volume at $localmount$count
/bin/echo "Mounting volume attached at /dev/$local_dev at $localmount$count."
sudo /bin/mount $mnt_opts /dev/$local_dev "$localmount$count"
$logger "mounted filesystem $localmount$count on $local_dev"
count=`expr $count + 1`
done
# Mount the EBS volumes.
count=1
for device in $($curl $instance_data_url/meta-data/block-device-mapping/ | grep ebs)
do
# Get the attachment point for the EBS volume to mount.
ebs_dev=$($curl $instance_data_url/meta-data/block-device-mapping/$device | sed 's/^s/xv/')
if [ ! -d "$mount$count" ] ; then
sudo /bin/mkdir -p "$mount$count"
fi
$logger "mounting /dev/$ebs_dev"
# Mount the EBS volume at $mount$count
/bin/echo "Mounting volume attached at /dev/$ebs_dev at $mount$count."
sudo /bin/mount /dev/$ebs_dev "$mount$count"
count=`expr $count + 1`
done
# # Check to see if we mounted any ephemeral drives.
# device=$($curl $instance_data_url/meta-data/block-device-mapping/ | grep ephemeral | wc -l)
# if [ "$device" != 0 ] ; then
# # Add a swapfile on the first ephemeral drive
# swapfile=${localmount}1/swapfile
# if [ ! -e "$swapfile" ] ; then
# sudo /bin/dd if=/dev/zero of=${localmount}1/swapfile bs=1M count=8192
# sudo /bin/chmod 600 ${localmount}1/swapfile
# sudo /sbin/mkswap ${localmount}1/swapfile
# sudo /sbin/swapon ${localmount}1/swapfile
# fi
# fi
# Ensure swapping is enabled.
sudo /sbin/swapon -a
$logger "swapping enabled"
# Mount the s3 buckets.
if [ -x "$s3fs" ] ; then
/bin/echo "Mounting S3 buckets under $s3mountbase"
$logger "mounting s3 buckets"
# Ensure sharing the S3 FUSE mounts is enabled.
sudo /bin/sed -i 's/#[ \t]*\(user_allow_other\)/\1/' /etc/fuse.conf
sudo chmod o+r /etc/fuse.conf
# Default s3 mount user to ubuntu.
if [ -z "$s3user" ] ; then
s3user="ubuntu"
fi
if [ ! -d "$s3mountbase" ] ; then
sudo /bin/mkdir -p $s3mountbase/{nimbus,client}
sudo /bin/chown $s3user.$s3user $s3mountbase/{nimbus,client}
sudo /bin/chmod 02777 $s3mountbase/{nimbus,client}
fi
sudo -u $s3user $s3fs $nimbusbucket $s3mountbase/nimbus -o iam_role=$role -o allow_other
sudo -u $s3user $s3fs $clientbucket $s3mountbase/client -o iam_role=$role -o allow_other
$logger "mounted $s3mountbase/nimbus and $s3mountbase/client"
fi
$logger "volumes mounted at ${localmount}* ${mount}* and ${s3mountbase}*"
}
stop() {
/bin/echo "Unmounting S3 FUSE Volumes."
sudo /bin/umount $s3mountbase/nimbus
sudo /bin/umount $s3mountbase/client
/bin/echo "Unmounting EBS Volumes."
count=1
for device in $($curl $instance_data_url/meta-data/block-device-mapping/ | grep ebs)
do
# Unmount the EBS volume at $mount$count
/bin/echo "Unmounting volume at $mount$count."
$logger "unmounting $mount$count"
sudo /bin/umount $mount$count
count=`expr $count + 1`
done
/bin/echo "Unmounting Ephemeral Volumes."
count=1
for device in $($curl $instance_data_url/meta-data/block-device-mapping/ | grep ephemeral)
do
local_dev=$($curl $instance_data_url/meta-data/block-device-mapping/$device/ | sed 's/^s/xv/')
if ! /bin/lsblk "/dev/$local_dev" ; then
continue
fi
# Unmount the ephemeral volume at $localmount$count
/bin/echo "Unmounting volume at $localmount$count."
$logger "unmounting $localmount$count"
sudo /bin/umount $localmount$count
count=`expr $count + 1`
done
$logger "Ephemeral, EBS, and S3 volumes unmounted (${localmount}*, ${mount}*, ${s3mountbase}*)"
# Disable swapping.
sudo /sbin/swapoff -a
$logger "swapping disabled"
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 5
start
;;
*)
echo "Usage: $SELF {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