RSS
 

How to Automate VM Shrink and Defrag with sdelete and contig

10 Dec

Now I’m certainly not the first to write an article on this topic (or any of the topics on this site) but I do like to put all the research from several locations together for a really nice solution.

VMs are a little rocky when it comes to performance sometimes. When your ESX servers are maxed out you need to take all the performance breaks you can get. What we are going to do is create a batch files with three steps. One that automatically defragments the VM, one that shrinks the VM, and one that reboots the VM.

Why?

Defragment a VM
– Like your physical computer, a VM will run smoother and quicker with a defragmented drive (to a point).

Shrink a VM – Whether you manually use VM Tools shrink utility or something else to zero fill the empty space on a VM this will cause the VMDK files to be smaller when backing up or snap shotting. This is because when you ‘delete’ a file in Windows the data is still on the disk, it is just designated as free space. Unless you fill the area on the disk that is free with zeros it will be backed up, increasing your backup size and window.


Reboot a VM
- Like your physical computer, a VM will run smoother and quicker after a reboot. That and users will forget to log off and create stale connections.

Step 1. What you Need
1. Download Contig from systernals (a very efficient command line disk defrag utility)
2. Download sdelete from systernals (a very efficient command line utility to zero-fill free space)
3. Create a batch file, vmmaintenance.bat

Step 2. What you Do
Put the 3 files from above, vmmaintenance.bat, contig.exe and sdelete.exe, into a folder, vm maintenance. I will be copying this folder to the Administrator My Documents folder (C:\Documents and Settings\Administrator\My Documents\vm maintenance\) on each of my VMs for consistency of scheduling this.

The batch file looks like this:


@echo off
cls
echo '
echo '
echo *************************************
echo *************************************
echo Defragmentting VM
echo It is recommended you let this finish
echo *************************************
echo *************************************
echo '
echo '

"C:\Documents and Settings\Administrator\My Documents\vm maintenance\contig.exe" -s -q C:\*

cls
echo '
echo '
echo *************************************
echo *************************************
echo Compressing Free Space on VM
echo It is recommended you let this finish
echo *************************************
echo *************************************
echo '
echo '

"C:\Documents and Settings\Administrator\My Documents\vm maintenance\sdelete.exe" -s -c C:\

cls
echo '
echo '
echo *************************************
echo *************************************
echo Rebooting VM
echo It is recommended you let this finish
echo *************************************
echo *************************************
echo '
echo '

shutdown -r -t 1800 -c "Weekly VM Reboot - You may cancel this by hitting any key in the command prompt window"

cls
echo '
echo '
echo *************************************
echo *************************************
echo Press any key while in this window to
echo Cancel the rebooting of this VM
echo *************************************
echo *************************************
echo '
echo '

Pause

shutdown -a

Now a lot of this batch file is just informing the remote user connected to the vm (if there is anyone connected) what is going on.

Step 3. Break Down the Batch File
“C:\Documents and Settings\Administrator\My Documents\vm maintenance\contig.exe” -s -q C:\*

-s will cause the command to move through all directories recursively.
-q will cause the command to run in quiet mode. It still informs the user what is happening but does not echo every file it touches.
C:\* indicates every file under the C:\ drive

…once that command completes

“C:\Documents and Settings\Administrator\My Documents\vm maintenance\sdelete.exe” -s -c C:\

-s will cause the command to move through all directories recursively.
-c will cause the command to only zero fill the free space (this program can also be used to securely delete files… don’t forget -c!!) *In fact, a note on this. RTFM-ed discusses how to rewrite the source for sdelete to eliminate the ability of the program to delete files. If you can find the source give it shot. I was able to find the source and follow the steps but on one of my VMs the new sdelete would not stop at 100% and ran indefinitely. *

…once that command completes

shutdown -r -t 1800 -c “Weekly VM Reboot – You may cancel this by running shutdown -a”

The built in Shutdown command is issued

-r signifies a reboot after shutting down
-t signifies the amount of time in seconds to wait before shutting down
-c will display what follows as a comment in the shutdown window

In this case I give the users 30 minutes to have the opportunity to abort the reboot in case they happen to be on the VM during the time this runs (hence all the echos informing the user of what’s going on).

… and finally

Pause

shutdown -a

Because the shutdown command does not have a cancel button I have the batch file Pause. If the user is not there the shutdown command will complete in 30 minutes (1800 seconds). If they are there they can hit any key in the command prompt window and the abort shutdown command will be issued (shutdown -a).

Take a look at my post: Remotely Copy Files and Schedule Them via Command Line to see how to easily set this up on numerous VMs from command line.


 
6 Comments

Posted in IT

 

Tags: , , , , , , ,

Leave a Reply

 

 
  1. Dave Abrahams

    June 15, 2011 at 9:14 pm

    Thanks for putting this together. I tried to set this up as a “Scheduled Task” on a Windows Server 2008 R2 VM, and I couldn’t really tell that it was working. Contig didn’t seem to be making any progress, and there was no window where I could monitor the output. Any hints?

     
  2. Dave Abrahams

    June 15, 2011 at 9:48 pm

    Hmm, now it’s working. One part of it was that I needed to check “Run with highest privileges.” Another part is that I had to run it by hand once to get past the EULA dialogs before it could be automated. That could have implications for your other post about remotely automating this.

     
  3. wattsup

    June 15, 2011 at 10:57 pm

    Hey Dave, thanks for the post. VERY good point, you need to run contig manually now that there is a EULA. I imagine the need to check “Run with highest privileges” is specific to Windows 2008 and possibly Windows 7. I really appreciate you giving some feedback. Post here any time.

     
  4. Dave Abrahams

    July 12, 2011 at 6:09 pm

    Thanks for the generous reception. Just to be clear, you only need to run in manually *once*. It can be automated thereafter.

     
  5. Anonymous

    September 3, 2011 at 12:39 am

    Have you found the reason why sdelete runs indefinitely on some VMs and runs beyond 100% when wiping/zeroing free space? It’s happening on my VMs too.

     
  6. wattsup

    November 1, 2011 at 12:49 pm

    I haven’t seen that issue, what is the exact command you are running?