Extending Partitions in Linux VMs after Expanding Virtual Disks
Let’s imagine this scenario whereby your Linux virtual machine is underperforming as a result of low disk space. Virtualization software such as Proxmox VE, VMware, etc, allow you to easily configure virtual hardware such as CPU, memory, and hard disk size for your virtual machine. You can extend the size of an existing virtual disk. However, you must resize a partition manually after that since disk space added after you extend a virtual disk is unallocated.
In this article, we are going to learn how to add extra space into an existing Linux partition.
Pre-requisites
- Backup your VM before doing disk operations.
- Snapshots must not be enabled on the virtual disk. If enabled, delete them before you resize the virtual disk.
Resizing a Virtual Disk in Linux
Step 1: Check the disk
Use the command below to check the disk.
sudo fdisk -l
Step 2: Extend boot partition
Use cfdisk
to extend boot partition,select it, (/dev/sda3 in my case) and select “Resize”
sudo cfdisk
- Click Enter, New size is shown
- Then select Write, when prompted, type Yes, and then select Quit and press Enter
- Run
fdisk -l /dev/sda
, to confirm whether partition is extended
- Get PV (Physical Volume) name ie (/dev/sda3)
sudo pvs
- Resize the physical volume, PV
sudo pvresize /dev/sda3
- Get root logical volume name (Filesystem value of
/
row; ex:/dev/mapper/ubuntu--vg-ubuntu--lv
)
df -h
- Expand the logical volume
sudo lvextend -r -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
Your disk space is now successfully increased.
Conclusion
Such an issue is quite common when running virtual machines and this way, you can be able to overcome it.