Linux9 min read

Disk management with LVM

linuxlvmstorageadministration

LVM (Logical Volume Manager) adds a layer of abstraction between your physical disks and the filesystems that use them. The main benefit: you can resize partitions while the system is running, span multiple disks, and take snapshots for backups.

The LVM hierarchy

Three layers:

  1. Physical volumes (PV) — actual disks or partitions
  2. Volume groups (VG) — pools of storage made from one or more PVs
  3. Logical volumes (LV) — virtual partitions carved out of VGs

Think of it as: disks → pool → slices from the pool.

Setting up LVM

Create a physical volume on a partition:

sudo pvcreate /dev/sdb1

Create a volume group:

sudo vgcreate datavg /dev/sdb1

Create a logical volume:

sudo lvcreate -L 50G -n datalv datavg

Format and mount it:

sudo mkfs.ext4 /dev/datavg/datalv
sudo mount /dev/datavg/datalv /data

Add to /etc/fstab for persistence:

/dev/datavg/datalv /data ext4 defaults 0 2

Inspecting LVM

Physical volumes:

sudo pvdisplay
sudo pvs

Volume groups:

sudo vgdisplay
sudo vgs

Logical volumes:

sudo lvdisplay
sudo lvs

These commands show sizes, free space, and the relationships between layers.

Resizing a logical volume

Extend an LV (make it bigger):

sudo lvextend -L +20G /dev/datavg/datalv
sudo resize2fs /dev/datavg/datalv

Or do both in one step:

sudo lvextend -r -L +20G /dev/datavg/datalv

The -r flag calls resize2fs automatically for ext filesystems.

Shrinking is riskier and requires unmounting. Always back up first:

sudo umount /data
sudo e2fsck -f /dev/datavg/datalv
sudo resize2fs /dev/datavg/datalv 30G
sudo lvreduce -L 30G /dev/datavg/datalv
sudo mount /dev/datavg/datalv /data

Don’t shrink the LV smaller than the filesystem or you’ll lose data.

Adding a disk to a volume group

When a VG runs out of space, add another disk:

sudo pvcreate /dev/sdc1
sudo vgextend datavg /dev/sdc1

Now the VG has more space and you can extend LVs into it.

Removing a disk from a volume group

This is harder. You need to move all physical extents off the disk first:

sudo pvmove /dev/sdb1
sudo vgreduce datavg /dev/sdb1
sudo pvremove /dev/sdb1

pvmove migrates data from one PV to others in the same VG. It can take a while depending on how much data needs moving.

Snapshots

LVM snapshots let you capture the state of an LV at a point in time. Useful for backups of live databases or consistent filesystem copies.

Create a snapshot:

sudo lvcreate -L 5G -s -n datalv_snap /dev/datavg/datalv

-s makes it a snapshot. -L sets the snapshot size (not the size of the original — just the space for tracking changes).

Mount the snapshot:

sudo mount /dev/datavg/datalv_snap /mnt/snapshot

Back it up, then remove it:

sudo umount /mnt/snapshot
sudo lvremove /dev/datavg/datalv_snap

If the snapshot fills up (more changes than its size), it’s invalidated. Size it based on how much you expect to change during the backup.

Thin provisioning

With thin provisioning, you can create LVs larger than the available space. The VG allocates space on demand.

Create a thin pool:

sudo lvcreate -L 100G --thinpool thinpool datavg

Create thin volumes:

sudo lvcreate -V 200G --thin -n thinvolume1 datavg/thinpool

This creates a 200G volume backed by a 100G pool. It works as long as actual usage stays under the pool size. Monitor usage with lvs to avoid running out.

Common mistakes

Not using LVM. If you partitioned your disks directly, you can’t resize without unmounting and repartitioning. LVM avoids this.

Shrinking without checking filesystem first. Always run e2fsck before shrinking. Shrinking a dirty filesystem corrupts data.

Snapshot running out of space. Monitor snapshot usage. If it fills up, the snapshot becomes invalid and your backup is useless.

Forgetting resize2fs after lvextend. The LV gets bigger but the filesystem doesn’t. You need resize2fs (or -r with lvextend) to expand the filesystem to use the new space.

Remarks

LVM is one of those things that seems complex until you use it a few times. The three-layer model (PV → VG → LV) makes sense, and the ability to resize on the fly without downtime is worth the extra setup. If you’re building a server that might need more storage later, use LVM from the start.

Related Posts