Skip to content

18. LVM Storage

Traditional disk partitioning (MBR and GPT) binds filesystems directly to physical disk boundaries. When a partition runs out of space, resizing it is complex, risky, and often requires taking systems offline.

Logical Volume Management (LVM) introduces an abstraction layer between physical storage devices and the operating system filesystems. It allows multiple physical drives to be aggregated into a shared storage pool, enabling online partition resizing, non-disruptive disk migrations, and point-in-time snapshots.

LVM organizes storage into three foundational layers:

Physical Disks / Partitions (/dev/sdb, /dev/sdc)
↓
Physical Volumes (PV) [Disks/partitions initialized with an LVM header]
↓
Volume Group (VG) [Unified storage pool combining all PVs]
↓
Logical Volumes (LV) [Virtual partitions carved out of the VG pool]
↓
Filesystem (ext4, xfs) [Mounted into Linux directory tree]

Physical Extents (PE) & Logical Extents (LE)

Section titled “Physical Extents (PE) & Logical Extents (LE)”

Under the hood, LVM divides storage space into small, fixed-size contiguous chunks called Extents:

  • Physical Extent (PE): The smallest allocatable storage block on a Physical Volume (default size is 4 MB).
  • Logical Extent (LE): The corresponding storage block on a Logical Volume. Each LE maps to a PE.

Because an LV is simply an ordered collection of extents, an LV can be expanded by assigning additional PEs from anywhere in the Volume Group, even across different physical disks!

  • Dynamic Online Resizing: Expand logical volumes and filesystems on the fly without unmounting or rebooting.
  • Unified Storage Pooling: Aggregate multiple physical drives (e.g., two 500 GB disks) into a single 1 TB Volume Group.
  • Snapshots: Create point-in-time copy-on-write copies of logical volumes for consistent database backups or pre-update testing.
  • Data Migration (pvmove): Move live data extents from an old physical drive to a new physical drive with zero downtime.
  • Performance Striping: Distribute data blocks evenly across multiple physical disks (similar to RAID 0) to maximize read/write throughput.
  • Redundancy (Mirroring): Store duplicate copies of extents across distinct physical disks (similar to RAID 1) for fault tolerance.
  • Hardware Abstraction: Underlying disk geometries and controllers are hidden from applications and users.

18.3 Step-by-Step LVM Workflow: Create, Format & Mount

Section titled “18.3 Step-by-Step LVM Workflow: Create, Format & Mount”

Identify raw disks or partitions using lsblk or fdisk -l, then initialize them:

Terminal window
# Initialize physical partitions/disks
sudo pvcreate /dev/sdb1 /dev/sdc1
# Verify creation (concise view)
sudo pvs
# View detailed physical volume attributes
sudo pvdisplay

Step 2: Create and Manage Volume Groups (VG)

Section titled “Step 2: Create and Manage Volume Groups (VG)”

Combine one or more PVs into a unified storage pool:

Terminal window
# Create Volume Group named 'vg_data' from the initialized PVs
sudo vgcreate vg_data /dev/sdb1 /dev/sdc1
# Verify Volume Group
sudo vgs
sudo vgdisplay
# Extend a Volume Group by adding a new disk later:
sudo pvcreate /dev/sdd1
sudo vgextend vg_data /dev/sdd1

Carve out virtual partitions from the VG pool:

Terminal window
# Create a 20 GB Logical Volume named 'lv_app'
sudo lvcreate -L 20G -n lv_app vg_data
# Alternatively, allocate 100% of remaining free space in the VG:
# sudo lvcreate -l 100%FREE -n lv_app vg_data
# Verify Logical Volumes
sudo lvs
sudo lvdisplay

Step 4: Format and Mount the Logical Volume

Section titled “Step 4: Format and Mount the Logical Volume”

The Logical Volume is accessible as a block device at /dev/vg_name/lv_name or /dev/mapper/vg_name-lv_name.

Terminal window
# Format with ext4 or xfs
sudo mkfs.ext4 /dev/vg_data/lv_app
# Create mount point and mount the volume
sudo mkdir -p /mnt/appdata
sudo mount /dev/vg_data/lv_app /mnt/appdata
# Verify mount status
df -h | grep /mnt/appdata

Add an entry to /etc/fstab so the volume mounts automatically on system boot:

/dev/vg_data/lv_app /mnt/appdata ext4 defaults 0 2

If storage utilization approaches capacity and the Volume Group has unallocated extents, extend the LV and its underlying filesystem immediately:

Terminal window
# Option A: Extend LV and resize filesystem in a single command using -r
sudo lvextend -r -L +10G /dev/vg_data/lv_app
# Option B: Manual two-step process
# 1. Extend the LV:
sudo lvextend -L +10G /dev/vg_data/lv_app
# 2. Resize the filesystem:
# For ext4:
sudo resize2fs /dev/vg_data/lv_app
# For XFS:
sudo xfs_growfs /mnt/appdata

Reducing a Logical Volume (Offline & Caution Required)

Section titled “Reducing a Logical Volume (Offline & Caution Required)”

[!WARNING] Shrinking a filesystem carries risk of data corruption. XFS filesystems cannot be shrunk. Only ext4 supports reduction, and the volume must be unmounted before shrinking.

  1. Unmount the filesystem:
    Terminal window
    sudo umount /mnt/appdata
  2. Force an integrity check on the filesystem:
    Terminal window
    sudo e2fsck -f /dev/vg_data/lv_app
  3. Shrink the filesystem first:
    Terminal window
    sudo resize2fs /dev/vg_data/lv_app 10G
  4. Shrink the Logical Volume second:
    Terminal window
    sudo lvreduce -L 10G /dev/vg_data/lv_app
  5. Remount the filesystem:
    Terminal window
    sudo mount /dev/vg_data/lv_app /mnt/appdata

An LVM snapshot is a point-in-time, copy-on-write image of an existing Logical Volume. It only consumes storage space as blocks on the origin volume change.

Terminal window
# Create a 5 GB snapshot named 'lv_app_snap' for 'lv_app'
sudo lvcreate -s -L 5G -n lv_app_snap /dev/vg_data/lv_app
# Mount snapshot in read-only mode for consistent backups
sudo mkdir -p /mnt/snapshot
sudo mount -o ro /dev/vg_data/lv_app_snap /mnt/snapshot
# Once backup finishes, unmount and delete snapshot
sudo umount /mnt/snapshot
sudo lvremove -y /dev/vg_data/lv_app_snap

18.6 Deleting an LVM Stack (Strict Tear-Down Order)

Section titled “18.6 Deleting an LVM Stack (Strict Tear-Down Order)”

When decommissioning storage, LVM layers must be removed in strict reverse order to maintain metadata integrity:

1. Unmount Filesystem (umount)
↓
2. Remove Logical Volume (lvremove)
↓
3. Remove Volume Group (vgremove)
↓
4. Remove Physical Volume (pvremove)
Terminal window
# Step 1: Unmount the filesystem and remove /etc/fstab entry
sudo umount /mnt/appdata
# Step 2: Remove the Logical Volume
sudo lvremove /dev/vg_data/lv_app
# Step 3: Remove the Volume Group
sudo vgremove vg_data
# Step 4: Remove the Physical Volume headers
sudo pvremove /dev/sdb1 /dev/sdc1

| Operation | Primary Command | Notes | |||| | Create Physical Volume | sudo pvcreate /dev/sdX1 | Writes LVM identifier header | | List Physical Volumes | sudo pvs / sudo pvdisplay | Shows PV size and PE allocation | | Create Volume Group | sudo vgcreate vg_name /dev/sdX1 | Combines PVs into storage pool | | Expand Volume Group | sudo vgextend vg_name /dev/sdY1 | Adds new PV to existing VG | | Create Logical Volume | sudo lvcreate -L 20G -n lv_name vg_name | Allocates virtual partition | | List Logical Volumes | sudo lvs / sudo lvdisplay | Shows LV sizes and snapshot states | | Extend LV & Filesystem | sudo lvextend -r -L +10G /dev/vg/lv | Online resize (-r handles fs) | | Create Snapshot | sudo lvcreate -s -L 5G -n snap /dev/vg/lv | Copy-on-write snapshot | | Delete Logical Volume | sudo lvremove /dev/vg/lv | Must unmount filesystem first | | Delete Volume Group | sudo vgremove vg_name | Requires all LVs to be removed first | | Delete Physical Volume | sudo pvremove /dev/sdX1 | Clears LVM disk header |