When working in the VPC environment, you may notice notice growth in the the virtual hard disk (.VHD) files. As files are saved within the VPC environment, dynamic VHDs will expand to meet the need. However, the VHDs do not return the space to the host hard drive without manual optimization.
No warranty, expressed or implied. This is just a record of what I did. Please be sure to read the entire article before doing anything; I had some issues with VPC SP1, for which I provide a workaround.
Notes:
- The compacting and zeroing operations require VPC Service Pack 1.
- Make a backup of the VPC image before performing optimization.
- Do not perform optimization without shutting down the VPC image to be optimized (i.e., do not perform optimization on a "saved state" VHD).
- These steps assume an administrator account.
- You will need lots of disk space on the host to perform these operations; the VHD file will grow during the process prior to optimization and the optimization process iteself takes space roughly equivalent to the size of the VHD being processed.
Steps:
Defragment the host drive(s). Prior to defragmentation:
- Empty the recycle bin on all HDDs on the host.
- Disable hibernation while defragmenting the drive containing the 'hiberfil.sys' file. Doing this should remove the file, but if it doesn't, remove the file manually once hibernation is disabled and reboot.
- Unmount/move/remove any unused VPC/VCD/ISO images while defragmenting. Restore them after defragmenting is complete.
Note: if your images are stored on a data drive (say D:\), the drive to which the TEMP environment variable points (typically C:\) will also need to be cleaned and defragmented. The compacting process uses significant temporary space.
Now we can defragment the drive. In Windows Explorer:
- Right-click the hard drive to defragment.
- Click the "Tools" tab.
- Click "Defragment Now".
- Have a cup of coffee, as this will take some time, depending on the size of your hard drive.
Repeat the process for the system and data HDDs.
Remove unnecessary items from the VPC image.
Notes:
- Your individual needs will be the ultimate determining factor as to applications, backups, etc.
- The steps below assume a Windows Server 2003 SP1 virtual image.
- Start your VPC image; do NOT perform these steps on the host.
- Use 'Shift-Delete' when deleting files so the files are not moved to the recycle bin.
Remove the paging file on the virtual computer:
- Log in as an administrator account.
- Click Start, Settings, Control Panel, System, Advanced, Performance, Settings, Advanced.
- Click the Virtual memory section and click Change.
- In the Virtual Memory dialog box, under "No Paging file", click Set, Yes and OK.
- Click OK to all changes and close System settings.
- Reboot the VPC.
Clear the DLL cache on the virtual computer:
- Log in as an administrator account.
- Open a command prompt; click Start, Run, type "cmd" and click OK.
- At the command prompt type "sfc /cachesize=1" and press enter.
- Close the command prompt window.
- On the Start Menu, click Shut Down.
- In the Shut Down Windows dialog box, click "Operating System: Reconfiguration (Planned)" with "DLL Cache Cleared" as the reason and click OK.
Purge the DLL Cache on the virtual computer:
- Restart the VPC.
- Log in as an administrator account.
- Open a command prompt; click Start, Run, type 'cmd' and click OK.
- At the command prompt type "sfc /purgecache" and press enter.
- Close the command prompt window.
Clear the C:\Windows\Driver Cache\i386 folder on the virtual computer:
- In Windows Explorer, navigate to C:\Windows\Driver Cache\i386.
- Select the "driver.cab" file and click Shift-Delete.
Clear the C:\Program Files\WindowsUpdate folder and Update uninstall programs on the virtual computer:
- In Windows Explorer, navigate to C:\Program Files\WindowsUpdate.
- Select all files in this folder and delete.
- Navigate to C:\windows and highlight all the $NTUninstall$ folders left behind by Windows Update.
- Click Shift-Delete to delete the files.
Note: Do not remove "$hf_mig$". See "Description of the contents of Windows XP Service Pack 2 and Windows Server 2003 software update packages" for details.
Clear the C:\Windows\System32\WBEM\Autorecover folder on the virtual computer:
- In Windows Explorer, nagivate to C:\Windows\System32\WBEM\Autorecover.
- Select all files in this folder and click shift-Delete.
Clear the C:\Windows\Web\Wallpaper folder on the virtual computer:
- In Windows Explorer, navigate to C:\Windows\Web\Wallpaper.
- Select all files in this folder and click shift-Delete.
Stop any application services on the virtual computer:
- Check for BizTalk, MSSQL, MSSQL AS, MSSQL RS, etc.
- Stop these services.
Delete unnecessary log files on the virtual computer:
- In Windows Explorer, right-click the C:\ drive icon and click Search.
- In the Search Results window, select the 'All or part of the file name' text box and enter '*.log'.
- Click the Search button.
- In the results window, select all the files EXCEPT for the following:
- C:\Windows\debug\PASSWD.log
- C:\Windows\debug\usermode\chkacc.log
- C:\Windows\SoftwareDistribution\edb.log
- C:\Windows\SoftwareDistribution\ReportingEvents.log
- C:\Windows\WindowsUpdate.log
- C:\Windows\system32\msdtc\msdtc.log
- C:\Windows\system32\msdtc\trace\dtctrace.log
- C:\Windows\system32\wbem\logs\wmiprov.log
- Delete all files except those above by pressing Shift-Delete. You may find other files that cannot be removed; exclude them from the deletion process as you encounter them.
Set and clear Event Logs on the virtual computer:
- Open Event Viewer
- For each log, perform the following steps:
- Click the Action menu and Properties.
- Set the Maximum log size to 512.
- Select "Overwrite Events as Needed" (creating a circular log).
- Click Clear Log (without saving)
Clear the temp directory on the virtual computer:
- In Windows Explorer, right-click Start and click 'Explore all users'.
- Navigate to the current logged-in user, click Local Settings, and Temp.
- Select all files and delete. If you encounter files that are being used by the system, exclude them from deleting.
Some other optimization opportunities on the virtual computer:
- Disable the clock.
- Use ClearType instead of the default smooting screen fonts (Display, Effects). Skip this if you're using the image to present on a projector as it may make it difficult for the audience to read.
Defragment the virtual disk on the virtual computer:
- Empty the Recycle bin.
- Open the My Computer icon.
- On the File Menu, right click C:\ drive and click properties.
- On the Tools tab, click Defragment now.
At this point, if you have not installed VPC SP1 on the host, you will need to do so. Shut down the VPC (do not save state) and apply the Service Pack to the host. Restart the VPC once SP1 is applied.
Mount the Virtual Disk Precompactor on the host:
- Using an ISO-as-CD utility (I use Daemon Tools), mount the “Virtual Disk Precompactor.iso“ file from “C:\Program Files\Microsoft Virtual PC\Virtual Machine Additions“.
- If you have AutoPlay, the application will prompt you to prepare the disk for compaction. Say “No“ (you won't perform this action on the host drive).
Zero the free space on the disk on the virtual computer:
- Log on to the virtual machine as an administrator.
- Confirm application services (MSSQL, BizTalk, etc.) are stopped.
- In the CD menu of VPC, use the physical drive you just mounted. The application will prompt you to perform the precompact; answer "yes".
- Once precompacted, the application will prompt you to shut down. Before doing so, clear the event logs and temp directory.
Compress the virtual hard disk:
- In the Virtual PC Console on the host, click File, Virtual Disk Wizard.
- On the Welcome page, click Next.
- On the disk options page, select “Edit an existing virtual disk” and click Next.
- On the Virtual disk to Edit page, browse to the location and file name of the VHD file and click Next.
- On the Virtual Disk Information and Options page, ensure “Compact it” is selected and click Next.
- On the Virtual Hard Disk Compaction page, select “Replacing the original file“, unless you want to save the new file under a different name (prudent, but requires more disk space).
The process will take several minutes; click Finish when complete.
Once the disk is compacted, restart the VPC:
- Add a Windows-managed paging file.
- Restart/Autostart any services you stopped during the process.
- Test, before deleting the original image backup.
This process reduced the size of a 512MB RAM VHD containing Windows 2003 Server with SP1 from 3.038GB to 1.756GB (with the new paging file).
This process reduced the size of a 512MB RAM VHD containing Windows 2003 Server with SP1, MSSQL, VS.NET, BizTalk and a variety of other developmen tools from from 7.833GB to 5.117GB (with the new paging file).