2

I have a Hyper-V server running on WS2012. I have a Windows 7 running as a dynamic disk. I delete files and more from within to attempt to save disk space and reduce the size. However, the size does not shrink even after I turn it off and run a shrink on the VHD.

Do I need to do something else that will allow me to shrink the VHD so that I can keep the VM from crashing due to a lack of disk space?

EDIT: I also mounted the disk and ran DEFRAG: E: /V /X to defrag the disk in hopes that I could compact the .VHD but the size still remains over 30GBs over the actual used space inside the drive.

Do I need to 0 out the empty space for a VHD before running the shrink option from Hyper-V?

Running out of disk space: enter image description here

VHD: enter image description here

Disk inside the VHD:

enter image description here

PS C:\Users\Administrator> get-vhd

cmdlet Get-VHD at command pipeline position 1
Supply values for the following parameters:
Path[0]: X:\NAME.VHD
Path[1]:


ComputerName            : NAME
Path                    : X:\NAME.VHD
VhdFormat               : VHD
VhdType                 : Dynamic
FileSize                : 86707423744
Size                    : 256060514304
MinimumSize             : 256059113472
LogicalSectorSize       : 512
PhysicalSectorSize      : 512
BlockSize               : 2097152
ParentPath              :
FragmentationPercentage : 16
Alignment               : 0
Attached                : False
DiskNumber              :
IsDeleted               : False
Number                  : 
  • possible duplicate of [How to zero fill a virtual disk's free space on windows for better compression?](http://serverfault.com/questions/165070/how-to-zero-fill-a-virtual-disks-free-space-on-windows-for-better-compression) – Zoredache Oct 02 '14 at 14:31
  • This has nothing really to do about compression though? It's more about how to reduce the size of the VHD ;) –  Oct 02 '14 at 16:50
  • What do you think a shrink is other than 'compressing' all the sectors on a disk that are empty? BTW did you actually read the question and answers, and not just the title? It basically describes the same thing you want, even if the OP choose an incorrect term for their subject. – Zoredache Oct 02 '14 at 16:55
  • Are you using the VHDX format on the virtual disk? There are ways to do it with VHD but it is a total kludge. – Rex Oct 02 '14 at 16:57
  • @Zoredache - actually, that is not what he is trying to do. He is trying to shrink the size of the actual vhd/vhdx file used by hyper-v. All the solutions in the post you linked to would require him to zip/gzip/compress the file using some other utility to produce an archive and not reduce the size of the actual VHD/VHDX file in use by Hyper-V – Rex Oct 02 '14 at 17:00
  • @Rex, What the heck are you talking about? Did you read the question and answers? It is about zeroing out the 'unused' filesystem blocks so the shrink process would recognize them as empty... – Zoredache Oct 02 '14 at 17:03
  • @Zoredache the post you link to is asking about zeroing out space to make it smaller when they backup the virtual machines. The accepted answer uses sdelete to zero out the space. From my experience, this has not helped with shrinking the vhd though Hyper-V (in fact, sdelete for me has always only made the dynamic vhd grow). The only other answer that would help refers to the precompact utility that could be used to generate a new vhd file without the empty space and thus be smaller. The OP of this question would then need to attach the new VHD and detach the old one. – Rex Oct 02 '14 at 17:06
  • @TravisJackson can you post the results of the `get-vhd` command against the vhd in question? Along with the current size of the drive? – Rex Oct 02 '14 at 17:16
  • Added images to the post. –  Oct 03 '14 at 03:10
  • @TravisJackson That's not what he asked for. Use the Get-Vhd cmdlet in PowerShell. – pauska Oct 03 '14 at 11:02
  • Ah, ok Updated! :) –  Oct 04 '14 at 19:52

1 Answers1

2

Native support for shrinking Hyper-V virtual disk files was introduced with Windows Server 2012 and the new VHDX file format. If you are using the legacy VHD format then you will need to convert the file to VHDX first.

Before you begin, if possible ensure you have a good backup. You shouldn't need it, but it's always sensible.

Both of these operations can be carried out using the Hyper-V manager UI. The virtual machine must be shutdown first; these operations can't be carried out online. Select the VHD and choose 'edit'. The resulting wizard has both a convert option and a shrink option. Run the wizard once to convert and then again a second time to shrink the new VHDX.

You can also perform the same using Powershell using cmdlets Convert-VHD and Resize-VHD.

You can find a good step by step on this page;

http://www.techrepublic.com/blog/data-center/working-with-and-resizing-windows-server-2012-vhds/

I should add that there is generally no downside to converting to VHDX; it is a more modern, scalable file format with a host of benefits (brief summary on Technet here: http://technet.microsoft.com/en-gb/library/hh831446.aspx). However, if you wish to convert back to a VHD after performing the shrink process then you can do so; it is a fully reversible operation.

I hope that helps.

Steve365
  • 1,253
  • 9
  • 16
  • Is copying the VHD to a NAS considered a backup option or do I need to do something more advanced to backup the VHD? -- I've tried to run the SHRINK option when doing a edit on the VHD but never seemed to shrink the drive. I guess maybe I will try VHDX –  Oct 07 '14 at 12:28
  • Yes, it doesn't have to be a complicated backup solution. Just as long as you feel comfortable that you will be able to revert back to a known good state if you run into trouble. In this case a straight copy of the VHD would probably suffice (taken whilst the VM is offline). – Steve365 Oct 07 '14 at 13:09
  • @Steve365 It isn't clear to me from the documentation what exactly Resize-VHD is doing. Can you elaborate. Does it handle resizing the filesystem and partitions contained within the VHDX? What does it do if the filesystem isn't NTFS? – Zoredache Oct 08 '14 at 17:50
  • @Zoredache Yes it handles resizing the partitions, only if they're NTFS. If not, it will only expand the disk and leaves non-NTFS partitions alone... So exactly what you would expect. – Chris S Oct 13 '14 at 14:16