Linker IT Software
Google
Web www.oraxcel.com
menubar-top-links menubar-top-rechts
Home Help Search Login
Welcome, Guest. Please Login.
SQL*XL: Database to Excel bridge litLIB: Excel power functions pack ExcelLock: Locking and securing your valuable Excel spreadsheets encOffice: Protect your Excel file easy and safe encOffice: Protect your Excel file easy and safe
Pages: 1
Defragment and shrink VMWare Virtual Disks (Read 61471 times)
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 10
Defragment and shrink VMWare Virtual Disks
09.04.06 at 08:29:16
 
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.
Back to top
 
« Last Edit: 18.04.07 at 07:24:10 by Gerrit-Jan Linker »  

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
continuum
YaBB Newbies
*


I Love SQL*XL

Posts: 0
Re: Defragment and shrink VMWare Virtual Disks
Reply #1 - 17.04.07 at 23:43:43
 
Hi
 
please do all potential users of your batchs a favour:
 
put a FAT WARNING on it that warns against usage on vmdks with snapshots!!!
 
Please see this post in VMTN to see what I mean
http://www.vmware.com/community/thread.jspa?threadID=80802&tstart=0
 
 
Thanks
 
Ulli  
http://sanbarrow.com
Back to top
 
« Last Edit: 17.04.07 at 23:44:53 by continuum »  
Email   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 10
Re: Defragment and shrink VMWare Virtual Disks
Reply #2 - 18.04.07 at 07:28:17
 
Apartenly, disks with snapshots may have a problem when using the above defrag script.  I have marked my post with a warning in red.
Back to top
 
 

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
continuum
YaBB Newbies
*


I Love SQL*XL

Posts: 0
Re: Defragment and shrink VMWare Virtual Disks
Reply #3 - 18.04.07 at 15:28:46
 
Thanks - hey - what about making the batch more idiot-proof ?
 
I really like the idea behind it.
 
You could scan each directory for vmdk-files - and if you find files with
*-00000*.vmdk  
or *REDO*.vmdk
you could warn or abort.
 
Better abort !
 
 
Also it would be nice if you would exclude *-flat.vmdks and *-s***.vmdks from the processing.
 
Ulli
Back to top
 
 
Email   IP Logged
Pages: 1