I have always thought that VMDK could only grow, so that has also been my default response to colleagues when they expanded a disk too much. Sure a storage vMotion could reclaim unused space in a thin disk, but the “down arrow” for storage capacity would never work. But then someone mentioned that he had done shrinking of disks a couple of times, I decided to investigate.
The official VMware kb isn’t too much help – somewhere discussing it on StackOverflow. But then I found an older post back from 2016 that seems to have found the approach so that’s what we are going to test out.
This is not supported in any way, use at your own responsibility. If you want a supported solution, then VMware converter in a v2v manner is kind of the only way. If you still want to try out the method, then be sure to have a valid backup! And by backup, it’s not a VMware snapshot.
From the VMware documentation, it seems shrinking disk is not allowed under the following circumstances:
- The virtual machine is hosted on an ESX/ESXi server.ESX/ESXi Server can shrink the size of a virtual disk only when a virtual machine is exported. The space occupied by the virtual disk on the ESX/ESXi server, however, does not change.
- The virtual machine has a Mac guest operating system.
- You preallocated all the disk space to the virtual disk when you created it.
- The virtual machine contains a snapshot.
- The virtual machine is a linked clone or the parent of a linked clone.
- The virtual disk is an independent disk in nonpersistent mode.
- The file system is a journaling file system, such as an ext4, xfs, or jfs file system.
The test scenario:
I have a windows 2019 VM, here is the process I want to try out
- Expand VMDK disk in vCenter
- Extent disk in VM guest using diskpart
- Shrink disk in VM guest using diskpart
- calculate new sector size
- edit VM *.vmdk with the newly calculated sector size
- Storage migrate to other datastore
- Check if VM is still ok.
Calculating the “extent description”:
So there is now 10GB free space we can shrink the VMDK with.
A virtual disk described as monolithic and flat consists of two files. One file contains the descriptor. The other file is the extent used to store virtual machine data.
Considering our existing extent
RW 94371840 VMFS “win2019-flat.vmdk”
This means that the file win2019-flat.vmdk is 94371840 sectors × 512 bytes/sector = 48318382080 bytes = 48318MB in size.
Let’s calculate the new value from GB to sectors.
36GB x 1024(mb) x 1024(kb) x 1024(byte) / 512byte pr sector = 75.497.472
before proceeding, we need to power off the VM. The .vmdk file is loaded into memory, so even if we can edit it now and start storage vMotion our changed value will just change back.
It worked, we were able to add more space to the VM, extent, and shrink the guest os filesystem. We then calculated the number of sectors for the .vmdk file and storage vMotion did its magic and made the VMDK smaller in physical size.
I have also tried this in a couple of cases, also real life senairoes where people have added 4TB to much…. Then its sometimes easier to shrink than having to move files around.