How To Shrink a VMDK File in ESX 4 and Windows

Today, I would like to share a howto reduce or shrink a VMware VMDK file (aka hard disk) for a Windows VM . Bare in mind that this is not reducing or shrinking a volume in Windows and leaving a free partition space. For this howto, I will be reducing or shrinking the Hard Disk 2 for a Windows VM from 30GB to 5GB whereby the example VMDK file name will be sourcedisk_1.vmdk which is D:\ drive. Logon to your Windows VM and shrink the target disk 30GB to 5GB. In the Disk Management, right click on the partition that you want to shrink and select “Shrink Volume” from the menu.

In the Shrink dialog, you will need to enter the amount you want to shrink by and not the new size. For example, if you want to shrink to 5GB partition you need to enter 25600 into the box and you will have 5117 total size after shrink in MB which is 5GB. Click Shrink button to proceed and you will now have 5GB NTFS partition and the rest is RAW partition. You can proceed to shutdown this VM.

Logon to your ESX host and nagivate to the sourcedisk_1.vmdk file. Edit the VMDK file and look for Extent description section as below: –

# Extent description
RW 62914560 VMFS "sourcedisk_1-flat.vmdk"

The value between RW and VMFS is the size of disk. You can use the formula below to determine the value whereby DiskSize is in GB.

(DiskSize * 1024 * 1024 * 1024)/512 = 62914560

So, we will then change the value to 10485760 to represent 5GB and it should look like below: –

# Extent description
RW 10485760 VMFS "sourcedisk_1-flat.vmdk"

Next, run the vmkfstool to clone to a new disk and called it targetdisk_2.vmdk as below: –

vmkfstool -i sourcedisk_1.vmdk targetdisk_2.vmdk

Once the clone is completed, you will discover that the cloned disk now has a new size of 5GB as below: –

-rw------- 1 root root  5368709120 Jun  9 18:08 targetdisk_2-flat.vmdk
-rw------- 1 root root         423 Jun  9 18:08 targetdisk_2.vmdk

Now, this is an important part. Edit the targetdisk_2.vmdk file and comment the following line as below: –

#ddb.geometry.sectors = "63"
#ddb.geometry.heads = "255"
#ddb.geometry.cylinders = "7832"

Next, remove the sourcedisk_1.vmdk from the VM in Edit Settings and do not delete disk. You can delete it later in the datastore browser. Then, add the new targetdisk_2.vmdk in Edit Settings of the VM.

Finally, power up the VM but you will not be able to see the 5GB hard disk. You’ll need to switch the disk to Online status in Disk Management.

I know this is a long process but it does help me. You may reboot the VM one time to make sure you are able to see the 5GB hard disk. If everything is fine, you can proceed to delete of the sourcedisk_1.vmdk file in the datastore browser.

Hope this is useful to all VMware Administrator and please do correct me if this is wrong or I had missed out something. Thank you.