RightScale (www.rightscale.com) is a great and easy way to setup, monitor and run Amazon EC2 virtual server instances.
Within RightScale, you can define "RightScripts" to be run during boot, run or shutdown of the server instance.
With this, it is quite easy to automatically setup an EBS volume for the instance.
Just create a new RightScript and paste the following script into it:
#!/bin/sh
ebs_device_node=$EBS_DEVICE_NODE
ebs_device_node_partition="${ebs_device_node}1"
# todo: check of the device node exists.
if [ ! -b $ebs_device_node ]; then
echo "Block Device $ebs_device_node does not exist. Exiting"
exit 1
fi
if [ ! -d $EBS_MOUNT_POINT ]; then
echo "EBS Storage not provisioned, setting up..."
echo "STEP1: Partition EBS STORAGE $ebs_device_node"
# note: next step needs the sequence "n<enter>p<enter>1<enter><enter><enter>w<enter>" to work!
fdisk $ebs_device_node << EOF
n
p
1
w
EOF
ret=$?
if [ $ret != 0 ]; then
echo "FDISK FAILED. Aborting".
exit 1
fi
echo "formatting $ebs_device_node_partition"
mkfs.ext3 $ebs_device_node_partition
ret=$?
if [ $ret != 0 ]; then
echo "formatting $ebs_device_node_partition FAILED. Aborting".
exit 1
fi
echo "Creating mount point $EBS_MOUNT_POINT"
mkdir $EBS_MOUNT_POINT
if [ $ret != 0 ]; then
echo "Creation of mount point $EBS_MOUNT_POINT FAILED. Aborting".
exit 1
fi
echo "Adding EBS Storage mount point $EBS_MOUNT_POINT to /etc/fstab"
echo "$ebs_device_node_partition $EBS_MOUNT_POINT ext3 defaults 0 0" >> /etc/fstab
echo "Mounting $EBS_MOUNT_POINT"
mount $EBS_MOUNT_POINT
else
echo "EBS Storage already set up. Exiting."
exit
fi
echo "finished."
Note: You need to have the two blank lines for fdisk in the script, as this is needed to partition the volume propperly.
- If you got the script, assign it to a server template.
- Now create an EBS volume in a zone with device node /dev/sdj and any size you need.
- Then launch an instance of the server template in the same zone as the EBS volume.
- Fill in the Inputs of the instance (e.g. /dev/sdj as the device node and /EBS-VOLUME as the mount point)
- And immediately attach the EBS volume to the instance.
Thats it.
The script will check during start if the volume is already setup. This means, it runs only once.