Friday, 27 November 2015

Reclaim Space For Thin Provisioned VMDKs

Written by Suhas Savkoor



Most of the time, we come across the issue where the disk space reported from the Guest OS perspective does not match the Virtual machine's Used storage. This is seen in the case where we have a VM with thin provisioned disk.
When we delete data from the Guest OS level, the drive space within the guest reflects the appropriate storage space. However, since VMware does not do automatic space reclamation, this would not have been updated in vSphere. Which is why we need to manually perform the space reclamation task.

So how does this process work? 

Data is written in blocks on your hard disk. Now when data is deleted from the drives it is actually not removed from the blocks, it's just that it is removed from the file allocation table. Data is truly removed when the blocks are zeroed out, in this case it is not. 

Then the first step would be to carry out the zeroing out the blocks from the Guest OS. There are two paths here.

Path 1: Windows

We have to use Sdelete. The guest OS will be seeing the updated space, but since the blocks aren't zeroed out, VMware will not recognize this free space. So Sdelete will track down these unused blocks and write zeroes on them. Download Sdelete from here

Open Command prompt with elevated permissions and run the command:



The drive letter must be replaced with the drive that has to be written with zero. Run this multiple disks if required.

Path 2: Linux

Shutdown down all the services that make any read-write changes to the disk while carrying out the zeroing operation. If you have multiple disks, this has to be run on each one of them.



The mounted volume must be replace with the drive that needs to be zeroed out.


Once the operation is done from the Guest OS, it is then we have to run the space reclamation command from the ESXi end.

Before proceeding with any step you will have to Power OFF the virtual machine. If you perform the space reclamation when the VM is powered ON, then you will receive an error called disk is locked or failed to release lock.

Once the VM is powered OFF, take a SSH (Putty) Session to the host where this virtual machine resides.
Then run the following command:



The VM.vmdk has to be the path to where this vitual machine's vmdk resides. This has to be executed for the vmdk (descriptor file) and not the flat file (data file)

Once this is done, the updated space is shown in the vSphere Client.