What is mounting and how to mount an EBS volume in EC2 (Linux)?

As we know all UNIX systems have a single directory tree structure. That means all storage associated with system must have associated location pointing to a single tree structure. However this is opposite in Windows. In windows, each drive contains separate directory tree structure.

As like in Windows the highest directory structure is C: drive whereas in UNIX it is root directory, which is the top directory structure in entire filesystem. The root directory is mounted on / .To make any partition visible in a directory tree structure, we mount a partition. Mounting is basically associating a storage device to a particular location in directory tree structure. For example, if you are inserting an USB-stick in Windows, you would get a new drive e.g G: and if you are mounting a partition on /data/usb, then the top directory would be visible as /data/usb in the tree structure and /data/usb could be further accessible for your use cases.

The below section would explain how to mount an EBS volume in Linux EC2.

1. Type lsblk to list your available disk devices. As you could see, your root device (xvda) is mounted on / .However the partition xvdf is not mounted yet. So we would mount this partition on /data in below sections

2. You need to create a filesystem (/data) first if not there before you mount your device partition (/dev/xvdf) into it. Type file -s /dev/xvdf [sudo file -s /dev/xvdf if you have not logged in as root]

Here in below snippet, the output shows only data, which means there is no filesystem available for the device partition and you must have to create one here. However for root device(/dev/xvda) , the filesystem is already available

3. Create a filesystem on the device. Type mkfs -t xfs /dev/xvdf [Note: Do not execute the above command, if your file system already contains data]

4. Create a mount point directory for your EBS volume. type mkdir /data

5. Use the following command to mount your EBS volume to the directory created in Step 4. Type mount /dev/xvdf /data

6. Verify the mount point. Type lsblk or you could verify it using df -h [useful for disk space measurement]

The filesystem is ready to use now.

7. To unmount the volume, you need to execute umount /dev/xvdf

How to automount an EBS after EC2 reboot

To mount an attached EBS volume on each system reboot, you need to add an entry for the device to /etc/fstab file.

We could directly make an entry like below to /etc/fstab file using vim or vi editor for device /dev/xvdf

device_name   mount_point    file_system_type    fs_mntops    fs_freq    fs_passno
/dev/xvdf    /data    xfs    defaults,nofail    0    0

AWS recommends to automount an EBS using device’s 128-bits universally unique identifier (UUID).

1. Create a backup of your /etc/fstab file. Type cp /etc/fstab /etc/fstab.bak

2. Type blkid to find the device UUID

3. Type vi /etc/fstab to edit the file

4. Add your UUID entry in /etc/fstab file & save it

5. To verify your changes, type following commands to unmount the device and mount all filesystem into /etc/fstab

umount /data
mount -a

If the above commands shows no error, then your entry in /etc/fstab is okay and the EBS volume now automatically mounted on each reboot.

I hope this blog helps to mount and unmount an EBS volume in AWS EC2 Linux instance. Please provide your comments if you have any questions related to this blog.

Leave a Reply

Discover more from TWWIP

Subscribe now to keep reading and get access to the full archive.

Continue reading