How to extend HDD space in ubuntu 20 VM (this example is hosted on ESXi)

After extending the hard disk of the VM in ESXi or any other hypervisor, reboot the VM and boot using a bootable GPARTED iso image.

Before the extention, this VM has 200GB HDD which is almost full now

Once the GPARTED gui comes up, confirm that the free space is showing up and then select the partition which needs to be extended and use the slider to extend it my moving it towards right side. Then click on SAVE to write the changes. We are done here so we can now reboot the machine to claim the newly added space in the OS. If the disk is 100% used then the OS will not let you make/save any partition chanes to the disk which is counter intutive but that is how it is designed so when the OS is rebooted, press escape button on keyboard and then select advanced ubuntu startup options and drop to root shell. Once here, run apt-get clear && apt-get autoremove this will remove enough space to enable you to write changes to the disk.

GPARTED – extending the partition
Reboot GPARTED

After the reboot, run these commands in this sequence to extend the volume.

######## ANOTHER METHOD #########

  • sudo parted
  • print free space (This will show the partitions with numbers and the free space, we will use the partition number and free space next)
  • resizepart <NUMBER> <FREE SPACE i.e. 90GB>
  • write
  • quit
  • sudo lsblk (Note down the name of the partition and volume)
  • sudo pvresize /dev/sda3 (it was /dev/sda3 in this case as shown in output of lsblk, make sure it is correct)
  • sudo lvextend -l +100%FREE /dev/mapper/ubuntu–vg-ubuntu–lv
  • sudo resize2fs /dev/mapper/ubuntu–vg-ubuntu–lv
    • df -h (to check the resizing)