Sep 09

How to shrink a VMDK the hacky way

Source

Disclaimer

I do not recommend using this method, following the below steps is at your own risk!

Problem

You create a VMDK with a large size using Thin Provisioning and you realise that it’s not a good idea. You have your system installed and don’t want to have to reinstall everything.

Preliminary

Make a backup of the VMDK files before you do anything. Reduce the size of your system inside the VMDK to the size you want. Defragment the disk.

My virtual machine is called VMwareConverter, the disk files are called
VMwareConverter.vmdk
VMwareConverter-flat.vmdk

Edit the file VMwareConverter.vmdk and look for the section “Extent description”

# Extent description

RW 524288000 VMFS “VMwareConverter-flat.vmdk”

This value is calculated as follow
vmdk_size= (x*1024*1024*1024)/512 where x is the size of the vmdk in GB

Which means that the existing size is 524288000×512÷1024÷1024÷1024 = 250GB

We will go simple and bring it down to 25GB so we just have to take a zero out.
# Extent description
RW 52428800 VMFS “VMwareConverter-flat.vmdk”

Now to make it effective we need to clone the disk
vmkfstools -i VMwareConverter.vmdk ../VMwareConverter.vmdk

Once the operation is complete.
rm VMwareConverter.vmdk VMwareConverter-flat.vmdk
mv VMwareConverter*.vmdk .

If you need to rollback, restore your backed up VMDK files.