How to Shrink a Thin VMDK on ESXi 5.0
I always like to keep my template small. This means the files inside the VM, but the size of the VMDK as well. The reason for this is that I often need to download the VM to USB storage of whatever and transfer it to remote sites.
So I wanted to shrink my VMDK size as much as possible. After cleaning up the VM itself and running sdelete, it’s time to shrink the VMDK.
First let’s review the actual usage of it:
The virtual disk size can be seen with ls –lh *.vmdk. In my example, I have a 40 GB virtual size.
To see the ‘real’ size of the vmdk, run du –h *.vmdk. This gives us a 36 GB size (while the VM itself actually uses about 10 GB).
Issue the command vmkfstools –punchzero VM.vmdk. This will take a while so be patient (and oh yes, you VM must be powered off).
On a 40 GB VMDK it took me about 10 minutes, but this will depend mostly on your SAN speed.
After it’s done, let’s re-issue du –h *.vmdk.
Nice, now it’s actually 10.8 GB. Makes it a lot easier to transfer around!
Excelent document!!!!
Life Saving Blog! thanks
So are you saying we don’t need to use convertor any longer?. Thanks for clarification in advance.
I do this as a ‘quick fix’ to shrink my vmdk of the template after moving some files around in it.
Downside with this is that you need to dive into the CLI… Some people are uncomfortable with it…
It is my pleasure to read this article, saving my work time.
Dont’ work in ESXi 5.1 my vmdk thin not reduced
Work very well.
Excellent post & really appreciated. My friend let his vdmk’s expand to fill the data store. I have been battling with a spare starved hyper visor all weekend. This article is just what I was looking for 🙂 – Thanks
Hi,
I am missing something.
the steps you describe above work: my 80GB vmdk is really but 6GB.
But when I use scp or ftp to copy the files, the full 80 GB data is again transferred.
That’s correct, i noticed that too when downloading the file using the Datastore Browser.
Could you please explain how you do this: “….I often need to download the VM to USB storage”
while taking advantage of the smaller real size?
I read an usb stick cannot be vmfs-formatted. So as Alex pointed out, a thick provisioned file would be the result
dank je voor je hulp
Doesn’t work on ESXi 5.1 for me too, what a pain!
You can’t have a thinprovisioned disk outside the datastore, if you download it to your workstation it will be a thickfile. Just if you move it between datastores it will keep the size
When exporting to OVF, the smaller size seems to help, the resulting file was about 3 GB (from thin real 6Gb, thick 80GB)
My goal is to get copies of the VM offsite by as simple a method as possible.
The ftp/scp route would be ideal, but for now the ovf will have to do
Nice one, good to know 🙂
This worked for me, Thanks!
…although I had to use:
vmkfstools -K vm.vmdk
(note the -K switch is upper case)
…as the -punchzero switch did not work.
I’m running ESXi 5.1, so maybe that’s why it didn’t work for other people?? It kept coming back saying “invalid block length specifier: unchzero”
…wait – maybe that’s because the switch above is wrong? should it be:
vmkfstools –punchzero vm.vmdk
?? (note two dashes in front fo punchzero)? I’m incredibly rusty with my *nix CLI, and it’s 3:30am here – so I’m not testing my hypothesis – 😉
Anyway, I was dealing with a VM lab i inherited at work where all the drives are thin-provisioned, and the SAN they are on is massivly overprovisioned. This was my only hope – – – and it worked! 🙂
FWIW punchzero is a signal not a flag. so you need two dashes not one… if you copy and paste the above you’ll see there are 2 characters… they blend together into what looks like 1 dash (i.e. a flag)
I just noticed that even on my comment, the blog got rid of the two dashes i put in front of punchzero. I’d say it’s overhelpful blog syndrome. 😉
Yeah… two dashes… Wonder what smart ass invented that 🙂
the –punchzero command only works if the blocks have been zeroed out in advance with a tool such as sdelete. That is why you are seeing it not working in 5.1. Try sdelete first then vmkfstools –punchzero
Oh how I love computers that do me favors. The command is -K or – -punchzero. (2 dashs together). The software keeps converting the two dashes to one big dash.
I’m not a big fan of the two dashes (–) either… wonder who on earth invented that 🙂
i tried to shrink a disk, but it’s not working. Nothing is happening after giving the vmkfstool command. The disk is thin provisioned and on a NFS share.
I first prepared the disk with sdelete -c and sdelete -z
Here is the result.
/vmfs/volumes/…/Win2003Test # vmkfstools -K Win2003Test.vmdk
/vmfs/volumes/…/Win2003Test #
We are running ESX5.0
Try doing a storage vMotion to a VMFS volume and back to the NFS disk. That should do it!
Comments above are correct, ESXi 5.1 is -K. Also, if you’re doing it on a thin provisioned disk, you’ll have VM.vmdk and VM-flat.vmdk. Target vm.vmdk.
I’ve done sdelete -z c: on the guest.
When I do vmkfstools -K vm.vmdk nothing happens….? Storage vmotion will do the trick. But it takes so long.
It baffles me that the vSphere Client can reach version 5+ without having tools in the GUI to compact thinly provisioned disks. Is this not a routine maintenance activity? Or am I just overlooking some (not CLI) tool for doing this? Don’t get me wrong, I’m comfortable in the CLI, but it seems a bit terse for such a routine activity.
Caution: Accessing the files direcly via SCP or Download via vSpere Client – Datastore will cause problems for thin provisioned files. They will expand to their maximum size. Thus I would recommend to export the virtual machine as an ovf file (Open Virtualization Format). This option can be found in the vSphere Client 6.0 via main menu > File > Export – not in the context menu of the virtual machine. The resulting files are surprisingly small.