Automatically mount SSD instance storage on AWS EC2 in Ubuntu 16.04
I am using an Ubuntu 16.04 EC2 from AWS on c5d.2xlarge HW. It comes with a 200 GB SSD called /dev/nvme1n1
.
I am able to mount this drive using:
$ sudo mkfs.ext4 -E nodiscard /dev/nvme1n1
$ sudo mount -o discard /dev/nvme1n1 /home/ubuntu
To try to get it to automatically mount I also added to the /etc/fstab
:
/dev/nvme1n1 /home/ubuntu/spda ext4 defaults,users,nofail,discard 0 2
My issues:
It does not seem to automatically mount when I stop / start the instance. I am not sure how to fix / debug.
When I mount it manually the folder created belongs to the root and I can't access it as a user.
My goal is to be able to start the instance and already have the drive mounted and accessible to the users.
The 200GB SSD disk that you see is called Instance storage (or ephemeral storage) and is destroyed everytime you stop the instance and created new every time you start the instance.
That means two things:
-
Don't store any precious data you want to retain over stop/start - it will be all gone when you stop it. These instance storage disks are great for caches, temporary dirs, swap space, etc. Anything that can be easily recreated if it's lost.
-
Every time you start the instance the disk is blank - you must format it first (e.g.
mkfs.ext4
) before you can use it. The next time you stop/start it will be blank again and you mustmkfs
it again.That's why simply adding it to
/etc/fstab
isn't enough - the disk won't be formatted at the time the boot script attempts to mount it.
To resolve your problem you will have to create a custom script, e.g. /usr/local/sbin/mount-instance-store.sh
with roughly this content:
mkfs.ext4 -E nodiscard -m0 /dev/nvme1n1
mount -o discard /dev/nvme1n1 /home/ubuntu/spda
chown ubuntu:ubuntu /home/ubuntu/spda
Then you'll have to make sure the script is executed during boot time. The way to do that is different for different distributions, for Ubuntu 16.04 this should work: How to automatically execute shell script at startup boot on systemd Linux
Hope that helps :)
With a newer version of cloud-init you can use the Disk Setup module. Cloud-init is available on all clouds for all major OS (even windows) afaict. Use the follwing line in /etc/fstab (modify as needed for the device, and mount point obviously):
/dev/nvme1n1 /mnt auto defaults,nofail,x-systemd.requires=cloud-init.service,comment=cloudconfig 0 2
No cloud-init? No problem, as newer systemd also supports x-systemd.makefs and some other ones as documented here:
https://manpages.debian.org/testing/systemd/systemd-makefs.8.en.html https://manpages.debian.org/testing/systemd/systemd.mount.5.en.html