Automatically mount an EBS volume upon starting an Amazon EC2 Linux instance
Asked Answered
A

4

45

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?

Arcadian answered 15/5, 2011 at 6:7 Comment(2)
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.Annamariaannamarie
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.Inspan
H
30

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.

Hoffarth answered 17/5, 2011 at 5:24 Comment(4)
Although that works, here is what Amazon supports says about the matter: I recommend looking into using RC init scripts instead of using the fstab for this purpose (for EC2 instances). If a device listed in the fstab fails to be mounted then this will halt the boot process and you will not be able to ssh into the instance. Instead, using an RC script could allow a "soft failure" to occur so that you could still ssh in and then fix the problem. See the full thread here: forums.aws.amazon.com/message.jspa?messageID=304528#304528Maraschino
Best of both worlds - use an fstab entry but specify defaults,noauto which tells it not to automatically mount on boot. Then use the rc script to mount the filesystem. This works around the issue of a mount failure stopping boot and keeps your drive mount points in fstab.Asp
May be a good idea to do /dev/sdf /data ext3 defaults 0 0 The last two flags set as 0 mean: <dump> Enable or disable backing up of the device/partition (the command dump). This field is usually set to 0, which disables it. <pass num> Controls the order in which fsck checks the device/partition for errors at boot time. The root device should be 1. Other partitions should be 2, or 0 to disable checking. (From help.ubuntu.com/community/Fstab)Getty
As @Sitsang notes below, the nofail option is now recommended.Karelian
S
40

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

Sitsang answered 2/7, 2014 at 0:56 Comment(5)
...a thing to note for Debian/Ubuntu users: Debian derivatives, such as Ubuntu, must also add the nobootwait mount option.Kalakalaazar
@Kalakalaazar about the nobootwait option... what problem does that solve?Washwoman
If your EBS volume fails to mount for some reason, your instance might fail to boot without that option. unix.stackexchange.com/questions/53456/…Kalakalaazar
nobootwait dropped in ubuntu 16.04 askubuntu.com/questions/786928/…Lovmilla
The latest docs suggest using the device UUID (from blkid) instead of the /dev pathSofko
H
30

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.

Hoffarth answered 17/5, 2011 at 5:24 Comment(4)
Although that works, here is what Amazon supports says about the matter: I recommend looking into using RC init scripts instead of using the fstab for this purpose (for EC2 instances). If a device listed in the fstab fails to be mounted then this will halt the boot process and you will not be able to ssh into the instance. Instead, using an RC script could allow a "soft failure" to occur so that you could still ssh in and then fix the problem. See the full thread here: forums.aws.amazon.com/message.jspa?messageID=304528#304528Maraschino
Best of both worlds - use an fstab entry but specify defaults,noauto which tells it not to automatically mount on boot. Then use the rc script to mount the filesystem. This works around the issue of a mount failure stopping boot and keeps your drive mount points in fstab.Asp
May be a good idea to do /dev/sdf /data ext3 defaults 0 0 The last two flags set as 0 mean: <dump> Enable or disable backing up of the device/partition (the command dump). This field is usually set to 0, which disables it. <pass num> Controls the order in which fsck checks the device/partition for errors at boot time. The root device should be 1. Other partitions should be 2, or 0 to disable checking. (From help.ubuntu.com/community/Fstab)Getty
As @Sitsang notes below, the nofail option is now recommended.Karelian
S
9

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!

Schaaff answered 22/7, 2013 at 13:15 Comment(2)
EBS volumes are not actually dependent on the net device initialization within the EC2 instance, they're presented to it as local block devices.Famished
Thank you. It works in my case, though it seems to be non standardUnkindly
V
5

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

Voltaire answered 9/5, 2021 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.