During a physical-to-virtual conversion, you often need to resize hard drives and virtual hard disks. Avoiding storage bottlenecks is paramount to the success of the migration, but the Hyper-V disk management toolkit does not yet contain a tool to resize virtual hard disks. Despite this missing link, you'll still be able to set the appropriate size of the virtual hard disk by using PowerShell commands, VHDResizer and other tools.
The PowerShell method
Ben Armstrong, Microsoft Hyper-V program manager, presented this method in a blog and it shows great promise for quickly shrinking a virtual hard drive and alleviating some administrative burdens. These are the necessary steps:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-Management-PowerShell
Mount the VHD file: Use the following command to mount the VHDX file, and replace the path below with the location of the VHDX file you would like to shrink.
Mount-VHD "C:\VMs\ResizeMe.vhdx" –passthru | get-disk | get-partition | get-volume
It is important to note this process only works for VHDX files, which are only compatible with Hyper-V in Windows Server 2012. The process to convert a VHD to a VHDX is simple, but requires VM downtime while the virtual hard disks are converted.
Resize the Partition: You must first resize the partition before you can shrink the VHDX file. You can only shrink the file down to the size of the partition. Leave yourself a reasonable buffer and remember tasks like defragmentation work best when at least 20% of the virtual hard disk space is unused. The command below resizes the partition. Be sure to include KB, MB, GB or TB as part of the size parameter.
Resize-Partition –driveletter D –size 40MB
Dismount the VHD: At this point, partition management is complete. You now need to resize the virtual hard disk (VHD) itself. The simple command below will shrink the VHDX to the new size you set for the partition.
Resize-VHD "C:\VMs\ResizeMe.vhdx" –ToMinimumSize
Use the following command to check the new size of the VHDX file.
Get-VHD "C:\VMs\ResizeMe.vhdx" | fl path, vhdformat, vhdtype, size
Resizing VHDs with VHDResizer
The VHDResizer free toolkit allows you to mount VHDs and shrink them to the size you choose. The Resize-VHD command I mentioned above only works for VHDX files. VHDResizer is an older tool, and does not support VHDX files. The toolkit requires extra time because it does a block-level copy of the existing virtual hard drive to a new VHD file.
You could use the PowerShell commands above to shrink the partition within the VHD before utilizing the VHDResizer.
First, shut down the VM. Then mount the VHD, substituting the path below for the location of your VHD.
Mount-VHD "C:\VMs\ResizeMe.vhd" –passthru | get-disk | get-partition | get-volume
Resize the partition using the following example command, leaving a reasonable buffer.
Resize-Partition –driveletter D –size 40MB
As you can see, attempting to use the Resize-VHD PowerShell command results in an error stating VHDX files are the only allowed format. At this point you need to open VHDResizer to shrink the VHD and complete the process. You can mount and resize the partition remotely, but you must use a local system path to run VHDResizer.
When you launch VHDResizer, it asks for the source VHD.
The next window asks for the location of the VHD you are about to create, the size you want and whether it should be dynamic or fixed. Once you set those parameters, click OK.
VHDResizer does not change the size of the existing VHD, but rather creates a second VHD at the size you chose.
When the process completes, open Hyper-V manager, remove the previous larger disk and add the newly resized VHD.
Alternative methods to resize VHDs
If you have a NetApp storage area network (SAN) and have VHD or VHDX files on these logical unit numbers you can use the Data ONTAP PowerShell Toolkit to resize virtual disks. This tool kit allows you to shrink your virtual hard disks and to zero out the blocks on the SAN, which enables space reclamation.
Depending on your network speed, a physical-to-virtual (P2V) migration can take anywhere from 30 minutes to several hours. Adding extra time to go through one of the disk-shrinking options above could take even longer and prolong application downtime. The new PowerShell commands for Windows Server 2012 show promise, but don't support VHDs and require the VM to be down during the shrinking process, adding additional downtime and disruption to the P2V process. Hopefully this administrative process will become part of automated P2V, storage migration or a wizard-based disk editing task within Hyper-V.
Rob McShinsky asks:
Which method do you use to resize VHDs?
1 ResponseJoin the Discussion