Sub topic 8
Doing Both Steps at Once with `lvextend -r`
Assume you are comfortable with sudo, block devices, mount points, and filesystems such as ext4 and XFS. You should also know how to read df, lsblk, and basic LVM output. This chapter focuses on extending an existing logical volume and then growing its filesystem in one command.
What `lvextend -r` does
LVM separates storage into layers:
- A physical volume (PV) represents storage prepared for LVM.
- A volume group (VG) combines one or more PVs and contains free space.
- A logical volume (LV) receives space from the VG.
- A filesystem, such as ext4 or XFS, stores files inside the LV.
Extending the LV changes only the block device size. The filesystem does not automatically know that more blocks are available. The -r, or --resizefs, option tells lvextend to grow the filesystem after it grows the LV.
flowchart TD
A[Free extents in volume group] --> B[lvextend grows logical volume]
B --> C[lvextend -r detects filesystem]
C --> D[Filesystem resize tool runs]
D --> E[Applications see more free space]The -r option normally selects the appropriate filesystem utility:
| Filesystem | Resize utility | Input used by the utility |
|---|---|---|
| ext4 | `resize2fs` | Logical volume device |
| XFS | `xfs_growfs` | Mounted filesystem path |
The filesystem must support online growth for the operation to run while mounted. Both ext4 and XFS support growing while mounted in normal Linux configurations.
Check the layout first
Do not begin with lvextend until you know the exact logical volume, volume group, filesystem, mount point, and available free space.
sudo pvs
sudo vgs
sudo lvs
lsblk -f
df -hT /srv/dataThe vgs output includes a VFree column. This is the space currently available in the volume group. The logical volume can use only this free space unless you first add capacity to the VG.
The lsblk -f output helps match a device to its filesystem and mount point. The df -hT command shows the filesystem type and the capacity currently visible to applications.
A typical relationship might look like this:
- Volume group:
vg_data - Logical volume:
lv_app - Device:
/dev/vg_data/lv_app - Mount point:
/srv/data - Filesystem:
xfs - Free space in VG:
25 GiB
The device path is the target for lvextend. The mount point is useful for verification and for some filesystem-specific tools.
Extend by a fixed amount
To add exactly 20 GiB, use -L +20G. The plus sign is important. It means “add this amount” rather than “set the total size to this amount.”
sudo lvextend -r -L +20G /dev/vg_data/lv_appThe command performs these operations:
- Checks whether the VG has at least 20 GiB available.
- Extends
/dev/vg_data/lv_app. - Identifies the filesystem.
- Runs the suitable filesystem growth operation.
- Reports the resulting sizes.
If the VG has insufficient free space, LVM stops before completing the requested extension. Read the error carefully. Do not substitute another device path without checking the layout again.
Use all remaining free space
You can allocate all free extents in the volume group with -l +100%FREE.
sudo lvextend -r -l +100%FREE /dev/vg_data/lv_appThe uppercase -L measures logical volume size in bytes, such as 20G. The lowercase -l works with logical extents. +100%FREE means all currently unallocated extents in the VG.
This option is convenient, but it leaves no free space for another logical volume or a later emergency expansion. Use it only when consuming the entire remaining VG capacity is intentional.
What happens internally
The logical volume is made from extents, which are fixed-size allocation units in a volume group. When lvextend runs, LVM assigns additional free extents to the LV. The block device then reports a larger size.
The filesystem has its own metadata describing how many blocks it can use. The filesystem resize step updates that metadata and creates any required structures for the new space. Until this second step succeeds, the extra LV capacity may exist but remain unusable for files.
The two steps are connected by -r, but they are still logically separate. If the LV extension succeeds and filesystem growth fails, the LV can be larger than the filesystem. This situation is usually recoverable by running the correct filesystem tool manually.
For ext4, the manual operation uses the device:
sudo resize2fs /dev/vg_data/lv_appFor XFS, the filesystem must be mounted, and the command uses the mount point:
sudo xfs_growfs /srv/dataVerify both layers
After lvextend -r completes, verify the LV and the filesystem separately.
sudo lvs /dev/vg_data/lv_app
lsblk -f
df -hT /srv/datalvs confirms the logical volume size. df confirms the size available to the filesystem and applications. These values can differ temporarily if the LV was extended without a successful filesystem resize.
You can also inspect the command's exit status immediately afterward:
sudo lvextend -r -L +20G /dev/vg_data/lv_app
echo $?An exit status of 0 indicates that the command reported success. Still check df, because verification should measure the result rather than rely only on a command status.
Limits and recovery
Growing is generally safer than shrinking. XFS can grow, but it cannot be shrunk in place. If an XFS filesystem is too large, the usual solution is to create a smaller filesystem, copy the data, and switch the mount configuration.
Do not interrupt a resize without understanding its state. Check LVM and filesystem output before retrying. Keep a tested backup before changing production storage. A backup does not prevent every storage error, but it gives you a recovery path if the wrong LV is changed or the underlying device has a problem.
Exercise
The volume group has 30 GiB free, and /dev/vg_data/lv_app contains an ext4 filesystem mounted at /srv/data. Write one command that adds 20 GiB and grows the filesystem, then name the command you would use to verify the space visible to applications.
Show answer
Run sudo lvextend -r -L +20G /dev/vg_data/lv_app, then run df -hT /srv/data. The first command grows the LV and ext4 filesystem; the second reports the filesystem capacity and free space.