Sub topic 3
Finding out how much free space the volume group has
Before you extend a logical volume, you need to know whether the volume group can supply the space. This chapter covers how LVM tracks free space, which commands report it, and how to read their output without mixing up the different layers.
Prerequisites
This chapter assumes you can:
- Use the Linux command line with
sudo. - Read
dfandlsblkoutput. - Explain the relationship between physical volumes (PVs), volume groups (VGs), and logical volumes (LVs) at a basic level.
You don't need to have resized anything yet. Every command in this chapter only reads state and changes nothing.
Why the volume group is the place to look
A logical volume can grow only into extents that its volume group has not yet handed to any logical volume. So the question "can I add 20 GiB to lv_app?" is really "does vg_data have at least 20 GiB of unallocated extents?"
Two other numbers are easy to confuse with that answer:
- Free space on a disk is not volume group free space until the disk is a PV in that volume group.
- Free space in a filesystem is not volume group free space. It is space inside an LV that the filesystem has not yet used.
If you read the wrong number, you may add a disk you didn't need, or you may start an extend that fails halfway through your change window.
How LVM counts space: extents
LVM does not allocate in bytes. It divides every PV into fixed-size chunks called physical extents (PEs). The default extent size is 4 MiB and applies to the whole volume group. A logical volume is a list of extents, and its size is always a whole number of them.
This gives you three facts that explain most of the output you will see:
- Sizes are multiples of the extent size. If you request an LV size that isn't a multiple of the extent size, LVM rounds it up.
- A PV is slightly smaller than its disk. LVM writes metadata at the start of the device and aligns the first extent, so a 200 GiB disk yields a bit less than 200 GiB of extents.
- Free space is a count of unallocated extents.
VFreeis that count multiplied by the extent size.
The sections below use one example volume group. It has a single 200 GiB disk, /dev/sdb, as its only PV. The extent size is 4 MiB. It holds two logical volumes: lv_app at 120 GiB and lv_logs at 50 GiB.
The quick answer: vgs
vgs prints one line per volume group:
sudo vgs VG #PV #LV #SN Attr VSize VFree
vg_data 1 2 0 wz--n- <200.00g <30.00g| Column | Meaning |
|---|---|
| `VG` | Volume group name. |
| `#PV` | Number of physical volumes in the group. |
| `#LV` | Number of logical volumes, including hidden ones such as thin pool or RAID sub-volumes counted by LVM. |
| `#SN` | Number of snapshots. |
| `Attr` | Six attribute characters, explained below. |
| `VSize` | Total size of all PVs' usable extents. |
| `VFree` | Size of the extents not allocated to any LV. |
The < in front of <200.00g means the real value is slightly below the rounded number shown. Here the group is 51199 extents, which is a little under 200 GiB because of the metadata area on the PV.
Reading the Attr field
wz--n- is a position-based string. For planning an extend, three positions matter most:
- Position 1 is
wwhen the group is writable.rmeans read-only, and you can't extend anything. - Position 2 is
zwhen the group is resizable, which means you can add or remove PVs and LVs. - Position 4 is
pwhen the group is partial, which means one or more PVs are missing.
The detailed answer: vgdisplay and custom columns
vgdisplay shows the extent arithmetic directly:
sudo vgdisplay vg_data --- Volume group ---
VG Name vg_data
Format lvm2
VG Access read/write
VG Status resizable
Cur LV 2
Cur PV 1
VG Size <200.00 GiB
PE Size 4.00 MiB
Total PE 51199
Alloc PE / Size 43520 / 170.00 GiB
Free PE / Size 7679 / <30.00 GiBThe lines to read are PE Size, Total PE, Alloc PE, and Free PE. They satisfy two relations you can check yourself:
Total PE = Alloc PE + Free PE: 51199 = 43520 + 7679.Free size = Free PE x PE Size: 7679 x 4 MiB = 30716 MiB, which is just under 30 GiB.
vgs can print the same fields in a table, and you choose the columns with -o:
sudo vgs -o vg_name,vg_extent_size,vg_extent_count,vg_free_count,vg_free --units m VG Ext #Ext Free VFree
vg_data 4.00m 51199 7679 30716.00mThe --units option controls the size suffix. Lowercase units (k, m, g, t) are powers of 1024. Uppercase units (K, M, G, T) are powers of 1000. This matters when you compare LVM output with a vendor's disk size or with df -H.
Free space per physical volume: pvs
The volume group total hides how the free extents are spread across disks. pvs shows it per device:
sudo pvs -o pv_name,vg_name,pv_size,pv_free PV VG PSize PFree
/dev/sdb vg_data <200.00g <30.00gWith one PV the two views agree. With several, they can tell you something the total does not.
- For ordinary linear logical volumes, the group total is what matters. LVM can place an extension on any PV that has free extents.
- For striped or RAID logical volumes, each stripe or leg needs free extents on a different PV. A group can have plenty of total free space and still be unable to extend a striped LV because only one PV has any free extents.
- If you want a volume to grow onto a particular disk, you name that PV at the end of the
lvextendcommand. It must have enoughPFree.
Also check for PVs that are not in any volume group. pvs lists them with an empty VG column. Their space can't be used until you run vgextend, which a later chapter covers.
Putting the numbers in context: the layers side by side
Each layer has its own "free" number, and each tool reports a different one. This table maps question to command:
| Question | Command | Number to read |
|---|---|---|
| How much of the group is not assigned to any LV? | `vgs` | `VFree` |
| How much of one disk is not assigned to any LV? | `pvs` | `PFree` |
| How big is one logical volume? | `lvs` | `LSize` |
| How much of the filesystem is used? | `df -hT` | `Avail`, `Use%` |
| Is there raw disk space outside LVM? | `lsblk` | Device size vs. its children |
Only the first two numbers tell you whether you can extend a logical volume without adding capacity. df tells you whether you should.
Take this combination, for example. df shows /srv/data at 96% used, and vgs shows VFree of <30.00g. You can grow the volume immediately. If VFree showed 0, you would first need new capacity from a new disk or an enlarged one.
What consumes free space without looking like data
When VFree is smaller than you expect, check these before assuming a mistake:
- Snapshots. A classic LVM snapshot takes extents from the volume group when you create it, and those extents are gone from
VFreewhether or not the snapshot fills up. The#SNcolumn counts them, andlvslists them. - Thin pools. A thin pool is a logical volume with its own internal free space.
vgsshows only the extents allocated to the pool and its metadata. The pool's fullness appears in theData%andMeta%columns oflvs. - Mirrored and RAID volumes. A
raid1volume of 20 GiB uses about 40 GiB of extents, plus a small amount for metadata. Extending it by 20 GiB consumes about 40 GiB ofVFree. - Hidden volumes. Use
lvs -ato see internal volumes such as[lv_thinpool_tdata]that count against the group.
sudo lvs -a -o lv_name,vg_name,lv_attr,lv_size,data_percent,metadata_percent,devices vg_dataThe devices column shows which PV each volume sits on. That helps when a striped or pinned allocation behaves unexpectedly.
Checking the answer before acting
After you read VFree, ask whether the request fits with room to spare. Two habits help:
- Keep a reserve. Don't allocate 100% of a volume group on a server that uses snapshots, because snapshot creation needs free extents. Leave a margin, such as 5 to 10 percent, or whatever your backup tooling needs.
- Test the command. LVM tools accept
--test(or-t), which runs the command logic without writing metadata. It catches impossible requests, such as asking for more than the group has, before you commit.
sudo lvextend --test -L +20G /dev/vg_data/lv_appWith only 30 GiB free this request is valid. A request for +40G fails with a message that there are not enough free extents in the volume group. This is your confirmation that the group is the limit.
Turning the check into a script
If you do this on many servers, put the comparison in a small script. The script below reads free space in bytes, which avoids the < and rounding in human-readable output, and compares it with the request:
#!/usr/bin/env bash
set -euo pipefail
vg="vg_data"
need_gib="${1:?usage: $0 SIZE_IN_GIB}"
free_bytes=$(sudo vgs --noheadings --nosuffix --units b -o vg_free "$vg" | tr -d ' ')
need_bytes=$(( need_gib * 1024 * 1024 * 1024 ))
if (( free_bytes >= need_bytes )); then
echo "OK: $vg has $(( free_bytes / 1024 / 1024 )) MiB free; $need_gib GiB requested."
else
echo "NOT ENOUGH: $vg has $(( free_bytes / 1024 / 1024 )) MiB free; $need_gib GiB requested." >&2
exit 1
fiRun as ./check-vg-free.sh 20. On the example group, free space is 32208060416 bytes, or 30716 MiB, so a 20 GiB request prints OK and a 40 GiB request exits with status 1.
Deciding what to do next
The outcome of this check decides which of the next steps you need. This diagram shows the flow:
Re-checking VFree after adding capacity is cheap, and it confirms the new space arrived in the group before you run lvextend.
Common mistakes
- Reading
dfinstead ofvgs.dfshows what the filesystem has left. It says nothing about whether the volume group can grow it. - Confusing GB with GiB.
vgs --units guses powers of 1024, and--units Guses powers of 1000. A 30 GiB margin is about 32.2 GB. Pick one unit and keep it for the whole change. - Assuming a bigger disk means a bigger group. If you enlarge a virtual disk,
VFreedoes not change until the partition and PV are resized.lsblkshows the new disk size, butpvsandvgsdo not. - Ignoring the rounding.
-L +20Gis converted to extents, so the result may be a few MiB off from what you typed in unusual cases. This only matters when you run the group close to full. - Missing a partial group. A
pinAttror a warning about a missing device in thevgsoutput means the group is not healthy enough for changes. - Using up the last extents.
-l +100%FREEis convenient but leaves no room for a snapshot. Decide on purpose whether you want that.
Summary
VFreeinvgsis the number that limits how far you can extend logical volumes without adding capacity.- LVM counts in extents, so sizes are multiples of the extent size and a PV is a little smaller than its disk.
pvsshows how free space is spread across disks, which matters for striped, RAID, and pinned allocations.- Snapshots, thin pools, and RAID layouts consume extents in ways that
dfdoesn't show. - A
--testrun oflvextendconfirms that a request fits before anything changes.
Exercise
A server's /srv/data filesystem is 97% full. df -hT shows the filesystem is 300 GiB, lsblk shows the disk /dev/sdb is now 400 GiB (it was enlarged last week), and vgs shows VFree of 0. Can you extend lv_app right away? Explain which number is blocking you.
Show answer
No. VFree of 0 is the blocking number. The volume group has no unallocated extents, so lvextend has nothing to allocate. The enlarged disk is not yet visible to LVM. You need to grow the partition and run pvresize so the PV (and therefore the group) gains the extra extents, then re-check VFree with vgs.
Exercise
A volume group has an extent size of 4 MiB, Total PE of 25599, and Alloc PE of 20000. How much free space does it have, in MiB, and is it enough for a 20 GiB extension of a linear logical volume?
Show answer
Free PE is 25599 - 20000 = 5599 extents. Multiplying by 4 MiB gives 22396 MiB, which is about 21.87 GiB. A 20 GiB (20480 MiB) extension of a linear LV fits, leaving 1916 MiB. If the LV were raid1, it would need about 40 GiB of extents and would not fit.
Exercise
vgs shows 60 GiB of VFree in a group with two PVs. You try to extend a striped logical volume (2 stripes) by 40 GiB and it fails. pvs shows PFree of 55 GiB on one PV and 5 GiB on the other. Why did it fail, and what does this tell you about reading VFree?
Show answer
A striped LV needs free extents on each stripe's PV in equal amounts. Extending by 40 GiB with 2 stripes needs 20 GiB free on each PV. The second PV has only 5 GiB, so the allocation fails even though the group total is 60 GiB. The group total is only sufficient for linear allocations, and for striped or RAID volumes you must also check PFree per PV.