Compacting a Dynamic VHD image

13. July 2006 02:21 by Jaguilar in General  //  Tags:   //   Comments (0)

One of the nicest features of using Dynamic VHDs on Virtual Server 2005 is that they grow as needed, so system administrators can optimize the disk space usage between several VMs. Dynamic VHDs grow automatically, but they don’t shrink automatically. In order to shrink them, you need to run several tools both inside and outside of the virtual machine.

Recently, I installed Windows Vista Beta 2 on a virtual machine on Virtual Server. Right after the installation, this are the disk usage stats:


Inside the virtual machine, however, the total disk space usage was smaller:


In order to compact the VHD image to only use the ~9GB allocated, we need to follow this procedure:

The first step is to defragment the VHD you are going to compact. You do this from within the Virtual Machine. Once the defragmentation is completed, you need to run Virtual Server’s Precompactor. To do this, mount the  image on the virtual machine from the Virtual Server console:


The precompactor will then execute and leave the VHD ready for compaction:


If you are running the precompactor under Vista, remember to do a “Run as administrator”. Otherwise it won’t work.
This process may (will) take a long time, so be patient.

Once it completes, you need to shut down the virtual machine, and compact the VHD from the Virtual Server web console. This can be done from the option Virtual Disks->Inspect, then select the correct VHD on the Known virtual hard disks drop-down box, and click on the Inspect button. Then select the link to Compact Virtual Hard Disk:


You need to click on the Compact button on the next screen, and the process will then start. This will also take a while, but once it finished, you will reclaim all the hard drive space from your VHD file!