Skip to content

Logical Volume Management [LVM]

  • Traditional partitioning strictly binds filesystems to physical disks (e.g., /dev/sda1).
  • Logical Volume Manager (LVM) creates an abstraction layer between physical hardware and the operating system.

Core Advantages:

  • Zero-Downtime Expansion: Resize partitions on the fly without rebooting Server.
  • Storage Pooling: Combine multiple physical disks into a single massive virtual drive.
  • Snapshots: Create point-in-time backups for safe updates or data recovery.
  • Agility: Easily allocate, shrink, or move storage as application needs change.

lvm.png

LVM operates in a strict, three-tier hierarchy: Hardware Disk → Physical Volume (PV) → Volume Group (VG) → Logical Volume (LV) → Filesystem

  1. Physical Volumes (PV): The raw building blocks. These are physical disks (e.g., /dev/sdb) or standard partitions initialized for LVM use.
  2. Volume Groups (VG): The storage pool. PVs are grouped together to create a single, large storage reservoir. Logic: Always leave some unallocated free space in a VG for future snapshots or LV expansion.
  3. Logical Volumes (LV): The usable partitions. Carved out of the VG, these act as virtual hard drives that you format (ext4, xfs) and mount to your directory structure.

A Physical Volume is:

  • A whole disk (e.g. /dev/sdb)
  • OR a partition (e.g. /dev/sdb1)
  • Initialized for LVM
Terminal window
sudo pvcreate /dev/sdb
Terminal window
sudo pvs

Detailed:

Terminal window
sudo pvdisplay

A Volume Group:

  • Combines one or more PVs
  • Acts like a storage pool
Terminal window
sudo vgcreate my_vg /dev/sdb

Add another disk to existing VG:

Terminal window
sudo vgextend my_vg /dev/sdc
Terminal window
sudo vgs

Detailed:

Terminal window
sudo vgdisplay

A Logical Volume:

  • Created inside a VG
  • Works like a normal partition
  • You format and mount it
Terminal window
sudo lvcreate -L 10G -n my_lv my_vg

Or use all free space:

Terminal window
sudo lvcreate -l 100%FREE -n my_lv my_vg
Terminal window
sudo lvs

Detailed:

Terminal window
sudo lvdisplay

After creating LV:

Terminal window
sudo mkfs.ext4 /dev/my_vg/my_lv
Terminal window
sudo mkdir /mnt/mydata
Terminal window
sudo mount /dev/my_vg/my_lv /mnt/mydata

Add to /etc/fstab:

/dev/my_vg/my_lv /mnt/mydata ext4 defaults 0 0

This is why LVM is powerful.


Terminal window
sudo lvextend -L +5G /dev/my_vg/my_lv

For ext4:

Terminal window
sudo resize2fs /dev/my_vg/my_lv

For xfs:

Terminal window
sudo xfs_growfs /mnt/mydata

🔽 Reduce Logical Volume (Careful ⚠️)

Section titled “🔽 Reduce Logical Volume (Careful ⚠️)”

⚠️ Always shrink filesystem first.

Terminal window
sudo umount /mnt/mydata
Terminal window
sudo e2fsck -f /dev/my_vg/my_lv
Terminal window
sudo resize2fs /dev/my_vg/my_lv 5G
Terminal window
sudo lvreduce -L 5G /dev/my_vg/my_lv

Used for:

  • Backups
  • Safe upgrades
  • Testing changes
Terminal window
sudo lvcreate -L 2G -s -n my_snapshot /dev/my_vg/my_lv
Terminal window
sudo lvremove /dev/my_vg/my_snapshot

Terminal window
sudo lvremove /dev/my_vg/my_lv
Terminal window
sudo vgremove my_vg
Terminal window
sudo pvremove /dev/sdb
CategoryCommandPurpose
Monitoring/InfolsblkList block devices and their tree structure.
pvs / vgs / lvsQuick summary of PVs, VGs, or LVs.
pvdisplay / vgdisplay / lvdisplayDetailed metadata for PVs, VGs, or LVs.
watch -n 2 df -hContinuously monitor disk space every 2 seconds.
blkid <path>Get the UUID of a block device (needed for fstab).
Physical Volumespvcreate <disk>Initialize disk for LVM use.
pvremove <disk>Wipe LVM metadata from a disk.
Volume Groupsvgcreate <name> <pv>Create a new pool from physical volume(s).
vgextend <name> <pv>Add a new physical disk to an existing pool.
vgremove <name>Delete a volume group.
Logical Volumeslvcreate -L 5G -n <name> <vg>Create a new logical volume.
lvextend -L +5G <lv_path>Increase LV size by 5GB.
lvextend -l +100%FREE <lv> -rUse all free VG space AND automatically resize filesystem.
lvreduce -L 5G <lv_path>Shrink LV size to 5GB (Shrink FS first!).
lvchange -an <lv> / -ay <lv>Deactivate (-an) or Activate (-ay) an LV.
lvremove <lv_path>Delete a logical volume.
Filesystemsmkfs.ext4 <lv_path>Format the LV with the ext4 filesystem.
mount <lv_path> <mount_dir>Mount the filesystem to a directory.
umount <mount_dir>Unmount the filesystem.
mount -aTest /etc/fstab configuration to ensure safe reboots.
resize2fs <lv_path>Expand/shrink ext4 filesystem to match LV size.
e2fsck -f <lv_path>Force check filesystem for errors (required before shrinking).
Snapshotslvcreate -s -L 1G -n <name> <lv>Create a 1GB point-in-time snapshot.
lvconvert --merge <snap_path>Rollback the original LV to the snapshot’s state.
Testing/Utilscat /dev/zero > <file>Fill a file with zeros to test 100% disk usage.
truncate -s 0 <file>Empty a file instantly to free space.
apt cleanClear local package cache to free emergency space.

Step 1: Initialize Physical Volumes: sudo pvcreate /dev/sdb /dev/sdc

Step 2: Pool into a Volume Group: sudo vgcreate data_vg /dev/sdb /dev/sdc

Step 3: Carve out a Logical Volume

Terminal window
# Create a specific size LV
sudo lvcreate -L 10G -n my_lv data_vg
# OR use 100% of the available free space
sudo lvcreate -l 100%FREE -n my_lv data_vg

Step 4: Format and Mount

Terminal window
sudo mkfs.ext4 /dev/data_vg/my_lv
sudo mkdir -p /mnt/mydata
sudo mount /dev/data_vg/my_lv /mnt/mydata

(To make permanent, add to /etc/fstab: /dev/data_vg/my_lv /mnt/mydata ext4 defaults 0 0)


  • Scenario A: Adding a new disk to vg_1

    Terminal window
    # Initialize the new disk
    sudo pvcreate /dev/nvme4n1
    # Extend the Volume Group
    sudo vgextend vg_1 /dev/nvme4n1
  • Scenario B: Resizing an existing EBS volume If you increased from 50GB to 70GB in AWS:

    Terminal window
    # Update LVM metadata to recognize the new physical size
    sudo pvresize /dev/nvme1n1
  • Extending Storage (Safe & Online)
    1. Add underlying space (if VG is full): sudo vgextend data_vg /dev/sdd
    2. Extend the LV: sudo lvextend -L +5G /dev/data_vg/my_lv (or l +100%FREE to use all space).
    3. Extend the Filesystem to match: sudo resize2fs /dev/data_vg/my_lv (Use xfs_growfs for XFS).
Terminal window
# 1. Extend the LV
sudo lvextend -L +10G /dev/vg_1/lv_data1
# 2. Resize the Filesystem (Online)
# For XFS (Amazon Linux default):
sudo xfs_growfs /mnt/data1
# For EXT4:
# sudo resize2fs /dev/vg_1/lv_data1
# Check LV size
sudo lvs
# Check usable disk space
df -h /mnt/data1
  • Reducing Storage (High Risk ⚠️)

    Rule: Always shrink the filesystem BEFORE shrinking the Logical Volume to prevent data corruption.

    1. Unmount: sudo umount /mnt/mydata
    2. Check File System: sudo e2fsck -f /dev/data_vg/my_lv
    3. Shrink File System: sudo resize2fs /dev/data_vg/my_lv 5G
    4. Shrink LV: sudo lvreduce -L 5G /dev/data_vg/my_lv
  • Permanent Mount

    • Update /etc/fstab Append these lines to the end of /etc/fstab:

      Terminal window
      /dev/vg_1/lv_data1 /mnt/data1 xfs defaults 0 0
      /dev/vg_2/lv_data2 /mnt/data2 xfs defaults 0 0
    • Verify the Configuration

      Crucial Step: Test the fstab entries immediately. If there is a syntax error, the system may fail to boot.

      Terminal window
      # Unmount current sessions
      sudo umount /mnt/data1 /mnt/data2
      # Attempt to mount everything defined in fstab
      sudo mount -a
      # Check if they are back
      df -h | grep data
    • Logic: mount -a reads the fstab file. If it returns no errors, your configuration is safe for reboot.


Snapshots capture the exact state of an LV at a specific moment. They require free space in the VG.

  • Create Snapshot: sudo lvcreate -s -L 2G -n my_snap /dev/data_vg/my_lv
  • Restore Data (Merge):
    1. Unmount original LV: sudo umount /mnt/mydata
    2. Merge snapshot: sudo lvconvert --merge /dev/data_vg/my_snap
    3. Reactivate LV: sudo lvchange -ay /dev/data_vg/my_lv
    4. Remount: sudo mount /dev/data_vg/my_lv /mnt/mydata

  • Scenario: You have two 500GB drives, but need an 800GB database partition.
  • LVM Logic: Initialize both as PVs -> Combine into one 1TB VG -> Create an 800GB LV -> Mount.
  • Interview Question: “A production server’s disk is at 100%. How do you fix it without downtime?”
  • Answer: Insert new disk -> pvcreate -> vgextend -> lvextend -> resize2fs. Total uptime maintained.

DUMMY Disks and Partitions