In this article we will show how to reduce the size of a virtual disk (vmdk) file of the virtual machine running on VMWare ESXi. Unlike expanding a disk of a VMWare virtual machine you can do on-the-fly without shutting the virtual machine down, it is more complicated to reduce the size of a vmdk file on the VMWare ESXi hypervisor, and you cannot do it via the vSphere Web Client graphical interface. The vSphere Client doesn’t allow you to specify a smaller virtual disk size.
Prior to reducing the size of a virtual disk, do the following on your VMWare ESXi host:
- Delete all snapshots of the virtual machine (otherwise, you may damage the virtual disk);
- Create a full backup of the virtual machine, or create a copy of the vmdk and flat files you want to shrink. Use these commands:
cp vmname.vmdk backup_vmname.vmdk
cp vmname-flat.vmdk backup_vmname-flat.vmdk
The procedure of reducing a virtual disk size consists of two steps:
- Shrink the partition inside the guest OS;
- Reduce the size of the VMDK file of the VMWare virtual machine on the VMFS (NFS) datastore.
How to Shrink the Partition Size in the Guest OS?
First of all, reduce the size of the disk partition in the guest operating system. If your virtual machine is running Windows OS, use the Disk Manager diskmgmt.msc
(the Shrink Volume operation is available in all supported Windows versions; sdelete
tool is used in older Windows versions to reduce the partition size) or third-party tools (like Acronis Disk Director). If you don’t do it, after then your file system may crash after shrinking the virtual disk size.
Suppose, you want to reduce the disk size by 40 GB. So the volume size must be reduced by 40960 MB (40 GB x 1024
). Enter this value in the corresponding field of the Shrink Volume wizard. After reducing the partition size, there will be some unallocated space (40 GB in our example) to the right of your partition.
Reduce the VMDK File Size of the VMWare Virtual Machine
Then reduce the size of the VMDK file on the VMWare VMFS file system.
- Shut down the virtual machine you want to reduce disk size;
- Using SSH, connect to the console of the ESXi host the VM is registered on (you can use putty or the built-in Windows SSH client);
- Go the directory the VMDK file of your VM is located in (you can get a path to it in the virtual disk properties of the vSphere client):
cd /vmfs/volumes/datastore/test-VM
- Display the contents of the virtual disk configuration file (*.vmdk) using the cat command:
# cat test_vm_3.vmdk
The size of the vmdk disk is shown in the #Extent description section (after the RW characters). In this case, it is 167772160 (80 GB *1024*1024*1024 / 512
); - I want to reduce my VMDK disk from 80 to 40 GB. It means that I have to specify 83886080 (
40 GB*1024*1024*1024 / 512
) in the Extent description section. Set a new size of your virtual disk using a text editor (vi or nano); - I am using vi:
# vi test_vm_3.vmdk
- Using the down arrow key, go 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; - Then just clone or migrate (using Storage vMotion) the virtual machine to another datastore. After moving the virtual machine files, the new size of its virtual disk will be displayed in its properties.Tip. If you have only one ESXi host, one VMFS datastore, you won’t be able to use the Storage vMotion. Then you can clone vmdk using this command:
vmkfstools -i test_vm_3.vmdk test_vm_3_newsize.vmdk
Remove the source VMDK disk, clone the reaming VMDK disk again and rename it by specifying 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.
- Then start the VM, login the guest OS and make sure that the unallocated area has disappeared, and the disk size has been reduced.
- Reducing the virtual disk size by converting a virtual machine using VMware vCenter Converter. Specify a disk size smaller than the source one to convert it (we will show it in the next section);
- You can clone virtual disk contents using third-party software (like Acronis True Image or Symantec Ghost32). This task in performed by adding a new smaller disk to the virtual machine and cloning the contents of a larger disk to it. You can remove the larger disk then.
Shrinking a VMDK Using VMware vCenter Converter Standalone
Another way to reduce a virtual disk size of a VMWare virtual machine is to use a free GUI tool VMware vCenter Converter Standalone.
Cons:
- V2V conversion is slow;
- There must be enough space on the datastore to save the new VM;
- The new virtual machine will have a new MAC address.
Pros:
- You don’t need to open the vCenter or ESXi host console;
- An exact copy of a VM is created;
- The source disk cannot be damaged in case of any errors in conversion task settings.
Specify the ESXi host address.
Select the source VM.
Set the parameters of your new VM.
Go to the disk edit mode (Data to copy -> Edit).
Select the copy mode: Select volumes to copy.
Specify the new disk size for your new VM. As you can see, 48 GB is occupied in the guest OS, and the disk size is 150 GB. We will reduce the size of the virtual disk to 60 GB.
Run the VM conversion process. Then you can power off the source VM and power on the new one. Make sure that the disk size has been reduced. After that the original VM may be removed.
16 comments
Thanks for article. Note that ssh won’t connect to the ESXi host by default for security purposes.
https://kb.vmware.com/s/article/1019852
For some reason i do not get that second option on my VMware vCenter Converter Standalone. Dont know if that is a license thing or something else. Do i perform same procedure for a linux vm ?
Thank you! better service than some premier support I won’t mention
Thank you for this helpful article. I’ve used it a few times now. Some comments:
– It seems like the “Pros” and “Cons” above are transposed
– If you use vVols in your environment, you may wish to move the VM to regular storage first as resulting VM may not tolerate snapshots
– I usually shrink the drive in Windows more than needed, then come back at the end and expand it just to be safe
Successfully shrunk my HDD, thanks!
But there is still unallocated space.
How can I delete it?
I’ve tried reconnecting the HDD in the VM settings.
I converted the VM with the remaining unallocated space into a template.
The unallocated area disappeared from the deployed VM.
I think I’ll deal with this for now.
Sorry for the inconvenience.
– use translation tool
Perhaps you just needed to refresh the data in the disk management console.
This worked amazingly well. It’s just too bad it only works on offline VMs. If there was a way to do this online, that would be wizard!
After having shrinked one of the disk of a VM (edit vmdk file) I’m unable to Storage vMotion the VM. Get the following error: “Failed waiting for data. Error 195887107. Not found.” Any idea?
Have you checked the following VMware kb? You should disable the multi-writer attribute on your vmdk drives (the Sharing option).
Storage vMotion fails with Error 195887107 (66850)
https://kb.vmware.com/s/article/66850
Thank you for these instructions.
Reducing the VMDK worked well for guests running Windows.
With Ubuntu guests and LVM I had to manually correct and repair the GPT table before the machine would boot correctly
Thanks, I was able to clone physical Dell W10LTSC to Dell ESXi standalone host with to small datastore. Used sysprep, Clonezilla. New VM in ESXi overprovison size(to enable restore with Clonezilla). In VM, cmd.exe diskpart and “select volume x” and “delete volume x override”(recovery etc partitions), shrink c:, ssh to ESXi, resize in vmdk conf file. Unregister/register VM in ESXi to have it show the right size. 😀
This worked great for me – almost. The drives on the VM are the correct size, and the disks on the VM configuration are the right size, but the Datastore files are still reporting the original size for the VMDKs. How do fix this?
If it is a thin provisioned disk you may have to run this command on the ESXi host on that VMDK file. vmkfstools -K disk_name.vmdk
I had to do this to get my thin provisioned VMDK file back down in size after deleting a bunch of files. Check this KB for the steps I took. https://kb.vmware.com/s/article/2136514
This unfortunately does not work for NFS datastores and without sufficient space on a vmfs datastore to migrate the vm to you’re stuck with creating a new drive, copying the data and deleting the old or using v2v.
this seems to work well for MBR based disks. for GPT based ones, i had to shrink the main partition at least 2MB more than the projected total size of the disk (i even shrunk it 128MB more.. to be on the safe side). after manipulating the VMDK file and migrating the VM, i booted a GParted live distro and used gdisk’s (r)ecovery option to rebuild the backup GPT. in detail: i used the d option [use main GPT header (rebuilding backup)] and then (w)rote the GPT to the disk. hope this keeps someone from growing grey hair 😉