Partilhar via


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.

Comments

  • Anonymous
    May 17, 2005
    cool, thx for the link to the defragger

  • Anonymous
    May 18, 2005
    Q: Lach writes with this question: "I have 2 virtual machines installed, and had them set up as dynamic...

  • Anonymous
    May 19, 2005
    I stumbled upon Guoqiang Wu's post the other day on slimming down the virtual hard disk files for...

  • Anonymous
    July 07, 2005
    Tried QEMU and its QCOW format? First defrag (and zero-fill free space) as usual, then convert a flat image to QCOW (zlib-compressed).

    XP (no sp) in roughly half a gig image file. Still way too large I think, considering the functionality XP provides, but at least better than wasting a gigabyte of host disk space for ... often nothing.

  • Anonymous
    July 15, 2005
    Recently, I had to send out a demo VPC to a coworker.  No problem, I simply need to zip up the .VHD...

  • Anonymous
    July 22, 2005
    I brought my disk down to 1.8G from 5.8G, this was great. What is the smallest the VHD can be, this still seems a bit big.

  • Anonymous
    August 16, 2005
    Had a 50 Gig (physical size).vhd, (disk set to grow dynamically largest virtual size 130 gig) followed the steps - took nearly 4 1/2 hours, but now the vhd is 17 gig.

  • Anonymous
    February 24, 2006
    I had a 14 GB vhd I could not shrink using all the methods I could find on the net.  The XP defragger would not move some files towards the beginning and prevented the precompact.iso from working to shrink it much.  As a last resort I resized the partition to 4 GB and it moved all the data together but it still would not shrink the unallocated space.

    After trying everything for 2 days including the Whitney Defragger (I didn't find this blog until after resizing the partion) it still did not shrink.  But in reading how it works it occured to me that it was working but could not work on the unpartitioned space.  And even after partitioning the precompactor.iso didn't work.  You actually need to create some partition with a drive letter for it to work.  You can then follow the steps at the top of this blog and it shrank to 4 GB.  You can then delete the empty partition and the virtual disk will not expand beyond the partition you have defined yet will allow you to add on.  A disk imaging software would have been much faster but I didn't have that available.

  • Anonymous
    March 06, 2006
    The other choice is using GHOST to image your current VHD disk to a new VHD blank disk, the size is significent drop. You do not need do a dragment first

  • Anonymous
    September 08, 2006
    I finally gave up this week. My main VPCs were getting pretty slow and I had an real annnoying VPC issue...

  • Anonymous
    April 08, 2007
    A Little Virtual PC Maintenance

  • Anonymous
    March 18, 2008
    PingBack from http://desktopcomputerreviewsblog.info/virtual-pc-guys-weblog-tips-for-compacting-virtual-hard-disks/

  • Anonymous
    July 11, 2008
    Quienes trabajen construyendo aplicaciones de servidor, especialmente aquellas basadas en SharePoint