Resizing a VM Disk and root-partition on Ubuntu

ubuntu Sep 16, 2020

In a recent project, I encountered a scenario where I needed to resize a virtual machine (VM) disk due to space constraints. The original disk size proved inadequate for its intended use. Utilizing an XCP-NG server for my VMs, I increased the disk size to a suitable level. However, upon rebooting the VM, the filesystem did not reflect the changes. Below, I outline the steps taken to resize the disk and ensure the filesystem utilized the newly allocated space.

Initial Assessment

First, I assessed the current disk usage and partition layout using:

$ df -h

This confirmed that the root filesystem was fully utilized (100% used), specifically bound to /dev/xvda2.

Resizing the Partition

To resize the partition, I executed:

$ sudo growpart /dev/xvda 2

The output indicated the partition expansion:

CHANGED: partition=2 start=4096 old: size=20965376 end=20969472 new: size=41938911,end=41943007

Next, I verified the change with:

$ lsblk

The output confirmed the updated disk and partition sizes:

NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0     7:0    0 96.6M  1 loop /snap/core/9804
loop1     7:1    0 97.1M  1 loop /snap/core/9993
sr0      11:0    1 1024M  0 rom
xvda    202:0    0   20G  0 disk
├─xvda1 202:1    0    1M  0 part
└─xvda2 202:2    0   20G  0 part /

The previous disk size was 10GB, now expanded to 20GB.

Verifying Filesystem Size

Upon checking the filesystem size with:

$ df -h

The output indicated the filesystem hadn't been resized:

Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda2      9.8G  9.3G  8.3M 100% /

Resizing the Filesystem

To utilize the entire partition, I resized the filesystem using:

$ sudo resize2fs /dev/xvda2

The output was:

resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/xvda2 is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/xvda2 is now 5242363 (4k) blocks long.

Final Verification

Finally, I confirmed the filesystem resize with:

$ df -h

The updated output showed the available disk space:

Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda2       20G  9.3G  9.5G  50% /

By following these steps, I successfully resized the VM disk and ensured the filesystem took full advantage of the new space.

Photo from: @heapdump

Tags