Compacting Microsoft Virtual PC VHD Images 101
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.
October 14th, 2009 at 1:37 am
FTR, you don’t need to hassle with fetching and familiarizing yourself with BartPE for step 2 — simply make a copy of the original XP virtual system (including the drive), then configure it to mount the as a SECOND DRIVE the VHD from the base system you’re compacting. Then you can run utilities from within a non- “ram resident” copy of Windows on it. May require a bit more disk initially, but once you’ve performed the file deletions, defragged and precompacted, you can delete the second copy, thus freeing up the space which will be necessary for the actual compaction step.
Note also that when you capture the precompaction iso, you should ABORT the precompact, then manually launch the program with options, so that the OS drive isn’t compacted:
h:\precompact -SetDisks:d
if your cd is h: and the second drive is mounted as d
(“precompact /?” will provide commandline args)
This spares you the overhead of compacting ALL drives, which precompact would otherwise do by default.
Note that you should avoid running with undo for the compaction process – when you shut down, you still have to apply the changes.
BTW, I use a portable-ized Virtual PC config, which I’ve stripped out the codepage directories (the numbered dirs, except 1033 which is US English), the non-english documentation, edited the two ISO images to remove the 2MB spacer file (the Windows emulated CD driver doesn’t have a minimum size requirement), about 18.9MB of alternate language MSIs from the VMAdditions.ISO, as well as the 2.9MB OS/2 installer (I’m not about to use it…), which in sum total removes a bit over 25MB of cruft from the Virtual PC host environment – the zip file with the portable content is all of 11MB in size, which of course pales in comparison to the VHD files I carry with it, but still, it’s barely a third the size of the MS installer for Virtual PC. The portable-ized edition makes it easy to copy from USB to a machine in the field from when you need to access an alternate OS environment. On top of all that, I’ve added Dave Whitney’s defrag to the precompact ISO image, so it’s right there when I mount that ISO, and the autorun file can be eliminated so precompact isn’t launched automatically.
To save your readers the hassle of discovering it for themselves, if you attempt to defrag a difference-based VHD, it will essentially merge the parent image into the difference one. However, when you go to run the wizard to get to compacting it, it’ll end up prompting you to merge with the parent then (at it would if you attempted to run the wizard on an un-defrag’ed VHD). In my experience, you cannot reclaim the space lost to the pagefile and temporary content, etc in a difference VHD.
Oh, insert step 3.5 — EMPTY THE RECYCLE BIN, just in case pagefile.sys and hiberfil.sys are being retained there. Until emptied, they’re not gone from the file system.
November 25th, 2009 at 9:02 am
Is there a good reason not to run the defrag on the vhd from your OS (rather then the copy of the VM)? Using Windows 7 disk management interface (or using diskpart.exe), you can mount vhd files as a seperate volume.
November 25th, 2009 at 9:22 am
You are right in Windows 7 you can now mount the VHD and run utilities on it like defrag. Although I haven’t tried it yet, I’ve done this same procedure with VMware; they have a disk mount utility similar to the diskpart utility. If you have the VHD engaged in a operating system that it belongs to (i.e. you’ve booted the VMC file with the VHD attached and you are trying to defrag a live system. It will not work, you cannot defrag a host operating system. The defrag process needs full access to the entire disk, that means no files (like pagefile) can be opened and used. I will be loading Windows 7 and trying the new tools, I will probably follow up with a new post.
March 12th, 2010 at 9:19 pm
Awesome post. I don’t suppose you’d be against it if I added your blog to my link exchange directory?