Sub topic 4
Adding Capacity from a New Disk
Assume you are comfortable using the Linux command line with sudo, reading lsblk and df output, and understanding mount points. This chapter uses LVM, the Logical Volume Manager. LVM lets you combine storage devices into flexible pools and allocate that pooled space to logical volumes.
How the New Disk Fits into LVM
LVM uses three main layers:
- A physical volume (PV) is a disk or partition prepared for LVM.
- A volume group (VG) is a pool made from one or more physical volumes.
- A logical volume (LV) is a virtual block device allocated from a volume group.
A new disk must move through these layers before applications can use its space.
For example, /dev/sdc can join vg_data. The volume group then has more free space, but the mounted filesystem does not grow automatically. You must extend the logical volume and then grow its filesystem.
Inspect the Existing Layout
Before changing storage, identify the new disk and the logical volume that needs more space. Device names can change between systems, so do not assume that /dev/sdc is unused.
sudo pvs
sudo vgs
sudo lvs
lsblk -f
df -hT /srv/dataThe commands show different parts of the layout:
| Command | What it shows |
|---|---|
| `pvs` | Physical volumes and the volume groups using them |
| `vgs` | Volume groups and their free space |
| `lvs` | Logical volumes and their sizes |
| `lsblk -f` | Block devices, filesystems, UUIDs, and mount points |
| `df -hT /srv/data` | Filesystem type and currently available space |
Use lsblk -f to confirm that the new disk has no filesystem or important mount point. In this example, /dev/sdc must be the new disk, while the existing LVM devices must remain untouched.
A volume group’s VFree column in vgs is the space that can be assigned to a logical volume. If it already contains enough free space, you may not need to add a disk at all.
Create a Physical Volume
After confirming the disk is unused, initialize it as an LVM physical volume:
sudo pvcreate /dev/sdc
sudo pvspvcreate writes LVM metadata to the device. This action changes how the disk is recognized and can make existing data inaccessible. Never run it on a disk that contains data you need.
You can use a whole disk as a physical volume, as shown here. Some organizations instead create a partition such as /dev/sdc1 and initialize that partition. Follow the storage layout required by your server standards. Do not mix the whole-disk and partition approaches accidentally.
Add the Physical Volume to a Volume Group
Add the new physical volume to the existing volume group:
sudo vgextend vg_data /dev/sdc
sudo vgs vg_data
sudo pvsThe vgextend command makes the disk’s capacity available to vg_data. Check the VFree column in the output. It should increase by approximately the usable size of /dev/sdc.
At this point, the new capacity belongs to the volume group, but it is not yet part of a logical volume. Applications still see the old filesystem size.
Extend the Logical Volume
Find the logical volume mounted at /srv/data in the lvs and df output. You can then assign some or all of the volume group’s free space to it.
To add a fixed amount:
sudo lvextend -L +20G /dev/vg_data/lv_appThe +20G means “add 20 gigabytes.” Without the plus sign, -L 20G means “set the final logical volume size to 20 gigabytes,” which could reduce the volume or fail if it is already larger.
To allocate all remaining free space:
sudo lvextend -l +100%FREE /dev/vg_data/lv_appThe lowercase -l option works with allocation extents, which are LVM’s units of storage. +100%FREE assigns every currently free extent in the volume group. This is convenient, but it leaves no reserve for another logical volume or future expansion.
Extending the logical volume does not always expand the filesystem inside it. Verify the filesystem type with lsblk -f or df -hT, then use the matching filesystem tool:
# For an ext4 filesystem:
sudo resize2fs /dev/vg_data/lv_app
# For an XFS filesystem mounted at /srv/data:
sudo xfs_growfs /srv/dataresize2fs takes the logical-volume device path. xfs_growfs takes the mounted directory. Both operations grow the filesystem into available space; neither can create space that was not first assigned to the logical volume.
Verify the Result
Check all three layers after the change:
sudo lvs /dev/vg_data/lv_app
sudo vgs vg_data
df -hT /srv/dataThe logical volume should show its new size. The filesystem reported by df should also be larger. If lvs grew but df did not, the filesystem resize step was missing or used the wrong command. If vgs still shows free space, that may be intentional if you added only a fixed amount.
Exercise
You added /dev/sdc to vg_data, and vgs shows 100 GiB of free space. The logical volume mounted at /srv/data is still unchanged. What step is missing?
Show answer
The free space must be assigned to the logical volume with lvextend, and the filesystem must then be grown with resize2fs for ext4 or xfs_growfs for XFS.