Skip to content

Instantly share code, notes, and snippets.

@samuraisam
Created August 12, 2015 01:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuraisam/0eb88d0be50727920be1 to your computer and use it in GitHub Desktop.
Save samuraisam/0eb88d0be50727920be1 to your computer and use it in GitHub Desktop.
#!/bin/bash
# this script will run only once. it is to be placed in the /etc/init.d/ directory
# it will create an ebs volume and mount it
# add it to /etc/init.d/mounter
# and run sudo update-rc.d mounter defaults
# to make it run on boot
FLAG="/var/log/mounter.log"
if [ ! -f $FLAG ]; then
DEVICE="/dev/xvdk"
MOUNTPOINT="/data"
SIZE="6000"
IOPS="10000"
EC2_INSTANCE_ID=`curl -s http://169.254.169.254/latest/meta-data/instance-id`
EC2_AVAIL_ZONE=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone`
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"
echo "region=$EC2_REGION"
echo "instance_id=$EC2_INSTANCE_ID"
echo "avail_zone=$EC2_AVAIL_ZONE"
# create a volume
VOLUME_INFO=`aws ec2 create-volume --availability-zone $EC2_AVAIL_ZONE --region $EC2_REGION --volume-type io1 --size $SIZE --iops $IOPS --encrypted`
echo "volume_info=$VOLUME_INFO"
VOLUME_ID="`echo ${VOLUME_INFO} | grep -Po '(?= .VolumeId.:.)*vol-[a-z0-9]*[a-z0-9](?=")'`"
echo "volume_id=$VOLUME_ID"
# name our volume
VOLUME_NAME="MongoData-`date +%F`"
echo "volume_name=$VOLUME_NAME"
aws ec2 create-tags --region $EC2_REGION --resources ${VOLUME_ID} --tags Key=Name,Value=${VOLUME_NAME}
sleep 60 # wait for volume to become available
# attach the volume to the current instan c
aws ec2 attach-volume --region $EC2_REGION --volume-id ${VOLUME_ID} --instance-id ${EC2_INSTANCE_ID} --device $DEVICE
# fomat and mount
mkfs.ext4 $DEVICE
mkdir -p $MOUNTPOINT
mount $DEVICE $MOUNTPOINT
echo "$DEVICE $MOUNTPOINT ext4 defaults,auto,noatime,noexec 0 0" >> /etc/fstab
echo "/data should now be accessible"
touch $FLAG
else
echo "Go away"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment