Reclaim free space of a Virtual-Machine in VMware

When working with an active virtual machine we often encounter a problem with the storage space of the volume, occupied by the host/storage.

Background:
When you create a virtual machine there are two best practices to create the virtual hard-drive: Thin Provisioning and thick-provisioning, as explained in details here.
Well, In thin provisioning, we expect the volume to contain only the size of data, and not as shown in the operating system itself.
But in many cases, because of constant changes in the usage of the operating system like: files being downloaded/copied/added the storage occupied in the host, will expand but will not be reclaimed automatically.

Scenario:

Step 1:
You create a virtual machine with a thin-provisioned virtual hard drive of 100GB.
volume size: 100 GB.
vHdd files (vmdk): ~500 KB.
OS HDD size: irrelevant.

Step 2:
Installation of the operating system (i.e. Ubuntu desktop 18.04) and updates, packages and tools,
volume size: 100 GB.
vHdd files (vmdk): ~10 GB.
OS HDD size: 10/100 GB taken

Step 3:
Copy/Download of massive amount of data to the virtual machine (i.e. 50GB)
volume size: 100 GB.
vHdd files (vmdk): ~60 GB.
OS HDD size: 60/100 GB taken

Step 4:
Delete all the data added in step 3 from the virtual machine
volume size: 100 GB.
vHdd files (vmdk): ~60 GB.
OS HDD size: 10/100 GB taken

Of course, real life scenarios are much more complicated,
but the result is always the same:
storage space claimed by the OS will take storage space of the host but when deleted from the Operating system will not shrink the virtual Hard Drive files in the host.

After struggling with such case recently (just different numbers), and trying many manipulations to reclaim the unused storage space by:

  • Defrag to the virtual machine storage space (not a standard operation in Linux)
  • V2V: create a new virtual machine based on the existing machine
  • Image/Restore with a 3rd party software (CloneZilla/Acronis)

I figured out nothing is actually efficient, the size of the vmdk’s stay large, significantly larger than the storage used by the operating system.

But then, I realized, the best way to handle such case must be with the agent of the host inside the Operating system, in VMware it is VmWareTools.
usually , the host suggests to install VmWareTools:

  • to allow better drivers support in the operating system
  • to allow smooth transition between the host/management environment and the virtual machine,
  • to allow perfect resolution of the guest operating system
  • etc……

But of-course, there is much more to VmWareTools than the user experience, this is were I found the command line utility in Linux’s VmWareTools installation.
The utility is vmware-toolbox-cmd executable.

Warning:
make sure you backup the virtual machine before performing this operation

Running the command:

sudo vmware-toolbox-cmd disk shrink /

Works from the operating system, on the vmdk files and in the operating system itself.
While running, the utility even asks you to ignore storage space errors while running.

Bottom line: after struggling 6 hours with different approaches to reclaim my storage space in the host, this tool did it in 4 minutes and the .vmdk files in my host shrunk back to the desired size (cleaning ~70% of the storage space).

Leave a Comment