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.

5 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.

Anonymous said...

Using Precompact.exe, I'm trying to compact a non-ntfs vhdx guest virtual machine hard drive file running on a hyper-v 2012r2 host.

Path on host is c:\hyper-v\dimension\data\dimension_data.vhdx

'dimension_data.vhdx' is a Linux-formatted WatchGuard drive used for logging that is over 5tb in size.

I looked over the switches and am perplexed on syntax needed to target drive since it isn't a 'disk' mapped by host OS.

PRECOMPACT -SetDisk:

Any thoughts on how to target this .vhdx file?

Thanks,
Ryan

Kjetil Klaussen said...

Hi, Ryan!

Thanks for your comment, but I have to admit that I haven't kept up with virtualization lately so vhdx is unfortunately not something I know much about. But PreCompact is suppose to run on the virtual machine, and although I'm not a Linux guy, I believe running the precompact.exe on Linux is not supported :)

As for a solution to your problem I'd suggest you take a look at the 'Edit virtual hard disk' wizard in the Hyper-V management console. A part from that I'm afraid I can't help you out here.

-kjetil