VHD compaction is a process that reduces the size of a virtual hard disk file on the physical disk. Virtual Server 2005 R2 SP1 comes with a compaction tool that only achieves minor reductions in virtual hard disk file size, if used by itself. A three-step process that includes defragmentation, precompaction, and compaction will provide better results. The defragmentation and precompaction prepare the virtual hard disk file for the compaction process, resulting in greater reductions in virtual hard disk file size.
VHD compaction can only be performed on dynamically expanding disks. Fixed size virtual hard disks have to be converted to a dynamically expanding disk prior to being compacted. Special dynamically expanding virtual hard disks, like differencing or undo disks, cannot be directly compacted. Differencing disks and undo disk changes must be merged into their parent disk, and the parent disk can be compacted, if it is a dynamically expanding disk.
Because of processor and disk resource requirements, use a non-production server, when possible, to perform the virtual hard disk compaction process. In Virtual Server 2005 R2 SP1, you can perform the defragmentation step within the virtual machine or offline. However, unless you have a good reason, perform defragmentation, precompaction and compaction with the virtual hard disk file offline.
Step 1: Defragmenting the Virtual Hard Disk File
The first step in the process to reduce the size of a virtual hard disk file is defragmentation. As new information is written to disk, data can be saved in non-contiguous disk blocks. In time, as you delete data on the disk, empty blocks will be randomly filled with file fragments. Performance is adversely affected when disk fragmentation is excessive since it takes longer to retrieve data spread across a disk than if it were located in contiguous disk blocks. Defragmentation reduces or eliminates the number of fragmented files on a disk, resulting in larger areas of empty contiguous blocks.
In order to defragment a virtual hard disk offline, you can use the new VHDMount command-line tool to mount the virtual hard disk file. Once the virtual hard disk file is mounted, use the Windows Defrag utility (or your favorite defragmentation tool) to defragment the virtual hard disk file. The time required to defragment the virtual hard disk file will depend on several factors, including the degree of fragmentation, file size, and disk characteristics.
Step 2: Precompacting the Virtual Hard Disk File
The second step in the process is precompaction. Virtual Server 2005 R2 SP1 includes a Virtual Disk Precompactor tool that is designed to write zeros in empty disk blocks of a virtual hard disk file. This step is crucial to ensure that the compaction tool can make the virtual hard disk file as small as possible.
The Virtual Disk Precompactor tool is contained in the Precompact.iso disk image located in the %systemdrive%\Program Files\Microsoft Virtual Server\Virtual Machine Additions folder. Use your favorite virtual CD tool, to mount the precompact.iso image on your Virtual Server 2005 R2 SP1 host and retrieve the precompact.exe tool.
Virtual Disk Precompactor Command Line Options
-Help
-Version
-Silent
-SetDisks:<list>
Below is an example of using precompact.exe to precompact virtual hard disks mounted to drive letters F and G, in unattended mode:
If you want to precompact virtual hard disk files from within a virtual machine, once you capture the precompact.iso to the virtual machine CD or DVD drive, you can double-click on the drive to launch the Virtual Disk Precompactor. However, you can not specify which virtual hard disks to precompact. Instead, the precompaction tool will precompact all virtual hard disks attached to the virtual machine.
Step 3: Compacting the Virtual Hard Disk File
The third and final step in the process is virtual hard disk compaction. After running the Virtual Disk Precompactor tool, empty disk blocks in the virtual hard disk file contain zeros. The Virtual Server compaction process will find the disk blocks that contain zeros and remove them, reducing the virtual hard disk file size.
The Virtual Server compaction tool requires that you have enough disk space to concurrently store the original virtual hard disk file and an additional temporary file that contains the compacted virtual hard disk. The original virtual hard disk file will be deleted at the end of the compaction process and replaced with the compacted virtual hard disk file. If the disk runs out of space before completing the compaction process, an event will be recorded in the Virtual Server event log.
To use the Virtual Server compaction tool, follow these steps:
1. Open the Virtual Server 2005 R2 SP1 Administration Website.
2. Turn off the virtual machine associated with the dynamically expanding virtual hard disk that you want to compact.
3. In the navigation pane, under Virtual Disks, click Inspect.
4. In Known Virtual Hard Disks, select the virtual hard disk to compact. If the virtual hard disk file does not appear in the list, type the fully qualified path to the virtual hard disk in Fully Qualified Path To File.
5. Click Inspect.
6. Under Actions, click Compact Virtual Hard Disk.
7. In Compact Virtual Hard Disk, click Compact.
The VHD compaction process can also be scripted using the Virtual Server 2005 R2 COM API. This allows you to compact the virtual hard disk files outside of the Virtual Server Administration Website.
Share this article in del.icio.us | Digg | reddit