Creating LEAN_AND_MEAN Virtual Hard Disk (.vhd) Files

Not so recently I discovered a way that can significantly reduce VHD file sizes: I was able to reduce the sizes of all my VHDs down to 100MB or less bigger than what their guest operating systems report. Just today I compacted a Japanese Windows XP SP2 VHD  with a OS reported size of 1.70GB to 1.72GB, whereas the standard procedure can only get down to about 2.3GB. If you are obsessed, like me, in reducing the size of bloated VHDs, you'd know this result is just amazing.

 

The secret turns out to be very simple. As you may already know, the standard procedure to compact dynamic expanding VHDs is as the following:

 

1) Inside the VM, run disk defragmenter

2) Inside the VM, run Virtual Disk Precompactor or other disk utility program to "zero out" deleted data

3) Shutdown the VM, run "Virtual Disk Wizard" from VPC console to compact the VHD.

 

And the key lies in step 1. Instead of using the defragmenter comes with Windows, you should use the one created by Dave Whitney, which can be download for free here. The special thing about Dave's defragmenter is that it can defrag not only occupied spaces, but also free spaces. If you think about it, this totally makes sense why it helps compacting the VHDs. So far my experience is that no matter how the image is built and how bloated the virtual disk is, this procedure will give you the real size!

 

Of course, nothing comes without cost. From my experience, it takes 5 to 10 more minutes to finish than the Windows defragmenter. It could take much longer if the disk size is bigger. However, considering the huge saving on disk spaces(500MB per disk at the least) and the network bandwidth if you are distributing them to others, it is definitely worth the extra wait. What is more, I suspect the guest OS performance would be better. No hard evidence on that though.

 

One thing to clarify: I usually relocate page files to a secondary virtual disk therefore all disk sizes I mentioned here do not include the page files: because they usually have big reported sizes, but actually take little space on the disk, it may give you false sense of compactness. If you ever make the size of your VHD smaller than what guest OS reports, I am almost certain that you have page files included in the equation.

 

At last, this is totally my personal experience (and opinion). Your experience may vary. I would be very interested to know how it works on very big VHDs. So please do report back your result if you try this on VHDs bigger than 8G, 10G, or maybe even 16G.