#! /bin/sh # Oracle VM VirtualBox # Slackware init script # # Modified for Slackware by M. Broek # PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH DEVICE=/dev/vboxdrv GROUPNAME=vboxusers MODPROBE=/sbin/modprobe SCRIPTNAME=/etc/rc.d/rc.virtualbox # Should be a bit longer then the slowest machine needs. # This script kills machines after this timeout. STOPTIME=60 if $MODPROBE -c | grep -q '^allow_unsupported_modules *0'; then MODPROBE="$MODPROBE --allow-unsupported-modules" fi [ -f /etc/vbox/vbox.cfg ] && . /etc/vbox/vbox.cfg [ -f /etc/vbox/autorun.cfg ] && . /etc/vbox/autorun.cfg [ -r /etc/default/virtualbox ] && . /etc/default/virtualbox fail_msg() { echo " ...failed!" echo " ($1)" } begin_msg() { [ -z "${1:-}" ] && return 1 if [ -z "${2:-}" ]; then echo -n "$1" else echo -n "$1: $2" fi } failure() { fail_msg "$1" exit 0 } running() { lsmod | grep -q "$1[^_-]" } start_kernel() { begin_msg "Starting VirtualBox kernel modules " if ! running vboxdrv; then if ! rm -f $DEVICE; then failure "Cannot remove $DEVICE" fi if ! $MODPROBE vboxdrv > /dev/null 2>&1; then failure "modprobe vboxdrv failed. Please use 'dmesg' to find out why" fi sleep .2 fi # ensure the character special exists if [ ! -c $DEVICE ]; then MAJOR=`sed -n 's;\([0-9]\+\) vboxdrv;\1;p' /proc/devices` if [ ! -z "$MAJOR" ]; then MINOR=0 else MINOR=`sed -n 's;\([0-9]\+\) vboxdrv;\1;p' /proc/misc` if [ ! -z "$MINOR" ]; then MAJOR=10 fi fi if [ -z "$MAJOR" ]; then rmmod vboxdrv 2>/dev/null failure "Cannot locate the VirtualBox device" fi if ! mknod -m 0660 $DEVICE c $MAJOR $MINOR 2>/dev/null; then rmmod vboxdrv 2>/dev/null failure "Cannot create device $DEVICE with major $MAJOR and minor $MINOR" fi fi # ensure permissions if ! chown :$GROUPNAME $DEVICE 2>/dev/null; then rmmod vboxnetadp 2>/dev/null rmmod vboxnetflt 2>/dev/null rmmod vboxdrv 2>/dev/null failure "Cannot change owner $GROUPNAME for device $DEVICE" fi if ! $MODPROBE vboxnetflt > /dev/null 2>&1; then failure "modprobe vboxnetflt failed. Please use 'dmesg' to find out why" fi if ! $MODPROBE vboxnetadp > /dev/null 2>&1; then failure "modprobe vboxnetadp failed. Please use 'dmesg' to find out why" fi echo "done" } dmnstatus() { if running vboxdrv; then if running vboxnetflt; then if running vboxnetadp; then echo "VirtualBox kernel modules (vboxdrv, vboxnetflt and vboxnetadp) are loaded." else echo "VirtualBox kernel modules (vboxdrv and vboxnetflt) are loaded." fi else echo "VirtualBox kernel module is loaded." fi for i in $SHUTDOWN_USERS; do # don't create the ipcd directory with wrong permissions! if [ -d /tmp/.vbox-$i-ipc ]; then export VBOX_IPC_SOCKETID="$i" VMS=$(su $i -c "/usr/bin/VBoxManage --nologo list runningvms 2>/dev/null") if [ -n "$VMS" ]; then echo echo "The following VMs are for user '$i' are currently running:" echo "$VMS" fi fi done # Now list system wide autorun machines export VBOX_IPC_SOCKETID="$VBOXHOST" VMS=$(su $VBOXHOST -c "VBoxManage --nologo list runningvms 2>/dev/null") if [ -n "$VMS" ]; then echo echo "The following system wide VMs are currently running:" echo "$VMS" fi else echo "VirtualBox kernel module is not loaded." fi } run_machine() { # Don't start a running machine su $VBOXHOST -c "VBoxManage showvminfo ${VBOX_UUID[$1]} |grep '^State:' |grep 'running' >/dev/null" && { return } # Start the machine echo "Starting VirtualBox machine \"${VBOX_NAME[$i]}\"" su $VBOXHOST -c "VBoxHeadless --startvm ${VBOX_UUID[$1]} --vrdeproperty TCP/Ports=${VBOX_PORT[$1]} 1>/dev/null 2>/dev/null" & sleep .5 } start_autorun() { i=0 export VBOX_IPC_SOCKETID="$VBOXHOST" while [ true ]; do [ "${VBOX_UUID[$i]}" = "" ] && break; if [ "${VBOX_AUTO[$i]}" = "true" ]; then run_machine $i fi i=$(($i+1)) done } stop_autorun() { # See how many machines are defined i=0 while [ true ]; do [ "${VBOX_UUID[$i]}" = "" ] && break; i=$(($i+1)) done # Stop the machines s=0 export VBOX_IPC_SOCKETID="$VBOXHOST" while [ $i -gt 0 ]; do i=$(($i-1)) if [ "${VBOX_AUTO[$i]}" = "true" ]; then su $VBOXHOST -c "VBoxManage --nologo showvminfo ${VBOX_UUID[$i]} |grep '^State:' |grep 'running' >/dev/null" && { echo "Shutdown VirtualBox machine \"${VBOX_NAME[$i]}\"" s=1 su $VBOXHOST -c "VBoxManage --nologo controlvm ${VBOX_UUID[$i]} ${VBOX_STOP[$i]}" 1>/dev/null } fi done if [ $s -gt 0 ]; then # If we stopped some machines using the power button, we must wait until # the shutdown is completed before we can continue. At least one second # is wasted here else module unloading will fail. echo -n "Waiting until all machines are stopped " i=0 while [ $i -lt $STOPTIME ]; do sleep 1 VMS=$(su $VBOXHOST -c "VBoxManage --nologo list runningvms 2>/dev/null") [ -z "$VMS" ] && break; echo -n "." i=$(($i+1)) done if [ $i -ge $STOPTIME ]; then # Still some machines running, do it the hard way... i=$j while [ $i -gt 0 ]; do i=$(($i-1)) if [ "${VBOX_AUTO[$i]}" = "true" ]; then su $VBOXHOST -c "VBoxManage --nologo showvminfo ${VBOX_UUID[$i]} |grep '^State:' |grep 'running' >/dev/null" && { su $VBOXHOST -c "VBoxManage --nologo controlvm ${VBOX_UUID[$i]} poweroff" 1>/dev/null 2>/dev/null } fi done fi echo "done" fi } case "$1" in start) start_kernel start_autorun ;; stop) stop_autorun ;; restart) stop_autorun sleep 1 start_autorun ;; status) dmnstatus ;; *) echo "Usage: $0 {start|stop|restart|status}" esac