Skip to content
Visual explainer 5h to learn 5 steps

LVM: physical volumes, volume groups & logical volumes

Build, extend and resize logical volumes online without downtime.

By Invicly Tech, , in Linux Administration (RHCSA-style). About 10 minutes to read.

Topic 21 of 42

In short

LVM puts a flexible layer between disks and file systems. Physical volumes are pooled into a volume group, and logical volumes are carved from that pool and can be grown, or for some file systems shrunk, without repartitioning. It is the standard way to manage server storage and a large part of the RHCSA.

Explained simply

Normal partitions are like walls built in concrete. Once the room sizes are set, changing them means a big rebuild. LVM, the Logical Volume Manager, gives you movable walls instead.

You hand LVM one or more disks. It pools all their space into one big bucket called a volume group. From that bucket you cut logical volumes of any size, put file systems on them, and mount them like any partition.

The power shows up later. A volume is getting full? Make it bigger in seconds, while the app keeps running. Out of space in the bucket? Add a new disk to the pool. Need to retire an old disk? Move the data off it while everything stays online.

Almost every RHEL install uses LVM by default for the root file system, so knowing these commands is not optional for a Linux admin, and it is a big part of the RHCSA exam.

Press Play on a diagram to walk through it one step at a time.

LayersLVM stack
  1. 1
    File system

    xfs or ext4 mounted at /srv/web.

  2. 2
    Logical volume (LV)

    /dev/vgdata/lvweb, sized in extents.

  3. 3
    Volume group (VG)

    vgdata: a pool of extents from all PVs.

  4. 4
    Physical volumes (PV)

    /dev/vdb2 and /dev/vdc initialised with pvcreate.

  5. 5
    Disks and partitions

    The real block devices.

SequenceGrowing a full volume with a new disk
Admin
LVM
File system
  1. 1pvcreate /dev/vdc
    Admin to LVM: pvcreate /dev/vdc
  2. 2vgextend vgdata /dev/vdc
    Admin to LVM: vgextend vgdata /dev/vdc
  3. 3VG now has free extents
    LVM to Admin: VG now has free extents
  4. 4lvextend -r -L +10G /dev/vgdata/lvweb
    Admin to LVM: lvextend -r -L +10G /dev/vgdata/lvweb
  5. 5fsadm runs xfs_growfs
    LVM to File system: fsadm runs xfs_growfs
  6. 6df shows the new size, no downtime
    File system to Admin: df shows the new size, no downtime
ComparePlain partitions vs LVM
Plain partitions
  • Fixed size, set at creation
  • Live on one disk only
  • Growing often needs free space right after
  • Simplest, fewest layers
LVM
  • Grow online in one command
  • One volume can span many disks
  • pvmove migrates data while online
  • Snapshots and thin volumes

In depth

Extents: how LVM maps space

When you create a volume group, LVM divides every physical volume into equal chunks called physical extents (PEs), 4 MiB by default. A logical volume is just an ordered list of extents, and each logical extent points to one physical extent on some PV. The extents of one LV do not need to be next to each other or even on the same disk.

The kernel's device-mapper does the actual translation. Each LV appears as /dev/dm-N, with friendlier links at /dev/mapper/vgdata-lvweb and /dev/vgdata/lvweb. If a name contains a hyphen, device-mapper doubles it: VG my-vg becomes /dev/mapper/my--vg-...

The extent size is set once with vgcreate -s and is the smallest step for LV sizes. lvcreate -L 1G rounds up to whole extents, while lvcreate -l 250 asks for exactly 250 extents. -l also accepts percentages: -l 100%FREE takes everything left in the VG.

FlowA read from an LV, through device-mapper
  1. Step 1
    App reads a file

    File system asks for a block on /dev/vgdata/lvweb

  2. Step 2
    Logical extent

    Block falls in LE 300 of the LV

  3. Step 3
    Mapping table

    LE 300 maps to PE 44 on /dev/vdc

  4. Step 4
    Physical read

    The kernel reads that offset on vdc

Where LVM keeps its metadata

Every PV carries a label and a metadata area near its start, and each holds a full text description of the whole volume group: its PVs, LVs and extent maps. Because every PV has a copy, a VG can be read even if some metadata is damaged. LVM also saves backups to /etc/lvm/backup after each change and older versions to /etc/lvm/archive; vgcfgrestore can bring one back.

On RHEL 9, LVM uses a devices file, /etc/lvm/devices/system.devices, that lists which disks it may use. pvcreate and vgcreate add disks to it automatically. A disk moved from another machine may not appear in pvs until you add it with lvmdevices --adddev /dev/vdd or vgimportdevices.

  • pvs -o +pv_uuid shows PV IDs
  • vgcfgbackup and vgcfgrestore save and load metadata
  • lvmdevices lists the devices file entries

Growing, shrinking and moving

Growing is safe and online. If the VG has free extents, lvextend -r -L +5G /dev/vgdata/lvweb grows the LV and its file system together; -r calls fsadm, which runs xfs_growfs or resize2fs for you. If the VG is full, add a disk first with pvcreate and vgextend.

Shrinking is dangerous. xfs cannot shrink at all. ext4 can, but only unmounted and only if the file system is shrunk before the LV. lvreduce -r does both in the right order; plain lvreduce on a mounted file system cuts off data.

To retire a disk, pvmove /dev/vdb2 moves all its extents to other PVs in the VG while the LVs stay in use. Then vgreduce vgdata /dev/vdb2 removes it from the VG and pvremove /dev/vdb2 wipes the label.

  • lvextend -r -l +100%FREE uses all free space
  • pvmove needs enough free extents on other PVs
  • Never lvreduce without -r unless you shrank the file system first

Snapshots and other LV types

A snapshot is a frozen view of an LV at one moment. lvcreate -s -L 1G -n web_snap /dev/vgdata/lvweb creates a classic snapshot that stores only blocks changed after it was taken, so it can be much smaller than the original. If those changes exceed the snapshot's size, the snapshot becomes invalid, so watch the Data% column in lvs. Snapshots are good for a consistent backup or a quick rollback before a risky upgrade (lvconvert --merge rolls back).

LVM can also create mirrored volumes (lvcreate --type raid1 -m 1) and thin pools, where volumes get space only as data is written, which allows overcommitting. Thin pools need monitoring, because a full pool stops writes for every thin volume in it.

Learn it in 5 short steps

About 1h each. Track them with a timer in your plan.

  1. 1
    Create PVs, a VG and LVs~1h
  2. 2
    Extend a VG with a new disk~1h
  3. 3
    Grow an LV and its file system~1h
  4. 4
    Set physical extent size~1h
  5. 5
    Inspect with pvs, vgs and lvs~1h

Key points

  • Order of operations: pvcreate, vgcreate, lvcreate, mkfs, mount.
  • vgcreate -s sets the physical extent size, and lvcreate -l sizes in extents.
  • Extend online: vgextend to add a disk, then lvextend -r to grow the LV and file system together.
  • pvs, vgs and lvs give quick summaries; the display variants give details.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
sudo pvcreate /dev/vdb2
sudo vgcreate -s 8M vgdata /dev/vdb2
sudo lvcreate -n lvweb -L 1G vgdata
sudo mkfs.xfs /dev/vgdata/lvweb
sudo mkdir -p /srv/web && sudo mount /dev/vgdata/lvweb /srv/web
sudo pvcreate /dev/vdc && sudo vgextend vgdata /dev/vdc
sudo lvextend -r -L +500M /dev/vgdata/lvweb
sudo pvs; sudo vgs; sudo lvs

Build a VG with 8 MiB extents, create an LV, then grow it online.

An exam-style task: exact extents and size

Task: create VG vgexam with 16 MiB extents on /dev/vdb, then an LV lvexam of exactly 40 extents with ext4, mounted persistently at /exam. Step 1: create the PV and the VG with -s 16M. Step 2: create the LV with -l 40, giving 640 MiB. Step 3: make the file system and mount point. Step 4: add an fstab line using the /dev/mapper path or the UUID, and test with mount -a. Step 5: verify the numbers with vgdisplay and lvs.

bash
sudo pvcreate /dev/vdb
sudo vgcreate -s 16M vgexam /dev/vdb
sudo lvcreate -n lvexam -l 40 vgexam
sudo mkfs.ext4 /dev/vgexam/lvexam
sudo mkdir -p /exam
echo '/dev/mapper/vgexam-lvexam /exam ext4 defaults 0 2' | sudo tee -a /etc/fstab
sudo systemctl daemon-reload && sudo mount -a
sudo vgdisplay vgexam | grep 'PE Size'
sudo lvs -o lv_name,lv_size,seg_size_pe vgexam

Replace an old disk without downtime

/dev/vdb2 in vgdata is showing errors, and the LVs on it serve a live website. Step 1: add a new disk /dev/vdd to the VG. Step 2: run pvmove to copy every extent from vdb2 to the free space, while the site keeps running. Step 3: once pvs shows vdb2 with no used extents, remove it from the VG and wipe its label. Step 4: confirm nothing in the VG uses vdb2 anymore.

bash
sudo pvcreate /dev/vdd
sudo vgextend vgdata /dev/vdd
sudo pvmove /dev/vdb2
sudo pvs -o pv_name,vg_name,pv_size,pv_used
sudo vgreduce vgdata /dev/vdb2
sudo pvremove /dev/vdb2
sudo lvs -o lv_name,devices vgdata

Quick reference

  • pvcreate DISK, vgcreate VG DISK, lvcreate -n LV -L SIZE VG, mkfs, mount
  • vgcreate -s 16M sets the extent size; lvcreate -l 40 asks for 40 extents
  • lvextend -r -L +5G /dev/vg/lv grows the LV and its file system together
  • lvextend -r -l +100%FREE /dev/vg/lv uses all free space in the VG
  • pvmove /dev/old, then vgreduce VG /dev/old and pvremove /dev/old retires a disk
  • lvcreate -s -L 1G -n snap /dev/vg/lv takes a snapshot; watch Data% in lvs
  • lvs -o +devices shows which PVs each LV uses
  • /dev/vg/lv and /dev/mapper/vg-lv are links to the same /dev/dm-N device

Common mistakes

  • Growing the LV but forgetting to grow the file system (use -r).
  • Shrinking an xfs LV, which destroys the file system.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A VG uses 8 MiB extents. How big is an LV created with lvcreate -l 25?

Question 2

2What does the -r option of lvextend do?

Question 3

3Which command moves all data off a physical volume while LVs stay online?

Question 4

4A VG is named web-vg and an LV is data. What is its device-mapper path?

Question 5

5A classic snapshot's Data% in lvs reaches 100%. What happens?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Show every PV, VG and LV on your system in short form, and find the extent size and free space of each VG.

  2. Exercise 2medium

    Grow the LV that holds /srv/web by 500 MiB, assuming the VG has free space, and show the file system size before and after.

  3. Exercise 3hard

    Before upgrading an app stored on /dev/vgdata/lvapp (ext4, mounted at /srv/app), take a 1 GiB snapshot. Then roll back to it as if the upgrade failed.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Does LVM slow down my disks?
The overhead of a plain linear LV is negligible for normal workloads. Classic snapshots do slow writes while they exist, so remove them when you are done.
Is a snapshot a backup?
No. It lives on the same disks as the original, so a disk failure loses both. Use it to take a consistent copy to another place, or as a short-term rollback point.
Why does pvs not show a disk I attached?
It may have no LVM label yet (run pvcreate), or on RHEL 9 it may be missing from the LVM devices file; check lvmdevices and add it with lvmdevices --adddev.
What extent size should I choose?
The 4 MiB default is fine for almost everything. Pick a different one only when a task tells you to, or when you want LV sizes to be exact multiples of a larger unit.

Go deeper

ShareXLinkedInWhatsApp