The information in this post is based on VMware Player 7 running on a 64 bit Windows 7 host with a 32 bit Windows 7 guest. It may or may not be valid for other versions and platforms.
I usually don’t bother much with managing the size of my virtual machine disks as I store them in a location with plenty of space, but I recently had a Windows 7 virtual machine go on a rampage with its disk and grow the virtual disk file to almost 125 GB, even though it reported the actual used size of the disk to only be about 20 GB. I think the problem was caused by some issues with Windows Update that would sometimes churn on the hard disk for several hours, but I’m not sure of that.
Anyway, a virtual disk this size was getting to be a bit of a pain to backup daily, so I decided to attempt to reclaim the free space and shrink the disk file. To my surprise, the shrink option in VMware Player finished very quickly, stating that the disk had been successfully shrunk – but on inspection, it actually still took up exactly the same amount of space.
After some research I discovered that you need to run a VMware tool from within the virtual machine to mark all the free space as something that can be reclaimed. This step was complicated by the fact that most of the documentation I found mentioned an option in the VMware Tools GUI inside the host, but that option does not exist in current versions of VMware Tools. Instead, you need to rely on a command prompt utility.
This will temporarily grow the size of the virtual disk to the maximum allowed size, so make sure to store it on a hard disk with sufficient space for this. Also make sure to have enough time to complete it, as you should not interrupt it during shrinking or you risk permanent damage to your virtual machine (making a backup before doing this is highly recommended regardless).
The exact procedure turned out to be:
1. Defragment the disk from within the guest operating system.
2. Still inside the guest, start a command prompt with elevated privileges (Run as Administrator on Windows) and execute the following commands (obviously replacing “c:\” with the drive you actually want to shrink):
cd "C:\Program Files\VMware\VMware Tools" vmwaretoolboxcmd disk shrink c:\
This will run for quite a while, depending on the size of your virtual disk and the speed of the physical hard drive it resides on. A rough estimate of needed time is probably something along the lines of the time it would take to write the maximum size of the virtual disk twice – so, for example, if your virtual machine is configured with a maximum hard disk of 128 GB, expect the running time of this to be roughly the time it would take to write 256 GB to the hard disk in question.
3. Shut down your virtual machine and verify that the vmdk file is now closer to what you’d expect. If this is not the case, you may need to use the defragment and shrink options from the VMware Player GUI as well, but when I did it it automatically shrunk the disk file after the above command finished.
As a final note: if your guest operating system is Linux (say, Ubuntu) instead of Windows, the procedure is pretty much the same. The command is just called vmware-toolbox-cmd instead and is located in the /usr/sbin directory by default. So, to shrink the root file system you might call something like this:
vmware-toolbox-cmd disk shrink /
Other than that, it works the same 🙂