Linux › Section D · Disks and filesystems › Topic D.5

The Logical Volume Manager

Fixed partitions are hard to change once a system is in use. The Logical Volume Manager puts a flexible layer between disks and filesystems, so space can be pooled across disks and volumes can grow while mounted. Software RAID, its close neighbour, adds redundancy.

Why add another layer?

With plain partitions, each filesystem is tied to one region of one disk. If /home fills up, you cannot simply borrow space from another disk; you must add a disk, pick a new mount point and move data by hand, often with reboots. The Logical Volume Manager (LVM) solves this by pooling storage. Block devices are gathered into a pool, and filesystems live on volumes carved from the pool rather than from a particular disk. Volumes can then be enlarged, moved between disks or snapshotted, usually without unmounting anything. Many installers offer LVM by default, and it suits cloud servers where new virtual disks can be attached at any time.

PVs, VGs and LVs

LVM has three layers. A physical volume (PV) is any block device prepared for LVM, usually a partition (type 8e on MBR, Linux LVM on GPT) or a whole disk. A volume group (VG) combines one or more PVs into a single pool of space. A logical volume (LV) is a block device carved from a VG; you format it with mkfs and mount it like a partition. Space is handed out in physical extents (PEs), fixed-size chunks that are commonly 4 MiB by default. An LV is simply a list of extents, which need not be contiguous or even on the same disk: that indirection is what makes resizing easy.

Looking at an LVM system

Each layer has a short and a long listing command: pvs and pvdisplay, vgs and vgdisplay, lvs and lvdisplay. vgs shows each group's number of PVs and LVs, its total size (VSize) and unallocated space (VFree), which is the figure you check before creating or growing a volume. An LV is reached by two stable names: /dev/<vg>/<lv>, for example /dev/datavg/projects, and /dev/mapper/<vg>-<lv>, here /dev/mapper/datavg-projects. In the mapper name any hyphen inside a VG or LV name is doubled, so a VG called ubuntu-vg appears as ubuntu--vg. Both names are links to a kernel device such as /dev/dm-0.

Worked example: building a volume group

Two new disks have one LVM partition each: /dev/sdX1 of 500 GiB and /dev/sdY1 of 1,000 GiB. 1. Mark them as PVs: pvcreate /dev/sdX1 /dev/sdY1 (vgcreate would also do this for you). 2. Create the pool: vgcreate datavg /dev/sdX1 /dev/sdY1. 3. Count extents at 4 MiB each: 500 GiB is \(500 \times 256 = 128{,}000\) extents and 1,000 GiB is 256,000, so about 384,000 in total; LVM keeps a little metadata, about 1 MiB per PV by default, so vgs will show slightly less. 4. Create a 1 TiB volume: lvcreate -L 1T -n projects datavg uses \(1024 \times 256 = 262{,}144\) extents. 5. Free space left: about \(384{,}000 - 262{,}144 = 121{,}856\) extents, which is 476 GiB. 6. Format and mount: mkfs.ext4 /dev/datavg/projects, then mount it on /projects.

Worked example: growing a mounted volume

Months later /projects is nearly full, and datavg still has about 476 GiB free. 1. Check first: vgs shows VFree, and df -h /projects shows the filesystem's size. 2. Grow the LV and its filesystem together, while mounted: lvextend -r -L +200G datavg/projects. The -r option runs fsadm, which calls resize2fs for ext4 or xfs_growfs for XFS. 3. Extents used: \(200 \times 256 = 51{,}200\), leaving about 276 GiB free in the VG. 4. Confirm with lvs, which shows the LV at about 1.2 TiB (1,224 GiB), and df -h /projects, which should show the larger filesystem. 5. To use every free extent instead, lvextend -r -l +100%FREE datavg/projects. Without -r, the LV would grow but the filesystem would stay its old size until resized separately.

Shrinking, removing and snapshots

Growing is safe and online; shrinking is not. Warning: shrinking an LV below the size of its filesystem destroys data. ext4 can be shrunk only while unmounted, and only if the filesystem is reduced first, which lvreduce -r coordinates. XFS can be grown but not shrunk. Back up before any reduction. lvremove datavg/scratch deletes one LV and asks for confirmation; note the slash, because giving two space-separated names makes it treat each as a whole VG. A snapshot (lvcreate -s) is a copy-on-write view of an LV at one moment, handy for a consistent backup, but it lives in the same VG, so it is not a backup by itself.

How LVM works inside

LVM is mostly user-space tools. Each PV carries a header and metadata describing its VG and every LV in it. At startup, LVM commands scan block devices for PVs, check that all PVs of a VG are present and work out each LV's map of extents. They hand that map to the kernel's device mapper, a general driver that creates virtual block devices by redirecting each request to the right offset on a real device. dmsetup ls lists the mapped devices and dmsetup table prints their maps in 512-byte sectors. The device mapper also underlies disk encryption (dm-crypt) and multipath, so the same /dev/mapper names appear for those too.

Software RAID with mdadm

RAID combines disks for speed or redundancy, and Linux does it in software with the md driver, managed by mdadm. RAID 0 stripes data with no redundancy; RAID 1 mirrors identical copies; RAID 5 adds one disk's worth of parity and survives one failure; RAID 6 uses two and survives two; RAID 10 stripes across mirrored pairs. An example: mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdX1 /dev/sdY1 builds a mirror. cat /proc/mdstat shows each array's state and any rebuild in progress, and mdadm --detail /dev/md0 gives full detail. An md array is a block device, so LVM or a filesystem can sit on top. RAID protects against a failed disk, not against deletion or corruption: it is not a backup.

Usable RAID capacity

With n identical disks of size s: RAID 0 gives \(ns\), RAID 1 gives \(s\), RAID 5 gives \((n-1)s\), RAID 6 gives \((n-2)s\), and RAID 10 gives \(ns/2\). For four 2 TB disks: RAID 0 gives 8 TB with no protection, RAID 5 gives 6 TB and tolerates one failed disk, RAID 6 gives 4 TB and tolerates any two, and RAID 10 gives 4 TB and tolerates one failure per mirrored pair. A two-disk RAID 1 of the same disks gives 2 TB. When a disk fails, the array keeps running in degraded mode, and replacing the disk starts a rebuild that /proc/mdstat reports as a percentage.

Common mistakes to avoid

First, growing the LV but not the filesystem: use -r, or the extra space stays invisible to df. Second, shrinking an LV before its filesystem, which cuts off data. Third, trying to shrink XFS. Fourth, typing lvremove datavg scratch with a space instead of datavg/scratch. Fifth, confusing VFree in vgs with free space inside a filesystem: one is unallocated pool space, the other is reported by df. Sixth, treating snapshots or RAID as backups. Seventh, building a VG across disks with no redundancy and forgetting that losing any one PV can take the LVs on it with it.

Key takeaways

LVM pools block devices: PVs form a VG, and LVs are carved from it in extents, commonly 4 MiB. List them with pvs, vgs and lvs; reach an LV at /dev/vg/lv or /dev/mapper/vg-lv. Build with pvcreate, vgcreate and lvcreate -L size -n name vg; grow online with lvextend -r. Shrinking is risky, needs unmounting for ext4 and is impossible for XFS. The kernel's device mapper does the redirection. mdadm builds software RAID 0, 1, 5, 6 and 10; check /proc/mdstat. Neither RAID nor snapshots replace backups.

Flashcards

Flashcard

LVM

Logical Volume Manager: pools disks into resizable volumes

A layer between disks and filesystems.

Test your understanding

Question 1 of 5

A VG uses 4 MiB extents. How many extents does lvcreate -L 20G allocate?

Question 2 of 5

After lvextend -L +50G datavg/projects (without -r), df still shows the old size. Why?

Question 3 of 5

Which LVM operation is unsafe or impossible as stated?

Question 4 of 5

You build software RAID 6 from five 4 TB disks. What usable capacity do you get?

Question 5 of 5

What is the correct command to delete only the logical volume scratch in the VG datavg?

← Previous topicNext topic →