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:

"C:\Programme\VMware\VMware Server\vmware-vdiskmanager.exe" -k "Other Linux 2.6.x kernel.vmdk"

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).

 

Share this page:

20 Comment(s)