I had previously listed some scripts that I use for VMware Workstation management on the Intrinium blog, but have found a few annoyances with running them. So, I’m re-releasing the scripts in their modified form.
The objective performed by these scripts are as follow:
- They provide a simple way to suspend all virtual machines on host shutdown and resume only a select few on host startup.
- They provide a way to check if certain virtual machines are running. If they are not running, the script will send an alert (and attempt to start the machine).
- Backups will occur on a per-machine basis (that can be set to be different than the running list) and will only suspend one machine at a time for backups, starting them back up once the backup is finished. The backup will be sent to a samba share in my example, but this can be configured to be anything you can cp/scp to.
I’ve also modified the scripts to be a little more flexible in terms of modularity (user set directories) and included some error checks. Other than that, you know the routine – test it before you run it and don’t blame me if you delete everything.
First, the startup script I’ve dropped as /etc/init.d/vmwaresuspend (and of course, run “update-rc.d vmwaresuspend defaults”). This script runs on startup and shutdown of the host, making sure to suspend all running virtual machines and start up only machines on the machine list when the host starts back up:
#!/bin/bash ### BEGIN INIT INFO # Provides: vmwaresuspend # Required-Start: # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Required-Stop: 0 1 6 ### END INIT INFO USER="media" #the user to run VMware as MLIST="/media/raid/vmware/machine.list" #the machine list for machines to START on startup (see example below) case "$1" in start) if [ $# -gt 1 ] then su ${USER} -c "vmrun start '$2' nogui" else while read VM; do echo "Starting $VM" su ${USER} -c "vmrun start '$VM' nogui" done < $MLIST fi exit 0 ;; stop) if [ $# -gt 1 ] then su ${USER} -c "vmrun suspend '$2'" else vmrun list|grep vmx$|while read VM do echo "Suspending $VM" su ${USER} -c "vmrun suspend '$VM'" done fi exit 0 ;; restart) # Nothing to be done for restart exit 0 ;; esac exit 0
The machine list is simply the full path to the vmx file for the virtual machine, one per line. Example:
/home/vmware/vm1/vm1.vmx /home/vmware/vm2/vm2.vmx
Next, the script that checks to see if VMs are running. I set the list to be the same one as the startup script above, but you can change it to a different one if you feel like it. I saved this in /root/cron/checkVMs.sh and updated cron to have it run this script every 5 minutes:
#!/bin/bash MLIST="/media/raid/vmware/machine.list" LOCKFILE="/media/raid/vmware/backup.lock" if [ -f ${LOCKFILE} ]; then #checks for the lock file made by backup script exit 0 fi # vmrun list | tail -n+2 | awk -F/ '{print $NF}' | rev | cut -d\. -f2- | rev ...or we can just ps grep :) while read VM; do if ! ps ax | grep -v grep | grep "$VM" > /dev/null then echo "$VM is down!" #include mail -s here if you don't receive output for cron jobs. #include "vmrun start $VM" if you want to start the VM automatically if down # - it might not work due to other factors (rebuilding vmware modules, etc) fi done < $MLIST
Lastly, the backup script. Like with the checking script, you can set a different machine list for it if you are wanting to backup other machines. Do note, this script does try to suspend the machine before backup and then start it after the backup completes - if your machine was stopped and you prefer it to remain stopped after the backup, you will have to change the logic of the script. Otherwise, it will start up. I saved this as /root/cron/backupVMs.sh and set cron to run it every week on Sunday night:
#!/bin/bash LOCKFILE="/media/raid/vmware/backup.lock" BACKUP_DIR="/home/media/vmwarebackup" SMB_DIR="//yourShareServer/vmwarebackup" MLIST="/media/raid/vmware/machine.list" SUSPEND="/etc/init.d/vmwaresuspend" CREDFILE="/root/cron/smbcred" if [ -f $LOCKFILE ]; then echo "A backup is currently in progress" exit 1 fi touch $LOCKFILE mount -t smbfs -o credentials=$CREDFILE $SMB_DIR $BACKUP_DIR if mountpoint -q $BACKUP_DIR #did it mount? If not, bad things could happen to your tiny 60GB SSD drive then find $BACKUP_DIR -mtime +30 | xargs rm -rf #remove backups over 30 days old datetime=$(date '+%d_%m_%y_%H_%M') mkdir $BACKUP_DIR/${datetime} cd $BACKUP_DIR/${datetime} while read VM; do mkdir $(basename ${VM}) ${SUSPEND} stop $VM cp -R $(dirname ${VM})/* ./$(basename ${VM})/ ${SUSPEND} start $VM done < $MLIST umount -l $BACKUP_DIR else echo "Samba share failed to mount: $BACKUP_DIR" fi rm $LOCKFILE
Modify to your hearts content.