Sub topic 1
How LVM Layers Storage: PVs, VGs, and LVs
Prerequisites
This chapter assumes you can do the following:
- Use the Linux command line with
sudo. - Explain what a block device, a partition, and a mount point are.
- Recognize common filesystems such as ext4 and XFS.
- Read the output of
lsblkanddf.
You do not need to have used LVM before. This chapter builds the working model that the rest of the tutorial relies on. It only reads system state, so it changes nothing on your server.
Why LVM exists
A plain partition has a fixed size and a fixed location on one disk. If /srv/data sits on /dev/sdb1 and the partition fills up, you have limited options. You can resize the partition if free space happens to follow it. You can also copy everything to a bigger disk, which usually means downtime.
LVM (Logical Volume Manager) adds a layer between the disks and the filesystems. The filesystem no longer sits directly on a partition. It sits on a logical volume, whose size and placement are managed in software. Because LVM controls the mapping between logical volumes and physical disks, it can:
- Grow a volume while the filesystem is mounted and in use.
- Combine several disks into one pool of space.
- Move data from one disk to another while the system runs.
- Take snapshots of a volume.
This tutorial focuses on the first two: growing a running server's storage. Every later chapter uses the vocabulary introduced here.
The three layers
LVM has three main object types. Each one is built from the one below it.
| Layer | Abbreviation | What it is | Typical device |
|---|---|---|---|
| Physical volume | PV | A disk or partition that LVM has claimed and labeled | `/dev/sdb`, `/dev/sda3` |
| Volume group | VG | A pool of space made from one or more PVs | `vg_data` |
| Logical volume | LV | A slice of the pool that looks like a block device | `/dev/vg_data/lv_app` |
Physical volumes
A physical volume is a block device that has been initialized for LVM with pvcreate. That command writes a small LVM label and metadata area onto the device. The device can be a whole disk (/dev/sdc) or a partition (/dev/sda3). After initialization, LVM treats the device as a source of storage. The PV holds no filesystem of its own.
Volume groups
A volume group is the pool. It is created from one or more PVs and has a name you choose, such as vg_data. The total capacity of the VG is the sum of the capacity of its PVs, minus a little overhead for metadata. A VG can span several physical disks, so a single logical volume can be larger than any one disk.
Logical volumes
A logical volume is a block device carved out of a VG. You create a filesystem on it, mount it, and use it like any partition. Its device path appears in two equivalent forms:
/dev/vg_data/lv_app, a symlink in a directory named after the VG./dev/mapper/vg_data-lv_app, the device-mapper name. A hyphen inside a VG or LV name is doubled in this form.
Both names point at the same device. Tools such as df and mount usually show the /dev/mapper/ form.
Read the diagram from top to bottom. Disks become PVs, PVs are pooled into a VG, and the VG is divided into LVs. Filesystems live at the bottom, on the LVs. When you later grow storage, you work up the same stack in the opposite direction: first add capacity at the PV level, then extend the LV, then grow the filesystem.
Extents: how the pool is divided
LVM does not hand out space byte by byte. It divides every PV into fixed-size chunks and allocates whole chunks.
- On a PV, a chunk is a physical extent (PE).
- On an LV, the matching chunk is a logical extent (LE).
- Each LE maps to exactly one PE (in the default linear layout).
The default extent size is 4 MiB. It is set when the VG is created and applies to every PV in that VG. Two consequences follow:
- Sizes are rounded to whole extents. If you ask for a 10 MiB LV with 4 MiB extents, you get 12 MiB.
- The VG tracks free space as a count of free extents. The
VFreecolumn invgsis that count shown as a size.
The mapping from LEs to PEs is what makes LVM flexible. An LV does not need contiguous space. LV extents can come from different regions of a disk, or from different disks. To grow an LV, LVM finds free PEs in the VG and appends them to the LV's mapping. No existing data moves.
In this example lv_app spans two disks. The filesystem on top sees one continuous block device and has no idea that its blocks live on different disks.
Seeing the layers on a real system
Three commands report on the three layers. Each has a short form (pvs, vgs, lvs) with one line per object, and a long form (pvdisplay, vgdisplay, lvdisplay) with full detail.
sudo pvs
sudo vgs
sudo lvsTypical output on a server with one data volume group looks like this:
PV VG Fmt Attr PSize PFree
/dev/sdb vg_data lvm2 a-- <50.00g 0
/dev/sdc vg_data lvm2 a-- <50.00g <30.00g
VG #PV #LV #SN Attr VSize VFree
vg_data 2 1 0 wz--n- <99.99g <30.00g
LV VG Attr LSize Pool Origin Data% Meta%
lv_app vg_data -wi-ao---- <70.00gHow to read it:
PSizeandPFreeshow each PV's total and unallocated space. Here/dev/sdbis fully used and/dev/sdchas 30 GiB free.VSizeandVFreeshow the pool's total and unallocated space.VFreeis the number that decides whether you can extend an LV without adding a disk.#PV,#LVtell you how many PVs back the VG and how many LVs it holds.LSizeis the size of the logical volume. The filesystem on it may be smaller, which is covered below.- The
Attrcolumn is a string of flags. For an LV, the fifth characterameans active, and the sixth,o, means the device is open (mounted or otherwise in use). For a VG, thewmeans writable and thezmeans it can be resized. For the first character of a PV'sAttr,ameans allocatable.
Note the < before some sizes. It means the value was rounded and is slightly less than shown. It is normal.
Connecting LVM to the rest of the block device tree
lsblk shows how LVM objects relate to disks and partitions, and df shows the filesystem on top:
lsblk -f
df -hT /srv/dataNAME FSTYPE LABEL UUID MOUNTPOINTS
sdb LVM2_member aB1c-...
└─vg_data-lv_app ext4 5f0e-... /srv/data
sdc LVM2_member dE2f-...
└─vg_data-lv_app ext4 5f0e-... /srv/data
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg_data-lv_app ext4 69G 41G 25G 63% /srv/dataTwo details are worth noticing:
- Disks that are PVs show the filesystem type
LVM2_member. That label tells you the disk belongs to LVM and must not be formatted directly. - The LV appears under every PV that holds some of its extents, which is why
vg_data-lv_appshows up under bothsdbandsdc.
Three sizes that are easy to confuse
When you grow storage, three different sizes are involved, and they are changed by three different commands.
| Size | Where you see it | Changed by |
|---|---|---|
| PV size | `pvs` (`PSize`) | Adding a disk with `pvcreate` and `vgextend`, or enlarging a disk and running `pvresize` |
| LV size | `lvs` (`LSize`) | `lvextend` |
| Filesystem size | `df` (`Size`) | `resize2fs` or `xfs_growfs` |
A common mistake is to run lvextend and then wonder why df shows the old size. The LV is bigger, but the filesystem inside it still covers only the old range of blocks. Each layer has to be told about the new space, in order. Later chapters take each step in turn, and a later chapter shows how lvextend -r combines the last two.
You can see a mismatch directly by comparing the LV size with the filesystem size:
sudo lvs --units g -o lv_name,lv_size /dev/vg_data/lv_app
df -h --output=source,size /srv/dataIf lv_size is larger than the df size by more than a small overhead, the filesystem has not yet been grown.
What LVM stores and where
LVM keeps its configuration on the disks themselves. Each PV carries a metadata area that describes the VG: its PVs, its LVs, and the extent maps. This design has practical effects:
- Moving a set of disks to another machine lets that machine discover the VG automatically, in most cases.
- The metadata is written on every PV in the VG, so damage to one copy is not fatal.
- Every LVM change rewrites this metadata. LVM also saves copies of older versions under
/etc/lvm/backupand/etc/lvm/archive. These are text files thatvgcfgrestorecan use to roll back a configuration change. They describe layout only and never contain your data.
Keep that last point in mind. A metadata backup helps if you mistype an LVM command. It does not protect the contents of your files.
Where LVM sits in the storage stack
LVM is implemented on top of the kernel's device mapper (dm) subsystem. When LVM activates an LV, it asks device mapper to create a block device with a table that maps ranges of the LV to ranges on the PVs. That is why you see /dev/mapper/ names.
In a typical server the stack reads, from bottom to top:
- Physical or virtual disk (a SATA drive, an NVMe device, a cloud block volume).
- Optionally a partition on that disk.
- The LVM physical volume.
- The volume group.
- The logical volume (a device-mapper device).
- The filesystem (ext4 or XFS).
- The mount point your applications use.
LVM can also sit on top of software RAID (md) or encrypted devices (LUKS), and LUKS or RAID can sit on top of LVM. If your server uses either, lsblk shows the extra layers. The rule for growing still holds: change each layer from the bottom up.
Design choices that affect growth later
How you set up LVM on day one determines how easy growth is later. Keep these points in mind when you inspect an existing system.
- Leave free space in the VG. Allocating all of a VG to LVs on day one leaves no room to grow a volume quickly. A common practice is to allocate what you need now and keep the rest of the VG free. Free VG space can be handed to whichever LV needs it first.
- One VG can serve many LVs. Separate LVs for
/var,/srv/data, and/homelimit the damage when one fills up, and they all draw on the same pool. - Whole disk versus partition as a PV. A whole-disk PV is simple, and growing it later needs only
pvresize. A PV inside a partition is common on boot disks, and growing it needs the partition resized first. Cloud images usually use the partition layout. - Striped or mirrored LVs. LVs are linear by default. Striping and mirroring are available but place extra constraints on how you extend them, because new extents must come from the right number of PVs. The default linear layout is assumed throughout this tutorial.
Common misunderstandings
- "A VG is a disk." It is not. A VG is a named pool that can contain many disks, and one disk belongs to at most one VG.
- "Extending an LV grows the filesystem." It does not. Only the block device changes size.
- "LVM is a backup mechanism." It is not. Snapshots help with consistent backups, but they live on the same disks as the data.
- "VG free space is on one particular disk." It is not.
VFreeis the sum across the PVs. When the free extents sit on different PVs, LVM can still use all of them for one LV, in the linear layout. - "I can format a PV to use it directly." Running
mkfson a PV destroys the LVM label and the data in the VG that depends on it. Checklsblk -fforLVM2_memberfirst.
A worked reading exercise
Imagine you see this output on a server and need to decide whether /srv/data can grow by 20 GiB without a new disk.
VG #PV #LV #SN Attr VSize VFree
vg_data 2 2 0 wz--n- 149.99g 12.00g
LV VG Attr LSize
lv_app vg_data -wi-ao---- 100.00g
lv_logs vg_data -wi-ao---- 38.00gWork through it with the three-layer model:
- The VG has two PVs and about 150 GiB in total.
- The two LVs together use 138 GiB (100 + 38).
VFreeis 12 GiB, which agrees with the arithmetic: 150 − 138 = 12.- A 20 GiB extension needs 20 GiB of free extents, but only 12 GiB exist.
So the answer is no, not without adding capacity at the PV level first. The next chapters show how: add a new disk, or enlarge an existing one. The reasoning pattern is what matters here. Read VFree first, because it tells you whether the VG layer or the PV layer is the limiting factor.
Exercise
A server has a volume group vgdata with VSize 200 GiB and VFree 0. It holds one logical volume lvapp of 200 GiB, mounted at /srv/data as ext4. df shows /srv/data is 98% full. A colleague suggests running lvextend -L +10G /dev/vgdata/lvapp. Explain why that command will fail, which layer is the real constraint, and which two layers must change before the filesystem can be grown.
Show answer
The lvextend command fails because the VG has no free extents (VFree is 0). An LV can only grow by taking free extents from its VG, so the constraint is at the VG and PV level. First, capacity must be added to the pool, either by turning a new disk into a PV and adding it to vg_data (pvcreate and vgextend), or by enlarging an existing disk and running pvresize on its PV. With free extents available, lvextend can grow the LV. Only after the LV has grown can the ext4 filesystem be grown with resize2fs, because the filesystem can only expand into block-device space that already exists.
Exercise
lsblk -f shows /dev/sdd with FSTYPE LVM2_member. You were about to run mkfs.ext4 /dev/sdd to use it as a plain data disk. Why should you stop, and what command would you run to learn which volume group the disk belongs to?
Show answer
The LVM2_member type means /dev/sdd is already a physical volume that belongs to a volume group. Running mkfs.ext4 on it would overwrite the LVM label and metadata and destroy any logical volume data stored on it. Run sudo pvs (or sudo pvs /dev/sdd) and read the VG column to see which volume group it belongs to. If the VG column is empty, the disk is a PV that is not in any group yet, and you can then decide deliberately what to do with it.