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
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
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
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.
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 – 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.