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.
- Download SDelete
- run sdelete.exe -z [Drive]
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.
- Identify free space with df
- 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.
Shrink VMDK File
- Power off the Virtual Machine, or disconnect the virtual Disk you want to shrink
- Connect to the ESXi Host with SSH
- Navigate to the Virtual Machine Folder
- Verify disk usage with du
- Run vmkfstools -K [disk]
- 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
thank you! an important part that my eyes had first bypassed is that we hole-punch the small .vmdk, not the -flat.vmdk
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!
Hi, Josh.
You can do it completely without downtime with Extrasphere disks (not VM) migration to another datastore even on free licensed ESXi host.
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.
Was wanting an answer to this as well.
Once you migrate to another host the provisioned storage reflects the shrunk size (so migrate to another host and back again). Note: without vMotion you'll need to have the VM powered off for the duration.
Provisioned storage is the amount of space the disk can grow to. So it's correct that it does not decrease when shrinking disks.
Sorry, typo. The actual VM "Storage Usage" size is properly reflected in the vSphere UI. Provisioned size obviously does not change.
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!
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.
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.
Ok thanks.
Thanks for this! Very useful in helping "Deflating" our thin VMs that allocated space once upon a time.
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?
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.
I've tried this on a server I want to shrink in size. I used SDelete -z to zero out the drives which increases the used storage to the full size of the disks but now when I run vmkfstools -K on the vmdk files it completes but they do not get any smaller and I am stuck with a server that is consuming almost 1TB on disk when it only has about 100GB of data on it's drives. Any idea why the vmkfstools command isn't working for me? Using ESXi 6.0 and all the drives are thin provisioned.
Oh nvm my last post, I was using ls -l to view the files but that shows the full provisioned size. du -h *.vmdk shows the actual disk usage size which was being shrunken correctly.
Hi,
hope somebody can help.
Is there any way to check if a Virtual Disk (VMware) was used or teated with "sdelete.exe -z" ?
I can not remember on which VMs i executed "sdelete.exe -z"
I would like to shrink the Virtual Disks because I need to safe as much space as possible on my Storage where the VMs are running.
Can somebody help?
Thank you :-)
Hi,
Is it possible to reduce the disk size to a specified size as i dont see any option to reduce to specific size.
Thank you for this article. That was very helpful for me and solve my problem. PS: This procedure do not shrink the VMDK provisioned size, only the VMDK file size.
Thanks!!
Somehow, this didn't work...
It's a Linux-VM with a thin-disk of 8GB. In use are ...GB.
On the ESX 6.0u3 I can see it has 8GB:
8589934592 Jan 16 11:10 SLES-WEBSVR-flat.vmdk
After zeroing:
#> dd if=/dev/zero of=/test.dat bs=4k
#> rm /test.dat
#> poweroff
I shrink the disk:
#> vmkfstools -K SLES-WEBSVR.vmdk
vmfsDisk: 1, rdmDisk: 0, blockSize: 1048576
Hole Punching: 100% done.
And it's the same size.
I'd welcome any thoughts and hints!
cheers!
Sry, forgot the "in use" value:
in use are 5.1GB, meaning the "shrink" should result in a 2.9GB lower .vmdk file size
Congratulations MBIT class of 2020!!