Question ID : 205
Created on 2012-04-01 at 2:02 PM
Author : Guru Corner [email@example.com]
Online URL : http://kb.guru-corner.com/question.php?ID=205
Ran into an issue recently with one of my VMs. Wanted to back it up but was 200GB. I knew there wasn’t that much data on it anymore, it had just grown to that. But in Windows when you delete a file you don’t delete it really, Windows just flags it to be overwritten.
You can use this tool first but don’t think it’s needed. CCleaner and Fileshredder can do the same. That is, wipe the free space while in Windows.
SDelete Sysinternals as MS
In Windows go to a CMD and to the location of sdelete and type
That cleans the free space. Then shut down the VM.
Once shut down go to a CMD and go to where VMWARE is installed. Mine is
D:Program Files (x86)VMwareVMware Workstation
Then run vmware-vdiskmanager.exe to shrink the VM. This is done with the -k switch.
My VM was in a different location to the diskmanager tool so ended up like this
vmware-vdiskmanager.exe -k “f:vmtest labxp.vmdk”
Once you hit enter you should see a Shrink: 0% meaning it’s started. Could take a while. I just left mine running while at work so don’t know how long it took.
The ” are needed it appears.
It works great. Mine went from 200GB to 26GB.
Use the build-in defrag-tool in Windows or a third part tool of your choice.
Go ahead and open the VMware toolbox and find the “Shrink” function
When you click the “Prepare to shrink” you start a two stage process; first the application fills all vacant space on the harddrive with large empty files
This process will take quite some time if you have a lot of unused space on your disk. If you look at the root of the harddrive, you’ll find that it simply generates a number of 2 gig files named “wiper0″, “wiper1″ etc..
These files are simply used to overwrite any “deleted” files on the disk; when Windows deletes a file, it doesn’t actually remove the data, it just removes any reference to it – therefore it still takes up space on your disk. The “wiper”-files allow ESX to identify truly unused blocks on your disk.
The next step the Toolbox is to supposed to do, is to tell the ESX host to do the Shrink
But unfortunately this is never done. Hence we need to do some additional work:
This is done to have a static non-changing disk file to work on. When a snapshot is active, every change to the harddrive is written into a separate delta-file.
Either use a SSH client or access the service console directly – make sure to be “root” before you try to proceed.
Change directory to where the virtual machines stores its files:
In this example my storage is named “netapp10″ and the virtual machine is called “Win2003helper”.
A simple “ls -l” command reveals which files exists, the interesting ones are:
-rw------- 1 root root 16795648 Oct 7 13:15 Win2003helper-000001-delta.vmdk -rw------- 1 root root 235 Oct 7 13:14 Win2003helper-000001.vmdk -rw------- 1 root root 8589934592 Oct 7 13:14 Win2003helper-flat.vmdk -rw------- 1 root root 19395 Oct 7 13:14 Win2003helper-Snapshot1.vmsn -rw------- 1 root root 406 Oct 7 12:59 Win2003helper.vmdk
The file “Win2003helper.vmdk” contains metadata about the actual harddisk file, which is named “Win2003helper-flat.vmdk”. As you can see we also have a snapshot in place; the .vmsn file contains metadata about the snapshot, and the aforementioned delta disk file is named “Win2003helper-000001-delta.vmdk” (with a matching metafile).
If you’re the curios type, you can do a “cat
Now it’s time to do the magic! Use the “vmkfstool” command to make a clone of the virtual harddrive:
vmkfstools -i Win2003helper.vmdk -d thin Win2003helper_thin.vmdk
The “-d” option tells vmkfstools to make it a thin provisioned disk. The clone will be made from the non-changing snapshot (remember that changes are written to the delta-disk), so we don’t get into trouble with inconsistent drives.
Destination disk format: VMFS thin-provisioned Cloning disk 'Win2003helper.vmdk'... Clone: 100% done.
Wait for the cloning to finish. When it’s done, a “ls -l” will reveal the new disk:
-rw------- 1 root root 8589934592 Oct 7 13:24 Win2003helper_thin-flat.vmdk -rw------- 1 root root 437 Oct 7 13:24 Win2003helper_thin.vmdk
You might be surprised to see that the file size is exactly the same at the bloated disk, you were hoping to shrink – but don’t panic, this is only due to the fact that the service console sees the size of the drive, and not how much space it occupies on the storage. Go ahead and browse the storage from the VMware Infrastructure Client, and you should see the true picture
Go ahead and overwrite the thick disk with the clone just created:
mv -f Win2003helper_thin-flat.vmdk Win2003helper-flat.vmdk
And then remove the obsolete meta file “Win2003helper_thin.vmdk” either with the Datastore Browser or from the command line:
rm -f Win2003helper_thin.vmdk
Finally you can delete the snapshot you created in the Snapshot Manager
Just press “Delete All”, and it will apply all changes that might have happened to the harddrive while we were cloning the disk.
If you take a look at the Datastore Browser, you will now find everything cleaned up, and the virtual disk shrunk to fit the actual usage in the virtual machine.
Of cause the file will grow as you add data to your virtual disk, and if you delete a lot of data, you might consider going thru the procedure again, to reclaim that space.
Back to Original Question