LVM — Logical Volume Manager

30 minLesson 10 of 16

Learning Objectives

  • Understand the LVM architecture (PV, VG, LV)
  • Create physical volumes, volume groups, and logical volumes
  • Format and mount logical volumes
  • Extend and reduce logical volumes online

Why LVM?

Traditional partitions are rigid — resizing requires unmounting, moving data, and risking loss. LVM adds a flexible layer between physical disks and file systems.

ProblemLVM Solution
Can't resize partitions easilyExtend/shrink volumes online
One partition = one diskSpan volumes across multiple disks
Cryptic device namesMeaningful volume names
Risky data migrationMove data between disks live

LVM Architecture

┌─────────────────────────────────────────────┐
│           File Systems (ext4, xfs)          │
├─────────────────────────────────────────────┤
│         Logical Volumes (LV)                │
│    /dev/vg_data/lv_web   /dev/vg_data/lv_db│
├─────────────────────────────────────────────┤
│           Volume Groups (VG)                │
│              vg_data                        │
├──────────────────┬──────────────────────────┤
│  Physical Volume │  Physical Volume         │
│   /dev/sdb1      │   /dev/sdc1             │
├──────────────────┴──────────────────────────┤
│         Physical Disks                      │
│      /dev/sdb        /dev/sdc              │
└─────────────────────────────────────────────┘

Three layers:

  1. Physical Volumes (PV) — Disks or partitions prepared for LVM
  2. Volume Groups (VG) — Pools combining multiple PVs
  3. Logical Volumes (LV) — Virtual partitions carved from VGs

Install LVM

sudo apt install lvm2 -y

Step 1: Create Physical Volumes

# Scan available disks
sudo lvmdiskscan
 
# Create PVs from disks or partitions
sudo pvcreate /dev/sdb
sudo pvcreate /dev/sdc
 
# Verify
sudo pvs
sudo pvdisplay /dev/sdb

Step 2: Create a Volume Group

Combine physical volumes into a pool:

# Create VG named "vg_data" from two PVs
sudo vgcreate vg_data /dev/sdb /dev/sdc
 
# Verify
sudo vgs
sudo vgdisplay vg_data

Output from vgs:

VG      #PV #LV #SN Attr   VSize  VFree
vg_data   2   0   0 wz--n- 9.99g  9.99g

Step 3: Create Logical Volumes

Carve virtual partitions from the volume group:

# Create a 5GB logical volume
sudo lvcreate -n lv_web -L 5G vg_data
 
# Create another 3GB logical volume
sudo lvcreate -n lv_db -L 3G vg_data
 
# Verify
sudo lvs

Output:

LV     VG      Attr       LSize
lv_web vg_data -wi-a----- 5.00g
lv_db  vg_data -wi-a----- 3.00g

Step 4: Format and Mount

# Format with ext4
sudo mkfs -t ext4 /dev/vg_data/lv_web
sudo mkfs -t ext4 /dev/vg_data/lv_db
 
# Create mount points
sudo mkdir -p /var/www
sudo mkdir -p /var/lib/mysql
 
# Mount
sudo mount /dev/vg_data/lv_web /var/www
sudo mount /dev/vg_data/lv_db /var/lib/mysql
 
# Verify
df -hT | grep vg_data

Make Persistent

# Add to /etc/fstab
echo '/dev/vg_data/lv_web /var/www ext4 defaults 0 2' | sudo tee -a /etc/fstab
echo '/dev/vg_data/lv_db /var/lib/mysql ext4 defaults 0 2' | sudo tee -a /etc/fstab
 
# Test
sudo mount -a

Extending Logical Volumes

The killer feature of LVM — grow storage without downtime:

# Check available space in VG
sudo vgs
 
# Extend logical volume by 2GB
sudo lvextend -L +2G /dev/vg_data/lv_web
 
# Resize the file system to use new space
sudo resize2fs /dev/vg_data/lv_web
 
# Verify
df -h /var/www

Extend VG with New Disk

# Add new disk as PV
sudo pvcreate /dev/sdd
 
# Add PV to existing VG
sudo vgextend vg_data /dev/sdd
 
# Now extend LV with the new space
sudo lvextend -L +5G /dev/vg_data/lv_web
sudo resize2fs /dev/vg_data/lv_web

Reducing Logical Volumes

⚠️

Reducing volumes can cause data loss. Always back up first and unmount the file system before shrinking.

# Unmount first
sudo umount /var/www
 
# Check file system
sudo e2fsck -f /dev/vg_data/lv_web
 
# Shrink file system first
sudo resize2fs /dev/vg_data/lv_web 3G
 
# Then shrink logical volume
sudo lvreduce -L 3G /dev/vg_data/lv_web
 
# Remount
sudo mount /dev/vg_data/lv_web /var/www

Removing LVM Components

Remove in reverse order (LV → VG → PV):

# Unmount
sudo umount /var/lib/mysql
 
# Remove logical volume
sudo lvremove /dev/vg_data/lv_db
 
# Remove volume group (removes all LVs in it)
sudo vgremove vg_data
 
# Remove physical volumes
sudo pvremove /dev/sdb /dev/sdc

Command Reference

CommandPurpose
pvs / pvdisplayList physical volumes
vgs / vgdisplayList volume groups
lvs / lvdisplayList logical volumes
pvcreateInitialize disk for LVM
vgcreateCreate volume group
lvcreateCreate logical volume
lvextendGrow logical volume
lvreduceShrink logical volume
vgextendAdd PV to volume group
resize2fsResize ext4 file system

Summary

  • LVM provides flexible storage management with three layers: PV → VG → LV
  • Logical volumes can be extended online without downtime
  • Volume groups can span multiple physical disks
  • Always resize2fs after lvextend to use the new space
  • Reducing volumes requires unmounting and is risky — back up first
  • Use meaningful names: vg_data, lv_web, lv_db

Next Steps

With storage mastered, let's move to networking — understanding TCP/IP, IP addressing, and network configuration on Linux.