Shrink VMware virtual disks

Virtual disks usually only expand, their sizes won’t get reduced even files are removed.
The problem originates from the fact how files are managed by a file system. When a file is removed from a disk, its contents remain, only spaces taken by the removed file are reclaimed by the file system.

I used Virtual PC, VirtualBox and QEMU, and is using VMware Fusion exclusively on Mac (sorry, Parallels).
I don’t know there is a single step process to shrink virtual disks, but here’s how I manage to get my virtual machines’ sizes in control by:
1. On VM, remove unnecessary files such as browser’s cache, temporary files, etc.
2. defragging disks for Windows VMs. Linux ext3 and ext4 file systems don’t need defrag.
The purpose of defragmentation is to improve file I/O performance, and have a better opportunity to shrink size more.
Enter the following from Console to launch Disk Defragmenter.
For XP Windows VM, such as XP or Windows 2003, type “dfrg.msc”:

XP or Windows 2003 Disk Defragmenter

For Windows 7 VM, including Windows Server 2008, type “dfrgui”:

Win7 or Windows Server 2008 Disk Defragmenter

3. On VM, zeros out previously used and reclaimed disk spaces.
For Windows, you can find Sysinternals SDelete. To zero out, say c: drive, you can launch it from Console as: “sdelete -c c:”.

SDelete

For Linux, navigate to any folder within the disk you want to zero out, and run the following command as a super or root user: “cat /dev/zero > zero.fill;sync;sleep 1;sync;rm -f zero.fill”.
It may take some time to finish. You will see a disk full message. It is OK, the generated file is removed right away.

Zeros

4. On host, shrink virtual disks. You can use UI to do it, as the following screen shots.

VM Settings

VM Disks

Or, it can be done by the command line tool vmware-vdiskmanager with -k option, which is located at:
/Library/Application Support/VMware Fusion. You only need specify the master virtual disk (Disk DescriptorFile).
My xubunty.vmdk is split into 2 GB files, and here’s how to shrink it:

vdiskmanager

Advertisements

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: