Where to start First steps track · 15 stages
Linux Labs

LVM logical volumes

5 min Ubuntu 24.04 · LVM2

LVM: physical volumes, volume groups and logical volumes (PV → VG → LV), extending and snapshots.

Cheat sheet

8 commands Printable version
  • lsblk block device tree
  • pvs physical volumes
  • vgs volume groups
  • lvs logical volumes
  • sudo lvextend -r -l +100%FREE /dev/vg/lv extend the LV+fs onto all free space
  • sudo vgextend vg /dev/sdc add a disk to the group
  • sudo lvcreate -L 2G -s -n snap /dev/vg/lv a snapshot
  • sudo lvconvert --merge /dev/vg/snap roll back to the snapshot

Overview

LVM adds a layer of abstraction over disks: volumes can be extended, moved and snapshotted without repartitioning.

physical disks/partitions → PV (physical volume)
        PV(s)             → VG (volume group, a "pool" of space)
        VG                → LV (logical volume, holds a filesystem)

Status of the whole stack: pvs, vgs, lvs (or the detailed pvdisplay/vgdisplay/lvdisplay).

Recipes

3

Commands by layer

Layer Create Show Extend
PV pvcreate /dev/sdb pvs —
VG vgcreate data /dev/sdb vgs vgextend data /dev/sdc
LV lvcreate -L 10G -n vol1 data lvs lvextend -r -L +5G ...
bash
lsblk                             # the block device tree (what's inside what)
pvs
vgs
lvs                                # a quick status of each layer
vgs -o +vg_free                   # how much free space is left in the group

Creating and extending

bash
# from scratch:
sudo pvcreate /dev/sdb
sudo vgcreate data /dev/sdb
sudo lvcreate -L 10G -n vol1 data           # fixed size
sudo lvcreate -l 100%FREE -n vol1 data      # take all the VG's free space
sudo mkfs.ext4 /dev/data/vol1
sudo mount /dev/data/vol1 /mnt/vol1

# EXTENDING (the most common task):
sudo vgextend data /dev/sdc                  # add a disk to the group (if the VG is full)
sudo lvextend -r -L +5G /dev/data/vol1       # +5G to the LV AND grow the fs right away (-r)
sudo lvextend -r -l +100%FREE /dev/data/vol1 # give the volume all remaining free space
# manually, without -r:
sudo lvextend -L +5G /dev/data/vol1
sudo resize2fs /dev/data/vol1                # ext4 (xfs: sudo xfs_growfs /mnt/vol1)

Snapshots and moving data

bash
# a snapshot (for backup/rollback; needs free space in the VG):
sudo lvcreate -L 2G -s -n vol1_snap /dev/data/vol1   # -s: snapshot
sudo mount -o ro /dev/data/vol1_snap /mnt/snap        # mount it to copy from
sudo lvconvert --merge /dev/data/vol1_snap            # ROLL BACK to the snapshot
sudo lvremove /dev/data/vol1_snap                     # remove the snapshot

# move data off a disk and drop it from the VG:
sudo pvmove /dev/sdb              # evacuate extents from sdb onto the other PVs
sudo vgreduce data /dev/sdb      # remove the disk from the group

Troubleshooting

5

Extended the LV, but there's no extra space

The LV isn't the filesystem. After lvextend, grow the filesystem itself: resize2fs (ext4) or xfs_growfs (xfs). Or use lvextend -r directly.

vgs shows little free space (VFree=0)

The group is full — add a PV: pvcreate /dev/sdc + vgextend data /dev/sdc.

A snapshot "overflowed" and became invalid

A snapshot stores changes; if the original volume is written to heavily and the snapshot is too small, it overflows (lvs shows 100%). Size snapshots generously and take the copy quickly.

The volume is missing after a reboot

sudo vgchange -ay (activate it), and check auto-activation and /etc/fstab (mount by /dev/<vg>/<lv> or UUID).

Accidentally ran lvremove on the wrong volume

Data is gone. Before lvremove/lvreduce, always double-check against lvs and make a snapshot/backup.

Danger zone

Danger. lvremove deletes a logical volume and its data with no way to undo it; vgremove/pvremove destroy the group's and physical volume's metadata entirely. Cross-check against lvs/vgs and confirm a backup exists before deleting — verify by UUID or mount point, not just by name.

Running mkfs on top of an existing LV wipes its filesystem and data just as irreversibly as on a regular partition. lvconvert --merge to roll back to a snapshot irreversibly loses every change made after the snapshot was taken.

Terms

9
physical volume (PV)
A device prepared for membership in an LVM volume group. Also appears in: Disk space: df, du, inode, Filesystems: ext4, xfs, fsck, lsblk, Software RAID with mdadm and 3 more
volume group (VG)
A pool of physical volume space from which LVM allocates logical volumes. Also appears in: resize2fs / xfs_growfs, Disk space: df, du, inode, Filesystems: ext4, xfs, fsck, lsblk and 3 more
logical volume (LV)
An LVM block device receiving space from a volume group. Also appears in: resize2fs / xfs_growfs, Disk space: df, du, inode, Filesystems: ext4, xfs, fsck, lsblk and 3 more
physical extent
A unit of PV space allocation within an LVM volume group.
LVM snapshot
A representation of a volume at a point in time; a classic snapshot depends on its origin and COW reserve.
snapshot origin
The volume whose state a snapshot captures; a classic snapshot uses its unchanged blocks. Also appears in: git: change history, undo and a remote repository, unattended-upgrades: security updates with no one at the keyboard, The kernel
copy-on-write (COW)
A mechanism preserving earlier data when shared blocks change; used by classic LVM snapshots. Also appears in: Container images
application consistency
The property of captured data forming a valid coherent state for an application.
snapshot merge
Returning an origin to a snapshot state, discarding subsequent changes within the rollback scope.

See also

  • disk-usage — Where the space and inodes went — df, du, finding large files, gotchas.
  • mount — Mounting filesystems, persistent mounts via /etc/fstab, UUIDs.
Loading…