42
votes

I have an EBS volume (e.g. /dev/sdf) that has been attached to an EC2 instance (which boots from a different EBS volume), and I have mounted the volume (through mount /dev/sdf /data). When I stop and start again the instance, the volume is still attached but no longer mounted, and I have to manually mount it again.

Is there a way to make the volume /dev/sdf automatically mounted to /data upon starting the instance?

4
You can place it to /etc/fstab but the EBS volume must be attached first. Another option is to write a initscript that will attached the volume and mount it on the instance.Rodney Quillo
Can somebody explain how to write such init script and how to make it executable on system start? This is still part of main question according to the title :) Thanks, if somebody can answer.Anton Babenko

4 Answers

30
votes

Make an entry to /etc/fstab

Entry would be like:

/dev/sdf    /data   ext3    defaults    1 1

This will automatically mount the volume during reboot.

38
votes

It would seem that the official ec2 documentation now recommends plain old fstab entries with nofail -

/dev/xvdf       /data   ext4    defaults,nofail        0       2

ref - http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-using-volumes.html

8
votes

I recommend using an /etc/init conf file that do that:

  • login with root
  • create a new file (not executable) name it like this : mountec2vol.conf
  • paste into it this code :
# /etc/init/mountec2vol.conf
#
# description: Mounts the EBS Volume
#
start on net-device-up
exec mount /dev/xvdf1 /myVolume`
  • Reboot if you want to test

that's all what you have to do!

0
votes

The proofable proof is official doc

You need a piece of code like:

DEVICE=/dev/$(lsblk -rno NAME | awk 'FNR == 3 {print}')
MOUNT_POINT=/data/

cp /etc/fstab /etc/fstab.orig
UUID=$(blkid | grep $DEVICE | awk -F '\"' '{print $2}')
echo -e "UUID=$UUID     $MOUNT_POINT      xfs    defaults,nofail   0   2" >> /etc/fstab
umount /data
mount -a

In case you're going to use Terraform to Launch an Instance, EBS with Attaching and mounting you may use all the code from the cheatsheet of mine AWS-EBS-Attach-Mount