If you attach an EBS volume to an Amazon EC2 instance, you can configure its device name. However, the block device driver of newer Linux kernels might randomly assign a different name. In this AWS guide, you will learn how to edit the fstab configuration file to ensure that an EBS volume automatically mounts on the correct mount point.
Latest posts by Michael Pietroforte (see all)

Attaching an EBS volume to an EC2 instance

You can create and attach a new EBS volume in the AWS console by navigating to Elastic Block Storage > Volumes and clicking Create Volume in the upper right corner. The new EBS volume then appears in the list, and you can attach it to your EC2 instance by selecting the volume and clicking Action. This brings up the dialog box in the screenshot below.

The block device driver might differ from the name you assign

The block device driver might differ from the name you assign

The device name you can assign (/dev/sdf in the example here) is essentially useless with Linux instances running in EC2 instances of the fifth generation or higher that use NVMe EBS volumes because their device names in Linux are different and usually start with "/dev/nvme." What's more, the block device driver can assign NVMe device names randomly, that is, in a different order than that configured in the block device mapping when Linux boots up. Thus, configuring fstab is a bit more complicated than it used to be.

Finding the EBS volume's device name

After Linux boots up, you first have to find the name of the EBS volume by running the lsblk command. If you have attached multiple EBS volumes of the same size, identifying the names can be a challenge. In the example below, you can see that nvme0n1 is used for the root volume. Thus, we know that nvme1n1 must be the name of our newly attached EBS volume. You can also find the names of volumes that have not yet been mounted with df -h and then compare the list with lsblk.

Finding the newly attached EBS volume

Finding the newly attached EBS volume

Formatting the EBS volume

Next, you have to format the volume. However, before you do that, I recommend ensuring that you are dealing with an empty volume:

sudo file -s /dev/nvme1n1

Then you can format the volume with this command:

sudo mkfs -t ext4  /dev/nvme1n1

Linux supports many different file systems. In my example, I used ext4.

If you now run sudo file -s /dev/nvme1n1 again, you can see that the UUID has been assigned, which is essential for mounting the volume automatically when Linux boots up.

Finding the UUID

Finding the UUID

You can now mount the volume with this command:

sudo mount /dev/nvme1n1 /mnt

But this wouldn't be permanent, and if you configure fstab as you are used to, using the device name, you will find that when Linux boots up, the mount points might get mixed up if you have multiple EBS volumes attached to your EC2 instance.

That is why we need the UUID, which always stays the same. If you already have multiple EBS volumes, you can find the UUIDs with lsblk -f.

Finding the UUID using lsblk f

Finding the UUID using lsblk f

Configure fstab for NVMe EBS volumes

Before you mess with the fstab configuration file, you should create a backup because, in case of a faulty configuration, your Linux machine might no longer be able to boot up:

sudo cp /etc/fstab /etc/fstab.bk

I won't go into the details of fstab here. The crucial thing here is that you don't use the device name for the first fstab field. To edit fstab, you can use the nano editor:

sudo nano /etc/fstab

Instead, we have to add the new volume by using its UUID:
test
 

UUID=59d68aff-8bf3-48da-be4c-7369acefcac1 /mnt ext4 defaults,nofail 0 2
Editing fstab with nano

Editing fstab with nano

The nofail option in the fifth fstab field is also important here. If your EBS volume is unavailable (for instance, if you detached it from your EC2 instance), Linux will fail to boot up. For the other fstab option, I configured the default setting. In the last fstab field, you should use "2" for nonroot devices. This means that fschk will check the volume after it is done with the root volume.

Subscribe to 4sysops newsletter!

Before you now reboot, you should first run sudo mount -a which verifies that your fstab configuration file does not contain errors.

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account