vboxdrv.sh.in revision d766e56621531afa7cc104d93a53697d293f4bfb
af062818b47340eef15700d2f0211576ba3506eevboxsync#! /bin/sh
af062818b47340eef15700d2f0211576ba3506eevboxsync# Sun xVM VirtualBox
af062818b47340eef15700d2f0211576ba3506eevboxsync# Linux kernel module init script
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync#
af062818b47340eef15700d2f0211576ba3506eevboxsync# Copyright (C) 2006-2007 Sun Microsystems, Inc.
af062818b47340eef15700d2f0211576ba3506eevboxsync#
af062818b47340eef15700d2f0211576ba3506eevboxsync# This file is part of VirtualBox Open Source Edition (OSE), as
af062818b47340eef15700d2f0211576ba3506eevboxsync# available from http://www.virtualbox.org. This file is free software;
af062818b47340eef15700d2f0211576ba3506eevboxsync# you can redistribute it and/or modify it under the terms of the GNU
af062818b47340eef15700d2f0211576ba3506eevboxsync# General Public License (GPL) as published by the Free Software
af062818b47340eef15700d2f0211576ba3506eevboxsync# Foundation, in version 2 as it comes in the "COPYING" file of the
af062818b47340eef15700d2f0211576ba3506eevboxsync# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
af062818b47340eef15700d2f0211576ba3506eevboxsync# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
af062818b47340eef15700d2f0211576ba3506eevboxsync#
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync# chkconfig: 35 30 60
af062818b47340eef15700d2f0211576ba3506eevboxsync# description: VirtualBox Linux kernel module
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync#
4b9d6701570cb98fd36e209314239d104ec584d3vboxsync### BEGIN INIT INFO
4b9d6701570cb98fd36e209314239d104ec584d3vboxsync# Provides: vboxdrv
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync# Required-Start: $syslog
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync# Required-Stop:
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync# Default-Start: 2 3 4 5
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync# Default-Stop: 0 1 6
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync# Short-Description: VirtualBox Linux kernel module
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync### END INIT INFO
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsyncPATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
af062818b47340eef15700d2f0211576ba3506eevboxsyncDEVICE=/dev/vboxdrv
af062818b47340eef15700d2f0211576ba3506eevboxsyncGROUPNAME=vboxusers
af062818b47340eef15700d2f0211576ba3506eevboxsyncLOG="/var/log/vbox-install.log"
af062818b47340eef15700d2f0211576ba3506eevboxsyncNOLSB=%NOLSB%
af062818b47340eef15700d2f0211576ba3506eevboxsyncNETFLT=%NETFLT%
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync[ -f /lib/lsb/init-functions ] || NOLSB=yes
af062818b47340eef15700d2f0211576ba3506eevboxsync[ -f /etc/vbox/vbox.cfg ] && . /etc/vbox/vbox.cfg
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsyncif [ -n "$INSTALL_DIR" ]; then
af062818b47340eef15700d2f0211576ba3506eevboxsync VBOXMANAGE="$INSTALL_DIR/VBoxManage"
af062818b47340eef15700d2f0211576ba3506eevboxsync BUILDVBOXDRV="$INSTALL_DIR/src/vboxdrv/build_in_tmp"
af062818b47340eef15700d2f0211576ba3506eevboxsync BUILDVBOXNETFLT="$INSTALL_DIR/src/vboxnetflt/build_in_tmp"
af062818b47340eef15700d2f0211576ba3506eevboxsyncelse
af062818b47340eef15700d2f0211576ba3506eevboxsync VBOXMANAGE="/usr/lib/%PACKAGE%/VBoxManage"
af062818b47340eef15700d2f0211576ba3506eevboxsync BUILDVBOXDRV="/usr/share/%PACKAGE%/src/vboxdrv/build_in_tmp"
af062818b47340eef15700d2f0211576ba3506eevboxsync BUILDVBOXNETFLT="/usr/share/%PACKAGE%/src/vboxnetflt/build_in_tmp"
af062818b47340eef15700d2f0211576ba3506eevboxsyncfi
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsyncif [ -n "$NOLSB" ]; then
af062818b47340eef15700d2f0211576ba3506eevboxsync if [ -f /etc/redhat-release ]; then
af062818b47340eef15700d2f0211576ba3506eevboxsync system=redhat
af062818b47340eef15700d2f0211576ba3506eevboxsync elif [ -f /etc/SuSE-release ]; then
af062818b47340eef15700d2f0211576ba3506eevboxsync system=suse
af062818b47340eef15700d2f0211576ba3506eevboxsync elif [ -f /etc/gentoo-release ]; then
af062818b47340eef15700d2f0211576ba3506eevboxsync system=gentoo
af062818b47340eef15700d2f0211576ba3506eevboxsync fi
af062818b47340eef15700d2f0211576ba3506eevboxsyncfi
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync[ -r /etc/default/%PACKAGE% ] && . /etc/default/%PACKAGE%
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsyncif [ -z "$NOLSB" ]; then
af062818b47340eef15700d2f0211576ba3506eevboxsync . /lib/lsb/init-functions
af062818b47340eef15700d2f0211576ba3506eevboxsync fail_msg() {
af062818b47340eef15700d2f0211576ba3506eevboxsync echo ""
af062818b47340eef15700d2f0211576ba3506eevboxsync log_failure_msg "$1"
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync succ_msg() {
af062818b47340eef15700d2f0211576ba3506eevboxsync log_success_msg " done."
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync begin_msg() {
af062818b47340eef15700d2f0211576ba3506eevboxsync log_daemon_msg "$@"
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsyncelse
af062818b47340eef15700d2f0211576ba3506eevboxsync if [ "$system" = "redhat" ]; then
. /etc/init.d/functions
fail_msg() {
echo -n " "
echo_failure
echo
echo " ($1)"
}
succ_msg() {
echo -n " "
echo_success
echo
}
elif [ "$system" = "suse" ]; then
. /etc/rc.status
fail_msg() {
rc_failed 1
rc_status -v
echo " ($1)"
}
succ_msg() {
rc_reset
rc_status -v
}
elif [ "$system" = "gentoo" ]; then
. /sbin/functions.sh
fail_msg() {
eerror "$1"
}
succ_msg() {
eend "$?"
}
begin_msg() {
ebegin "$1"
}
if [ "`which $0`" = "/sbin/rc" ]; then
shift
fi
else
fail_msg() {
echo " ...failed!"
echo " ($1)"
}
succ_msg() {
echo " ...done."
}
fi
if [ "$system" != "gentoo" ]; then
begin_msg() {
[ -z "${1:-}" ] && return 1
if [ -z "${2:-}" ]; then
echo -n "$1"
else
echo -n "$1: $2"
fi
}
fi
fi
failure()
{
fail_msg "$1"
exit 0
}
running()
{
lsmod | grep -q "$1[^_-]"
}
start()
{
begin_msg "Starting VirtualBox kernel module"
if ! running vboxdrv; then
if ! find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|grep -q vboxdrv; then
failure "No suitable module for running kernel found"
fi
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 vboxnetflt 2>/dev/null
rmmod vboxdrv 2>/dev/null
failure "Cannot change owner $GROUPNAME for device $DEVICE"
fi
if [ -n "$NETFLT" ]; then
if ! modprobe vboxnetflt > /dev/null 2>&1; then
failure "modprobe vboxnetflt failed. Please use 'dmesg' to find out why"
fi
fi
succ_msg
}
stop()
{
begin_msg "Stopping VirtualBox kernel module"
if running vboxdrv; then
if running vboxnetflt; then
if ! rmmod vboxnetflt 2>/dev/null; then
failure "Cannot unload module vboxnetflt"
fi
fi
if ! rmmod vboxdrv 2>/dev/null; then
failure "Cannot unload module vboxdrv"
fi
if ! rm -f $DEVICE; then
failure "Cannot unlink $DEVICE"
fi
fi
succ_msg
}
# enter the following variables in /etc/default/%PACKAGE%:
# SHUTDOWN_USERS="foo bar"
# check for running VMs of user foo and user bar
# SHUTDOWN=poweroff
# SHUTDOWN=acpibutton
# SHUTDOWN=savestate
# select one of these shutdown methods for running VMs
stop_vms()
{
wait=0
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=`$VBOXMANAGE -nologo list runningvms 2>/dev/null`
if [ -n "$VMS" ]; then
if [ "$SHUTDOWN" = "poweroff" ]; then
begin_msg "Powering off remaining VMs"
for v in $VMS; do
$VBOXMANAGE -nologo controlvm $v poweroff
done
succ_msg
elif [ "$SHUTDOWN" = "acpibutton" ]; then
begin_msg "Sending ACPI power button event to remaining VMs"
for v in $VMS; do
$VBOXMANAGE -nologo controlvm $v acpipowerbutton
wait=30
done
succ_msg
elif [ "$SHUTDOWN" = "savestate" ]; then
begin_msg "Saving state of remaining VMs"
for v in $VMS; do
$VBOXMANAGE -nologo controlvm $v savestate
done
succ_msg
fi
fi
fi
done
# wait for some seconds when doing ACPI shutdown
if [ "$wait" -ne 0 ]; then
begin_msg "Waiting for $wait seconds for VM shutdown"
sleep $wait
succ_msg
fi
}
setup()
{
stop
if find /lib/modules/`uname -r` -name "vboxnetflt\.*" 2>/dev/null|grep -q vboxnetflt; then
begin_msg "Removing old VirtualBox netflt kernel module"
find /lib/modules/`uname -r` -name "vboxnetflt\.*" 2>/dev/null|xargs rm -f 2>/dev/null
succ_msg
fi
if find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|grep -q vboxdrv; then
begin_msg "Removing old VirtualBox kernel module"
find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|xargs rm -f 2>/dev/null
succ_msg
fi
begin_msg "Recompiling VirtualBox kernel module"
if ! $BUILDVBOXDRV \
--save-module-symvers /tmp/vboxdrv-Module.symvers \
--no-print-directory install > $LOG 2>&1; then
failure "Look at $LOG to find out what went wrong"
fi
if [ -n "$NETFLT" ]; then
if ! $BUILDVBOXNETFLT \
--use-module-symvers /tmp/vboxdrv-Module.symvers \
--no-print-directory install >> $LOG 2>&1; then
failure "Look at $LOG to find out what went wrong"
fi
fi
succ_msg
start
}
dmnstatus()
{
if running vboxdrv; then
if running vboxnetflt; then
echo "VirtualBox kernel modules (vboxdrv and vboxnetflt) are loaded."
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=`$VBOXMANAGE -nologo list runningvms 2>/dev/null`
if [ -n "$VMS" ]; then
echo "The following VMs are currently running:"
for v in $VMS; do
echo " $v"
done
fi
fi
done
else
echo "VirtualBox kernel module is not loaded."
fi
}
case "$1" in
start)
start
;;
stop)
stop_vms
stop
;;
stop_vms)
stop_vms
;;
restart)
stop && start
;;
force-reload)
stop
start
;;
setup)
setup
;;
status)
dmnstatus
;;
*)
echo "Usage: $0 {start|stop|stop_vms|restart|force-reload|status|setup}"
exit 1
esac
exit 0