Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 37 ยท Storage and Filesystems

LVM: Physical Volumes, Volume Groups and Logical Volumes

Use Logical Volume Manager to pool disks, create resizable logical volumes, extend filesystems online, take snapshots and add or replace disks without downtime.

Partitions are rigid: once created, growing one means moving things around. LVM (Logical Volume Manager) adds a flexible layer between disks and filesystems. You pool disks into a volume group, carve out logical volumes of any size, and later grow, shrink, move or snapshot them while the system runs.

The three layers

  • PV, physical volume: a disk or partition prepared for LVM.
  • VG, volume group: a pool of one or more PVs.
  • LV, logical volume: a slice of the pool that holds a filesystem, like a flexible partition.
flowchart TD
    A["/dev/sdb"] --> P1["PV"]
    B["/dev/sdc"] --> P2["PV"]
    P1 --> VG["VG vg0"]
    P2 --> VG
    VG --> L1["LV data"]
    VG --> L2["LV logs"]
    L1 --> F1["ext4 at /data"]
    L2 --> F2["xfs at /var/log"]

Disks feed a pool; volumes come out of the pool.

Building it

These commands destroy data on the named devices. Practice on virtual disks first.

Terminal
sudo pvcreate /dev/sdb /dev/sdc
sudo vgcreate vg0 /dev/sdb /dev/sdc
sudo lvcreate -n data -L 50G vg0
sudo lvcreate -n logs -l 100%FREE vg0          # use all remaining space
sudo mkfs.ext4 /dev/vg0/data
sudo mount /dev/vg0/data /data

The volume appears as /dev/vg0/data or /dev/mapper/vg0-data. Put the UUID or the mapper path in /etc/fstab (fstab).

Inspect with:

Terminal
sudo pvs ; sudo vgs ; sudo lvs
sudo pvdisplay ; sudo vgdisplay ; sudo lvdisplay
lsblk

Growing a volume

The most common operation. Check free space in the VG with vgs, then:

Terminal
sudo lvextend -L +20G /dev/vg0/data            # add 20 GiB
sudo resize2fs /dev/vg0/data                   # ext4, grow filesystem
# or: sudo xfs_growfs /data
# one step: lvextend -r -L +20G /dev/vg0/data

-r (--resizefs) resizes the filesystem for you.

If the VG is full, add a disk first:

Terminal
sudo pvcreate /dev/sdd
sudo vgextend vg0 /dev/sdd

Shrinking (ext4 only)

Shrinking can lose data if done in the wrong order. Back up first, unmount, check, shrink the filesystem, then the volume:

Terminal
sudo umount /data
sudo e2fsck -f /dev/vg0/data
sudo resize2fs /dev/vg0/data 30G
sudo lvreduce -L 30G /dev/vg0/data
sudo mount /dev/vg0/data /data

lvreduce -r does the filesystem step in the right order. XFS cannot shrink.

Moving data off a disk

Terminal
sudo pvmove /dev/sdb                 # move extents to other PVs in the VG
sudo vgreduce vg0 /dev/sdb
sudo pvremove /dev/sdb

This allows replacing a disk without downtime.

Snapshots

A snapshot freezes a view of a volume at one moment, using copy-on-write, so you can back it up consistently or roll back after a risky change.

Terminal
sudo lvcreate -s -n data-snap -L 5G /dev/vg0/data
sudo mount -o ro /dev/vg0/data-snap /mnt/snap
# copy files out, then:
sudo umount /mnt/snap
sudo lvremove /dev/vg0/data-snap

The 5 GiB is room for changes made to the origin. If it fills up, the snapshot becomes invalid, so watch it with lvs. A snapshot is not a backup: it lives on the same disks.

Extents

The VG divides space into equal-sized physical extents, 4 MiB by default. LV sizes are multiples of the extent size. You can request sizes in extents (-l) or bytes (-L); the tool rounds up.

Why use LVM, and when not to

Benefit Cost
Resize online One more layer to understand
Pool many disks A VG with striped or spanning LVs fails if one disk fails
Snapshots Snapshots slow writes while active
Move data between disks Needs free extents

LVM is not redundancy: use RAID or a redundant backend underneath for that (RAID).

A stand-in: extent math

This model rounds requested sizes up to 4 MiB extents and reports what is left in a made-up VG.

Python Stand-in: extents and free space

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A stand-in: a tiny volume group

This model keeps pools and volumes in a dictionary to show how extending a VG makes room for a larger LV.

Python Stand-in: extend the pool, then the volume

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Extending the volume but not the filesystem.
  • Shrinking the volume before the filesystem, which destroys data.
  • Letting a snapshot fill up.
  • Thinking LVM protects against disk failure.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the correct order of LVM layers from disk upward?

    Disks become PVs, PVs form a VG, and LVs are carved from the VG.
  2. 2. What does lvextend -r do?

    -r runs the matching filesystem resize automatically.
  3. 3. Which filesystem cannot be shrunk?

    XFS supports growing only.
  4. 4. Does LVM by itself protect you from a disk failure?

    LVM is flexibility, not redundancy.

Summary

  • PVs form VGs; LVs are flexible slices that hold filesystems.
  • Extend with lvextend -r; add disks with pvcreate and vgextend.
  • Shrink only ext4, in the right order, with backups.
  • Snapshots help consistent backups but are not backups themselves.

Next: RAID and Disk Health