#! /bin/sh # vboxcontrol Startup script for VirtualBox Virtual Machines # # chkconfig: 345 98 02 # description: Manages VirtualBox VMs # processname: vboxcontrol # # pidfile: /var/run/vboxcontrol/vboxcontrol.pid # ### BEGIN INIT INFO # ### END INIT INFO # # Version 20090301 by Kevin Swanson based on: # Version 2008051100 by Jochem Kossen # http://farfewertoes.com # # Released in the public domain # # This file came with a README file containing the instructions on how # to use this script. # # Source function library. if [ -f /etc/init.d/functions ] ; then . /etc/init.d/functions elif [ -f /etc/rc.d/init.d/functions ] ; then . /etc/rc.d/init.d/functions else exit 1 fi ################################################################################ # INITIAL CONFIGURATION VBOXDIR="/etc/virtualbox" VM_USER="virtualizacion" export PATH="${PATH:+$PATH:}/bin:/usr/bin:/usr/sbin:/sbin" SU="su $VM_USER -c" VBOXMANAGE="VBoxManage -nologo" ################################################################################ # FUNCTIONS # Determine if USE_NAT is set to "yes" #use_nat() { #if [ "$USE_NAT" = "yes" ]; then #return `true` #else #return `false` #fi #} log_failure_msg() { echo $1 } log_action_msg() { echo $1 } # Check for running machines every few seconds; return when all machines are # down wait_for_closing_machines() { RUNNING_MACHINES=`$SU "$VBOXMANAGE list runningvms" | egrep -o "^\"[a-zA-Z0-9]*\"" | wc -l` while [ "$RUNNING_MACHINES" != 0 ];do RUNNING_MACHINES=`$SU "$VBOXMANAGE list runningvms" | egrep -o "^\"[a-zA-Z0-9]*\"" | wc -l` sleep 3 done } ################################################################################ # RUN case "$1" in start) if [ -f /etc/virtualbox/machines_enabled ]; then cat /etc/virtualbox/machines_enabled | while read VM; do log_action_msg "Starting VM: $VM ..." $SU "$VBOXMANAGE startvm "$VM" -type headless" RETVAL=$? done touch /var/lock/subsys/vboxcontrol fi ;; stop) # NOTE: this stops all running VM's. Not just the ones listed in the config $SU "$VBOXMANAGE list runningvms" | egrep -o "^\"[a-zA-Z0-9]*\"" | while read VM; do log_action_msg "Shutting down VM: $VM ..." $SU "$VBOXMANAGE controlvm $VM acpipowerbutton" done rm -f /var/lock/subsys/vboxcontrol wait_for_closing_machines ;; start-vm) log_action_msg "Starting VM: $2 ..." $SU "$VBOXMANAGE startvm "$2" -type vrdp" ;; stop-vm) log_action_msg "Stopping VM: $2 ..." $SU "$VBOXMANAGE controlvm "$2" acpipowerbutton" ;; poweroff-vm) log_action_msg "Powering off VM: $2 ..." $SU "$VBOXMANAGE controlvm "$2" poweroff" ;; status) echo "The following virtual machines are currently running:" $SU "$VBOXMANAGE list runningvms" | while read VM; do echo -n "$VM (" echo -n `$SU "VBoxManage showvminfo ${VM%% *}|grep Name:|sed -e 's/^Name:s*//g'"` echo ')' done ;; *) echo "Usage: $0 {start|stop|status|start-vm |stop-vm |poweroff-vm }" exit 3 esac exit 0