Grow a volume with the filesystem still mounted

short · 30 min · Objective 1.3

Task

Build the full LVM stack from loop devices, fill a volume, then extend it and its filesystem online -- and demonstrate the two-layer trap by extending the volume without the filesystem first.

Steps

  1. Create physical volumes on two of the loop devices with pvcreate, and confirm with pvs. Use the device names losetup printed: they are /dev/loop0 to /dev/loop2 on a fresh machine, but losetup -f takes the first FREE devices, and anything already using loops shifts the numbers -- pvcreate on the wrong one destroys whatever is on it.
  2. Create a volume group from them: vgcreate labvg /dev/loop0 /dev/loop1. Check the free space with vgs.
  3. Create a 400M logical volume, format it xfs, and mount it at /mnt/lab. (Not smaller: current xfsprogs refuses to make an xfs filesystem under 300 MB.)
  4. Fill it to about 90% with dd from /dev/zero and confirm with df -h.
  5. Now make the classic mistake deliberately: lvextend -L +100M WITHOUT -r. Run df -h and observe that the filesystem is unchanged. Explain the two layers.
  6. Complete it properly with xfs_growfs /mnt/lab -- note that it takes the MOUNT POINT, not the device -- and confirm df now shows the new size.
  7. Add the third loop device to the group with pvcreate and vgextend, and confirm the free extents increased. This is the answer to "the volume group is full".
  8. Try lvreduce -L -100M on the xfs volume and read the refusal: current lvm2 (2.03.17 and later, RHEL 9.2 onwards) checks the filesystem first and refuses, because xfs cannot shrink. Older lvm2 does not check -- it prints a warning and asks Do you really want to reduce ...? [y/n]. Answer n: y cuts the end off a filesystem that cannot shrink, and the data with it.

Verify

vgs labvg -o vg_name,vg_size,vg_free --noheadings
lvs labvg -o lv_name,lv_size --noheadings
df -h /mnt/lab | tail -1
xfs_info /mnt/lab | head -2
mount | grep -q '/mnt/lab' && echo "still mounted throughout"

The volume size from lvs and the filesystem size from df must agree after step 6 and must DISAGREE after step 5. That disagreement is the thing to be able to recognise.

Notes

Clean up by unmounting, vgremove labvg, pvremove each device and losetup -d each loop, then delete the image files. Doing the teardown is worth the two minutes -- an orphaned volume group on loop devices reappears confusingly after a reboot.

This is an independent study companion for CompTIA Linux+ and is not produced by or endorsed by CompTIA.