Ubuntu 22.04 – Extending the root partition

Introduction

I run several virtual machines based on Ubuntu. I create a base image that is cloned when I need a new virtual machine. Since most of my VMs don’t need a large disk image I keep the base image on the small side. So I extend the size of the disk image when needed. After the disk image size is increased you need to allocate that space to use it. The steps below will show you how to extend the root partition to the new size.

The Steps

I’m assuming that the disk image or physical hard drive of the machine has been increased.

Do a df -h to get the logical volume name of the partition you want to extend.
> df -h

Filesystem                        Size Used Avail Use% Mounted on
tmpfs                             393M 1.1M  392M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  24G 9.0G   14G  41% /
tmpfs                             2.0G    0  2.0G   0% /dev/shm
tmpfs                             5.0M    0  5.0M   0% /run/lock
/dev/sda2                         2.0G 367M  1.5G  21% /boot
tmpfs                             393M 4.0K  393M   1% /run/user/1000

Now check to be sure the volume group has free space. The free space is the possible max amount you can extend the partition.
> vgdisplay

With the information above I can expand the partition 23G. Here’s the command I would use.
> sudo lvextend -L +23G /dev/mapper/ubuntu–vg-ubuntu–lv

Finally we extend the filesystem to the new logical volume size.
> sudo resize2fs /dev/mapper/ubuntu–vg-ubuntu–lv                     

Now that we’re done, lets reboot the system.
> sudo reboot

Log back into the ubuntu system. Check that your partition has been extended.
> df -h

Conclusion

Thanks to LVM the process of extending the root partition is simple and straight forward.