How to Shrink a VMDK file in ESX

by darky on September 18, 2009

While it is a somewhat known fact that you used to be able to shrink disks using vmkfstools -X in ESX 2.x, the option was removed as of ESX 3.0. The reason for this probably stems from those few customers that were not familiar with using the command properly and ended up cutting off too much of the disk and corrupted their Guest OS file system as a result.

Ever since the option was removed there have been more experienced users wanting the option to be added back to vmkfstools as they are aware of the risks and want that ability again. For those people I just mentioned, I have a treat for you:

** Use this technique at your own risk. I do not and will not take responsibility for your corrupted vmdk’s as a result of following this process, whether correctly or incorrectly **

If you have ever taken a look at the descriptor file for a vmdk file, you will the “Extent description” section:

# Extent description
RW 52428800 VMFS “foo-flat.vmdk”

The value between ‘RW’ and ‘VMFS’ is the size of disk when divided by 512:

52428800 * 512 = 26843545600 (25G)

-rw——- 1 root root 26843545600 Sep 18 19:10 foo-flat.vmdk
-rw——- 1 root root 370 Sep 18 19:11 foo.vmdk

If you want to shrink (or increase) the size of your vmdk file, alter this value in the descriptor file to reflect your new desired size*. You can do with this ‘vi’ or ‘nano’:

* For this example I will to shrink the disk to 12.5G

# Extent description
RW 26214400 VMFS “foo-flat.vmdk”

After making this change you can use ‘vmkfstools -i’ to clone this disk:

# vmkfstools -i foo.vmdk foo_1.vmdk
Destination disk format: VMFS thick
Cloning disk ‘foo.vmdk’…
Clone: 100% done.

You will discover that the cloned disk now reflects the new size of 12.5G:

-rw——- 1 root root 13421772800 Sep 18 19:13 foo_1-flat.vmdk
-rw——- 1 root root 372 Sep 18 19:13 foo_1.vmdk
-rw——- 1 root root 26843545600 Sep 18 19:10 foo-flat.vmdk
-rw——- 1 root root 370 Sep 18 19:11 foo.vmdk

This technique will also work if you try to cold migrate the VM or Storage VMotion it. This would be less cumbersome than trying to play around with another disk at the command line during a simple shrink process. Change your values and then migrate the disks via VI Client/VC to a different datastore. This will reduce disk space required for this operation and it will be a whole lot cleaner than a manual clone with vmkfstools and then a remove of the original.

SVMotion would be the a favourable option for those that do not want to incur downtime for their VM.

{ 2 trackbacks }

Tweets that mention How to Shrink a VMDK file in ESX -- Topsy.com
September 18, 2009 at 12:10 pm
How to shrink a VMDK file in ESX « PixeLabs
August 2, 2010 at 6:06 am

{ 9 comments }

Designer Eyeglasses September 19, 2009 at 4:38 pm

I am just finding for this solution. Thank you so much for this useful post.

Brochure Printing September 26, 2009 at 1:24 pm

I was just looking for this idea. Very good study material. keep posting like this. Thanks.

Brian October 8, 2009 at 8:53 pm

It's *sick* how easy this was versus the other solutions I found. Bravo!

Brian October 9, 2009 at 1:53 am

It's *sick* how easy this was versus the other solutions I found. Bravo!

Luzian January 15, 2010 at 9:36 am

I've used this method on vSphere to shrink a VMs HDD. Edited vmdk file to new size, cloned using 'vmkfstools -i' and then removed the old .vmdk and -flat.vmdk and replaced with the newly cloned one. It works fine but there is one problem I have not been able to solve: When you go to “Edit Settings” in vCenter and click on the “Hard disk 1″, there it still shows the old size. Does anyone know where from this information is read?

George January 26, 2010 at 2:02 pm

Where can I find the descriptor file ?

John July 23, 2010 at 12:29 pm

You can clone the VM. The VM cloned show the new size.

John July 23, 2010 at 7:29 am

You can clone the VM. The VM cloned show the new size.

Arief Online August 31, 2010 at 8:46 am

thanks very much, very helpful !

Comments on this entry are closed.

blog comments powered by Disqus

Previous post:

Next post: