When we work with thin provisioned virtual machines we want the virtual machine to be as storage efficient as possible, especially when we make the virtual machine portable by using ovftool or exporting the machine.
When working with Windows virtual machines we also encounter storage excesses due to the the WinSxS folder, which Microsoft states that ” the component store (WinSxS folder) contains all the components that make-up Windows to allow you operate your system.” So if you need to uninstall or re-install a component of the OS you can easily do so since this directory retains all version installed or previously installed, including service packs and updates.
It is my experience that the WinSxS folder takes up way too much space. I have observed over 10 GB reduced ova size by using the following tools and procedure.
The first thing I do is remove the excessive stuff in the WinSxS directory. This can be done by using a tool for Deployment Imaging Service and Management called dism.exe (DISM).
The following command is the one I prefer using to clean out all applied updates and service packs:
Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase
Clean the Drive
Once the WinSxS is cleaned up I like to run the disk cleanup tool. This tool can be found by typing Disk Cleanup.
In the disk cleanup tool you will want to select as many of the items as you deem necessary.
Next you will need to defrag your drives. The first pass is a straight up defrag like the following:
The next defrag is for the whitespace. This is to get as much contiguous free (white) space as possible for our next step.
defrag c: /X
Remove white space
For this portion of the process we will need a tool called sdelete from sysinternals tools which is a part of Microsoft.
Just execute the following to zero the white space on the disk so it is ready to be trimmed by a hypervisor tool or technique.
sdelete -z c:
Trimming the white space
Trimming the white space can be done by using the Storage vMotion in ESXi by moving the virtual machine from datastore to datastore while setting the thin disk. There are other methods to trim white space, VMware Workstation or Fusion as well as vmkfstools.
The easiest for one-offs have been the Storage vMotion as you can select the same datastore as it currently resides upon and migrate it easily, just don’t forget to trim it.
Now we can export the virtual machine as ovf or ova.
- DISM Cleanup – https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/clean-up-the-winsxs-folder
- DISM Complete – https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/deployment-image-servicing-and-management–dism–command-line-options
- Disk cleanup – Use Disk Cleanup
- defrag – https://technet.microsoft.com/en-us/library/ff458356.aspx
- sdelete – https://docs.microsoft.com/en-us/sysinternals/downloads/sdelete
- vmkfstools – https://blah.cloud/infrastructure/zero-free-space-using-sdelete-shrink-thin-provisioned-vmdk/
- VMwareToolboxCmd – http://www.vmwarebits.com/workstationcommandlineshrinkwindows