VMWare Disk Shrinking

For Linux based Guest OSs:
>cat /dev/zero > zero.fill;sync;sleep 1;sync;rm -f zero.fill
Shutdown the guest OS
From the host OS run
>vmware-vdiskmanager -k myDisk.vmdk

For Windows based Guest OSs:
Before you can shrink a virtual disk, you must prepare each volume on the disk for shrinking. To prepare a volume, it must be located on a Windows host. First you must mount the volume. To mount the volume, use the VMware DiskMount Utility, available as a free download from the VMware Web site.

VMware DiskMount mounts individual volumes of a virtual disk. For optimal shrinking of a virtual disk, you should mount all the volumes and shrink them.

After you mount a virtual disk volume, use the virtual disk manager to prepare the disk for shrinking. To prepare the volume mounted at the M drive for shrinking, use the following:

vmware-vdiskmanager -p M:

Once the preparations are complete, unmount the volume. Repeat this process for each volume of the virtual disk. After you prepare all the volumes for shrinking, you can shrink the virtual disk.

Shrinking a Virtual Disk

To shrink a virtual disk, it must be located on a Windows host. Before you can shrink the virtual disk, make sure you prepare all the volumes of the virtual disk for shrinking. Then use the following:

vmware-vdiskmanager -k myDisk.vmdk

Remember, you cannot shrink a virtual disk if you preallocated all the disk space when you created the virtual disk. You cannot shrink a physical (raw) disk.

Shrink recipe:

  1. First backup your guest os disk image and other files (just in case something goes wrong) + make sure you have plenty of spare disk space on your harddrive.
  2. Start Windows guest OS in VMWare and login as administrator
  3. Clean up & defrag guest OS disk image from within guest OS
    * Delete all unused files, empty your trash can, delete your browser cache etc.
    * Defragment your guest OS (f.x. using “defrag c:” in the cmd prompt).
  4. Zero out unused disk space in guest OS disk image
    * Download Mark Russinovich’s disk erase tool SDelete v1.51+ and place it on the local guest drive (*1).
    * From a command prompt navigate to where you saved the above delete tool and write “sdelete -c c:”, which will clear all free disk space
  5. Close guest OS and VMWare
    * Shutdown your Windows guest OS.
    * Quit VMWare
  6. Shrink disk image using VMWare’s diskTool
    * Navigate to the VMWare tools directory.
    * Run VMWare command with the shrink option “-k” to compact your virtual drive (*.vmdk). E.g. type “vmware-vdiskmanager -k 0 full-path-to-your-virtual-desk-image.vmdk”. (*2)
  7. Finished!

*1) Nb. this erase tool is not documented to be compatible with Windows 2003/XP, but it works just fine on on my Win2003 R2 (as far as I can tell).