Hyper-V Shrink Disk

Hpyer-V Logo

In this article we are going to see how we can shrink a disk of a Virtual machine running on Hyper-V host to keep resource usage low not wasting valuable space when it is not needed. In my daily work one of the most common issue I come across when working in highly virtualized environments are oversized virtual machines.

Without going into too much detail of why this happen it is important to keep in mind a very simple rule when it comes to resource management in a virtual environment, start small as resources can always be added on the run and with Windows 2016 this will get even better.

Shrink Disks Limitations

Before actually going into details about the process it is worth noting that if you are using Dynamic disks within the guest OS, I’m of course not talking about dynamically expanding VHDXs, you cannot use the shrink process described in this post.

The second limitation is you cannot shrink a VHD disk file even if you can grown them of course.

Can I Shrink that Disk?

You can easily check if a disk can be shrunk using PowerShell with the Get-VHD cmdlet which will produce an output similar the following:

PS> Get-VHD -Path C:ClusterStorageVolume1VHDHeloCheck-ShrinkShrink-Disk.vhdx


ComputerName            : SRV-HYPERV02
Path                    : C:ClusterStorageVolume1VHDHeloCheck-ShrinkShrink-Disk.vhdx
VhdFormat               : VHDX
VhdType                 : Dynamic
FileSize                : 16412311552
Size                    : 80530636800
MinimumSize             : 53686059520
LogicalSectorSize       : 512
PhysicalSectorSize      : 4096
BlockSize               : 33554432
ParentPath              :
DiskIdentifier          : 783e65f8-6e5f-4da7-9c0d-45526d0882a8
FragmentationPercentage : 8
Alignment               : 1
Attached                : True
DiskNumber              :
Key                     :
IsDeleted               : False
Number                  :

As you can see the disk is 75GB in size with a minimum size of ~53GB

Shrink Disk

While someone could argue that a dynamically expanding disk could potentially never grow to full 75GB that’s not an insurance and we shrink the disk we can ensure a more predictable usage pattern and eventually add more space only when needed.

Shrink Disk – PowerShell Method

The first step to shrink the disk is making the Guest OS aware of the reduction which is easily done through PowerShell or Disk Manager.

You can shrink the disk via PowerShell easily with the Get-PartitionSupportedSize cmdlet you can find out Maximum and Minimum size of any disk

PS> Get-PartitionSupportedSize -DriveLetter C

SizeMin               SizeMax
-------               -------
14249545728           79976971776

With the Resize-Partition cmdlet you can resize the disk to any supported size

PS> Resize-Partition -DriveLetter C -Size 35GB

[su_spoiler title=”Shrink Disk via Disk Manager” style=”fancy” anchor=”DiskManager”]

Shrink Disk – Disk Manager Method

Right-Click the disk to shrink and from the contextual menu select Shrink Volume

Shrink Disk Guest OS

Shrink Disk Guest OS Query

Once the system has queried the disk service you will be greeted by the Shrink wizard page where you can see various statistics about the disk

Shrink Disk Wizard

The first field tells us the actual size of the disk, the second reports how much space can be taken away from the disk, the third field represents the amount of space that you want to remove and finally the fourth field represents what the final result will look like.

To write this article I have chosen to remove 50GB from the existing diskShrink Disk Guest Wizard

Once you are ready simply click the Shrink button and wait for the process to complete, the released space will appear as unallocated space

Shrink Disk Unallocated Space

[/su_spoiler]

 Shrink Disk – Shrink VHD Size

The final step to reclaim space that has been shrunk from the virtual disk is to shrink the VHD file which can easily be done witht he Resize-VHD cmdlet here’s the syntax

Resize-VHD C:ClusterStorageVolume1VHDHeloCheck-ShrinkShrink-Disk.vhdx -ToMinimumSize

While the command does not produce any output you can easily use the Get-VHD to get the disk new size

PS C:Windowssystem32> Get-VHD C:ClusterStorageVolume1VHDHeloCheck-ShrinkShrink-Disk.vhdx


ComputerName            : SRV-HYPERV02
Path                    : C:ClusterStorageVolume1VHDHeloCheck-ShrinkW2K12R2-Base-NoSysprep.vhdx
VhdFormat               : VHDX
VhdType                 : Dynamic
FileSize                : 16378757120
Size                    : 38134628864
MinimumSize             : 38134628864
LogicalSectorSize       : 512
PhysicalSectorSize      : 4096
BlockSize               : 33554432
ParentPath              :
DiskIdentifier          : 783e65f8-6e5f-4da7-9c0d-45526d0882a8
FragmentationPercentage : 8
Alignment               : 1
Attached                : False
DiskNumber              :
Key                     :
IsDeleted               : False
Number                  :

As you can see the VHD size is now circa 38GB down from the original 80.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s