On this page

How To Shrink VMware Virtual Disk Files (.vmdk)

Version 1.0
Author: Falko Timme

This guide shows how you can shrink the virtual disk files (they have the extension .vmdk) of your VMware virtual machines so that if you zip them, they will use much less space. It is then easier to upload and share them with other people.

This document comes without warranty of any kind! I do not issue any guarantee that this will work for you!

1 Preliminary Note

I'm using vmware-vdiskmanager with the -k switch to shrink disk files. The -k switch is supported on Windows hosts only, therefore I'm using Windows XP as the host and run VMware Server on it with a Debian VM. I got good shrink results with this constellation.

In a second step I've tried to shrink disk files on a Linux host (Ubuntu). vmware-vdiskmanager doesn't support the -k switch on Linux, but I've tried the -d switch (defragment) instead and got good results as well, but I'm not guaranteeing that this will work for you as well.

2 Windows Host

Before we try to shrink the virtual disk files, we should try to remove any unneeded files from the virtual machine to free space. For example, on Debian-based VMs, you can run

apt-get clean

to clear out the local repository of retrieved package files.

Next, run

cat /dev/zero > zero.fill;sync;sleep 1;sync;rm -f zero.fill

to fill the unused space with zeros.

Then power down the VM and open the command window on the Windows host:

Navigate to the directory where the .vmdk files are located, e.g.:

cd C:\Virtual Machines\apache2_mpm_itk_debian_etch

Try to find out where the vmware-vdiskmanager.exe program is located on your Windows system (mine is C:\Programme\VMware\VMware Server\vmware-vdiskmanager.exe), and how your .vmdk file is named (e.g. Other Linux 2.6.x kernel.vmdk). You can then shrink the .vmdk file as follows:

That way I was able to shrink a .vmdk file from ~1.6GB to 1.3GB, and compressed (.zip) from ~430MB to 240MB.

3 Linux Host

Before we try to shrink the virtual disk files, we should try to remove any unneeded files from the virtual machine to free space. For example, on Debian-based VMs, you can run

apt-get clean

to clear out the local repository of retrieved package files.

Next, run

cat /dev/zero > zero.fill;sync;sleep 1;sync;rm -f zero.fill

to fill the unused space with zeros.

Then power down the VM and open a terminal on the Linux host.

Navigate to the directory where the .vmdk files are located, e.g.:

cd /var/lib/vmware/Virtual\ Machines/Ubuntu\ 8.04\ Desktop/

You can defragment a .vmdk file as follows:

vmware-vdiskmanager -d Ubuntu\ 8.04\ Desktop.vmdk

If you take a look at the .vmdk file after the defragmentation, you will notice that its size hasn't changed, but if you compress the VM (e.g. .zip or .tar.gz), the compressed file will be much smaller than before the defragmentation. That way I was able to save 250MB for one compressed VM (before the defragmentation, the compressed VM was ~1GB, after the defragmentation it used only about 750MB).

I think vdiskmanager can shrink the vmdk 'offline'. While VMWare tools require the guest os on.

The other thing to consider is that if you run a Linux VM with no X, pure text mode, you will not be able to shrink it even with vmware tools. So it is better to do it offline using the tool. That's the point why VMWare Inc makes another tool available.

On the Windows side the VMware Converter may also be an option. You can convert an existing VM(Virtual Appliance) from your local disc, by selecting it as your Source/Source Type and then in your destination settings adjust the New Disk Space — you'll see a difference between Maintain Size and Minimal Size. I was able to shrink from a 29G to a 7G VM, after I deleted a lot of unwanted files from the initial conversion. It has worked with W2k, XP and Ubuntu 6.06 VMs for me.

Just so people know (and to remind myself when I google back to this page!) vmware-vdiskmanager on Mac VMWare Fusion is in /Library/Application Support/VMWare Fusion. The Mac version accepts both -k and -d parameters.

In our setup we use Lab Manager; this is similar to VMWare player/workstation with heavy utilization of linked clone.

The image I try to shrink is a child/clone of a consolidated disk image. I have run fillzero at level 0, I have copied 4Gb of data and run fill zero on image level 1.The delta file of vmdk was 4Gb after copying/deleting the file, and then 20Gb after running fill-zero (because fill-zero creates a file of the full amound of free space 20Gb)

I copied the level 1 image vmdk file and when I run shrink (vmware-vdiskmanager.exe -k "filename"), I get error:Failed to open the disk "filename" : The specified virtual disk needs repair (0xe00003e86).

(log file showssame error with "Failed to check Header")

vmware-vdiskmanager.exe -R has no effect and returns same error in vdiskmanager.log