If you are using virtual machines with Ubuntu Linux, sooner or later you may need to increase the available space on the virtual disk. The Ubuntu installer defaults to using logical LVM volumes for deploying the OS, so we will discuss disk expansion using LVM.
First, you need to increase the size of the virtual disk from your virtualization system.
After you have expanded the VM disk, you need to expand it in the guest OS (in this example, it is Ubuntu 22.04 LTS). To make Ubuntu see the added space, perform a forced disk rescan using the command:
$ echo 1>/sys/class/block/sda/device/rescan
To manage partitions, use the cfdisk utility:
$ sudo cfdisk
In this example, it can be seen that there is 1 GB of free space on the /dev/sda disk. Select the partition that needs to be extended (/dev/sda3 in this example) and choose Resize from the menu.
To save the changes to the disk partitions, select Write from the menu.
Close the cfdisk utility.
After increasing the size of the physical partition, you need to increase the size of the logical partition in Ubuntu. Make sure you are using LVM volumes:
$ sudo lsblk
In brief, let's remind the main concepts of LVM:
- Physical Volume (PV) - your physical disks /dev/sda, /dev/sdb, etc.
- Volume Group (VG) - groups of volumes that are combined from physical disks. For example, ubuntu-vg = /dev/sda + /dev/sdb. VG can also consist of only one disk.
- Logical Volume (LV) - a logical partition in a volume group. For example, ubuntu-vg/root, ubuntu-vg/home, etc.
Please check the free space in the LVM volume group using the following command:
$ sudo vgdisplayTo expand an LVM partition, you first need to increase the size of the PV (Physical Volume):
$ sudo pvresize /dev/sda3Now you can increase the logical volume. In this example, we will extend the volume to use all available space:
$ sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
Logical volume ubuntu-vg/ubuntu-lv successfully resizedNow it remains to expand the file system. For ext2, ext3 and ext4, execute:
$ sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lvTo check free space in Linux, use the command:
$ df -h
So, we have successfully expanded the LVM volume in Ubuntu.
If you are not using LVM volumes, use the parted utility to resize partitions in Linux.
- $ sudo parted
- print free
- resizepart n