What is LVM?
LVM (Logical Volume Manager) adds a layer of abstraction between physical disks and file systems. It allows you to resize volumes, add disks, take snapshots, and manage storage flexibly without downtime. LVM is especially valuable on Breezes and production servers where storage needs evolve over time.
LVM Concepts
- PV (Physical Volume) — a disk or partition initialized for LVM
- VG (Volume Group) — a pool of one or more PVs
- LV (Logical Volume) — a virtual partition carved from a VG (this is what you format and mount)
Installation
sudo apt update
sudo apt install -y lvm2
Creating LVM from Scratch
Step 1: Create Physical Volumes
# Initialize disks as physical volumes
sudo pvcreate /dev/sdb /dev/sdc
# Verify
sudo pvs
sudo pvdisplay
Step 2: Create a Volume Group
# Create VG named "vg_data" from both PVs
sudo vgcreate vg_data /dev/sdb /dev/sdc
# Verify
sudo vgs
sudo vgdisplay vg_data
Step 3: Create Logical Volumes
# Create a 50GB logical volume
sudo lvcreate -L 50G -n lv_www vg_data
# Create a volume using 100% of free space
sudo lvcreate -l 100%FREE -n lv_backups vg_data
# Verify
sudo lvs
sudo lvdisplay
Step 4: Format and Mount
# Format with ext4
sudo mkfs.ext4 /dev/vg_data/lv_www
# Mount
sudo mkdir -p /var/www
sudo mount /dev/vg_data/lv_www /var/www
# Add to fstab
echo "/dev/vg_data/lv_www /var/www ext4 defaults 0 2" | sudo tee -a /etc/fstab
Extending Volumes
# Extend a logical volume by 20GB
sudo lvextend -L +20G /dev/vg_data/lv_www
# Resize the filesystem to use the new space
sudo resize2fs /dev/vg_data/lv_www # ext4
sudo xfs_growfs /var/www # XFS
# Extend and resize in one command
sudo lvextend -L +20G --resizefs /dev/vg_data/lv_www
Adding More Physical Disks
# Add a new disk to the volume group
sudo pvcreate /dev/sdd
sudo vgextend vg_data /dev/sdd
# Now you can extend logical volumes using the new space
sudo lvextend -l 100%FREE /dev/vg_data/lv_backups --resizefs
LVM Snapshots
# Create a snapshot (requires free space in the VG)
sudo lvcreate -L 10G -s -n lv_www_snap /dev/vg_data/lv_www
# Mount snapshot as read-only for backup
sudo mkdir -p /mnt/snapshot
sudo mount -o ro /dev/vg_data/lv_www_snap /mnt/snapshot
# Remove snapshot when done
sudo umount /mnt/snapshot
sudo lvremove /dev/vg_data/lv_www_snap
Reducing Volumes (ext4 only)
# Unmount first
sudo umount /var/www
# Check filesystem
sudo e2fsck -f /dev/vg_data/lv_www
# Shrink filesystem then LV
sudo resize2fs /dev/vg_data/lv_www 30G
sudo lvreduce -L 30G /dev/vg_data/lv_www
# Remount
sudo mount /dev/vg_data/lv_www /var/www
Key Commands Summary
pvs,vgs,lvs— quick status overviewpvdisplay,vgdisplay,lvdisplay— detailed infopvcreate,vgcreate,lvcreate— creationvgextend,lvextend— expansionlvreduce,vgreduce— shrinking