Sub topic 8
Doing Both Steps at Once with lvextend -r
Earlier chapters grew storage in two separate steps. First you extended the logical volume with lvextend. Then you grew the filesystem with resize2fs or xfs_growfs. This chapter shows how lvextend -r runs both steps in one command, what it does internally, and where it can still leave you with half a resize.
This chapter assumes you can already do the following:
- Read
pvs,vgs,lvs,lsblk -f, anddf -hToutput. - Confirm that the volume group has free extents, either from an existing surplus or after
pvcreate/vgextendorpvresize. - Use
sudocomfortably on a server where a mistake affects real data.
What the -r option does
By default lvextend changes only the logical volume (LV). The block device gets bigger, but the filesystem on it keeps its old size. df shows no new space until the filesystem is grown too.
The -r option (long form --resizefs) tells lvextend to resize the filesystem after the LV has been extended. It does not grow the filesystem itself. It calls a helper script shipped with LVM, fsadm, and fsadm picks the right tool for the filesystem it finds:
| Filesystem on the LV | Tool that `fsadm` runs | Grows while mounted? |
|---|---|---|
| ext2, ext3, ext4 | `resize2fs` | Yes |
| XFS | `xfs_growfs` | Yes, and it must be mounted |
| ReiserFS | `resize_reiserfs` | Yes (rare today) |
You do not tell lvextend which filesystem you have. fsadm detects it from the device. That removes one source of error: running resize2fs against an XFS volume, or xfs_growfs with a device path instead of a mount point.
The basic command
Using the names from the earlier chapters, grow lv_app in vg_data by 20 GiB and resize its filesystem in one step:
sudo lvextend -r -L +20G /dev/vg_data/lv_app
df -hT /srv/dataWith an ext4 filesystem the output looks roughly like this (abbreviated):
Size of logical volume vg_data/lv_app changed from 100.00 GiB (25600 extents) to 120.00 GiB (30720 extents).
Logical volume vg_data/lv_app successfully resized.
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/mapper/vg_data-lv_app is mounted on /srv/data; on-line resizing required
old_desc_blocks = 13, new_desc_blocks = 15
The filesystem on /dev/mapper/vg_data-lv_app is now 31457280 (4k) blocks long.The output has two parts. The first lines come from LVM and describe the LV change. The remaining lines come from the filesystem tool that fsadm launched. Seeing both parts tells you both steps ran.
Sizing options work as before
The -r option changes nothing about how you ask for the size. All the usual forms apply:
# add a fixed amount
sudo lvextend -r -L +20G /dev/vg_data/lv_app
# set an absolute final size (must be larger than the current size)
sudo lvextend -r -L 150G /dev/vg_data/lv_app
# use every free extent in the volume group
sudo lvextend -r -l +100%FREE /dev/vg_data/lv_app
# use half of the volume group's total size as the increase
sudo lvextend -r -l +50%VG /dev/vg_data/lv_appTwo details cause most of the confusion:
- With a leading
+, the number is an increase. Without it, the number is the new total size. -Ltakes sizes (20G), and-ltakes extents or percentages (+100%FREE). Mixing them up produces an error, not a wrong resize.
You can also restrict which physical volumes supply the new extents by listing them at the end. This is useful when you have just added a fast disk and want the new space to come from it:
sudo lvextend -r -L +20G /dev/vg_data/lv_app /dev/sdcWhat happens inside the command
The sequence matters, because it explains how a failure can leave the system in a partial state.
The key point is that the two steps are not one atomic transaction. The LV extension is committed to the LVM metadata first. Only then does fsadm run. If the filesystem tool fails, the LV stays extended. This is not data loss, because the filesystem simply has not used the new space yet. It does mean that a failed lvextend -r is not rolled back, and lvs will show the larger size while df shows the old one.
Recovering when the filesystem step fails
When lvextend -r reports an error from fsadm, check the current state first:
sudo lvs vg_data
df -hT /srv/dataIf lvs shows the new size and df shows the old one, the LV step succeeded and only the filesystem step needs repeating. You have two options.
The first is to repeat the same command. On recent LVM versions, lvextend -r on an LV that is already at the requested size still attempts the filesystem resize. Older versions may stop with a message that the size is unchanged. If yours does, use the second option.
The second is to run the filesystem tool directly, as in the earlier chapter:
# ext4: takes the device path
sudo resize2fs /dev/vg_data/lv_app
# XFS: takes the mount point
sudo xfs_growfs /srv/dataCommon reasons for the filesystem step to fail:
- The filesystem is XFS but is not mounted.
xfs_growfsworks only on a mounted XFS filesystem. - The tool is missing. The XFS tools come from
xfsprogs, which some minimal images omit. - The filesystem is damaged.
resize2fscan refuse to resize untile2fsckhas been run. - The LV holds something
fsadmdoes not recognize, such as a raw database volume or a swap area.fsadmcannot resize swap, so useswapoff,mkswap, andswaponinstead.
A cautious wrapper script
On servers where several people run resizes, a small script can enforce the checks you would do by hand: confirm the LV exists, confirm the volume group has enough free space, and print before and after sizes. The script below does that for one LV and one increase in GiB.
#!/usr/bin/env bash
# grow-lv.sh: grow an LV and its filesystem by N GiB with lvextend -r
# Usage: sudo ./grow-lv.sh /dev/vg_data/lv_app 20
set -euo pipefail
lv_path="${1:?usage: $0 /dev/VG/LV GIB}"
add_gib="${2:?usage: $0 /dev/VG/LV GIB}"
if [[ "$EUID" -ne 0 ]]; then
echo "run as root (use sudo)" >&2
exit 1
fi
if ! lvs --noheadings "$lv_path" >/dev/null 2>&1; then
echo "logical volume not found: $lv_path" >&2
exit 1
fi
vg_name="$(lvs --noheadings -o vg_name "$lv_path" | tr -d ' ')"
free_gib="$(vgs --noheadings --units g --nosuffix -o vg_free "$vg_name" | tr -d ' ')"
# compare as decimals using awk, since bash only handles integers
if ! awk -v free="$free_gib" -v need="$add_gib" 'BEGIN { exit !(free >= need) }'; then
echo "not enough free space in $vg_name: ${free_gib} GiB free, ${add_gib} GiB requested" >&2
exit 1
fi
echo "Before:"
lvs -o lv_name,lv_size "$lv_path"
lvextend -r -L "+${add_gib}G" "$lv_path"
echo "After:"
lvs -o lv_name,lv_size "$lv_path"
findmnt -n -o TARGET,FSTYPE,SIZE,USE% "$lv_path" || trueThe awk comparison exists because vgs reports free space with decimals, such as 35.00, and bash [[ ... -ge ... ]] cannot compare those. The final findmnt line shows the mount point, filesystem type, and the size the filesystem now has, so the output ends with the result you care about. || true keeps the script from reporting failure for an LV that is not mounted.
When to use -r and when to do two steps
Both approaches reach the same end state. The choice is about control and risk.
| Situation | Better choice | Reason |
|---|---|---|
| Routine growth of a mounted ext4 or XFS volume | `lvextend -r` | One command, no chance of picking the wrong filesystem tool |
| Documented runbook for an operations team | `lvextend -r` | Fewer steps to skip or reorder |
| You want to inspect the LV before touching the filesystem | Two steps | You can check `lvs` and `lsblk` between them |
| Filesystem has known errors | Two steps | You can run `e2fsck` or `xfs_repair` before the resize |
| LV has a layer between it and the filesystem, such as LUKS encryption | Two steps, or check `fsadm` support | The extra layer needs its own resize step, and support in `fsadm` depends on version |
| Filesystem is not ext or XFS | Two steps | `fsadm` may not support it |
Verifying the result
The command reporting success does not by itself prove the application can use the space. Check at each layer:
sudo lvs vg_data
sudo vgs vg_data
lsblk -f /dev/vg_data/lv_app
df -hT /srv/dataRead the output in this order:
lvsshould show the new LV size.vgsshould showVFreereduced by the amount you added. If you used+100%FREE,VFreeshould be zero.df -hTshould show the new filesystem size. It is normally a little smaller than the LV size because of filesystem metadata, so a 120 GiB LV will show somewhat less than 120 GiB in theSizecolumn.- The
Use%value should have dropped as expected for the amount you added.
A mismatch between steps 1 and 3 means the filesystem step did not complete. Use the recovery steps above.
Edge cases
- Free space is exactly what you asked for. If you request
-L +20Gand the volume group has 19.99 GiB free, the command fails with an insufficient free space error and changes nothing. Use-l +100%FREEwhen you want everything that is there. - Extent rounding. LVM allocates in physical extents, 4 MiB by default. A request is rounded up to a whole number of extents, so the final size may differ slightly from what you typed.
- Mounted versus unmounted ext4. Growing a mounted ext4 filesystem is safe and online. If the ext4 filesystem is unmounted,
fsadmchecks it first withe2fsck, and that check can take a long time on a large volume. - Unmounted XFS.
xfs_growfscan only grow a mounted filesystem. If the LV is not mounted, mount it before extending, or use the two-step method and mount it for the second step. - Thin pools and snapshots. For a thin pool, extending the pool is a different operation from extending a thin volume.
-racts on the filesystem of the LV you name. Check pool free space withlvsbefore relying on it. - Scripts.
lvextendmay prompt in some situations. In unattended automation, test the command interactively on a non-production system first and check its exit status. A non-zero exit status after the LV was already extended is the partial state described earlier.
Summary of the workflow
The full growth path on a running server, using -r for the last two steps, looks like this:
- Inspect with
pvs,vgs,lvs,lsblk -f, anddf -hT. - Make free space available in the volume group: add a disk with
pvcreateandvgextend, or enlarge an existing disk and runpvresize. - Run
lvextend -r -L +SIZE /dev/VG/LVorlvextend -r -l +100%FREE /dev/VG/LV. - Verify with
lvs,vgs, anddf -hT.
Exercise
You run sudo lvextend -r -L +20G /dev/vg_data/lv_app. The output shows "Logical volume vgdata/lvapp successfully resized", followed by an error from fsadm. lvs shows lv_app at 120 GiB, but df -hT /srv/data still shows about 100 GiB. The filesystem is XFS. What state is the system in, and what do you run to fix it?
Show answer
The LV extension was committed before the filesystem step ran, and the failed filesystem step was not rolled back. So the LV is 120 GiB but the XFS filesystem still has its old size. No data is lost. First check that the filesystem is mounted at /srv/data and that xfsprogs is installed, since xfsgrowfs needs a mounted filesystem. Then grow it with `sudo xfsgrowfs /srv/data, using the mount point and not the device path. Confirm with df -hT /srv/data`.
Exercise
What is the difference between lvextend -r -L 150G /dev/vg_data/lv_app and lvextend -r -L +150G /dev/vg_data/lv_app when the LV is currently 100 GiB?
Show answer
Without the plus sign, 150G is the new total size, so the LV grows by 50 GiB to 150 GiB. With the plus sign, 150G is the increase, so the LV grows by 150 GiB to 250 GiB, which fails if the volume group does not have 150 GiB free.
Exercise
Give two situations in which you would run the LV and filesystem steps separately instead of using lvextend -r.
Show answer
Any two of these: you want to inspect the LV before changing the filesystem; the filesystem has known errors that you want to repair with e2fsck or xfs_repair first; there is an extra layer such as LUKS between the LV and the filesystem that needs its own resize step; the filesystem type is not one that fsadm supports.