How to shrink a VMDK: Shrinking a virtual disk in VMware ESXi

First open up Disk Management in Computer Management in your guest Windows environment.

Right click the volume on the disk you want to shrink.

Windows will inform you the maximum amount it can shrink the disk by. Choose an amount that you wish to actually shrink it by and click Shrink.

shrinkF

Windows will start the shrinking process and it might take some time and appear to be hanging as Windows will actually be defragmenting the disk in order to consolidate the free space towards the end of the disk before resizing the volume.

Once it is done and you are satisfied that the volume on the disk is the size you want it then you need to shut down the VM.

SSH into the host and copy the VMDK file to make a backup of it, just the descriptor file not the flat file.

cp vmname.vmdk vmname-original.vmdk

Open up the VMDK file in a text editor and find the line that describes the size of the flat file. Similar to the following

# Extent description

RW 209715200 VMFS “vmname-flat.vmdk”

The number is the size of the virtual disk in terms of disk sectors, where each sector is 512 bytes. So a 100GB virtual disk is 209715200 sectors.

You will need to change this number to correspond to the new disk size where x = size in GB

vmdk_size = [x * (1024*1024*1024)] / 512

I have chosen to shrink my disk to 60gb, so my new Extent description now reads as follows:

# Extent description

RW 125829120 VMFS “vmname-flat.vmdk”

You now need to clone the drive to get it to the new size:

vmkfstools -i vmname.vmdk vmname-new.vmdk

The bit we are interested in is the newly created vmname-new-flat.vmdk file.

Rename the old flat file from vmname-flat.vmdk to vmname-flat-old.vmdk

and rename the vmname-new-flat.vmdk file to vmname-flat.vmdk

Start the VM up and it should show the new smaller disk. When you are satisfied that everything is working you can now delete the old unneeded files from your datastore.