How to Expand Disk via Growpart and Resize2fs

To expand a disk via the command line, you can follow the below steps,

Note: Before proceeding, make sure you have a backup of your important data and take necessary precautions, as resizing disks can be risky if not done correctly.

Check Current Disk Usage

First, check the current disk usage and available space on your system using the fdisk -l command,

fdisk -l

Output:

root@vps:~# fdisk -l
...
Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: CA147C86-F18D-46FC-A68E-BC0F0EFB040E

Device       Start      End  Sectors  Size Type
/dev/vda1     2048     4095     2048    1M BIOS boot
/dev/vda2     4096  1052671  1048576  512M Linux swap
/dev/vda3  1052672 52428766 51376095 24.5G Linux filesystem

From the output above, we can notice the partition/device /dev/vda3 is the root and the partition which we want to resize.

Also the /dev/vda Disk size is displaying 100GB while the main or root partition /dev/vda3 is still at 25GB.

Expand the Virtual Disk

We should expand the remaining space of 75GB using the Growpart,

growpart /dev/vda X

Note: Replace X with actual partition number. In this case, it is /dev/vda3, so X is replaced by 3.

Example:

growpart /dev/vda 3

Output:

root@vps:~# growpart /dev/vda 3
CHANGED: partition=3 start=1052672 old: size=51376095 end=52428767 new: size=208662495 end=209715167

Resize the Filesystem

Now, you need to resize the filesystem to utilize the newly allocated space. The command to use depends on the filesystem type,

resize2fs /dev/vdaX

Note: Same as previous, since the root partition is on /dev/vda3, X value is 3.

Output:

root@vps:~# resize2fs /dev/vda3
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/vda3 is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 13
The filesystem on /dev/vda3 is now 26082811 (4k) blocks long.

Thats it, the partition has been expanded to 100GB now.

Verify Disk Expansion

Use the fdisk -l command again to verify that your filesystem has been successfully expanded and is now using the additional space.

fdisk -l

Output:

root@vps:~# fdisk -l
...
Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: CA147C86-F18D-46FC-A68E-BC0F0EFB040E

Device       Start       End   Sectors  Size Type
/dev/vda1     2048      4095      2048    1M BIOS boot
/dev/vda2     4096   1052671   1048576  512M Linux swap
/dev/vda3  1052672 209715166 208662495 99.5G Linux filesystem

This concludes our topic of expanding the disk using Growpart and Resize2fs.