LVM and Partition Management in Practice: Flexible Storage Operations

LVM and Partition Management in Practice: Flexible Storage Operations

What Is LVM and Why Use It?

LVM (Logical Volume Manager) abstracts physical disks into virtual volumes, enabling dynamic capacity expansion, snapshots, and pooling multiple disks — operations that standard partitions cannot do. Once you partition a disk conventionally, resizing is risky and requires downtime. LVM removes that constraint.

The core use case: "Add a disk and expand a live volume without downtime" and "Take a snapshot before a risky operation and roll back if something breaks."

When LVM is the right tool

  • A production server's /var or /home is running out of space
  • Future disk additions are expected
  • You need snapshot-based backups for databases or container volumes

Understanding the 3-Layer LVM Architecture

LVM is organized into three abstraction layers:

Layer Component Role
Physical PV (Physical Volume) Actual disk or partition
Middle VG (Volume Group) Storage pool combining one or more PVs
Logical LV (Logical Volume) Virtual partition carved from a VG
/dev/sdb  /dev/sdc  ←── PV (Physical Volumes)
       ↘      ↙
      vg_data      ←── VG (Volume Group)
       ↓     ↓
  lv_app  lv_logs  ←── LV (Logical Volumes)

Multiple PVs can be combined into a single VG, allowing a logical volume to span physical disks. LV sizes can be changed freely within the VG's available space.

Checking Current LVM Status

Always verify the current state before making changes. The pvs, vgs, and lvs commands give a concise summary.

sudo pvs
  PV         VG      Fmt  Attr PSize   PFree
  /dev/sdb   vg_data lvm2 a--  100.00g 50.00g
sudo vgs
  VG      #PV #LV #SN Attr   VSize   VFree
  vg_data   1   1   0 wz--n- 100.00g 50.00g
sudo lvs
  LV     VG      Attr       LSize  Pool Origin Data%
  lv_app vg_data -wi-ao---- 50.00g

For detailed information:

sudo pvdisplay /dev/sdb
sudo vgdisplay vg_data
sudo lvdisplay /dev/vg_data/lv_app

Building a New LVM Setup

Step 1: Create Physical Volume (PV)

sudo pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.

Step 2: Create Volume Group (VG)

sudo vgcreate vg_data /dev/sdb
  Volume group "vg_data" successfully created

To combine multiple disks into one pool:

sudo vgcreate vg_data /dev/sdb /dev/sdc

Step 3: Create Logical Volume (LV)

# Create with a specific size
sudo lvcreate -L 50G -n lv_app vg_data

# Use all available VG space
sudo lvcreate -l 100%FREE -n lv_app vg_data

Step 4: Create Filesystem and Mount

sudo mkfs.ext4 /dev/vg_data/lv_app
sudo mkdir -p /mnt/app
sudo mount /dev/vg_data/lv_app /mnt/app

For persistent mounts that survive reboots, add an entry to /etc/fstab:

# Get the UUID
sudo blkid /dev/vg_data/lv_app
/dev/vg_data/lv_app: UUID="a1b2c3d4-e5f6-7890-abcd-ef1234567890" TYPE="ext4"

Add to /etc/fstab:

UUID=a1b2c3d4-e5f6-7890-abcd-ef1234567890 /mnt/app ext4 defaults 0 2

Extending a Logical Volume

The key advantage of LVM: expand capacity while the volume is mounted (online resize).

When VG Has Free Space — Simplest Case

# Add 10GB and resize the filesystem at the same time (-r runs resize2fs automatically)
sudo lvextend -L +10G -r /dev/vg_data/lv_app
  Size of logical volume vg_data/lv_app changed from 50.00 GiB to 60.00 GiB.
  Logical volume vg_data/lv_app successfully resized.
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/vg_data/lv_app is mounted on /mnt/app; on-line resizing required
The filesystem on /dev/vg_data/lv_app is now 15728640 blocks long.

Without -r, resize the filesystem manually after lvextend:

sudo lvextend -L +10G /dev/vg_data/lv_app
sudo resize2fs /dev/vg_data/lv_app   # ext4
# sudo xfs_growfs /mnt/app           # XFS

XFS cannot be shrunk — only expanded. ext4 supports both shrink and grow, but shrinking requires unmounting the volume first.

When VG Has No Free Space — Adding a New Disk

# 1. Initialize the new disk as a PV
sudo pvcreate /dev/sdc

# 2. Add it to the existing VG
sudo vgextend vg_data /dev/sdc

# 3. Confirm the VG now has more free space
sudo vgs vg_data

# 4. Extend the LV
sudo lvextend -L +100G -r /dev/vg_data/lv_app

Check before extending:

# VG free space
sudo vgs vg_data
# Current LV size
sudo lvs /dev/vg_data/lv_app
# Filesystem usage
df -h /mnt/app

Creating and Using Snapshots

A snapshot captures the state of an LV at a specific point in time. Use it before risky operations or as a backup mechanism.

# Create a 5GB snapshot of lv_app
sudo lvcreate -L 5G -s -n lv_app_snap /dev/vg_data/lv_app

Mount the snapshot for read-only inspection:

sudo mkdir -p /mnt/app_snap
sudo mount -o ro /dev/vg_data/lv_app_snap /mnt/app_snap

Restore from snapshot (overwrites the original LV):

sudo umount /mnt/app
sudo lvconvert --merge /dev/vg_data/lv_app_snap
# The snapshot is automatically removed after the merge

If a snapshot fills up, it becomes invalid. Estimate the write volume to the origin LV and allocate enough space. Monitor usage with Data% in lvs output.

sudo lvs /dev/vg_data/lv_app_snap

Removing Logical Volumes and VGs

# 1. Unmount first
sudo umount /mnt/app

# 2. Remove the LV
sudo lvremove /dev/vg_data/lv_app

# 3. Remove the VG (only when no LVs remain)
sudo vgremove vg_data

# 4. Remove the PV label
sudo pvremove /dev/sdb

Troubleshooting Common LVM Issues

df still shows old size after lvextend

# The filesystem was not resized — do it manually
sudo resize2fs /dev/vg_data/lv_app   # ext4
sudo xfs_growfs /mnt/app             # XFS

vgextend fails — device not found

# Check whether the device is initialized as a PV
sudo pvs
sudo pvscan

Logical volume is inactive

sudo lvchange -ay /dev/vg_data/lv_app

Snapshot Data% is climbing toward 100%

The snapshot is filling up. Extend it before it becomes invalid:

sudo lvextend -L +5G /dev/vg_data/lv_app_snap

Next Reading