Compact Your VHD Files

Heath Stewart

Many times it’s necessary to debug solutions on other machines so that you don’t corrupt your developer machine, because of different platform requirements, or to attach a debugger without changing the state of the machine. Testing Windows Installer patches is no different. We test on daily target builds using daily upgrade builds. Installing daily builds isn’t always such a good idea, and because Windows Installer now caches MSP re-installing a patch with slightly different contents but the same PatchCode proves difficult.

Of course we have labs full of machines, but because these run through a complete installation of both the OS and other components such as service packs and lab tools, I prefer Microsoft Virtual PC and Virtual Server. Both at home and at work I’ve got a good base build with very little installed, including the latest service packs, hotfixes, and the Visual Studio .NET 2003 native remote debugger. Once that’s all set up, I enable undo disks. Now whenever I need to start over I simply turn off the virtual machine and discard the changes. When I restart I have a fresh new machine.

In order to decrease disk space requirements, you have to compact your virtual hard disks, or VHD files. If you run the Virtual Disk Wizard from the File menu and choose to edit and compact an existing VHD, the wizard states that, “Before you compact this virtual hard disk, you must zero out free space on it with a third-party tool that you install on the guest operating system.” The CHM file for Virtual PC 2005 Service Pack 1 has a similar message. Finding such a tool wasn’t so easy. SDelete from Sysinternals starts by zeroing free space, but then fills it with 0xff and then random bytes. It also grows the size of a dynamic disk because it creates a file to fill all available space which most likely hasn’t been allocated yet in the VHD.

Before writing such a utility I, of course, tried Microsoft Support. I was able to find one KB article — KB 888760 — that made mention of such a tool, which was not a third-party tool at all.

  1. Download and install Virtual PC 2004 Service Pack 1.
  2. Defragment your guest operating system.
  3. From the CD menu, select Capture ISO Image and browse to the Virtual PC additions directory, which is %ProgramFiles%Microsoft Virtual PCVirtual Machine Additions by default.
  4. Mount Virtual Disk Precompactor.iso. By default with auto-run enabled, the precompactor will start automatically.
  5. When it has finished, unmount the ISO from the CD menu.
  6. Run the Virtual Disk Wizard from the File menu and follow the instructions to compact your VHD file.

This can significantly reduce the size of your base VHD file. When it’s finished, click Settings to open the settings for your virtual machine, select Undo Disks, and check Enable Undo Disks.


Discussion is closed.

Feedback usabilla icon