If you have Ubuntu Linux virtual machines, it is highly likely that you will need to expand your virtual hard disk and increase the partition in the guest OS. By default, the Ubuntu installer offers to use logical volumes (LVMs) for the operating system, so we will study how to extend the LVM partition.
First, you need to increase the size of the virtual hard disk on your hypervisor.
Once you have increased the VM disk size, you must expand the Linux guest OS partition (Ubuntu 22.04 LTS in this example). Force a disk rescan, to make your Ubuntu see the added disk space:
$ echo 1>/sys/class/block/sda/device/rescan
Use the cfdisk to manage Ubuntu partitions:
$ sudo cfdisk
In this example, you can see that there is 1GB of free (unallocated) space on /dev/sda. Select the partition you want to extend (/dev/sda3) and select Resize from the menu.
Select Write from the menu to save the changes to the disk partition layout.
Once you have increased the size of the physical partition, you will need to increase the size of the logical partition in Ubuntu. Check that you are using LVM volumes:
$ sudo lsblk
- Physical Volume (PV) – your physical disks (
- Volume Group (VG) – contains one or more physical volumes (disks). For example,
- Logical Volume (LV) – is a logical volume in a volume group. For example:
Check the available free space in your LVM volume group:
$ sudo vgdisplay
In order to expand an LVM partition, you first need to increase the size of the PV (Physical Volume):
$ sudo pvresize /dev/sda3
Now you may extend the logical volume. In this case, we will resize the volume using all the free space available:
$ sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
Logical volume ubuntu-vg/ubuntu-lv successfully resized.
Then you have to expand the file system. For ext2, ext3 and ext4, run the command below:
$ sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
Check the available free space:
$ df -h
So we have successfully extended an LVM volume on Ubuntu.
$ sudo parted