Howto Shrink a Thin Provisioned Virtual Disk (VMDK)

Thin provisioned disks are a great feature to save capacity as you virtual machines filesystem will never use the full capacity. I do not know a single system where you do not have at least 10GB of free space for OS disks. I am not considering databases, applications or fileservers which will grow constantly. Having thin provisioned disk is usually no longer a performance problem so it is a valid design choice even in production.

A common issue with thin disks is that the size will grow when required, but never shrink. When you require the capacity only once you might want to get it back from the virtual machine. This post describes how to reclaim unused space from the virtual machine.

Virtual Machine Preparation (Windows)

Windows does not automatically zero deleted blocks. Microsoft provides a tool that can zero blocks after while deleting a file or zero out the entire free space. This is required to reclaim space back from the virtual disk.

  1. Download SDelete
  2. run sdelete.exe -z [Drive]
    sdelete

Wait a couple of minutes until the process is finished. Please note that you virtual disk file (VMDK) will grow to the full size during the process.

Virtual Machine Preparation (Linux)

Linux does not zero deleted blocks too. There are various tools available to create zeroed blocks. The best known tool is dd which should be available on all systems.

  1. Identify free space with df
  2. Fill the free space with dd
vma:/mnt/data # df -h
vma:/mnt/data # dd bs=1M count=8192 if=/dev/zero of=zero

This will zero 8GB of the available 8.2GB (1MB Blocksize * 8192 = 8GB). Please note that you virtual disk file (VMDK) will grow to the full size during the process.dd-fill-free-space-with-zero

Shrink VMDK File

  1. Power off the Virtual Machine, or disconnect the virtual Disk you want to shrink
  2. Connect to the ESXi Host with SSH
  3. Navigate to the Virtual Machine Folder
  4. Verify disk usage with du
  5. Run vmkfstools -K [disk]
  6. Verify disk usage with du
root@esx3:/vmfs/volumes/ds1/vma $ du -h vma_1-flat.vmdk
7.9G vma_1-flat.vmdk

root@esx3:/vmfs/volumes/ds1/vma $ vmkfstools -K vma_1.vmdk
vmfsDisk: 1, rdmDisk: 0, blockSize: 1048576
Hole Punching: 25% done.

root@esx3:/vmfs/volumes/ds1/vma $ du -h vma_1-flat.vmdk
1.9G vma_1-flat.vmdk

 

Leave a comment ?

13 Comments.

  1. thank you! an important part that my eyes had first bypassed is that we hole-punch the small .vmdk, not the -flat.vmdk

  2. Excellent post! I am going to do this on a volume that is 1.2TB - any ideas how long that will take? I need to build a communication for the environment on how long the VM will be down. Thanks!

  3. Hi, Josh.
    You can do it completely without downtime with Extrasphere disks (not VM) migration to another datastore even on free licensed ESXi host.

  4. Thanks for sharing this useful info. I have a question after applying these steps. flat.vmdk size has been decreased but VM Provisioned Storage is still showing the same size as original flat.vmdk. Please let me know how I can reduce this size as well? Any help is highly appreciable. Than You.

  5. Important addition regarding the Linux preparation step: the "dd" command creates a file named "zero" inside the current directory with the given size (8GB in your example) because of the parameter "of=zero". Therefore not only the virtual disk (VMDK) grows up but also the virtual drive inside the guest gets used nearly 100%. So you should mention that the file "zero" must be deleted after shrinking to free up the filled space inside the guest!

  6. Raimond Barbaro

    Shouldn't the --punchzero switch be used along with the vmkfstools -K command, as advised here:
    http://pubs.vmware.com/vsphere-51/index.jsp?topic=%2Fcom.vmware.vsphere.storage.doc%2FGUID-CBF31A6B-B36F-4552-B512-CC92B1943902.html

    • "-K" is the short version for "--punchzero". You don't have to use both switches.

      • Raimond Barbaro

        Thanks for your reply. How do you know this? It's not in VMware's own documentation on that page I linked. Can you please provide a source?

        Thanks
        Raimond

        • It is a common notation that the short and a long command is in the documentation and in manpages. Using both does not work:


          [root@esx4:/vmfs/volumes/57604a1d/pixel] vmkfstools -K --punchzero pixel.vmdk
          Failed to open virtual disk '--punchzero': The system cannot find the file specified (25)
          [root@esx4:/vmfs/volumes/57604a1d/pixel] vmkfstools --punchzero pixel.vmdk
          vmfsDisk: 1, rdmDisk: 0, blockSize: 1048576
          Hole Punching: 0% done.
          [root@esx4:/vmfs/volumes/57604a1d/pixel] vmkfstools -K pixel.vmdk
          vmfsDisk: 1, rdmDisk: 0, blockSize: 1048576
          Hole Punching: 0% done.

  7. Thanks for this! Very useful in helping "Deflating" our thin VMs that allocated space once upon a time.

  8. Does the required steps require you to have the same amount of space available on the datastore already to complete this? Our datastore has no space and we have no place to move the 2TB VMDK file. So would we need 2TB free on the datastore to do this to reduce the space?

  9. It is NOT recommended to zero Linux ext[234] filesystems by just using dd (it's simple but will degrade the filesystem). Please see https://unix.stackexchange.com/a/251804/134856 for references for why this is the case and for a list of alternatives.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>