Defragmenting and Shrinking VMware Workstation Disks
Cleaning up bloated disks is a snap
April 21, 2012
For years, I've used VMware Workstation for software testing, technical support, and application virtualization. I've learned that over the course ofmany cycles of use-particularly OS upgrades-the real disk space that a virtual disk uses can grow well beyond its virtual space, possibly degradingperformance. The VMware Workstation graphical tools can't always recapture this space, but you can clean up bloated disks by using a specific procedureand some downloadable VMware tools.
No Snapshots or Linked Clones
Let's start with an important caveat. If you're using snapshots or linked clones in VMware Workstation, then do not use the procedures that I outlinehere. Virtual machines (VMs) that run from a snapshot or a linked clone run from virtual disks that are overlays for the original VM. If you attempt todefragment the original VM or a clone, you add a great deal of complexity, ensuring that you'll actually experience worse performance. Overlays alreadyminimize the amount of space that's used for virtual disks; you're likely to increase the amount of space that's taken for the cloned disk.
Note that it's OK to use the techniques in this article to clean up what VMware calls full clones. A full clone is created from a complete copy of the original source VM but is not tied to it. If you aren't certain whether a VMware machine is a clone, snapshot,or source, then look at the machine details in VMware Workstation. Clones and snapshots explicitly mention the clone or snapshot source, as Figure 1shows. The machine in Figure 2 is unlinked and simply shows the VMware Workstation version.
Figure 1: Machine details for a clone machine
Figure 2: Machine details for an unlinked machine
Do I Need to Defragment My Virtual Disks?
Fragmentation-related performance and size issues for a virtual disk vary, based on multiple factors. I use two simple rules of thumb.
First, if I think I see a significant performance loss in a VM, then the machine might need cleanup. Second, I compare the size of the Virtual MachineDisk Format (VMDK) file in which the disk resides to the amount of space that the VM internally reports is used for the drive. This comparison willnot work as a measure if your VMware disks use preallocated space. Fixed disks still benefit from defragmentation, but the VMDK file willalways be the maximum possible size for the VM disk.
To find the space that the file uses directly, right-click the VM in the VMware Favorites list, and choose Settings from the context menu. In theVirtual Machine Settings window that opens (as Figure 3 shows), select the hard disk under the Device column on the left (under the Hardware tab).Then, look in the Capacity area in the right half of the window. The Current size value shows the current size of the file on disk. Next, bootthe guest OS and find the amount of space that it claims is in use. If the guest OS reports a significantly smaller usage than is listed for the VMDKfile, you'll probably benefit from defragmenting and shrinking the disk.
Figure 3: Virtual machine settings
Step 1: Clean Up from the Guest OS
The first step is to defragment the disk from within the guest OS. This is probably a good time to perform general maintenance such as OS andapplication updates. (When performed, these updates immediately cause some fragmentation and increase used space.) After defragmentation, shut down theVM.
Step 2: Use VMware Tools to Defragment and Shrink
Although VMware Workstation has options for mounting and shrinking a virtual disk via the Utilities button in the Virtual Machine Settings window,these options rarely have a significant effect on the virtual disk size. For example, I had a Windows 2000 Professional VMDK file with lots of slackspace. The Win2K guest reported that it was using only 2.31GB of space, but the disk file took up 6.6GB. Using the GUI tools did not reduce the spacerequirements on the disk. Instead, I recommend that you use VMware's downloadable command-line tools, vmware-vdiskmanager andvmware-mount, to radically shrink the disks.
To begin, you need to get the tools from the VMware website.You'll need to register with VMware, and then search for the tooldownload. On Windows 7 running VMware Workstation 7.1, I use the VMware Virtual Disk Development Kit (VDDK), which includes both tools. Install the VDDK, and make sure to take note of the folder in which the VDDK is installed. On 32-bit Windows systems, thatfolder is typically something like C:program filesvmwarevmware virtual disk development kitbin; on 64-bit Windows systems, it will be somethinglike C:program files (x86)vmwarevmware virtual disk development kitbin. Now you're ready to start:
1. Use an account with administrative privileges to open a command prompt.
2. Use the command
pushd
to set your current directory to the VDDK folder, as the code at callout A in Listing 1 shows. This step ensures that the VMware tools-and moreimportantly, their supporting DLLs-are at the front of the command search path. If you're running 64-bit Windows, I strongly advise performing thisstep as instructed, even if you think you know a better way. If other paths are searched first, the VMware virtual disk-mounting tool, vmware-mount,frequently fails.
3. Defragment the virtual disk file by using the VMware tools. At the command prompt, run the command
vmware-vdiskmanager
with the -d (for defragment) option and the complete path to the virtual disk file. The code at callout B in Listing 1 shows this command for a VMDKfile at E:win2k prow2kprodisk1.vmdk.
4. At this point, you need to map the file as a disk, by using vmware-mount. You must specify an unused drive letter and the path to the VMDK file. Thecode at callout C in Listing 1 maps drive Z to the W2kProDisk1.vmdk file.
5. To prepare the disk for shrinking, use the command
vmware-vdiskmanager
with the -p (for prepare) option and the drive letter of the drive on which the VMDK file is mounted, not the path to the VMDK file. Using the wrongdrive letter is the second source of problems for first attempts at disk shrinking; this is the only point at which you depend on a drive letter. Thecode at callout D in Listing 1 shows the correct procedure for a file that you've mounted as disk Z. You'll receive a continuously updated percentiledisplay that tells you about the progress in wiping the disk. What's being wiped isn't the disk as a whole, but the blank space in the file.
6. Next, unmount the disk by using the command
vmware-mount -d
as shown at callout E in Listing 1. With this command, "d" is for "dismount."
7. Finally, shrink the VMDK file by using the command
vmware-vdiskmanager -k
as shown at callout F in Listing 1. Here, "k" is a mnemonic for the word "shrink"; the letter "s" is used for another option.
At this point, you're finished. The technique can be extremely effective. The Win2K disk file that I mentioned earlier-the one that took up 6.65GB ofspace when the guest OS was using only 2.3GB of space-shrank to only 2.32GB. You can repeat this process for each guest OS that's taking excessivespace.
Step 3: Defragment the Host Disk
As a final step to improve performance, from the hosting OS, defragment the physical disk on which the VMs reside. If you have performance or spaceproblems because of fragmentation of guest drives, this step should resolve the problem.
Automating Disk Shrink
Although I find it simplest to perform guest OS maintenance manually when needed, the VMware disk defragmentation and shrinking operations can beautomated easily. (They work from the command line anyway.)
The batch file ShrinkVmdk.cmd can be downloaded to automate step 2 of the previous process. The one assumption that this file makes is that drive Z isunused; if this isn't true, you'll want to modify the line
set tmpdrive=Z:
near the top of the batch file, to point to a free drive letter.
After you've installed the VDDK, you can start an elevated command-line window and run the script with the complete path to the VMDK file that you needto defragment. Using the batch file, you can defragment the sample virtual disk file by using the command
shrinkvmdk "E:Win2k ProW2kProDisk1.vmdk"
In my experience, VMware's virtual disks are generally trouble-free and don't regularly need significant cleanup. However, when you do find that avirtual disk is eating up excessive drive space, you can use this process to quickly resolve the problem.
Listing 1: ShrinkVmdk.cmd
# BEGIN Callout A:: WARNING - the next two lines are ONE line that wraps in print.pushd "C:Program Files (x86)VMwareVMware Virtual Disk Development Kitbin"# END Callout A # BEGIN Callout Bvmware-vdiskmanager -d "e:Win2k ProW2kProDisk1.vmdk"# END Callout B # BEGIN Callout Cvmware-mount Z: "e:Win2k ProW2kProDisk1.vmdk"# END Callout C # BEGIN Callout D:: Prepare using the drive letter, not the path.vmware-vdiskmanager -p Z:# END Callout D # BEGIN Callout Evmware-mount -d Z:# END Callout E # BEGIN Callout F:: Now, shrink the VMDK file.vmware-vdiskmanager -k "e:Win2k ProW2kProDisk1.vmdk"# END Callout F
About the Author
You May Also Like