Start VirtualBox as a service on boot

 

From different sources an adjusted start and stop script for VirtualBox 4 for openSUSE 11.3.
Start or stop a headless installation of VirtualBox box through the command or start VirtualBox as a service on boot.
Too to start Virtualbox on linux as a service:
 
Used
openSUSE 11.3
VirtualBox 4.04
 
 
Create the init.d script VBOX
- create the file "vbox" in the etc\init.d directory.
- put the code in the file
- change the VM_OWNER of the installed virtual machine (important)
 
#!/bin/sh
#
# chkconfig: - 91 35
# description: Starts and stops vbox autostart VMs.

### BEGIN INIT INFO
# Provides: vbox
# Required-Start: $network $named $vboxdrv
# Required-Stop: $network $named
# Default-Start:
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: Autostart some Virtual Box VMs
# Description: Autostart some Virtual Box VMs that are mentioned in /etc/sysconfig/vbox file
### END INIT INFO

#. /etc/rc.d/init.d/functions
. /lib/lsb/init-functions

MANAGE_CMD=VBoxManage
VM_OWNER=vmuser

[ -r /etc/sysconfig/vbox ] && . /etc/sysconfig/vbox

prog=$"Virtual Box Machines"

start()
{
    echo -n $"Starting $prog: "
    RETVAL=0

    for vbox_name in ${VBOX_AUTOSTART}
    do
        SERVS=1
        echo -n "${vbox_name} "
        sudo -u $VM_OWNER $MANAGE_CMD startvm "${vbox_name}" --type headless
      RETVAL=$?
        [ "$RETVAL" -eq 0 ] || break
    done
    if [ -z "$SERVS" ]; then
        echo -n "No virtual machines configured "
        failure
        RETVAL=6
    else
        if [ "$RETVAL" -eq 0 ]; then
        echo "Started vm "${vbox_name}       
        else

        echo "Failed to start vm "${vbox_name}
        fi
    fi
    echo
    return "$RETVAL"
}

stop()
{
    echo -n $"Shutting down $prog: "
    for vbox_name in ${VBOX_AUTOSTART}
    do
       echo -n "${vbox_name}"       

       sudo -u $VM_OWNER $MANAGE_CMD controlvm "${vbox_name}" savestate   

    done
    RETVAL=$?
    [ "$RETVAL" -eq 0 ]
       echo "done vm "${vbox_name}
    return "$RETVAL"
}

status()
{
    for vbox_name in ${VBOX_AUTOSTART}
    do
        echo -n "${vbox_name} "
        sudo -u $VM_OWNER $MANAGE_CMD showvminfo "${vbox_name}"|grep "^State:\s*.*$"
    done
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart|force-reload)
    stop
    start
    ;;
  status)
    status
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}" >&2
    exit 3
    ;;
esac

:
 

 
Create the file for the virtual machines
In the directory  /etc/init.d create a file "vbox" with on each line the name of a virtual machine to start:
virtual_machine_name1
virtual_machine_name2
 
Starting and stopping the virtual machines
 command prompt:
service vbox start
service vbox stop
service vbox status