LVM Storage Management on VPS Linux
LVM (Logical Volume Manager) is an abstraction layer between physical disks and Linux filesystems. It lets you resize partitions, combine multiple disks into a pool, and create snapshots — all without unmounting or rebooting. It is ideal for VPS environments where storage needs grow over time.
How LVM Works
LVM introduces three layers between your disk hardware and your filesystem:
| Layer | Name | Description |
|---|---|---|
| Physical | PV (Physical Volume) | A real disk or partition, e.g. /dev/sdb |
| Group | VG (Volume Group) | A pool that combines one or more PVs |
| Logical | LV (Logical Volume) | A virtual partition carved out of a VG — used like a normal disk |
Key benefits: resize partitions online, snapshot before risky operations, and span a logical volume across multiple physical disks seamlessly.
Install LVM
# Ubuntu/Debian
apt update && apt install -y lvm2
# CentOS/Rocky Linux/AlmaLinux
yum install -y lvm2
# Enable the monitoring service
systemctl enable --now lvm2-monitor
Create a Physical Volume (PV)
Assume a new disk /dev/sdb has been attached to your VPS:
# Check available disks
lsblk
fdisk -l
# Initialize the entire disk as a PV
pvcreate /dev/sdb
# Or use a partition
pvcreate /dev/sdb1
# List PVs
pvs
pvdisplay
pvcreate directly on the raw device without creating a partition table first.Create a Volume Group (VG)
# Create VG named "vgdata" from the PV
vgcreate vgdata /dev/sdb
# Combine multiple PVs into one VG
vgcreate vgdata /dev/sdb /dev/sdc
# Inspect the VG
vgs
vgdisplay vgdata
Create Logical Volumes (LV)
# Create a 20G LV named "lvdata" in VG "vgdata"
lvcreate -L 20G -n lvdata vgdata
# Use all remaining free space
lvcreate -l 100%FREE -n lvdata vgdata
# Create a separate 5G LV for logs
lvcreate -L 5G -n lvlogs vgdata
# List LVs
lvs
lvdisplay /dev/vgdata/lvdata
Format and Mount
# Format with ext4
mkfs.ext4 /dev/vgdata/lvdata
# Or xfs (recommended for large file workloads)
mkfs.xfs /dev/vgdata/lvlogs
# Create mount points
mkdir -p /data /var/log/apps
# Mount
mount /dev/vgdata/lvdata /data
mount /dev/vgdata/lvlogs /var/log/apps
# Verify
df -h
Add to /etc/fstab for persistence across reboots:
/dev/vgdata/lvdata /data ext4 defaults 0 2
/dev/vgdata/lvlogs /var/log/apps xfs defaults 0 2
blkid /dev/vgdata/lvdata, then use UUID=xxx in fstab.Resize an LV Online (No Downtime)
This is LVM's killer feature — extend a partition while it is still mounted:
Extend an LV
# Add 10G to the LV
lvextend -L +10G /dev/vgdata/lvdata
# Use all remaining VG free space
lvextend -l +100%FREE /dev/vgdata/lvdata
# Grow the filesystem to match (ext4)
resize2fs /dev/vgdata/lvdata
# For xfs filesystems
xfs_growfs /data
Shrink an LV — requires unmount first
# Unmount first (always back up data before shrinking!)
umount /data
# Check the filesystem
e2fsck -f /dev/vgdata/lvdata
# Shrink filesystem first
resize2fs /dev/vgdata/lvdata 15G
# Then shrink the LV
lvreduce -L 15G /dev/vgdata/lvdata
# Remount
mount /dev/vgdata/lvdata /data
Add a New Disk to an Existing VG
# Add /dev/sdc to the existing VG
pvcreate /dev/sdc
vgextend vgdata /dev/sdc
# Check the new free space
vgs
# Extend the LV immediately
lvextend -l +100%FREE /dev/vgdata/lvdata
resize2fs /dev/vgdata/lvdata
LVM Snapshots — Safe Point-in-Time Copies
Snapshots use copy-on-write — perfect for creating a rollback point before upgrades or database migrations:
# Create a 2G snapshot (stores deltas between now and original)
lvcreate -L 2G -s -n lvdata-snap /dev/vgdata/lvdata
# Mount snapshot read-only for backup
mkdir -p /mnt/snap
mount -o ro /dev/vgdata/lvdata-snap /mnt/snap
# Back up from the snapshot
rsync -a /mnt/snap/ /backup/data-$(date +%Y%m%d)/
# Unmount and remove snapshot when done
umount /mnt/snap
lvremove /dev/vgdata/lvdata-snap
Restore from a Snapshot
# Revert the LV to the snapshot's state
umount /data
lvconvert --merge /dev/vgdata/lvdata-snap
# Reboot or reactivate the VG, then remount
mount /dev/vgdata/lvdata /data
Verify LVM Is Working Correctly
# List all PVs
pvs
# List all VGs with free space
vgs
# List all LVs with paths
lvs -o lv_name,lv_size,lv_path,vg_name
# Confirm filesystem is mounted correctly
df -hT /data
# Detailed LV view with segment info
lvdisplay -m /dev/vgdata/lvdata
# Check VG health
vgck vgdata
lvs -a to see hidden LVs including snapshot metadata and thin pool segments — very useful for debugging.Common Issues and Fixes
Error: "Can't open /dev/sdb exclusively"
This happens when the device is locked by another process, usually when trying to run pvcreate on a disk that is already mounted.
# Check if the disk is mounted
lsblk /dev/sdb
fuser -m /dev/sdb
# Unmount first
umount /dev/sdb1
# If a partition table is blocking, wipe first
wipefs -a /dev/sdb
pvcreate /dev/sdb
Error: "Insufficient free space"
The VG does not have enough free extents for a snapshot or LV extension.
# Check free extents in the VG
vgdisplay vgdata | grep "Free PE"
# Add a new disk to the VG
pvcreate /dev/sdc
vgextend vgdata /dev/sdc
# Or remove unused snapshots
lvremove /dev/vgdata/old-snap
Error: "resize2fs: Device or resource busy"
You cannot shrink a filesystem while it is mounted.
# Unmount before shrinking
umount /data
# If processes are using it
fuser -km /data
# Always run fsck before shrinking
e2fsck -f /dev/vgdata/lvdata
Error: "Volume group not found" after reboot
LVM failed to activate the VG automatically on boot.
# Scan for PVs
pvscan
# Manually activate the VG
vgchange -ay vgdata
# Ensure the service is enabled
systemctl status lvm2-monitor
systemctl enable lvm2-monitor
Snapshot filled up — data corruption risk
If a snapshot fills to 100%, it becomes invalid immediately — always monitor usage.
# Check snapshot usage (keep below 80%)
lvs -o lv_name,lv_size,data_percent /dev/vgdata/lvdata-snap
# Extend a snapshot that is getting full
lvextend -L +1G /dev/vgdata/lvdata-snap
# Enable auto-extend in /etc/lvm/lvm.conf:
# snapshot_autoextend_threshold = 80
# snapshot_autoextend_percent = 20
Frequently Asked Questions
How does LVM differ from ZFS? Which should I choose?
LVM is a pure volume manager — it always requires a separate filesystem (ext4/xfs). It is lightweight, natively supported by the kernel, and works well on any VPS. ZFS combines volume management and filesystem in one layer, with built-in compression, deduplication, and checksumming, but requires significantly more RAM (8 GB+ recommended). For most VPS workloads, LVM with ext4 or xfs is the better choice.
Can I run LVM on the root partition?
Yes. Ubuntu and Debian installers offer LVM as an option during setup. Most cloud-imaged Ubuntu VPS instances already use LVM for the root volume (commonly named ubuntu--vg-ubuntu--lv), which makes it easy to expand the root disk later without reinstalling.
Can LVM snapshots replace real backups?
No. A snapshot is a point-in-time copy that still lives on the same physical disk. If the disk fails, the snapshot is gone too. Use snapshots as a safety net before maintenance, but always copy real backups to a separate disk or cloud storage using tools like rsync or rclone.