Resizing Root on LVM

One of the advantages of using virtual machines is that you can easily create templates which you reproduce as needed. Of course the file systems are rarely the size you need them to be.

Fortunately, if you have either XFS or ext4 installed on LVM, expanding the root file system is fairly easy.
The steps:

1. Confirm the layout using lsblk and pvs

root@docker1:/home/maurice# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 55M 1 loop /snap/core18/1705
loop1 7:1 0 93.9M 1 loop /snap/core/9066
loop2 7:2 0 55M 1 loop /snap/core18/1754
loop3 7:3 0 127.9M 1 loop /snap/docker/443
loop4 7:4 0 69M 1 loop /snap/lxd/14804
loop5 7:5 0 68.9M 1 loop /snap/lxd/15272
loop6 7:6 0 27.1M 1 loop /snap/snapd/7264
loop7 7:7 0 30.3M 1 loop /snap/snapd/7777
sda 8:0 0 250G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 249G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 249G 0 lvm /
sr0 11:0 1 1024M 0 rom
root@docker1:/home/maurice# pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 ubuntu-vg lvm2 a-- <249.00g 0


sda3 in this case

2. Extend the partition using growpart
sudo apt install cloud-guest-utils (if needed)
growpart /dev/sda 3 (partition 3 of sda is to be extended)

3. Resize root physical volume to use all the space
pvresize /dev/sda3

4. Identify the logical volume

root@docker1:/home/maurice# df -hT | grep mapper
/dev/
mapper/ubuntu--vg-ubuntu--lv ext4 246G 3.7G 232G 2% /

5. Extend the logical volume to use all the space
lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv

6. Update the file system
resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv (ext4)
xfs_growfs / (xfs)

A full description can be found at https://computingforgeeks.com/extending-root-filesystem-using-lvm-linux/