Compacting Microsoft Virtual PC VHD Images 101Print This Post
I’ve been using Microsoft’s Virtual PC for the past 7 years, ever since I spoke with a kernel developer at Microsoft and found that they solely use Virtual PC for debug and development. Since then I have used it to build images and teach in the classroom as a learning device. It allows me to teach with what I call a surgical approach, because every lab is perfectly clean every time for every student. However it is all about the footprint when it comes to storage of the Virtual Hard Drives, sometime you need to keep several VHD images around. So over the past few years I have put a collection of tools together that lead to the best compaction method.
To compact a VHD it is a two part process; the first part is done in the guest operating system, with a general cleanup. The second part of the process is completed by booting into RAM a second guest OS that can manipulate the original guest’s virtual hard disk. At this point we can further delete some files and truly defragment the VHD.
So first we begin by booting the guest Operating System and cleaning it up of unnecessary files. You can start by opening the %systemroot% and deleting any service pack or system update uninstalls, they usually look like “$NtUninstallKB952069_WM9$”. By doing this you can shave almost 1GB of space or more, depending on how old the image is and how many updates have been done. Next drop the “Restore Points” if you are dealing with Windows XP, just go in to My Computer properties under the “System Restore” tab and check “Turn off System Restore on all drives”. By doing this you can cut the VHD down by up to 10%, again it all depends on how old the OS is and how many restore points it has retained. The next step is to right click on the C drive or Local Disk, go to properties and select “Disk Cleanup”. Do not compress old files, but delete temporary Internet Files and downloaded content junk… etc… Again this can sustainably free up space on the drive.
At this point we have cleaned up the guest operating system as much as possible. Now we need to boot into a live install of Window that is in RAM and run some tools. The best way and quickest way is to download a Bart’s PE toolkit and make a custom Bart’s PE ISO; you can find this toolkit at http://www.nu2.nu/pebuilder/.
You will need to add two programs to the Bart’s PE disk; the first is a standalone defrag tool the second is the precompaction.exe from the ISO in the Virtual PC program files. I will explain… The best tool to defrag with is a program by Dave Whitney, however it’s hard to find. My suggestion is to Google “Dave Whitney defrag”; Dave’s current web site is “The Flexomizer”. This defrag will work stand alone and give you just about 100% defragmentation on the disk. The second tool is an executable named “Precompact.exe” in an ISO named “Virtual Disk Precompactor.iso” located in “C:\Program Files\Microsoft Virtual PC\Virtual Machine Additions\”. Use daemon-tools and remove the executable to be included in the Bart’s PE build following the Bart’s PE directions to build the bootable Bart’s PE ISO.
Now boot the Virtual PC containing the VHD with the Bart’s PE ISO created with the tools above. Once booted up open a command prompt and switch to “C:” which is your original VHD hard drive. We need to remove the “pagefile.sys” and “hiberfil.sys”, both of these can shave gigabytes of data off the VHD. First “attrib –r –s –h pagefile.sys”, then delete it and then repeat this for the “hiberfil.sys” file. Now defrag the C drive with the new defrag tool you downloaded from Dave Whitney’s web site. Now that we have defragged the VHD it’s time for the pre-compaction, by running the precompact.exe tool. This tool will zero and bytes unused be the defrag process; you can it is basically like repairing and compacting a database.
Once the tool is done shutdown the Virtual PC and go into the disk wizard and click on edit an existing disk and compact the disk replacing the original disk. When it goes thru this process it will create a second file and replace the first file, so the file will be somewhat contiguous. However if you wish you can always run the “contig” program from the Microsoft System Internals web site; this can speed the VHD up.
It’s actually a pretty simple process:
1. Clean up the guest operating system, deleting any old junk.
2. Boot a Live version (RAM resident) of Windows.
3. Delete the page file and hibernation file.
4. Defragment the hard disk.
5. Precompact the hard disk.
6. Shut down and compact the hard disk.
7. Optionally run the contig program to defrag the file.