Search This Blog

Wednesday, February 7, 2018

MAC OSX Zero Free Space for VMWare Deduplication

Periodically working with virtual machines (vms),  enough files are created and deleted that the thin provisioned virtual disk (vdisk) expands to its maximum capacity even though the operating system (OS) file system shows free space.   This is typical and normal behavior.  Unfortunately, this consumes space on the underlying storage that is no longer being active used with the VM OS. 

For years I have been using the sdelete.exe Microsoft SDelete tool that securely erases deleted files using the option to just write zeros to all remaining free space.  In Linux, I use the dd tool to read from /dev/zero and write to a temporary file filling all free space with zeros then deleting the temporary file. 

These procedures simply write zeros to free space.  VMware and network storage systems will "see" these zeros and free allocated storage space to the virtual disk.  In essence, this shrinks the virtual file size on storage freeing unused space on the storage system.  Some advanced storage systems will automatically detect these zeros and automatically free the space.  Others you need to run a command line on ESXi to free the zeroed space. 

The MAC OS has a couple ways to write zeros on free space.  The graphical disk utility in Utilities menu and a command line too.  I haven't had much luck with the GUI tool, so this procedure uses the command line tool. 

First, open a terminal as an admin account.

type diskutil list to locate the drive you want to write zeros to.  In my case it is partition 2 on /dev/disk0.  Look for Apple_HFS macintosh HD.  the identifier for mine is disk0s2.

Now run the too and write some zeros

diskutil secureErase freespace 0 disk02

where...

secureErase = Secure Erace.  There are 5 levels, we want level 0 for single-pass zeros
freespace = only write zeros to free, unused space.  Does not affect files or OS
0 = secureErase level 0 single-pass zeros
disk0s2 = my partition that has my data

I have a SSD drive so it is pretty quick.    3 or 4 minutes.  The progress bar will indicate how long the zero write will take.  The estimate updates every few seconds.

When it is all done, use the vmware command line tool vmkfstool to free space that contains all zeros.  First SSH to the ESXi hot and login as root (there are other methods but I use SSH to esxi to run the command line tools).

For example on my VM, I used the command
vmkfstools -K /vmfs/volumes/datastore1/Mac_OS_Master/Mac_OS_Master.vmdk

The starting size of the VM was 105GB with used and non-shared at 95GB.  After zero write procedure, the provisioned size was still 105GB the used and not-shared were now 57GB.

If the VM has frequent file writes and deletes, the used space will slowly increase again and eventually warrant another shrinking.  If it is very frequent and expands the virtual disk to capacity in a short time, it may not be worth the effort to shrink the virtual disk.  It is up to you.



No comments: