Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Three Layers

LayerWhat it isCreated with
Physical volume (PV)A disk or partition handed to LVMpvcreate
Volume group (VG)A pool made of one or more PVsvgcreate
Logical volume (LV)A slice of the pool that holds a file systemlvcreate

A logical volume behaves like a partition: you format it and mount it. The difference is that it can grow into free space anywhere in the volume group, even on another disk.

Before You Start

  • Identify the device: run lsblk -f. pvcreate on the wrong disk destroys what is on it, and there is no undo.
  • Take a snapshot or backup before changing volumes on a server with data.
  • Install the tools if needed: the package is lvm2 on every distribution, with apt or dnf.

Worked Example: A Data Volume on a New Disk

The new, empty disk is /dev/vdb:

sudo pvcreate /dev/vdb
sudo vgcreate data_vg /dev/vdb
sudo lvcreate -n data_lv -L 50G data_vg
sudo mkfs.ext4 /dev/data_vg/data_lv
sudo mkdir -p /srv/data
sudo mount /dev/data_vg/data_lv /srv/data

This leaves the rest of the disk unallocated in the volume group, ready for later growth or a second volume. To use all of it at once, write -l 100%FREE instead of -L 50G. LVM also creates /dev/mapper/data_vg-data_lv, but the lvm manual asks other software to use the /dev/data_vg/data_lv form. Add it to /etc/fstab as described in /etc/fstab explained; file systems on top work as in how to format a disk in Linux.

See What You Have

sudo pvs
sudo vgs
sudo lvs

Each prints one line per item. In vgs, the VFree column shows unallocated space in the pool. For every detail, use pvdisplay, vgdisplay, and lvdisplay.

Extend a Logical Volume

sudo lvextend -r -L +20G /dev/data_vg/data_lv
sudo lvextend -r -l +100%FREE /dev/data_vg/data_lv

The first adds 20 GB, the second takes everything left in the group. -r grows the file system in the same step, for ext4 and XFS, while it is mounted. Without -r, the volume grows but df shows no change until you run resize2fs or xfs_growfs.

Add More Space to the Pool

With a second disk:

sudo pvcreate /dev/vdc
sudo vgextend data_vg /dev/vdc

If the existing disk was enlarged instead, run sudo pvresize /dev/vdb. When the PV is a partition, grow the partition first, as in how to grow a partition after a disk upgrade.

A volume group spread over two disks without RAID depends on both: if one disk fails, the volumes that used it are lost.

LVM Snapshots

sudo lvcreate -s -n data_snap -L 5G /dev/data_vg/data_lv

A snapshot freezes the volume's state and stores later changes in the space you give it, here 5 GB taken from the volume group. It is useful for a consistent copy or a quick rollback before an upgrade. If the changes outgrow that space, the snapshot becomes invalid and unusable; watch its Data% column in lvs. It also lives on the same disks, so it is not a backup. Remove it when done with sudo lvremove /dev/data_vg/data_snap.

Shrinking

Reducing a logical volume is possible for ext4, offline only, but risky, and XFS cannot be shrunk in practice. Treat it as a separate task with a backup and a tested restore, not as the reverse of lvextend.

Was this answer helpful?
Loading...