Why can't I shrink a virtual disk in Hyper-V

0

I have a virtual machine running Ubuntu 12.04 which I created using Hyper-V in a Windows 8.1 host. At one point, I had a very large file in my virtual machine which caused my dynamically expanding hard drive to grow to almost 200 GB. Since then, I have removed the file and I am trying to shrink the hard drive file back down. As you can see from this screenshot of GParted from within Ubuntu, I am not using a lot of disk storage:

192 GiB unused

I tried using the "compact disk" option within Hyper-V settings; however, this did not affect the size of the vhdx file in the host which remains close to 200 GB in size. I also tried following these instructions for using Windows' diskpart utility, but this also had no effect. Am I missing something? Is there something else I can try?

Daniel

Posted 2017-05-11T20:10:50.133

Reputation: 151

compact disk has never changed the size of the virtual hdd, just the amount of storage, required to store the virtual hdd. If you want to do that, you would have to create a new virtual hdd file, or use a command to resize the existing one (which of course would create a new virtual hdd). – Ramhound – 2017-05-11T20:13:44.803

@Ramhound I think either I'm not understanding what you're saying or you're not understanding what I'm saying. I don't want to reduce the capacity of the virtual hard drive. I want that to remain 200 GB. I just want to reduce the size of the .vhdx file because the drive is currently only storing 6 GB of data. – Daniel – 2017-05-11T20:18:11.627

I understand what you are saying. Your attempting to change the size of the virtual hdd with a command that simply reduces the amount of storage space that file consumes. The title of this question, states you want to shrink the vhdx file, my answer explains how to do that. In any event all you have to do is shrink it by 1 GB in order to solve your problem. – Ramhound – 2017-05-11T20:27:49.433

Answers

1

I strongly suggest you take the steps to make a copy of the .vhdx before you modify it. In order to change the size of an existing .vhdx file the first thing you should do is mount the virtual hdd by using the command: mount-vhd in an elevated PowerShell command prompt.

mount-vhd \\FS01\vmdata\AS02_C.vhdx -passthru | get-disk | get-partition | get-volume

The output of the command will look like

enter image description here

If you want to change the size of one of the partitions by using: resize-partition in an elevated PowerShell command prompt.

resize-partition -driveletter E -size 35GB

Before you can change the size of the virtual hdd you will have use the command: dismount-vhd in an elevated PowerShell command prompt.

dismount-vhd \\FS01\vmdata\AS02_C.vhdx

Once you have modifyed the partition size and dismounted the virtual hdd. You can modify the size of the virtual hdd by using the command: resize-vhd in an elevated PowerShell command prompt.

resize-vhd \FS01\vmdata\AS02_C.vhdx -ToMinimumSize

Working with and resizing Windows Server 2012 VHDs Resize-VHD

Ramhound

Posted 2017-05-11T20:10:50.133

Reputation: 28 517

This process seems to work specifically for NTFS drives. The one I'm dealing with is ext4. Running the first command in your answer yields no output at all. – Daniel – 2017-05-11T20:47:28.620

You need to make Windows understand how to read ext4 partitions in that case. In any event, resize-vhd, the command you want to use. – Ramhound – 2017-05-11T20:51:21.750