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:

Suggested articles

20 Comment(s)

Add comment

Comments

By: Mansh Singh

Thank you very much. This was very useful. I reduced the size of my Red Hat Enterprise Linux 4 VM image from 47 GB to 27 GB. Cheers. Manish

By: Terry Wang

Actually the vmware-vdiskmanager -k option does not work for Linux guest OS images. It's really weird at first. However, after seeing the workaround. I understand.

Thank you very much guys for the tips.

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

Works like a charm!!!

By:

VMware Tools installed in a Windows guest has a "shrink disk" option.  How does that compare with vdiskmanager?

By: Terry

@jhansonxi

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.

And thanks Falko for the great article!

By: haidy

WinMount supports mounting VMDK file as a virtual disk

1. Using right-click menu

On VMDK right-click drop down menu, click "WinMount3 -> Mount to new drive", WinMount will mount it to a new virtual disk.

. Mount to new drive : Mount the VMDK file to a new virtual drive.
. Mount to...: Assign a disk letter for the VMDK file. (Refer to "How to assign a disk?" on the bottom of this page.)

2. Using toolbar

1) Click the button "Mount File" on the toolbar of WinMount main interface;

Mount toolbar

2) Select a VMDK files, click "ok". WinMount will mount it to a new virtual disk.

3. Using drag

Drag a VMDK file to WinMount main interface, WinMount will mount it to a new virtual disk.

How to assign a disk?

Of course, you can assign a disk for the VMDK file, such as H: or K: or whatever you like.

1) Click the button "Mount to" on the toolbar of WinMount main interface( or right-click the VMDK file and select "Mount to...").

step1: mount VHD to an assigned disk

2) Select a VMDK file, select a drive, then WinMount would mount the VMDK file to the assigned disk.

Step2: Select a disk and input a path for the VHD file

You can also input a path for the VMDK file.
For example, select "H:\" and input "folder\folder1", then the VMDK file would be mount to "H:\folder\folder1".

By: Anonymous

From my experiance VMDK files might slow down your system preformance even if they are configured as preallocated there for there are two steps you should do in order to improve VMDK preformance,  first make sure the .vmdk file is a flat file and you must make sure the file is not fragmented there are linux utilities loke conit which make sure the file is not fragmented second once to a while recreate the file with the orginal size vmware-vdiskmanager -r sourceDisk.vmdk -t 2 targetDisk.vmdk , i did it and it improve my guest OS significant,  you are welcome to visit my linuxstorage blog  (this is not spam ;)  best wishes
 http://mylinuxstorage.com/2010/09/%d7%94%d7%9e%d7%a8%d7%aa-%d7%93%d7%99%d7%a1%d7%a7-%d7%95%d7%99%d7%a8%d7%98%d7%95%d7%90%d7%9c%d7%99-%d7%91%d7%a7%d7%95%d7%91%d7%a5-vmdk-%d7%9c%d7%a4%d7%95%d7%a8%d7%9e%d7%98-%d7%97%d7%93%d7%a9/

By:

http://kegel.com/linux/jeos-vmware-player/jeos-vmware-player-howto.html is my recipe for how to do it; this produced an 87 megabyte downloadable virtual machine.

By:

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.

By: Tomek

VMWare Coverter doesn't work for this. The key is to fill with zeros (or other char). You can also use Compact option from VMWare Workstation. But key is to fill with zeros.

Tomek

By: keyboard.operator

The zero fill technique helps, but I found that I had to the following:

  1. Write a file filled with non-zeros.
  2. Run sync
  3. Replace that file with all zeros.
  4. Run sync.
  5. Remove the zero fill file.
  6. Shutdown the OS and then run the shrink.
I tried this when I noticed that writing all zeros does not change the size of the underlying vmdk.  I assume there is an optimization somewhere that handles the zero case.

By: BugButcher

What is the syntax used to fill a file with non-zeros?

 Thanks!

By: Cybird

cat /dev/random > random.fill

Or make a loop for any specific char you may want.

By: John HP

If you want to squish the image size down in linux, you can convert the disk type to the same type after filling the free space with zeroes (as shown above). For example:

 vmware-vdiskmanager -r olddiskfile.vmdk -t 0 newdiskfile.vmdk

Seems to work better for me than the defrag util. Which doesn't seem to do much. 

By: Anonymous

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.

By: Julien

Hi,

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

Thanks

Julien

By: fred

Hi, it seem that latest vmware tools installed on guest are able to Shrink and wipe VMware Virtual Disk Files. On my linux guest I launch: [email protected]:~#apt-get clean [email protected]:~#vmware-toolbox-cmd disk shrink / That's all :-) [email protected]:~# vmware-toolbox-cmd -v 9.2.0.15626 (build-799703) [email protected]:~# vmware-toolbox-cmd help disk disk: perform disk shrink operations Usage: vmware-toolbox-cmd disk [args] Subcommands: list: list available locations shrink : wipes and shrinks a file system at the given location shrinkonly: shrinks all disks wipe : wipes a file system at the given location

By: Chris Harvey

Fred, lifesaver. This was by far the simplest way to shrink the guest disk images. Glag I stumbled on this, thank you.

By: rubo77

I found a version that works on http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1023856 in the "Attachments" section.

see: http://stackoverflow.com/a/28391167/1069083

By: Peter

Hi guys, I have done a lot of testing with Windows guest, also vmware-vdiskmanager didn't work for me (Workstation Pro 12). Here: https://wiitez.blogspot.sg/2016/12/vm-image-compression-and-optimization.html

By: Carmelo Ortiz

Hello

We are creating a VMDK format file with this command:

qemu-img convert -p -f qcow2 -O vmdk /image2/syst00.qcow2 cust2.vmdk

This created a huge file of 32GB. Is there any way to create break this into multiple files of less then 8GB so they fit in DL DVD's?