Minimising VHD size for WS2003 Enterprise SP1 in Virtual Server

I was playing this weekend trying to get the VHD (Virtual Hard Disk) size down to the barest minimum. I thought I did OK - WS2003 SP1 with Support Tools, Admin Pack, Group Policy Management Console, Small Office 2003 installation running Word+Excel patched with Office SP1 and latest security fixes, sysinternals background info, Security Configuration Wizard and a few more utilities in just under 1MB as viewed from inside the VM.

On the host Virtual Server though, the image was some 2.5GB - a far cry from 1MB. Getting the disk size as small as possible is really important for me when building many machines for demonstration. Disk space is an expensive commodity on a slowish 80GB laptop hard disk, and also size is directly proportional to overall performance. With a bit of patience, it is possible to get the VHD size down on the host to 1.2GB in this configuration. Here's some of the stuff I did which I hope you find useful:

- Change the page file on the VM to start at 16MB
- Delete *contents of* c:\windows\system32\dllcache
- Delete *contents of* c:\windows\cursors
- Delete *contents of* c:\windows\inf
- Run the disk cleanup wizard
(Note- there are many others things you _could_ delete, but this gets the big ones)
- Defragment the disk ***at least 5 times****
- Precompact the hard disk (you need VPC2004 SP1 for this - I see another blog entry coming on)
- Compact the hard disk in VS admin
   - Select "Virtual Disks/Inspect"
   - Find your .VHD file
   - Select "compact"
   - Wait 20 mins or more 🙂

For some more minimal shaving of space.... After doing the above, the VHD file was down to 1.3GB - some 340MB+ bigger than the data actually being used by the Virtual machine. I tried converting the VHD from an expanding disk to a fixed disk, and then back to an expanding disk again. This gave no improvement in disk size.

However, once you have the fixed disk, start the virtual machine up and precompact once more. (I also did a defrag again, just for good measure). Shut the machine down and convert it back to expanding disk again before finally compacting it. Net result - no change. However, sticking with the full-size disk, pre-compacting it, converting it back to expanding disk and compacting it again did shave a further 80MB off. Hence, hence net result 1.22MB which I thought wasn't too bad.

So, can you do better or have any additional tricks?


Comments (8)

  1. Wayne Taylor says:

    Why not use an external (3.5inch) USB 2.0 drive? Or is this method to slow? You could have all your images on one drive and not have to think about drive space, the only pain is having to get the drive out every time you wanted to you VS…..

  2. Wayne – yes, USB drive is an option, but you’re right, it’s a pain compared to having everything held straight on the laptop directly. Smaller VHDs also mean they’re a lot quicker moving around to and from external disks and/or network, you can have more of them for the multitude of environments I build, so hence the reason for trying to make them as small as possible.

  3. Robert Aitchison says:

    Good stuff,

    I thought I was doing good deleting the temp files, contents of the media folder, extra wallpaper, etc.

    One think I like to do if I’m making a master image is to attach the VHD file as another disk on a separate Windows 2003 virtual machine, then delete the page file, run defrag & the precompact utility while that system is offline and these tools can work better.

    The Windows Server 2003 SP1 master image I just did last week was about 1.5Gb and zips down to about half that, using the tipe above I can probably shave a bunch off.

  4. Just wanted to add….

    ____BEFORE___ anyone deletes the windowsinf directory for good, keep a backup of it somewhere (or don’t delete it at all). Reason is simple – although the VM runs fine as it is, "sysprep"ing it or "dcpromo"ing it will fail. Fortunately, although I didn’t mention it in the original blog entry, it’s probably best _not_ to remove the contents from this directory unless you keep a backup somewhere else. On the other hand though, after reinstating it, re-precompacting and compacting the hard disk, the resultant VHD was still only 10MB larger

  5. Andrew Dugdell says:

    Ben Armstrong has a good section on his blog about this:

  6. dan e says:

    i think removing DLLCACHE may also cause problems with Windows Security Protection but this is ok in test environments as your not registering any DLLs i guess.

    I tried removing it to reduce my RIS build size.

  7. Jon Robertson says:

    During the early days of Virtual PC (when Connectrix still had it), I read about using Norton Ghost for compacting my VHD. This works great and is still the method I use today.

    I have a MS-DOS VHD with Norton Ghost installed.

    I have a BLANKHD.VHD, which is a dynamic sizing 16GB HD. (Choose your own size.) Before I start a compact, I copy BLANKHD.VHD to NEWHD.VHD. This saves the step of running the Virtual Disk Wizard. The copy is very fast since the VHD is so small. But you could run the Wizard each time instead.

    The first time I compacted, I setup a Norton VPC. The VPC uses the MS-DOS/Norton VHD as Disk 1, my LARGE VHD that I want to compact as Disk 2, and NEWHD.VHD as Disk 3.

    I boot into the VPC, start Norton, let it sign the disks, then ghost from disk two to disk three.

    Once done, I rename the original large VHD, rename NEWVHD.VHD, and restart the VPC that uses the compacted VHD. Once I see everything starts, I delete the original large VHD.

    Now, once the Norton VPC is configured, all I have to do is copy BLANKHD.VHD to NEWHD.VHD and change the configuration of Disk 2 to the VHD that I want to compact.

    This results in extremely well compacted and defragmented VHDs. I can’t remember the last time I ghosted a physical HD. But I ghost VHDs almost weekly.

  8. Todd Bleeker says:

    Consider saving aside the c:MSOCache and c:WindowsInstaller directories and then deleting them out of your VM. This saves a ton of space on the VM disk.

    <Todd />

Skip to main content