Logical Volume Management (LVM)
What Is LVM?
Section titled “What Is LVM?”LVM (Logical Volume Management) is a storage abstraction layer built into the Linux kernel (via the device mapper framework). Instead of working directly with fixed partitions, LVM lets you:
- Pool multiple physical drives/partitions into a single storage group
- Carve flexible logical volumes from that pool - resized, moved, or snapshotted while online
- Name volumes meaningfully:
databases,backups,www
The current implementation is LVM2, which is what all modern Linux distributions use.
Why Use LVM?
Section titled “Why Use LVM?”| Problem | LVM Solution |
|---|---|
| ”Disk is full, but I can’t resize the partition” | Add a new disk to the volume group and extend the LV online |
| ”I need to take a snapshot before upgrading” | LVM snapshots are instant and space-efficient |
| ”I want data spread across multiple disks” | LVM striping (like RAID 0) |
| “I need redundancy” | LVM mirroring |
| ”I need to migrate data off a failing disk” | pvmove migrates data live without downtime |
LVM Architecture
Section titled “LVM Architecture”LVM layers abstractions on top of physical storage:
Physical Disks / Partitions | Physical Volumes (PV) - Raw storage marked for LVM use | Volume Group (VG) - Pool combining one or more PVs | Logical Volumes (LV) - Flexible "virtual partitions" carved from VG | Filesystem (ext4, xfs) - Formatted and mounted like any partitionThe Three Layers
Section titled “The Three Layers”| Layer | Command prefix | Description |
|---|---|---|
| Physical Volume (PV) | pv... | A disk, partition, or RAID array marked for LVM. LVM writes a PV header to claim it. |
| Volume Group (VG) | vg... | A pool combining one or more PVs. Presents unified storage. |
| Logical Volume (LV) | lv... | A slice of a VG. Acts like a partition - you format and mount it. |
Extents - The Allocation Unit
Section titled “Extents - The Allocation Unit”Each volume is carved into small, fixed-size chunks called extents:
- Physical extents (PE) on PVs
- Logical extents (LE) on LVs
- All volumes in a VG share the same extent size (default: 4 MB)
An LV is simply a mapping from logical extents to physical extents. This mapping can be:
- Linear: extents in sequence (default)
- Striped: extents alternating across multiple PVs (performance)
- Mirrored: extents duplicated across PVs (redundancy)
Because extents are the unit of allocation, LVM can expand, shrink, or move an LV simply by adjusting these mappings - even while the LV is mounted.
Practical Guide
Section titled “Practical Guide”1. Mark Disks as Physical Volumes
Section titled “1. Mark Disks as Physical Volumes”# Scan for available block devices LVM can usesudo lvmdiskscan
# Mark devices as physical volumessudo pvcreate /dev/sda /dev/sdb
# Check PV statussudo pvs # brief summarysudo pvdisplay # detailed infosudo pvdisplay /dev/sda # specific PVPV VG Fmt Attr PSize PFree/dev/sda lvm2 --- 200.00g 200.00g/dev/sdb lvm2 --- 100.00g 100.00g2. Create a Volume Group
Section titled “2. Create a Volume Group”# Create VG named LVMVolGroup from both PVssudo vgcreate LVMVolGroup /dev/sda /dev/sdb
# View VG statussudo vgs # briefsudo vgdisplay # detailedsudo vgdisplay LVMVolGroup
# Add a new PV to an existing VG latersudo vgextend LVMVolGroup /dev/sdc
# Remove a PV from a VG (must evacuate first)sudo pvmove /dev/sda # move all data off /dev/sdasudo vgreduce LVMVolGroup /dev/sda3. Create Logical Volumes
Section titled “3. Create Logical Volumes”Unlike partitions, you don’t need to know disk layout - LVM handles placement.
# Create LVs with fixed size (-L) and name (-n)sudo lvcreate -L 10G -n projects LVMVolGroupsudo lvcreate -L 5G -n www LVMVolGroupsudo lvcreate -L 20G -n db LVMVolGroup
# Allocate remaining free spacesudo lvcreate -l 100%FREE -n workspace LVMVolGroup
# View LV statussudo lvs # briefsudo lvdisplay # detailedsudo vgs -o +lv_size,lv_name # VG view with LV detailsLV VG Attr LSizeprojects LVMVolGroup -wi-a----- 10.00gwww LVMVolGroup -wi-a----- 5.00gdb LVMVolGroup -wi-a----- 20.00gworkspace LVMVolGroup -wi-a----- 264.99gLV device paths:
/dev/LVMVolGroup/projects/dev/mapper/LVMVolGroup-projects(same device, two paths)
4. Format and Mount
Section titled “4. Format and Mount”# Formatsudo mkfs.ext4 /dev/LVMVolGroup/projectssudo mkfs.xfs /dev/LVMVolGroup/db # XFS is a good choice for databases
# Create mount points and mountsudo mkdir -p /mnt/{projects,www,db,workspace}sudo mount /dev/LVMVolGroup/projects /mnt/projectssudo mount /dev/LVMVolGroup/www /mnt/wwwsudo mount /dev/LVMVolGroup/db /mnt/dbsudo mount /dev/LVMVolGroup/workspace /mnt/workspace5. Persist Mounts in /etc/fstab
Section titled “5. Persist Mounts in /etc/fstab”/dev/LVMVolGroup/projects /mnt/projects ext4 defaults,nofail 0 2/dev/LVMVolGroup/www /mnt/www ext4 defaults,nofail 0 2/dev/LVMVolGroup/db /mnt/db xfs defaults,nofail 0 2/dev/LVMVolGroup/workspace /mnt/workspace ext4 defaults,nofail 0 2Resizing Logical Volumes
Section titled “Resizing Logical Volumes”This is one of LVM’s most powerful features - you can grow a volume while it is mounted and in use.
Extend a Logical Volume
Section titled “Extend a Logical Volume”# 1. Extend the LVsudo lvextend -L +10G /dev/LVMVolGroup/projects # add 10Gsudo lvextend -L 50G /dev/LVMVolGroup/projects # set absolute size to 50Gsudo lvextend -l +100%FREE /dev/LVMVolGroup/projects # use all free space
# 2. Resize the filesystem (must be done separately)sudo resize2fs /dev/LVMVolGroup/projects # ext4 (online resize OK)sudo xfs_growfs /mnt/db # XFS (must be mounted)
# Or do both in one step (-r extends filesystem automatically)sudo lvextend -r -L +10G /dev/LVMVolGroup/projectsShrink a Logical Volume
Section titled “Shrink a Logical Volume”# 1. Unmountsudo umount /mnt/projects
# 2. Check filesystemsudo e2fsck -f /dev/LVMVolGroup/projects
# 3. Shrink filesystem FIRST (leave some buffer)sudo resize2fs /dev/LVMVolGroup/projects 8G
# 4. Then shrink the LVsudo lvreduce -L 8G /dev/LVMVolGroup/projects
# 5. Remountsudo mount /dev/LVMVolGroup/projects /mnt/projectsAlways shrink the filesystem before shrinking the LV. Shrink the LV before removing space from the VG.
LVM Snapshots
Section titled “LVM Snapshots”A snapshot creates an instant point-in-time copy of an LV. Under the hood, LVM uses Copy-on-Write (CoW): the snapshot initially shares all data blocks with the origin. When blocks are modified in the origin, LVM copies the original version to the snapshot before overwriting.
# Create a 5G snapshot of the 'db' LVsudo lvcreate -s -L 5G -n db_snapshot /dev/LVMVolGroup/db
# Mount the snapshot (read-only for backups is safest)sudo mount -o ro /dev/LVMVolGroup/db_snapshot /mnt/db_snapshot
# Take a backup from the snapshot (database is still running normally)sudo tar -czf /backup/db_$(date +%Y%m%d).tar.gz /mnt/db_snapshot
# After backup, unmount and remove snapshotsudo umount /mnt/db_snapshotsudo lvremove /dev/LVMVolGroup/db_snapshotMigration and Maintenance
Section titled “Migration and Maintenance”# Move data from one PV to another (online, no downtime)sudo pvmove /dev/sda # move all from sda to other PVs in VGsudo pvmove /dev/sda /dev/sdc # move sda data specifically to sdc
# Rename a VGsudo vgrename OldName NewName
# Rename an LVsudo lvrename LVMVolGroup old-lv new-lv
# Remove an LV (must be unmounted)sudo umount /mnt/projectssudo lvremove /dev/LVMVolGroup/projects
# Remove a VG (all LVs must be removed first)sudo vgremove LVMVolGroup
# Remove PV label (must be out of all VGs first)sudo pvremove /dev/sdaQuick LVM Command Reference
Section titled “Quick LVM Command Reference”| Category | Command | Description |
|---|---|---|
| PV | pvcreate /dev/sdX | Initialize PV |
pvs | Brief PV list | |
pvdisplay | Detailed PV info | |
pvmove /dev/sdX | Migrate data off a PV | |
| VG | vgcreate name /dev/sdX | Create VG |
vgs | Brief VG list | |
vgdisplay | Detailed VG info | |
vgextend name /dev/sdX | Add PV to VG | |
vgreduce name /dev/sdX | Remove PV from VG | |
| LV | lvcreate -L size -n name VG | Create LV |
lvs | Brief LV list | |
lvdisplay | Detailed LV info | |
lvextend -r -L +size /dev/VG/LV | Grow LV + filesystem | |
lvreduce -L size /dev/VG/LV | Shrink LV (unmount first!) | |
lvcreate -s -L size -n snap /dev/VG/LV | Create snapshot | |
lvremove /dev/VG/LV | Delete LV | |
| Scan | lvmdiskscan | Find available block devices |
lvs -a | List all LVs including internal |