3

If I create an expandable Virtual Hard Drive (VHD) with a maximum size of 50Gb, fill it with 48Gb of files, and then reduce that amount to 25Gb, what is the most efficient and sure-fire way of reducing the size of the VHD so that it is only 25Gb or thereabouts.

I'd like to do this without having to copy the contents of the VHD into a fresh one, or having to have twice the amount of space available for any reason.

I am aware that diskpart's VHD COMPACT doesn't recover all the space.

2 Answers 2

3

Not sure if you are using Windows 7 Virtual PC or Windows Server 2008 R2 Hyper-V or Windows Server 2012 Hyper-V, but I will address each scenario to be safe.

  1. If you are using Windows 7, then you will want to mount the precompactor.iso (found in Program Files (x86)\Windows Virtual PC\Integration Components) and run precompact.exe from the mounted ISO. After this completes, you can shutdown the Virtual Machine and edit the VHD to compact the size. This should reduce the size close to the 25 GB of actual used space.

  2. If you are running Windows Server 2008 or 2008 R2 Hyper-V, I would recommend that you grab the precompactor.iso file from your Windows 7 computer and follow the same directions I provided in step 1 for Windows 7.

  3. If you are running Windows Server 2012 Hyper-V, then convert the VHD to VHDX and Windows Server 2012 Hyper-V will automatically shrink the size of the file based on actual usage.

1
  • 1
    Do you have any references to documentation that supports your claim that Hyper-V Server 2012 automatically recovers space in VHDX? I actually see complaints to the contrary when I search MS forums (e.g., Expandable VHDX Disk Remains Expanded...).
    – merv
    Aug 25, 2014 at 21:05
-1

If you're looking for absolute space efficiency, build a new VHD and copy the files from the old one into the new one, preferably with file compression enabled in the file system within the target VHD. I know that you excluded this as a potential answer. But I mention it because it is the most space efficient option.

You can approximate this level of efficiency by defragmenting the file system within the VHD before compacting. These two operations, taken together, will end up taking a whole lot longer than building a new VHD, as there will be many more I/O operations generated and probably more data copies in total.

1
  • defrag does not allow a vhd to shrink smaller. if anything it may make it larger. however, a defrag plus a utility like the precompactor May yield better results than just the precompactor, but not very much.
    – longneck
    Feb 11, 2013 at 21:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .