Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

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.

share|improve this answer
add comment

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.

share|improve this answer
    
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 '13 at 21:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.