Sub topic 6
Extending the Logical Volume
This chapter assumes you can use sudo, read lsblk and df output, and identify a mounted filesystem. It also assumes that the logical volume's volume group already has free space, or that you have completed the earlier step of adding a disk or enlarging a physical volume.
A logical volume (LV) is the virtual block device that contains a filesystem. LVM can make the LV larger without moving the filesystem's mount point. You must still grow the filesystem afterward so applications can use the new capacity.
The LVM layer you are changing
LVM divides storage into layers:
- A physical volume (PV) is an LVM-managed disk or partition.
- A volume group (VG) combines one or more PVs into a pool of storage.
- A logical volume (LV) is a virtual device allocated from that pool.
- A filesystem, such as ext4 or XFS, stores files inside the LV.
The lvextend command changes only the LV layer unless you use its filesystem-resize option.
An LV can use free extents in its volume group. An extent is a fixed-size allocation unit that LVM uses internally. You usually do not need to calculate the extent size yourself. You can request an extension in gigabytes or ask LVM to use a percentage of the free extents.
Check the available space
Before changing anything, identify the exact LV and confirm that the volume group has enough free space.
sudo vgs
sudo lvs -o lv_name,vg_name,lv_size,lv_path
df -hT /srv/dataA vgs result includes a VFree column. This is the unallocated space in the volume group. The lvs command shows the LV path that you will pass to lvextend.
For example, suppose the output identifies /dev/vg_data/lv_app as the LV mounted at /srv/data, and VFree shows 30G. You can allocate up to that amount without adding more storage.
| Goal | `lvextend` size argument | Meaning |
|---|---|---|
| Add a fixed amount | `-L +20G` | Add 20 GiB to the current LV |
| Set a total size | `-L 100G` | Make the LV 100 GiB total |
| Use all free extents | `-l +100%FREE` | Add all currently free VG space |
| Use part of free extents | `-l +50%FREE` | Add half of the currently free VG space |
The plus sign matters. -L +20G adds 20 GiB. -L 20G requests a total LV size of 20 GiB, which could be smaller than the current size and may fail or lead to an unwanted change.
Add a fixed amount
To add 20 GiB to the LV, run:
sudo lvextend -L +20G /dev/vg_data/lv_appLVM normally reports the old and new sizes and asks for confirmation. Review the target path and the requested size before accepting.
You can also use the extent-based form:
sudo lvextend -l +20G /dev/vg_data/lv_appHowever, lowercase -l means extents, not ordinary size units. A value such as +20G is commonly accepted by current LVM tools as a size expression, but -L is clearer when you want to specify gigabytes. Use lowercase -l mainly for percentages such as +100%FREE.
To use every remaining extent in the volume group:
sudo lvextend -l +100%FREE /dev/vg_data/lv_appThis consumes all free space that LVM can allocate to the LV. It leaves no space in the VG for another LV or for later expansion, so reserve space when future workloads need it.
Extend the filesystem after the LV
After lvextend succeeds, the block device is larger, but the filesystem may still report its old size. Grow the filesystem as a separate operation.
For ext4, provide the LV device path to resize2fs:
sudo resize2fs /dev/vg_data/lv_appFor XFS, the filesystem must be mounted. Provide the mount point to xfs_growfs:
sudo xfs_growfs /srv/dataThe difference is important. resize2fs works with the ext4 device, while xfs_growfs operates on a mounted XFS filesystem and uses the directory where it is mounted.
Check the filesystem type before selecting a command:
findmnt -no SOURCE,FSTYPE,TARGET /srv/dataA mounted ext4 filesystem can normally grow while it is online. XFS can also grow while mounted. Confirm your distribution's tools are installed before starting. The resize2fs command is usually provided by the e2fsprogs package, and xfs_growfs is usually provided by an XFS utilities package.
Resize both layers in one command
The -r or --resizefs option asks lvextend to grow the filesystem after extending the LV:
sudo lvextend -r -L +20G /dev/vg_data/lv_appTo allocate all remaining VG space and resize the filesystem:
sudo lvextend -r -l +100%FREE /dev/vg_data/lv_appThis is convenient, but it does not remove the need to verify the filesystem type, target device, and available space. If the filesystem-resize tool is unavailable or the filesystem is not supported by the automatic method, the LV might be extended without the filesystem being grown. Check the command output carefully.
Common errors and safety limits
The most common mistake is extending the LV and forgetting the filesystem. Another is selecting the wrong LV path, especially on a server with several volume groups. Always compare findmnt, lvs, and vgs output before running a destructive storage command.
Keep a tested backup before resizing production storage. Although growing an LV and a filesystem is normally safe, power loss, hardware failure, a wrong device path, or an existing filesystem problem can cause data loss.
Growing is usually easier than shrinking. ext4 can be reduced only with a careful offline procedure that shrinks the filesystem before the LV. XFS cannot be shrunk in place. To make an XFS filesystem smaller, create a new smaller filesystem and copy the data to it.
Exercise
The volume group has 12 GiB free, and /dev/vg_data/lv_app contains an XFS filesystem mounted at /srv/data. Which command extends the LV by all free space and grows XFS?
Show answer
Run sudo lvextend -r -l +100%FREE /dev/vg_data/lv_app, then verify the result with df -hT /srv/data. If automatic resizing is unavailable, run sudo lvextend -l +100%FREE /dev/vg_data/lv_app followed by sudo xfs_growfs /srv/data.