Overview

Expanding the capacity of VM disks is easy, but shrinking their size is not straightforward on VMWare ESXi GUI.

Here, we will perform an action to reduce the disk size on VMWare ESXi using the command line.


Step-by-Step Guide

1. Shutdown the VM

Power off the virtual machine that contains the virtual disk you want to shrink and note that all snapshots of the VM need to be deleted before starting the action.

2. Enable SSH service on VMWare ESXi

2023-07-05_084829

3. Shrink VMDK Size on VMWare ESXi

Login to ESXi SSH console via Putty or other SSH client software.

Enter the VM storage directory (e.g., “/vmfs/volumes/Datastorage/”).

2023-07-04_123622

Edit the file named “hostname.vmdk” using a text editor (e.g. vi VM_TEST.vmdk).

In this case, the edited file was “VM_TEST.vmdk”.

2023-07-04_123631

Locate the “# Extent description” of the content, which is shown below:

# Extent description
RW 209715200 VMFS "VM_TEST-flat.vmdk"

Suppose the VM’s HDD current size is 100GB, which is equivalent to 209715200 of content. We would like to reduce the size by 40GB, which means shrinking it to 60GB. The new content number is “209715200 - (40*1024*1024) = 125829120”.

Update the content of VM_TEST.vmdk to:

2023-07-04_123657

RW 125829120 VMFS "VM_TEST-flat.vmdk"

Save and close the text editor.

4. Start the VM

Power on the virtual machine and log in to the guest OS.

Open the “Disk Management”.

2023-07-04_124340

2023-07-04_124422

The volume size is still 100GB, so take a “Shrink Volume…” action of the virtual disk.

2023-07-04_124440

Now, the virtual disk has been successfully shrunk to the new size.

5. Fix the Display Size (Optional)

Issue:

2023-07-04_130113

The STORAGE of the instance still displays 100GB, but the actual size has been shrunk to 60GB.

Solution:

To fix this, export and import the VM by OVF TOOL.

Export the VM

ovftool.exe vi://root:"P@ssw0rd"@vms01/VM_TEST c:\tools\VM_TEST.ova

Import the VM

ovftool.exe --sourceType=OVA -n=VM_TEST -ds=Datastorage "c:\tools\VM_TEST.ova" vi://root:"P@ssw0rd"@vms01

Conclusion

To shrink the size of a VMDK virtual disk on VMWare ESXi using the command line. The guide is easy to follow but caution should be exercised and backups should be made before proceeding with the steps.


Reference