I have spent some time researching for the best way to compact a Virtual Hard Drive. The key for me was finding a way to do an offline defrag on the VHD. I found a program called VHDMount that will allow you to mount your VHD drive on a Windows XP system. Unfortunately it does not come with VPC. Following are the steps to install VHDMount.
- Download the Virtual Server 2005 R2 SP1 installation. (free download)
- Launch the setup program and select to install Virtual Server 2005 R2 SP1.
- Accept the EULA, enter your details, and then select Custom for your setup type.
- Now you can unselect every feature except VHD Mount.
Compacting The Virtual Hard Drive
Following are my recommendations on how to compact a Virtual Hard Drive.
- Start your VHD and Remove all temporary files. I use CCleaner and ATF Cleaner to remove all unwanted files. When it's finished, shut down the guest OS.
- Mount the VHD using VHDMount.
Example: "C:\Program Files\Microsoft Virtual Server\vhdmount\vhdmount.exe" /p /f "C:\VHDs\OPC_MOSS_SB3.vhd"
- Do an offline defrag on the mounted VHD. Most the time I use the "Disk Defragmenter" that comes with Windows. If I have some rogue files that will not move nearer to the data pack then I use JkDefrag. Note, do not use JkDefrag in the guest OS because it will grow the VHD file size by about 4 gigs everytime you run it.
- Dismount the VHD.
Example: "C:\Program Files\Microsoft Virtual Server\vhdmount\vhdmount.exe" /u "C:\VHDs\OPC_MOSS_SB3.vhd"
- Do a Virtual Disk Precompaction. Start the VHD. Under the CD menu, select Capture ISO Image and browse to the Virtual PC additions directory, which is "C:\Program Files\Microsoft Virtual PC\Virtual Machine Additions". Mount Virtual Disk Precompactor.iso. The precompactor will start automatically. When it's finished, shut down the guest OS.
- Open the Virtual PC console and select the Virtual Disk Wizard. Select Edit existing disk, then select the ”Compact it” option. Next select "Saving the file as". I think the option "Replacing the original file" is a waste of time since all it does is make a copy of the compacted disk then deletes the old one. For me it is better to have the option to test the compacted VHD before deleting the old one. If you do not have room on your computer then You can use a shared drive on a second computer. Make sure the shared drive has Write access. This compaction will take about 2 - 3 hour.