Consolidating VMWare hard disks
When you create a virtual hard drive for use with VMWare, it doesn’t actually pre-allocate all the space for it (unless you ask it to). For example, if you chose to dedicate 20GB to your virtual machine, it won’t immediately take up 20GB of your (real) hard drive space. What will happen is the file size of your virtual size grows as your virtual machine starts using hard disk space. In other words, your virtual hard disk will only take up as much space as how much data your virtual machine uses.
Unfortunately, sometimes you move a large file into your virtual machine and delete it some time later and VMWare doesn’t recognise that it can reclaim some of your real hard disk space back. For example, my Windows 7 virtual machine tells me I am only using 17GB of the virtual hard disk space while the file size of the actual image is 28GB.
This happened when I installed a large program on my virtual machine and decided to uninstall it later since the SSD on which my virtual machine was hosted on was starting to run low on storage.
There are 3 steps to fix this issue:
- Defragment your virtual hard drive in the guest OS (optional)
- Run a program to zero out free space in the guest OS
- Shut the virtual machine down and tell VMWare to reclaim space.
The first step is recommended if your guest OS or filesystem supports it since it can consolidate the filesystem itself leaving large amounts of contiguous space for VMWare to clean up.
The second step zeros out the free space on the hard disk. When an operating system deletes a file, it often only marks the blocks used by the file as free and doesn’t actually write zeros to it for performance reasons. VMWare, because it is unaware of how the guest filesystem works, can’t tell the difference between useful data and discarded data. When we zero out free space, we’re essentially telling VMWare that ‘this space is blank, you can have it back’.
On Windows 7, I used a program called sdelete. I ran the program with the following parameters:
sdelete -z C:
I haven’t tested personally, but there should also be similar programs on Linux and other operating systems.
Lastly, we need to power the virtual machine down to let VMWare clean up and consolidate the virtual hard disk so we can have some space back. Once you power down and enter the virtual hard disk section of the virtual machine properties, VMWare should offer the option to clean up or reclaim disk space.
After running this procedure, I managed to reclaim just over 10GB of free space back from my virtual machine.