Server Management

LVM - Logical Volume Manager

Manage disk storage flexibly with LVM: resize volumes online, add disks, create snapshots, and use thin provisioning.

LVM (Logical Volume Manager) adds an abstraction layer between physical disks and filesystems, enabling online resizing, spanning multiple disks, and instant snapshots, all without downtime.

Core Concepts

LayerComponentDescription
PhysicalPV (Physical Volume)A raw disk or partition initialized for LVM
GroupVG (Volume Group)Pool of one or more PVs
LogicalLV (Logical Volume)Virtual partition carved from a VG
/dev/sdb  /dev/sdc        ← Physical disks
    PV        PV           ← Physical Volumes
    └────VG────┘           ← Volume Group (vg_data)
       ├── LV root (50G)   ← Logical Volumes
       └── LV data (80G)

Install LVM Tools

apt-get install -y lvm2       # Debian/Ubuntu
yum install -y lvm2           # RHEL/CentOS

Initial Setup on a New Disk

# 1. Initialize physical volume
pvcreate /dev/sdb

# 2. Create volume group
vgcreate vg_data /dev/sdb

# 3. Create logical volume (50G)
lvcreate -L 50G -n lv_data vg_data

# 4. Format
mkfs.ext4 /dev/vg_data/lv_data

# 5. Mount
mkdir -p /data
mount /dev/vg_data/lv_data /data

Add to /etc/fstab for persistent mount:

echo "/dev/vg_data/lv_data  /data  ext4  defaults  0 2" >> /etc/fstab

Display Information

pvs          # Summary of physical volumes
vgs          # Summary of volume groups
lvs          # Summary of logical volumes

pvdisplay    # Detailed PV info
vgdisplay    # Detailed VG info
lvdisplay    # Detailed LV info

Extend a Logical Volume Online

No unmounting required for ext4 or XFS:

# Extend by 20G
lvextend -L +20G /dev/vg_data/lv_data

# Resize the filesystem (ext4)
resize2fs /dev/vg_data/lv_data

# For XFS (must be mounted)
xfs_growfs /data

# One-command extend + resize (ext4 only)
lvextend -L +20G -r /dev/vg_data/lv_data

If the VG has no free space, add a new disk first (see below) before extending the LV.

Add a New Disk to an Existing Volume Group

# Initialize the new disk
pvcreate /dev/sdc

# Add to existing VG
vgextend vg_data /dev/sdc

# Now extend LV as normal
lvextend -L +100G -r /dev/vg_data/lv_data

Reduce a Logical Volume (ext4 only, offline)

Reducing a volume is risky. Always back up data first. XFS cannot be shrunk, only ext4 supports reducing.

# Unmount first
umount /data

# Check and resize filesystem (to 30G)
e2fsck -f /dev/vg_data/lv_data
resize2fs /dev/vg_data/lv_data 30G

# Reduce the LV
lvreduce -L 30G /dev/vg_data/lv_data

# Remount
mount /dev/vg_data/lv_data /data

LVM Snapshots

Snapshots capture the state of an LV at a point in time. Useful for consistent backups.

# Create a 5G snapshot of lv_data
lvcreate -L 5G -s -n snap_backup /dev/vg_data/lv_data

# Mount the snapshot (read-only)
mkdir /mnt/snap
mount -o ro /dev/vg_data/snap_backup /mnt/snap

# Backup from snapshot
tar czf /backup/data-$(date +%F).tar.gz /mnt/snap/

# Unmount and remove snapshot
umount /mnt/snap
lvremove /dev/vg_data/snap_backup

LVM snapshots are NOT a backup, they live on the same physical disk. If the disk fails, the snapshot is lost too. Copy the data off to a separate location.

Monitor snapshot usage (snapshots fill up as data changes):

lvs -o name,snap_percent

If a snapshot fills to 100%, it becomes invalid. Size it according to expected write activity during the backup window.

Thin Provisioning

Thin provisioning allows over-allocation, create more space than physically exists, and allocate on write.

# Create a thin pool (80G pool in vg_data)
lvcreate -L 80G --thinpool thin_pool vg_data

# Create thin volumes (can exceed physical size)
lvcreate -V 200G --thin -n lv_web vg_data/thin_pool
lvcreate -V 200G --thin -n lv_db  vg_data/thin_pool

# Format and use normally
mkfs.ext4 /dev/vg_data/lv_web

Monitor actual usage:

lvs -o name,lv_attr,size,pool_lv,data_percent,metadata_percent

Remove an LV / VG / PV

# Remove logical volume (must be unmounted)
umount /data
lvremove /dev/vg_data/lv_data

# Remove volume group (all LVs must be removed first)
vgremove vg_data

# Remove physical volume
pvremove /dev/sdb

Move Data Off a PV (before removing a disk)

# Move all extents from /dev/sdb to remaining PVs
pvmove /dev/sdb

# Then remove it from the VG
vgreduce vg_data /dev/sdb
pvremove /dev/sdb

Rename a Volume Group or LV

vgrename vg_data vg_storage
lvrename vg_storage lv_data lv_main

LVM on System Root

On Ubuntu/Debian installers, selecting "LVM" creates:

  • A small /boot partition (non-LVM)
  • A PV for the rest, with VG ubuntu-vg containing ubuntu-lv

Extend root online after adding disk space:

# Extend PV after disk resize (cloud VPS disk resize)
pvresize /dev/sda3

# Extend LV to use all free space
lvextend -l +100%FREE -r /dev/ubuntu-vg/ubuntu-lv

On this page