VMWare Workstation as a Service

Subscribe to Syndicate

I have found that you do not need the Workstation GUI up and running on the server in order to run your VM guests. I start the VMs using the vmrun command. I created a vmguests init.d startup script. This script depends on a /etc/sysconfig/vmguests config file to be present. This was created and tested on a RHEL/CentOS 5 and 6 installation,

/etc/sysconfig/vmguests

# VMGUESTS - Configure VM Guests to be auto started and stopped
 
# Virtual Machine home directory
VMHOME="/opt/vmware/Virtual_Machines"
 
# List all guests including subdirectory based on $VMHOME.
# Also, the order entered will be the order started,
# and reversed when stopped.
VMGUESTS=(
VM1/VM1.vmx
VM2/VM2.vmx
)

/etc/rc.d/init.d/vmguests

#!/bin/bash
#
# vmguests      Startup script for the VMWare Guests
#               Author: Mike Bloxham
#               Last updated: 08.02.10
#
# Run after the vmware service starts, and stop before the vmware service stops
# chkconfig: 235 99 07
# description: VMWare is a Virtualisation Application.
# config: /etc/sysconfig/vmguests
 
RUNUSER="runuser -l root -c"
VMRUN="/usr/bin/vmrun -T ws"
SUBSYS="vmguests"
RETVAL=0
 
# Sample /etc/sysconfig/vmguests file:
 
# VMGUESTS - Configure VM Guests to be auto started and stopped
#
# Virtual Machine home directory
#VMHOME="/opt/vmware/Virtual_Machines"
#
# List all guests including subdirectory based on $VMHOME.
# Also, the order entered will be the order started,
# and reversed when stopped.
#VMGUESTS=(
#VM1/VM1.vmx
#VM2/VM2.vmx
#)
# End /etc/sysconfig/vmguests
 
VMHOME=""
unset VMGUESTS
 
[ -f /etc/sysconfig/vmguests ] && . /etc/sysconfig/vmguests
 
start() {
        # Create the subsys lock file even if there are no guests
        # defined in the config file... just in case we manually
        # started some guests.
        [ -d /var/lock/subsys ] || mkdir -p /var/lock/subsys
        touch /var/lock/subsys/"$SUBSYS"
 
        echo -n "Starting VMWare guests: "
        NOSERV=1
        let count=0
        for GUEST in "${VMGUESTS[@]}"; do
                NOSERV=
                if [ $count -eq 0 ]; then echo ""; fi
                if [ $($VMRUN list | grep -c $GUEST) -eq 0 ]; then
                        echo $GUEST
                        #echo "$RUNUSER ${VMRUN} start ${VMHOME}/$GUEST nogui"
                        $RUNUSER "${VMRUN} start ${VMHOME}/$GUEST nogui"
                        sleep 30
                fi
                let count="$count+1"
        done
        RETVAL=$?
        if test -n "$NOSERV"; then echo "no vm guests configured "; fi
}
 
stop() {
        echo -n "Stopping VMWare guests: "
        let count=0
        # First stop the guests listed in /etc/sysconfig/vmguests
        # We do this as the first step in case the order in which
        # shutting down running VMs matters!
        len=${#VMGUESTS[@]}
        let i=0
        if [ $len -gt 0 ]; then
                while (( $i < $len/2 )); do
                        tmp=${VMGUESTS[$i]}
                        VMGUESTS[$i]=${VMGUESTS[$len-$i-1]}
                        VMGUESTS[$len-$i-1]=$tmp
                        let i="$i+1"
                done
                for GUEST in "${VMGUESTS[@]}"; do
                        if [ $count -eq 0 ]; then echo ""; fi
                        # Graceful shutdown
                        if [ $($VMRUN list | grep -c $GUEST) -gt 0 ]; then
                                echo $GUEST
                                #echo "$VMRUN stop $VMHOME/$GUEST soft"
                                $RUNUSER "$VMRUN stop $VMHOME/$GUEST soft"
                        fi
                        # If it is still up, power off the VM
                        if [ $($VMRUN list | grep -c $GUEST) -gt 0 ]; then
                                #echo "$VMRUN stop $VMHOME/$GUEST hard"
                                $RUNUSER "$VMRUN stop $VMHOME/$GUEST hard" >/dev/null 2>&1
                        fi
                        let count="$count+1"
                done
        fi
        # Now shutdown any other running VMs not listed in /etc/sysconfig/vmguests
        # Comment out these lines if you wish to keep other guests running.
        {
                vmrun -T ws list | while read GUEST; do
                        if [ -z "$(echo $GUEST | grep ^Total)" ]; then
                                if [ $count -eq 0 ]; then echo ""; fi
                                echo $GUEST
                                #echo "Failsafe: $VMRUN stop $GUEST soft"
                                #echo "Failsafe: $VMRUN stop $GUEST hard"
                                $RUNUSER "$VMRUN stop $GUEST soft"
                                $RUNUSER "$VMRUN stop $GUEST hard"
                                let count="$count+1"
                        fi
                done
        }
        RETVAL=$?
        echo Done.
}
 
# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        vmrun -T ws list
        RETVAL=$?
        ;;
  restart)
        stop
        start
        ;;
  *)
        echo $"Usage: $prog {start|stop|status|restart}"
        exit 1
esac
 
exit $RETVAL