Automatically mount SSD instance storage on AWS EC2 in Ubuntu 16.04

Solution 1:

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/ 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 :)

Solution 2:

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: