Home > VMware, vSphere, vSphere 5 > How to Shrink a Thin VMDK on ESXi 5.0

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.

image

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).

image

Issue the command vmkfstools –punchzero VM.vmdk.  This will take a while so be patient (and oh yes, you VM must be powered off).

image

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.

image

Nice, now it’s actually 10.8 GB.  Makes it a lot easier to transfer around!

  1. Jorge Santamaria
    November 8, 2012 at 20:48

    Excelent document!!!!

  2. Mark
    November 9, 2012 at 18:06

    Life Saving Blog! thanks

  3. December 16, 2012 at 20:09

    So are you saying we don’t need to use convertor any longer?. Thanks for clarification in advance.

    • December 17, 2012 at 17:08

      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…

  4. G
    December 18, 2012 at 06:32

    It is my pleasure to read this article, saving my work time.

  5. Hector
    December 18, 2012 at 16:57

    Dont’ work in ESXi 5.1 my vmdk thin not reduced

    • lakusz
      February 26, 2013 at 19:44

      Work very well.

  6. January 5, 2013 at 13:22

    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

  7. glenndm
    January 11, 2013 at 00:15

    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.

    • January 14, 2013 at 09:45

      That’s correct, i noticed that too when downloading the file using the Datastore Browser.

      • glenndm
        January 14, 2013 at 12:39

        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

  8. Alex
    January 11, 2013 at 23:27

    Doesn’t work on ESXi 5.1 for me too, what a pain!

  9. Alex
    January 14, 2013 at 10:52

    glenndm :
    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.

    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

    • glenndm
      January 14, 2013 at 12:47

      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

      • Alex
        January 14, 2013 at 13:44

        glenndm :
        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 🙂

  10. Braino
    March 22, 2013 at 10:55

    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! 🙂

    • August 30, 2013 at 21:55

      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)

  11. Braino
    March 22, 2013 at 11:18

    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. 😉

    • April 14, 2013 at 19:47

      Yeah… two dashes… Wonder what smart ass invented that 🙂

  12. Mordock
    April 10, 2013 at 17:20

    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

  13. Mordock
    April 10, 2013 at 17:22

    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.

    • April 14, 2013 at 19:25

      I’m not a big fan of the two dashes (–) either… wonder who on earth invented that 🙂

  14. Chopper Rob
    July 18, 2013 at 12:42

    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

    • August 1, 2013 at 09:21

      Try doing a storage vMotion to a VMFS volume and back to the NFS disk. That should do it!

  15. September 9, 2013 at 01:51

    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.

    • Adam Hutchins
      October 23, 2013 at 14:57

      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.

  16. James
    November 14, 2014 at 18:55

    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.

  17. Holger
    September 3, 2015 at 05:41

    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.

  1. No trackbacks yet.

Leave a comment