Shrink a Thick VMDK Disk in VMware ESXi
Before you start:
- Take a backup of the VM.
- Delete all snapshots.
- The VMDK must be thick. A thin disk can be inflated with
vmkfstools -j disk.vmdk. - Tested with Windows guests.
1. Shrink the volume inside Windows
defrag C: /X
In Disk Management, shrink the volume so the free space is left at the end of the disk. Then shut down the VM.
2. Create a temporary disk with the target size
SSH to the ESXi host where the VM lives and go to its folder (escape spaces with \):
cd /vmfs/volumes/Datastore/My\ VM/
vmkfstools -c 40G -d zeroedthick temp.vmdk
The new size must be bigger than the end of the last partition in Windows.
3. Note the geometry of the new disk
cat temp.vmdk
Write down the sector count on the extent line and the ddb.geometry values:
# Extent description
RW 83886080 VMFS "temp-flat.vmdk"
ddb.geometry.cylinders = "5221"
ddb.geometry.heads = "255"
ddb.geometry.sectors = "63"
4. Edit the original descriptor
Edit the original .vmdk (the small descriptor, not the -flat.vmdk) with vi and replace the extent sector count and the ddb.geometry values with the ones you wrote down.
vi basics: i to edit, Esc then :wq to save and quit, :q! to quit without saving.
5. Delete the temporary disk
vmkfstools -U temp.vmdk
This removes the descriptor and its -flat file.
6. Clone the original disk
vmkfstools -i original.vmdk shrunk.vmdk
7. Swap the disks
In the VM settings remove the old disk (don't delete it from the datastore yet), add Existing hard disk shrunk.vmdk, boot and test. When everything works, delete the old one.
Source: blanketvm.com.