Linux Volume Management

Linux Volume Management

Introduction to Linux Volumes and AWS EBS

1. Understanding Linux Volumes

In Linux, a volume refers to a storage unit that the operating system recognizes as a single entity. It can be a physical partition on a hard disk, a logical volume, or even a virtual disk in cloud environments.

Types of Storage in Linux

  • Physical Volumes (PV): Direct partitions on the disk, such as /dev/sda1.

  • Logical Volumes (LV): Created using Logical Volume Manager (LVM) to allow flexible disk management.

  • Mount Points: Directories where volumes are mounted, such as /mnt or /home

2. What is AWS EBS (Elastic Block Store)?

Amazon EBS (Elastic Block Store) is a scalable block storage service for EC2 (Elastic Compute Cloud) instances. It functions like a traditional hard drive but is designed for high availability and durability in AWS.

Key Features of AWS EBS
  • Persistent Storage: Unlike instance store volumes, EBS volumes persist even after an EC2 instance is stopped or terminated.

  • Scalability: Easily resize volumes without downtime using Elastic Volumes.

  • Performance: Available in different types (SSD, HDD) to match workload needs.

  • Backup and Snapshots: Supports snapshots for data backup and recovery.

3. AWS EBS Volume Types

Volume TypeDescriptionUse Case
gp3 (General Purpose SSD)High-performance, low-latency SSDWeb servers, databases
gp2 (General Purpose SSD)Older generation, balanced SSDGeneral workloads
io1/io2 (Provisioned IOPS SSD)High IOPS performanceDatabases, high-transaction apps
st1 (Throughput Optimized HDD)High throughput, lower costBig data, log processing
sc1 (Cold HDD)Lowest cost, for rarely accessed dataArchives, backups

4. Attaching an EBS Volume to an EC2 Instance

To attach and mount an EBS volume in AWS, follow these steps:

Step 1: Create and Attach EBS Volume
  1. Go to AWS EC2 DashboardVolumesCreate Volume.

  2. Select the volume type, size, and availability zone.

  3. Attach the volume to an EC2 instance.

ubuntu@ip-172-31-43-63:~$ lsblk
NAME     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
loop0      7:0    0 26.3M  1 loop /snap/amazon-ssm-agent/9881
loop1      7:1    0 73.9M  1 loop /snap/core22/1722
loop2      7:2    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
ubuntu@ip-172-31-43-63:~$ df -h
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  876K  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
ubuntu@ip-172-31-43-63:~$

Volume attached to EC2 Instance

ubuntu@ip-172-31-43-63:~$ lsblk
NAME     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
loop0      7:0    0 26.3M  1 loop /snap/amazon-ssm-agent/9881
loop1      7:1    0 73.9M  1 loop /snap/core22/1722
loop2      7:2    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   10G  0 disk
xvdg     202:96   0   12G  0 disk
xvdh     202:112  0   14G  0 disk

Physical vs Logical Volumes vs Volume Groups in Linux (LVM)

  • Physical Volume (PV):

    • Represents actual storage devices or partitions (e.g., /dev/sda1).

    • The raw storage units used in LVM (Logical Volume Manager).

    • Created using pvcreate command.

  • Volume Group (VG):

    • A collection of Physical Volumes (PVs) combined into a single storage pool.

    • Allows flexible allocation of storage space.

    • Created using vgcreate command.

  • Logical Volume (LV):

    • A virtual partition created within a Volume Group (VG).

    • Can be resized dynamically without affecting the underlying physical disks.

    • Created using lvcreate command.

Hierarchy in LVM

Physical Volumes (PVs) → Volume Groups (VGs) → Logical Volumes (LVs)

Managing AWS EBS on EC2 Instances

Steps to Create Physical Volumes in LVM on Ubuntu

  1. Switch to Root User

  2. Access LVM Shell

  3. Check Existing Physical Volumes

  4. Create New Physical Volumes

  5. Verify Physical Volumes

ubuntu@ip-172-31-43-63:~$ sudo su
root@ip-172-31-43-63:/home/ubuntu# lvm
lvm> pvs
lvm> pvcreate /dev/xvdf /dev/xvdg /dev/xvdh
  Physical volume "/dev/xvdf" successfully created.
  Physical volume "/dev/xvdg" successfully created.
  Physical volume "/dev/xvdh" successfully created.
lvm> pvs
  PV         VG Fmt  Attr PSize  PFree
  /dev/xvdf     lvm2 ---  10.00g 10.00g
  /dev/xvdg     lvm2 ---  12.00g 12.00g
  /dev/xvdh     lvm2 ---  14.00g 14.00g

Steps to Create a Volume Group in LVM on Ubuntu

  1. Create a Volume Group

    This combines both physical volumes into a single storage pool called tws_vg.

  2. Verify the Volume Group

lvm> vgcreate tws_vg /dev/xvdf /dev/xvdg
  Volume group "tws_vg" successfully created

lvm> vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  tws_vg   2   0   0 wz--n- 21.99g 21.99g

Steps to Create a Logical Volume in LVM on Ubuntu

  1. Create a Logical Volume

    • -L 10G → Allocates 10GB to the logical volume.

    • -n tws_lv → Names the logical volume as tws_lv.

    • tws_vg → Specifies the volume group where the LV is created.

  2. Verify Logical Volumes

    • This will display information about the logical volume, such as size, VG name, and attributes.
lvm> lvcreate -L 10G -n tws_lv tws_vg
  Logical volume "tws_lv" created.

lvm> lvs
  LV     VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  tws_lv tws_vg -wi-a----- 10.00g

LVM Logical Volume Mounting Steps

  1. Create a Mount Point

    • This creates a directory where the logical volume will be mounted.
  2. Format the Logical Volume with ext4 Filesystem

    • This formats tws_lv with the ext4 filesystem.

    • Displays UUID and superblock backup locations.

  3. Mount the Logical Volume - Mounts the LV to /mnt/tws_lv_mount.

  4. Verify Mounting with df -h

root@ip-172-31-43-63:/home/ubuntu# mkfs.ext4 /dev/tws_vg/tws_lv
mke2fs 1.47.0 (5-Feb-2023)
Creating filesystem with 2621440 4k blocks and 655360 inodes
Filesystem UUID: abcce1ea-e1b2-41ec-9cf6-29514a3d2920
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

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

root@ip-172-31-43-63:/home/ubuntu# mount /dev/tws_vg/tws_lv /mnt/tws_lv_mount
root@ip-172-31-43-63:/home/ubuntu# df -h
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  908K  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/mapper/tws_vg-tws_lv  9.8G   24K  9.3G   1% /mnt/tws_lv_mount

Verification of LVM Mount and File Creation

  1. Navigate to the Mounted Directory

    • This moves into the mounted Logical Volume (LV) directory.
  2. Create a New Directory (devops)

    • This creates a subdirectory named devops inside the mounted LV.
  3. Verify the Directory Creation

  4. Navigate to the devops Directory

  5. Create a New File (hello.txt)

  6. Go Back to Home Directory & Verify the File Content

root@ip-172-31-43-63:/home/ubuntu# cd /mnt/tws_lv_mount/
root@ip-172-31-43-63:/mnt/tws_lv_mount# mkdir devops
root@ip-172-31-43-63:/mnt/tws_lv_mount# ls
devops  lost+found
root@ip-172-31-43-63:/mnt/tws_lv_mount# cd devops/
root@ip-172-31-43-63:/mnt/tws_lv_mount/devops# vim hello.txt
root@ip-172-31-43-63:/mnt/tws_lv_mount/devops# cd
root@ip-172-31-43-63:~# cat /mnt/tws_lv_mount/devops/hello.txt
This is LVM Seession

Steps to Unmount and Remount the LVM Volume

  1. Unmount the LVM Volume

    Unmounts the logical volume, making its contents temporarily inaccessible.

  2. Try to Access the File (Fails) - Since the LV is unmounted, the file is no longer accessible.

  3. Remount the LVM Volume - Mounts the logical volume back to /mnt/tws_lv_mount.

  4. Verify the File Again -The file is restored, confirming that data is preserved inside the Logical Volume (LV).

root@ip-172-31-43-63:~# umount /mnt/tws_lv_mount/
root@ip-172-31-43-63:~# cat /mnt/tws_lv_mount/devops/hello.txt
cat: /mnt/tws_lv_mount/devops/hello.txt: No such file or directory
root@ip-172-31-43-63:~# mount /dev/tws_vg/tws_lv /mnt/tws_lv_mount
root@ip-172-31-43-63:~# cat /mnt/tws_lv_mount/devops/hello.txt
This is LVM Seession

Steps to Format and Mount a New Disk (/dev/xvdh)

  1. Create a Mount Point

    This creates a directory where the new disk will be mounted.

  2. Verify the Mount Points

  3. Format /dev/xvdh with ext4 Filesystem

    • The system warns that /dev/xvdh contains an LVM2_member filesystem.

    • You proceeded anyway by typing y.

    • This confirms the ext4 filesystem has been successfully created

  4. Mount the Formatted Disk

    Mounts /dev/xvdh to /mnt/tws_disk_mount.

  5. Verify the Mounted Disk

root@ip-172-31-43-63:~# mkdir /mnt/tws_disk_mount
root@ip-172-31-43-63:~# cd /mnt/
root@ip-172-31-43-63:/mnt# ls
tws_disk_mount  tws_lv_mount
root@ip-172-31-43-63:/mnt# mkfs -t ext4 /dev/xvdh
mke2fs 1.47.0 (5-Feb-2023)
/dev/xvdh contains a LVM2_member file system
Proceed anyway? (y,N) y
Creating filesystem with 3670016 4k blocks and 917504 inodes
Filesystem UUID: ef6f59fe-bb3e-4109-b9b9-f4c09df7b92a
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208

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

root@ip-172-31-43-63:/mnt# cd
root@ip-172-31-43-63:~# mount /dev/xvdh /mnt/tws_disk_mount/
root@ip-172-31-43-63:~# df -h
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  908K  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/mapper/tws_vg-tws_lv  9.8G   32K  9.3G   1% /mnt/tws_lv_mount
/dev/xvdh                   14G   24K   13G   1% /mnt/tws_disk_mount

Steps to Extend a Logical Volume (tws_lv) by 5GB

  1. Extend the Logical Volume (lvextend)

    This increased the Logical Volume (tws_lv) by 5GB.

lvm> lvextend -L +5G /dev/tws_vg/tws_lv
  Size of logical volume tws_vg/tws_lv changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents).
  Logical volume tws_vg/tws_lv successfully resized.
  1. Verify the Logical Volume Size
root@ip-172-31-43-63:/home/ubuntu# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
loop0             7:0    0 26.3M  1 loop /snap/amazon-ssm-agent/9881
loop1             7:1    0 73.9M  1 loop /snap/core22/1722
loop2             7:2    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   10G  0 disk
└─tws_vg-tws_lv 252:0    0   15G  0 lvm  /mnt/tws_lv_mount
xvdg            202:96   0   12G  0 disk
└─tws_vg-tws_lv 252:0    0   15G  0 lvm  /mnt/tws_lv_mount
xvdh            202:112  0   14G  0 disk /mnt/tws_disk_mount

LVM Display Commands in Linux

After creating Physical Volumes (PV), Volume Groups (VG), and Logical Volumes (LV), you can use the following commands to check their details:

1. Display Physical Volume Details

lvm> pvdisplay
  --- Physical volume ---
  PV Name               /dev/xvdf
  VG Name               tws_vg
  PV Size               10.00 GiB / not usable 4.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              2559
  Free PE               2559
  Allocated PE          0
  PV UUID               Z0FCJ2-NMWX-UIy8-PF4G-fdmN-NY0k-v7dKDN

  --- Physical volume ---
  PV Name               /dev/xvdg
  VG Name               tws_vg
  PV Size               12.00 GiB / not usable 4.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              3071
  Free PE               511
  Allocated PE          2560
  PV UUID               zBY2JO-5Bm8-zrJ6-F18H-nVHB-ocEm-NXiuLL

  "/dev/xvdh" is a new physical volume of "14.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/xvdh
  VG Name
  PV Size               14.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               ue1zCy-DLGw-euDq-QuDr-WgsP-koJu-UwXkTv

2. Display Volume Group Details

lvm> vgdisplay
  --- Volume group ---
  VG Name               tws_vg
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               21.99 GiB
  PE Size               4.00 MiB
  Total PE              5630
  Alloc PE / Size       2560 / 10.00 GiB
  Free  PE / Size       3070 / 11.99 GiB
  VG UUID               WgOCgI-QyP4-Z1jP-LauN-4fZa-KTEG-9l92ZS
  1. Display Logical Volume Details
lvm> lvdisplay
  --- Logical volume ---
  LV Path                /dev/tws_vg/tws_lv
  LV Name                tws_lv
  VG Name                tws_vg
  LV UUID                KJ8maX-zp5g-wyQa-vFho-92P4-j3QE-K2ije1
  LV Write Access        read/write
  LV Creation host, time ip-172-31-43-63, 2025-02-25 17:37:26 +0000
  LV Status              available
  # open                 0
  LV Size                10.00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

Conclusion

We successfully managed LVM storage on Ubuntu by:

✅ Creating Physical Volumes (PV), Volume Group (VG), and Logical Volume (LV)
✅ Formatting and mounting the LV (tws_lv) and a separate disk (xvdh)
✅ Extending the LV by 5GB and resizing the filesystem
✅ Verifying the updated storage and filesystem size

This demonstrates efficient LVM management, allowing flexible and scalable storage allocation in Linux.