Sub topic 2
Adding Capacity from a New Disk
This chapter assumes you can use the Linux command line with sudo, read df and lsblk output, and know that a volume group (VG) can hold several disks. You will add space to an existing LVM setup, where a logical volume (LV) is mounted at /srv/data and sits in the volume group vg_data.
Adding capacity from a new disk
Start by confirming the volume group has free space, then add the new disk as a physical volume (PV) and attach it to the group:
sudo vgs vg_data
sudo pvcreate /dev/sdc
sudo vgextend vg_data /dev/sdc
sudo vgs vg_dataThe VFree column should now show the new capacity. Nothing on the mounted filesystem changes yet.
Using space from an enlarged virtual or cloud disk
If the provider or hypervisor enlarged an existing disk, LVM does not see the extra space until you rescan the device and resize the physical volume. Assuming the disk is /dev/sda and LVM uses partition 3:
echo 1 | sudo tee /sys/class/block/sda/device/rescan
sudo growpart /dev/sda 3
sudo pvresize /dev/sda3
sudo vgsgrowpart comes from the cloud-guest-utils package on Debian and Ubuntu.
Extending the logical volume
Grow the LV by a fixed amount, or use all free space in the group:
sudo lvextend -L +20G /dev/vg_data/lv_app
# or
sudo lvextend -l +100%FREE /dev/vg_data/lv_appGrowing the ext4 or XFS filesystem
The filesystem must grow separately, using the tool for its type:
# ext4 takes the device path and works while mounted
sudo resize2fs /dev/vg_data/lv_app
# XFS takes the mount point and must be mounted
sudo xfs_growfs /srv/dataAlternatively, sudo lvextend -r -L +20G /dev/vg_data/lv_app extends the LV and resizes the filesystem in one step.
Exercise
You enlarged the disk /dev/sdb in your cloud console, and LVM still shows the old size. Which command makes the physical volume use the new space, and which command must run before it?
Show answer
Run sudo pvresize /dev/sdb after the partition is grown with growpart and the device is rescanned. The rescan and partition growth must come first, because pvresize only uses space the partition already exposes.