Expand hard disk on Hyper-v ubuntu vm

Written by

in

Once you have expanded the virtual hard disk (.vhdx) in Hyper-V, start up your Ubuntu virtual machine.

Because Ubuntu installations typically use LVMs by default, the process requires expanding the partition, resizing the physical volume, extending the logical volume, and finally resizing the filesystem.

Follow these steps directly inside your Ubuntu terminal:

Step 1: Install Cloud-Guest-Utils (if not already installed)

This package includes the growpart utility, which makes resizing partitions safe and easy.

sudo apt update
sudo apt install cloud-guest-utils

Step 2: Identify Your Partition Layout

Run the following command to check your disk structure:

sudo lvmdiskscan

Look for your main disk (usually /dev/sda) and find the partition number that acts as the LVM physical volume (typically /dev/sda3 or /dev/sda2).

Step 3: Expand the Partition

Use growpart to expand the partition into the newly available unallocated space. (Note the space between the disk name and the partition number).

sudo growpart /dev/sda 3

(Replace 3 with your actual partition number found in Step 2 if it differs).

Step 4: Resize the LVM Physical Volume (PV)

Tell LVM that the underlying partition has grown:

sudo pvresize /dev/sda3

(Again, replace /dev/sda3 with your specific partition path).

Step 5: Extend the Logical Volume (LV)

Next, expand your logical volume to consume 100% of the newly freed space. On standard Ubuntu installations, the root logical volume path is usually /dev/ubuntu-vg/ubuntu-lv.

sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv

Step 6: Resize the Filesystem

Finally, expand the actual filesystem so Ubuntu recognizes the usable space:

sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv

Step 7: Verify the Expansion

Check your updated storage capacity:

df -h

Your root file system (/) should now reflect the full expanded size you allocated in Hyper-V.