Skip to main content

Reduce Virtual Machine Disk Size (VMDK) on VMWare ESXi

Original Article: https://woshub.com/shrinking-vmdk-virtual-disk-vmware-esxi/

In this article, we will look at how to reduce the size of a virtual machine’s hard disk (VMDK) in VMware ESXi. From the vSphere Client GUI, you cannot reduce the size of a virtual disk (only the option to increase the VM disk size is available). An error occurs when you try to specify a smaller VM disk size: Enter a disk size which is larger than its original capacity.

can't reduce vmware vm disk size from vsphere client

This guide is not an officially supported solution for reducing the size of the VMDK disk in VMWare. I have used this method over a dozen times and it works reliably.

Before you can reduce the size of the virtual disk on the VMWare ESXi datastore:

  • Delete all virtual machine snapshots;
  • Backup the virtual machine, or at least make a copy of the *.vmdk and the *-flat.vmdk files of the virtual hard disk you want to shrink. In the ESXi console, run the following commands:
    cp vmname.vmdk backup_vmname.vmdk cp vmname-flat.vmdk backup_vmname-flat.vmdk

Reducing the virtual disk file in VMware is a two-step process:

  • Shrink the partition size within the guest OS;
  • Reduce the size of a VMware virtual machine’s VMDK file on VMFS (NFS) datastore.

How to Shrink Disk Partition in the Guest OS

First of all, reduce the size of the disk partition in the guest operating system

If the VM has a Windows guest OS installed:

  1. Open the Disk Management console (diskmgmt.msc);
  2. In this case, the WinRE Recovery Partition is to the right of the partition we want to shrink. To shrink the partition in Windows, there should be no other partitions to the right of the partition you are trying to shrink. You need to either move the winre.wim recovery image from a separate partition to the system one, or move the recovery partition to the beginning of the partition table (both options are described in detail in the post: Cannot extend volume blocked by a recovery partition);Cannot shrink disk in guest windows due to recovery partition
  3. You can now reduce the size of the main partition. In modern versions of Windows, you can reduce the size of the partition without rebooting; in older versions, you have to use the sdelete tool. Click on the partition and select Shrink Volume;shrink volume in Windows
  4. Suppose you need to reduce VM disk size by 40GB. In the Enter the amount of space to shrink in MB box, type 40960 MB (40 GB x 1024) and click Shrink;
    specify the amount of space to shrink in MB
  5. When the shrink task is complete, 40GB of unallocated space will appear to the right of your main partition.windows partition unallocated space

For the Linux VMs, you can boot the virtual machine from a GParted LiveCD (GNOME Partition Editor) if you need to reduce the size of the root partition. Shrink partitions using the GParted GUI’s Resize/Move menu. Move partitions if necessary. To apply changes, select Edit -> Apply all operations. The tool will resize partitions and reduce the file system using Rezise2fs.

reduce partition in guest linux

Boot the Linux guest VM and check that everything is working correctly.

You can resize mounted non-root partitions directly from the Linux guest using parted. The scenario is different when using LVM logical volumes.

Reduce the Size of a VMware Virtual Machine Disk (VMDK)

You can now reduce the size of the VMDK file on the VMware VMFS file system.

  1. Shut down the VM;
  2. Use SSH to connect to the ESXi host on which the virtual machine is registered;
  3. Navigate to the directory where the VM vmdk file is located (you can find the path to the VMDK file in the virtual disk properties in the vSphere client):
    cd /vmfs/volumes/datastore/test-VM
    vmware vm file on vmfs datastore
  4. List the virtual disk configuration file (*.vmdk) using the cat command:
    # cat test_vm_3.vmdk
    The size of the vmdk disk is set in the #Extent description section (after the RW symbols). In this case, it is 167772160 (80 GB *1024*1024*1024 / 512); vmware vmdk file size
  5. I want to reduce my VMDK disk by 40 GB. This means that I have to specify 83886080 in the Extent description section. This value is calculated as follows: 167772160 (current disk size) – 83886080 (40 GB*1024*1024*1024 / 512);
  6. Edit the virtual disk configuration file using vi: # vi test_vm_3.vmdk
  7. Use the down arrow key to scroll to the line containing the disk size and press i (to edit it). Specify the new size of the virtual disk. Press ESC to exit the edit mode and then type :wq -> Enter to save the changes; shrink vmdk file size on vmware datastore
  8. The remaining step is to migrate the VM to another datastore using Storage VMotion. The virtual machine properties will show the new virtual disc size after the virtual machine files are moved.
Tip. If you have only one ESXi host, the easiest way to update the disk size in the vSphere Client is to simply re-register the virtual machine. Click on the VM and select Unregister.

 

unregister vmware vm

Then go to the Storage section, find the VMFS/NFS datastore where the VM is located -> click Datastore Browser, find the VM directory, click on the VMX file, and select Register.

register vmware vm using its vmx file

Or you can clone the VMDK file using this command:

vmkfstools -i test_vm_3.vmdk test_vm_3_newsize.vmdk

Then delete the original VMDK file and clone the disk again, to revert to its original name:

rm test_vm_3.vmdk rm test_vm_3-flat.vmdk vmkfstools -i test_vm_3_newsize.vmdk test_vm_3.vmdk

Make sure that the new size of the virtual disk is now displayed in the VM properties.

new size of the virtual disk is now displayed in the vmware VM properties

Then start the VM, logon to the guest OS, and check that the unallocated space has disappeared, and the disk size has been reduced.

Shrink a Hard Drive Volume in Windows

Shrink a VMDK Disk Using VMware vCenter Converter Standalone

You can reduce the size of virtual machine hard disks using the VMware vCenter Converter Standalone. This free GUI tool allows you to clone a source VM and set the size of the new disk to be smaller than the original (there must be free space on the guest file system).

Cons:

  • V2V conversion is quite slow;
  • There must be enough free space on the datastore to save a new VM;
  • A new MAC address is assigned to the new VM.

Pros:

  • Simple graphical interface;
  • An exact copy of a VM is created;
  • The source VM won’t be corrupted by incorrect disk resizing actions.
Note. The source VM you want to resize must be shut down.

Run VMware Converter and specify the ESXi host or vCenter address.

vmware converter connect vcenter

Select the source VM.

select vm to convert

Set the virtual hardware settings for your new (destination) VM.

set VM properties

Go to the disk edit mode (Data to copy -> Edit).

edit disk properties in vmware converter

Select the copy mode: Select volumes to copy.

copy all disks and mainntain layuout

Specify the new virtual disk size for your new VM. In my example, 48 GB on the virtual disk is occupied by the guest OS, and the total disk size is 150 GB. We will reduce the size of the virtual disk to 60 GB.

mainttain disk size vm converter Shrink a VMDK using VMware Converter

Run the VM conversion process. When finished, turn off the source VM and power on the new one. Check that the virtual disk size has been reduced. The original VM can then be removed.