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.
| Problem | LVM Solution |
|---|---|
| Can't resize partitions easily | Extend/shrink volumes online |
| One partition = one disk | Span volumes across multiple disks |
| Cryptic device names | Meaningful volume names |
| Risky data migration | Move 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:
- Physical Volumes (PV) — Disks or partitions prepared for LVM
- Volume Groups (VG) — Pools combining multiple PVs
- Logical Volumes (LV) — Virtual partitions carved from VGs
Install LVM
sudo apt install lvm2 -yStep 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/sdbStep 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_dataOutput 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 lvsOutput:
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_dataMake 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 -aExtending 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/wwwExtend 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_webReducing 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/wwwRemoving 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/sdcCommand Reference
| Command | Purpose |
|---|---|
pvs / pvdisplay | List physical volumes |
vgs / vgdisplay | List volume groups |
lvs / lvdisplay | List logical volumes |
pvcreate | Initialize disk for LVM |
vgcreate | Create volume group |
lvcreate | Create logical volume |
lvextend | Grow logical volume |
lvreduce | Shrink logical volume |
vgextend | Add PV to volume group |
resize2fs | Resize 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
resize2fsafterlvextendto 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.