Friday, March 12, 2010

Configure VirtualBox Deamon

A while back I switched from VMware to VirtualBox(when switching to freebsd) and ran into a problem getting virtual machines to start on server boot. Virtual box supports background operation(headless mode) but freebsd does not come with and rc service for doing so. Included below is a freebsd version to accomplish startup/shutdown task as well as other common operations.

To install save this script into /usr/local/etc/rc.d/virtualbox and make it executable. Then add the following lines to your /etc/rc.conf as needed.
# Load the VirtualBox drivers and start any default machines.
vboxnet_enable="YES" # Enable drivers from package virtualbox-ose-kmod.
virtualbox_enable="YES"
virtualbox_autostart="ASG" # List of machine names to start on boot.
virtualbox_user="vboxuser"
virtualbox_group="vboxusers"

A few other notes: I actually set up a separate account for VirtualBox and the daemon supports this. Also please note that the autostart list is done by name and the names can not have spaces. You can also manually start and stop machines with the startvm and stopvm options.

/usr/local/etc/rc.d/virtualbox
#!/bin/sh
#

# PROVIDE: virtualbox 
# REQUIRE: LOGIN vboxnet

#
# Add the following lines to /etc/rc.conf to enable the virtualbox daemon:
#
# virtualbox_enable="YES"
# virtualbox_autostart="BOX1 BOX2"
#


. /etc/rc.subr

name="virtualbox"
rcvar=`set_rcvar`
extra_commands="status startvm stopvm"

start_cmd="${name}_start"
stop_cmd="${name}_start"
status_cmd="${name}_status"
startvm_cmd="${name}_startvm"
stopvm_cmd="${name}_stopvm"
poweroffvm_cmd="${name}_poweroffvm"

load_rc_config $name

virtualbox_user=${virtualbox_user:-"vboxuser"}
virtualbox_group=${virtualbox_group:-"vboxusers"}
virtualbox_autostart=${virtualbox_autostart:-""}

SU="su $virtualbox_user -c"
VBOXMANAGE="/usr/local/bin/VBoxManage -nologo"

virtualbox_start()
{
        echo "Starting ${name}."

        echo $virtualbox_autostart | awk -F , '{for(i=1;i<=NF;i++) print $i}' |  while read VM; do
            $SU "$VBOXMANAGE startvm \"$VM\" --type headless" 
        done
}

virtualbox_stop()
{
        echo "Stopping ${name}."
        $SU "$VBOXMANAGE list runningvms" | sed 's/.*"\(.*\)".*/\1/' | while read VM; do
            $SU "$VBOXMANAGE controlvm \"$VM\" acpipowerbutton"
        done

        wait_for_closing_machines
}

virtualbox_startvm()
{
        $SU "$VBOXMANAGE startvm \"$*\" --type headless"
}

virtualbox_stopvm()
{
        $SU "$VBOXMANAGE controlvm \"$*\" acpipowerbutton"
}

virtualbox_poweroffvm()
{
        $SU "$VBOXMANAGE controlvm \"$*\" poweroff"
}

virtualbox_status()
{

        $SU "$VBOXMANAGE list runningvms" | sed 's/.*"\(.*\)".*/\1/' | while read VM; do
            echo "$VM "
        done
}

wait_for_closing_machines() {
    RUNNING_MACHINES=`$SU "$VBOXMANAGE list runningvms" | wc -l`
    if [ $RUNNING_MACHINES != 0 ]; then
        sleep 5
        wait_for_closing_machines
    fi
}

run_rc_command "$@"

1 comment:

lauren said...

I was given an assignment to configure VirtualBox Deamon.I was searching for a detailed blog that can guide me on this.Thanks to you for help it saved me a lot of time.
sharepoint electronic signature