Thursday, August 4, 2011

Compact virtual space

Keeping a virtual machine tidy will make it run faster and the VHD file itself smaller. Here’s a little list of things you can do to tidy it up a bit.

The usual suspects


First things to do is what you’ll do on any machine – virtual or not – to keep things as optimal as possible; The usual maintenance jobs includes deleting temporary files, emptying the recycle bin, cleaning up the registry, etc. I’ve found Glary Utilities (links at the end of this blog post) to be a good tool for this. Just install the free version and run the ‘Scan for issues’ under ‘1-click maintenance’.

If this is a fresh VHD which haven’t been used much you’ll probably won’t gain much from the usual housekeeping, but defragmenting and checking the disk (chkdsk) and Windows system files (sfc /scannow) seldom hurts.

Shrink wrap


To shrink the size of the VHD file you can compact the disk, but before you do that you need to pre-compact it. Compacting the disk is an operation you’ll do on the VHD-file, while pre-compacting is something you’ll do on the running virtual machine to make the compacting operation as effective as possible.

To pre-compact the VHD you need to run the precompact.exe which you can find on an ISO in the Virtual PC install directory (on my box the precompact.iso is in the folder c:\program files (x86)\windows virtual pc\integration components, but the exact location will vary depending on your OS and Virtual PC version).

precompact.exe must be run inside the virtual machine. You can either boot into it or fire it up in Virtual PC. Once you’re running the virtual machine you need to open the command line and navigate to the folder where that precompact.exe resides. Make sure you’re running with administrator privileges and run the following command:

:\> precompact –Silent –SetDisks:C

The last parameter specifies which disks to pre-compact and if not specified it will pre-compact all disks. In my case that wouldn’t be very wise, since I also have access to the host partition from my virtual machines.

When pre-compacting is done (can take quite some time depending on the size of your disk(s)), shut down the virtual machine.

Now it’s time for the actual compact process, so from your host OS fire up the command line and start the disk part utility;

:\> diskpart

When running the diskpart, enter the following commands (assuming the VHD file is named ‘parent.vhd’ and is in a folder called ‘vhd’ on partition ‘d’);

DISKPART> select vdisk file=”d:\vhd\parent.vhd”
DISKPART> attach vdisk readonly
DISKPART> compact vdisk
DISKPART> detach vdisk
DISKPART> exit

And that’s about it. You should now have a small and fast virtual machine (everything’s relative, right?)

Resources


Glary Utilities – Optimization software for Windows. Comes in two flavors; a free version for the basic optimization that’ll be good enough for most users and a paid version with more advanced tools.

3 comments:

Emiel Nijhuis said...

Thank you for this great explanation.
Strange that this guide was so hard to find. Especially the DISKPART-part.

I used next query in Google to find this page:
"Windows virtual pc" compact vhd -2007 -"windows xp" -hyper

Mu VHD shrank for 30.4 Gb to 24.6 Gb.
Thank again,
Emiel

Anonymous said...

Does doing the precompact.exe command require a certain amount of free space to be available on the host?

Kjetil Klaussen said...

@Anonymous: I don't think the precompact requires any extra free space. All it does AFAIK is to clean up deleted data on the VHD.