AWS EC2 – Automatically Mount SSD Instance Storage on Ubuntu 16.04

amazon ec2amazon-web-servicesnvmessdubuntu-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.

Best Answer

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:

  1. 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.

  2. 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 must mkfs 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 :)

Related Topic