Volume Management in Linux with Amazon EBS

Volume Management in Linux with Amazon EBS

Introduction

Amazon Elastic Block Store (Amazon EBS) is a high-performance, scalable block storage solution designed for use with Amazon EC2 instances. It enables users to create, attach, manage, and persist storage volumes that function similar to physical hard drives.

In this guide, we will walk through the volume management process in Linux using Amazon EBS.

When you launch an EC2 instance, an 8GB EBS volume is attached by default, represented as /dev/xvda internally.

You can list all attached block devices using:

lsblk

Example Output:

NAME     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
loop0      7:0    0 25.2M  1 loop /snap/amazon-ssm-agent/7993
loop1      7:1    0 55.7M  1 loop /snap/core18/2829
loop2      7:2    0 55.4M  1 loop /snap/core18/2846
loop3      7:3    0 38.8M  1 loop /snap/snapd/21759
loop4      7:4    0 44.4M  1 loop /snap/snapd/23545
xvda     202:0    0    8G  0 disk        # default volume
├─xvda1  202:1    0    7G  0 part /
├─xvda14 202:14   0    4M  0 part
├─xvda15 202:15   0  106M  0 part /boot/efi
└─xvda16 259:0    0  913M  0 part /boot

Each partition of the default volume is represented as /dev/xvda1, /dev/xvda14, etc.


1️⃣ Creating a New EBS Volume

To create an EBS volume via AWS Management Console:

  1. Navigate to EC2 DashboardVolumes under the Elastic Block Store (EBS) section.

  2. Click Create Volume.

  3. Configure:

    • Size: e.g., 5 GiB

    • Volume Type: e.g., gp3 (General Purpose SSD)

    • Availability Zone (AZ): Must match your EC2 instance’s AZ.

  4. Click Create Volume and wait for it to be Available.


2️⃣ Attaching the EBS Volume to an EC2 Instance

  1. Select the newly created volume.

  2. Click ActionsAttach Volume.

  3. Choose the target EC2 instance.

  4. Set the Device Name (e.g., /dev/sdf).

    Linux may internally rename /dev/sdf to /dev/xvdf.

  5. Click Attach Volume.

  6. Verify attachment using:

     lsblk
    

    Example Output:

     NAME     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
     loop0      7:0    0 25.2M  1 loop /snap/amazon-ssm-agent/7993
     loop1      7:1    0 55.7M  1 loop /snap/core18/2829
     loop2      7:2    0 55.4M  1 loop /snap/core18/2846
     loop3      7:3    0 38.8M  1 loop /snap/snapd/21759
     loop4      7:4    0 44.4M  1 loop /snap/snapd/23545
     xvda     202:0    0    8G  0 disk
     ├─xvda1  202:1    0    7G  0 part /
     ├─xvda14 202:14   0    4M  0 part
     ├─xvda15 202:15   0  106M  0 part /boot/efi
     └─xvda16 259:0    0  913M  0 part /boot
     xvdf     202:80   0    5G  0 disk        # volume attached successfully✅
    

3️⃣ Partitioning the EBS Volume

Partitioning a volume is essential for efficient disk management. It allows you to:

  • Organize storage by separating system, application, and data files.

  • Improve performance by allocating specific partitions for different workloads.

  • Enhance security by isolating critical data.

  • Facilitate backups and recovery by creating smaller, manageable partitions.

📌 Creating a Partition (2GB)

  1. Launch fdisk:

     sudo fdisk /dev/xvdf        # /dev/xvdf refers to the newly attached volume
    
  2. Inside fdisk prompt:

    • Type g to create a new GPT partition table.

    • Type n to create a new partition.

    • Accept the default Partition number (Press Enter).

    • Accept the default First sector (Press Enter).

    • Enter +2G for the Last sector to create a 2GB partition.

    • Type w to save changes.

  Command (m for help): g
  Created a new GPT disklabel (GUID: 6AB96D5B-D68F-4B10-AC7E-40072C93EE0F).
  Command (m for help): n
  Partition number (1-128, default 1):
  First sector (2048 - 10485726, default 2048):
  Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048 - 10485726, default 10483711): +2G

  Created a new partition 1 of type 'Linux filesystem' and of size 2 GiB.
  Command (m for help): w
  The partition table has been altered.
  Calling ioctl() to re-read partition table.
  Syncing disks.

📌 Creating Another Partition (3GB)

  1. Run sudo fdisk /dev/xvdf.

  2. Inside fdisk prompt:

    • Type n to create a new partition.

    • Accept the default Partition number (Press Enter).

    • Accept the default First sector (Press Enter).

    • Accept the default Last sector (Press Enter). It will create a partition out of the entire remaining space i.e. 3 GB.

    • Type w to save changes.

Check partitions using:

lsblk

Example Output:

NAME     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
loop0      7:0    0 25.2M  1 loop /snap/amazon-ssm-agent/7993
loop1      7:1    0 55.7M  1 loop /snap/core18/2829
loop2      7:2    0 55.4M  1 loop /snap/core18/2846
loop3      7:3    0 38.8M  1 loop /snap/snapd/21759
loop4      7:4    0 44.4M  1 loop /snap/snapd/23545
xvda     202:0    0    8G  0 disk
├─xvda1  202:1    0    7G  0 part /
├─xvda14 202:14   0    4M  0 part
├─xvda15 202:15   0  106M  0 part /boot/efi
└─xvda16 259:0    0  913M  0 part /boot
xvdf     202:80   0    5G  0 disk
├─xvdf1  202:81   0    2G  0 part        # This is our first partition of size 2GB 🤩
└─xvdf2  202:82   0    3G  0 part      # This is our second partition of size 3GB 🔥

4️⃣ Formatting the Partitions

Formatting is necessary to prepare a partition for storing data by organizing it into a recognizable structure. Without formatting, an operating system cannot read from or write to the partitions. It also enables features like file indexing, access control, and error handling, ensuring efficient and reliable data storage.

Here, we format both partitions with ext4 filesystem:

sudo mkfs.ext4 /dev/xvdf1
sudo mkfs.ext4 /dev/xvdf2

Example Output

ubuntu@ip-172-31-87-6:~$ sudo mkfs.ext4 /dev/xvdf1
mke2fs 1.47.0 (5-Feb-2023)
Creating filesystem with 524288 4k blocks and 131072 inodes
Filesystem UUID: e18c8876-00d5-4c4c-87af-d1e40d1457f5
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

ubuntu@ip-172-31-87-6:~$ sudo mkfs.ext4 /dev/xvdf2
mke2fs 1.47.0 (5-Feb-2023)
Creating filesystem with 785920 4k blocks and 196608 inodes
Filesystem UUID: ead30175-0f94-446f-86d9-eab3f94a2b09
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

5️⃣ Mounting Partitions

Mounting refers to attaching a storage volume to a folder on the filesystem. It makes the storage device accessible from a specific directory (mount point).

Create mount points:

sudo mkdir /mnt/study /mnt/devops

Mount the partitions:

sudo mount /dev/xvdf1 /mnt/study
sudo mount /dev/xvdf2 /mnt/devops

Verify using:

df -h

Example Output

Filesystem      Size  Used Avail Use% Mounted on
/dev/root       6.8G  1.7G  5.1G  26% /
tmpfs           479M     0  479M   0% /dev/shm
tmpfs           192M  892K  191M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/xvda16     881M   76M  744M  10% /boot
/dev/xvda15     105M  6.1M   99M   6% /boot/efi
tmpfs            96M   12K   96M   1% /run/user/1000
/dev/xvdf1      2.0G   24K  1.8G   1% /mnt/study        # first partition mounted to /mnt/study✅
/dev/xvdf2      2.9G   24K  2.8G   1% /mnt/devops    # second partition mounted to /mnt/devops✅

6️⃣ Persistent Mounting

Every time the system reboots, these partitions get unmounted. To make mounting persistent across reboots, follow these steps:

  1. Unmount the partitions:

    If the partitions are already mounted, unmount them. Else, skip this step.

     sudo umount /dev/xvdf1
     sudo umount /dev/xvdf2
    
  2. Find the UUID of the partitions:

     sudo blkid /dev/xvdf1
     sudo blkid /dev/xvdf2
    
  3. Edit:

     sudo nano /etc/fstab
    
  4. Append the following entries in /etc/fstab:

     UUID=<UUID_OF_XVDF1> /mnt/study ext4 defaults 0 2
     UUID=<UUID_OF_XVDF2> /mnt/devops ext4 defaults 0 2
    

    Finally, the /etc/fstab file would look like:

     LABEL=cloudimg-rootfs   /        ext4   discard,commit=30,errors=remount-ro     0 1
     LABEL=BOOT      /boot   ext4    defaults        0 2
     LABEL=UEFI      /boot/efi       vfat    umask=0077      0 1
     UUID=3c5dfe18-de28-4a7d-a320-e712f17b49e5 /mnt/study ext4 defaults 0 2
     UUID=b7097ef2-61b6-42ec-992f-ad557e94d492 /mnt/devops ext4 defaults 0 2
    

    ⚙ While booting, the system reads this file and mounts everything included in it.

  5. Test the configuration:

    This will mount everything in /etc/fstab that are not mounted.

     sudo mount -a
    
  6. Verify that the partitions are mounted

     df -h
    

    Example Output

     Filesystem      Size  Used Avail Use% Mounted on
     /dev/root       6.8G  1.7G  5.1G  26% /
     tmpfs           479M     0  479M   0% /dev/shm
     tmpfs           192M  892K  191M   1% /run
     tmpfs           5.0M     0  5.0M   0% /run/lock
     /dev/xvda16     881M   76M  744M  10% /boot
     /dev/xvda15     105M  6.1M   99M   6% /boot/efi
     tmpfs            96M   12K   96M   1% /run/user/1000
     /dev/xvdf1      2.0G   24K  1.8G   1% /mnt/study
     /dev/xvdf2      2.9G   24K  2.8G   1% /mnt/devops
    

7️⃣ Unmounting & Detaching the Volume

  1. Unmount the partitions.

  2. Remove the corresponding entries from /etc/fstab.

  3. Navigate to AWS EC2 Dashboard.

  4. Select the EBS volume.

  5. Click ActionsDetach Volume.

  6. Confirm detachment.


Conclusion

Managing Amazon EBS volumes in Linux is crucial for handling storage effectively in AWS. By understanding creation, attachment, partitioning, formatting, and persistent mounting, you can efficiently manage storage in cloud environments.

Thank you for reading! 🚀 Happy Cloud Computing! 🌩️