Defragment and shrink vmdk files: VMWare virtual disk files Working with VMWare (see
www.vmware.com) is excellent however it suffers more than usual from performance degradation because of disk fragmentation. To defragment and shrink the virtual disk files (*.vmdk) there is a procedure that is somewhat time consuming and difficult to do. I have written a batch file to make it easier to defragment all my VMWare vmdk files. In this post I will take you step by step through the process and explain what all the lines in my batch file do.
I keep all VMWare virtual machines in one subdirectory on a separate partition: G:\Virtual Machines. In this directory I have created the batch files too that defragment the vmdk files.
There are two batch files needed. One that does the whole process. I have called this batch file cleanup.bat. The second batch file is called by cleanup.bat and will shrink a vmdk files: shrink_disk.bat.
The first step in the defragmentation process is to defragment the host disk. The first line of the cleanup.bat file is to delete unwanted log files:
del /S /Q *.log The second step in the cleanup.bat file will call the windows defrag program to defragment the host disk:
defrag g: -f -v In this command -f means force so it will always defragments even if the available space is small. The option -v specifies extended reporting.
Now I need to loop all directories that contain the vmdk files and call the shrink_disk.bat file for all found vmdk files:
for /R %%i IN (*.vmdk) do call shrink_disk "%%i" In this command /R specifies a recursive search. %%i is the variable that will contain the found vmdk file name in the loop.
Note that you should not shrink a vmdk disk with snapshots. Now the shrink_disk.bat file does the dirty work. First the vmdk disk needs to be mounted. The vmware-mount utility can be downloaded from vmware.
vmware-mount m: /d This command ensures the m: drive is dismounted. The m: drive I use for defragging the vmdk file later.
Now we have ensured the vmdk file is not mounted we can defrag it using;
vmware-vdiskmanager -d %1 Note that -d means defragment, %1 will be replaced by the parameter passed to the shrink_disk.bat file: the file name of the vmdk file.
After defragmenting we would like to shrink the disk. This is especially useful when the disks are specified to grow automatically. When deleting files from the virtual disk, the vmdk file will however not automatically shrink.
To shrink the file we first need to mount it. We use the m: drive:
vmware-mount m: %1 Now we mounted the virtual disk we need to prepare the disk for shrinking:
vmware-vdiskmanager -p M: Note that -p means that the disk should be prepared for shrinking.
To defragment the vmdk file it needs to be unmounted again. The next step will unmount the virtual disk file:
vmware-mount m: /d Finally we can shrink the disk:
vmware-vdiskmanager -k %1 Note that -k means shrinking and that %1 is the filename of the vmdk file passed in to the shrink_disk.bat file.
This completes the procedure of defragging and shrinking the vmdk files. For a complete defrag you must now defrag the volumes in the VMWare virtual machine with the normal windows or dos tools.
Summary: To summarise the whole procedure of shrinking and defragging, I use two batch files, the vmware-mount.exe and the vmware-vdiskmanager.exe programs.
cleanup.bat
Code:
del /S /Q *.log
defrag g: -f -v
for /R %%i IN (*.vmdk) do call shrink_disk "%%i"
echo Klaar
pause
Note that you should not shrink a vmdk disk with snapshots. shrink_disk.bat
Code:
vmware-mount m: /d
vmware-vdiskmanager -d %1
vmware-mount m: %1
vmware-vdiskmanager -p M:
vmware-mount m: /d
vmware-vdiskmanager -k %1
First the real hard disk is defragmented.
Then the virtual disks are defragmented and shrunk.
To complete the operation the virtual disks must be defragmented inside the VMWare virtual machine using the normal windows or dos defrag programs.