Vmware Shrink y’r Disk

Vickneswaran Ezhilraj
2 min readJul 12, 2021

VMware Workstation, Fusion, Esxi works hard to minimize the size of virtual hard disks for optimizing the storage in host-machine . On Windows(Fusion,Workstation) virtual machines, VMware has a “clean up” function, which detects newly unused space and makes the size of the virtual hard disk smaller accordingly.

VMware can be set to automatically optimize and shrink virtual hard disks as you add and, more importantly, remove files — but this automatic “clean up” setting is disabled by default. Either way, cleaning up virtual machines works like a charm…when you have Windows as a guest operating system with an NTFS disk.

As a developer, I have several VMs with various Linux-based guest OSes — and, for some reason, VMware doesn’t know how to optimize these.

Recently, I had a task on “Upgrading a Linux OS in one of our base_images(thick provisied) to a newer version”.

So, the next thing I decided is to bring up the base_image in a VMware Esxi server and do the normal “Linux Command Magic” to get the OS updated to the newer version.

Voila, I thought that is all to be done.

But,

The bigger soup was waiting !, I powered the VM down and exported the VM only to see the VMDK size has almost doubled from 4GB previously to 8GB now.

So what now, I googl’d and tried out possible ovftool, vmware-tool commands to get the disk to shrink or reduce or “I dont know what” to make it ~4GB. Because I am affirmative on the fact the OS upgrade should not eat up 4GB more.

Exactly my status “Stuck in the wall”

Photo by Fernando Jorge on Unsplash

Finally after sometime, I came to know, that OS’es do not clear up all the deleted files spaces, Vmware would also wont do that unless the guest os cleans up the free spaces.

Eureka!,

“The Magic Pill of Weight[disk] Reduction” is here. In order for VMware to detect the newly free space, we need to free it up ourselves, “Yes ourselves, and we all are looking forward to AI and machine learning, not kidding”. We’re going to have Linux overwrite the free space with a file full of zeros — the size of this file will be the size of however much space we’re freeing up (5 GB, in the example above) — and then delete it. These commands will create the file, wait a moment, and then delete the file:

cat /dev/zero > zero.fill
sync
sleep 1
sync
rm -f zero.fill

Now exporting the VM resulted in a slick 4.2GB file. Still, I could have trimmed it more?? May be thats for another day.

--

--

Vickneswaran Ezhilraj

Livin 4 Endorphins, Oxytocin, Serotonin n Dopomine #dancer #wanderer #softwaredev #musicProducer #graffitiArtist #petfather soundcloud.app.goo.gl/L7cRf