On Fri, Dec 17, 2010 at 5:58 PM, stan <[email protected]> wrote:
> I need to be able to run VBoxManage in system startup/shutdown scripts
> (Linux) where no X server is avaialble. I tried he -nolog flag, but that
> does not seem to be enough to remove it's dependency on X. I am using the
> non OSS version of VirtualBox, if that matters.
>
> How can I do this, or alternatively, how can I ahcieve teh goal of starting
> and stoping VM' at system boot/shutdown?

It's easy. I'm using a custom property "BootOnVBoxStart" and the init
script below. I wish it came packaged together with VirtualBox.

------8<------------------------------------
#!/bin/bash
### BEGIN INIT INFO
# Provides:          vboxvms
# Required-Start:    vboxdrv $syslog
# Required-Stop:     vboxdrv $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start VirtualBox VMs on system boot
# Description:       This scripts starts VBox virtual machines marked as
#                    "start on system boot" and stops them when the system
#                    shuts down
### END INIT INFO

# Author: Pau Garcia i Quiles <[email protected]>
#

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="VirtualBox virtual machines"
NAME=vboxvms
DAEMON=/usr/bin/VBoxManage
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

AVAILABLEVMS=`VBoxManage -q list vms | grep ^\" | cut -d '{' -f 2 |
cut -d '}' -f 1`
RUNNINGVMS=`VBoxManage -q list runningvms | grep ^\" | cut -d '{' -f 2
| cut -d '}' -f 1`

case "$1" in
  start)
        if ! [ -z "$AVAILABLEVMS" ]
        then
          for I in $AVAILABLEVMS
          do
            if [[ ! "$RUNNINGVMS" =~ $I ]]
            then
              BOOTONSTART=`VBoxManage -q getextradata $I 
Machine/BootOnVBoxStart`
              if [[ $BOOTONSTART =~ yes ]]
              then
                VMNAME=`VBoxManage -q list vms | grep $I | cut -d '"' -f 2 `
                [ "$VERBOSE" != no ] && log_daemon_msg "Starting VirtualBox 
$VMNAME ($I)"
                VBoxManage -q startvm $I -type headless
                case "$?" in
                        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
                esac
              fi
            fi
          done
        else
          log_failure_msg "No VirtualBox machines available"
        fi
        ;;
  stop)
        if ! [ -z "$AVAILABLEVMS" ]
        then
          for I in $AVAILABLEVMS
          do
            if [[ "$RUNNINGVMS" =~ $I ]]
            then
              SHUTDOWNONSTOP=`VBoxManage -q getextradata $I 
Machine/BootOnVBoxStart`
              if [[ $SHUTDOWNONSTOP =~ yes ]]
              then
                VMNAME=`VBoxManage -q list vms | grep $I | cut -d '"' -f 2 `
                [ "$VERBOSE" != no ] && log_daemon_msg "Stopping VirtualBox 
$VMNAME ($I)"

                # Try to shutdown properly. If it does not work, poweroff
                VBoxManage -q controlvm $I acpipowerbutton

                case "$?" in
                        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
                esac

                sleep 20

                VBoxManage -q controlvm $I poweroff

                case "$?" in
                        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
                esac
              fi
            fi
          done
        else
          log_failure_msg "No VirtualBox machines available"
        fi
        ;;
  status)
        if ! [ -z "$AVAILABLEVMS" ]
        then
          for I in $AVAILABLEVMS
          do
            STATE=`VBoxManage showvminfo $I | grep State | fmt -u | cut -d
':' -f 2 | cut -d "(" -f 1`
            VMNAME=`VBoxManage -q list vms | grep $I | cut -d '"' -f 2 `
            if [[ $STATE =~ "running" ]]
            then
              log_success_msg "VirtualBox $VMNAME is running"
            else
              log_failure_msg "VirtualBox $VMNAME is $STATE"
            fi
          done
        fi
        ;;
  *)
        echo "Usage $SCRIPTNAME {start|stop|status}" >&2
        exit 3
        ;;
esac

:
------8<------------------------------------


-- 
Pau Garcia i Quiles
http://www.elpauer.org
(Due to my workload, I may need 10 days to answer)

------------------------------------------------------------------------------
Lotusphere 2011
Register now for Lotusphere 2011 and learn how
to connect the dots, take your collaborative environment
to the next level, and enter the era of Social Business.
http://p.sf.net/sfu/lotusphere-d2d
_______________________________________________
VBox-users-community mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/vbox-users-community

Reply via email to