#! /bin/bash # # Author: Andrew Grant # Date: 12/06/2006 # ### BEGIN INIT INFO # Provides: vmware-player # Required-Start: $network VMware xdm # Required-Stop: $network VMware # Default-Start: 5 # Default-Stop: 0 1 2 6 # Description: Run a VMware machine as a service ### END INIT INFO # Variable explanations: # VMMachine = Friendly name of virtual machine # VMDir = Directory path of Virtual Machine # VMFile = Virtual Machine Configuration File (.vmx file) # VMDisplay = Virtual Machine Display (start with 1) # VMTerminal = Virtual Machine Terminal to use (start with 8) # VMDns = DNS name assigned to the Virtual Machine # VMShutdownTimer = Time to wait for VM shutdown (in seconds) # 5 Minutes is normally more than adequate # # Each virtual machine must have its own X display and terminal. # Assuming a standard installation that runs X on display 0 and # vt7, your first virtual machine will use: # X display 1 and vt 8 # The second: # X Display 2 and vt 9 # and so on. # Set this to what you need VMMachine="tapeworm" VMDir="/var/vmserver/Novell Open Enterprise Server" VMFile="SUSE Linux Enterprise Server.vmx" VMDisplay="1" VMTerminal="8" VMDnsName="tapeworm.ru.ac.za" VMShutdownTimer="300" # These shouldn't need to be changed VMFullName="$VMDir/$VMFile" VMBin="/usr/lib/vmware/bin/vmplayer" PID_FILE="/var/run/vmware-$VMMachine.pid" # Set paths export PATH=/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/opt/gnome/bin:/usr/lib/java/bin . /etc/rc.status rc_reset case "$1" in start) echo -n "Starting Virtual Machine ${VMMachine}" if [ ! -f "${VMFullName}" ]; then echo -n >&2 "Virtual Machine not found, ${VMFile} does not exist. " rc_status -s exit 6 fi checkproc -p ${PID_FILE} ${VMBin} case "$?" in 0) echo "- Warning: daemon already running. " ;; 1) echo "- Warning: ${PID_FILE} exists. " ;; esac if [ -f "/tmp/.X${VMDisplay}-lock" ]; then echo "Stale X lock for display ${VMDisplay} found, removing..." rm "/tmp/.X${VMDisplay}-lock" fi startproc -p ${PID_FILE} /usr/X11R6/bin/xinit /usr/bin/vmplayer "${VMFullName}" -- :${VMDisplay} vt${VMTerminal} > /dev/null & rc_status -v ;; stop) checkproc -p ${PID_FILE} ${VMBin} VMStatus=${?} case "$VMStatus" in 1) echo "- Warning: Virtual Machine ${VMMachine} not running but ${PID_FILE} exists. " exit 1 ;; 3) echo "- Warning: Virtual Machine ${VMMachine} not running. " exit 1 ;; esac echo -n "Shutting down Virtual Machine ${VMMachine} - this will take a moment..." /usr/bin/ssh root@${VMDnsName} '/sbin/shutdown -h now' if [ "$?" = 1 ] ; then echo "- Warning: Unable to contact Virtual Machine ${VMMachine} " exit 1 fi timer=0 while [ $? != 3 ] do /bin/sleep 1 let timer=timer+1 if [ "$timer" = "$VMShutdownTimer" ] ; then echo "- Warning: Shutdown of Virtual Machine ${VMMachine} failed (took too long)" exit 1 fi checkproc -p ${PID_FILE} ${VMBin} done echo "Shutdown of ${VMMachine} complete." rc_status -v ;; restart) $0 stop $0 start rc_status ;; status) echo -n "Checking for Virtual Machine ${VMMachine}" checkproc -p ${PID_FILE} ${VMBin} rc_status -v ;; *) echo "Usage: $0 {start|stop|status|restart}" exit 1 ;; esac rc_exit