vboxdrv.sh.in revision b39c3fa81cadaec00ebb2e7170a8db96998b7032
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#! /bin/sh
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# Oracle VM VirtualBox
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# Linux kernel module init script
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# Copyright (C) 2006-2010 Oracle Corporation
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# This file is part of VirtualBox Open Source Edition (OSE), as
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# available from http://www.virtualbox.org. This file is free software;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# you can redistribute it and/or modify it under the terms of the GNU
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# General Public License (GPL) as published by the Free Software
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# Foundation, in version 2 as it comes in the "COPYING" file of the
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# chkconfig: 35 30 70
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# description: VirtualBox Linux kernel module
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync### BEGIN INIT INFO
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# Provides: vboxdrv
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# Required-Start: $syslog
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# Required-Stop:
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# Default-Start: 2 3 4 5
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# Default-Stop: 1
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# Short-Description: VirtualBox Linux kernel module
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync### END INIT INFO
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncPATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncDEVICE=/dev/vboxdrv
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncLOG="/var/log/vbox-install.log"
7e032664d31552364e83b411950d6e7c96b0b880vboxsyncNOLSB=%NOLSB%
49ad08ab15f3b96d2d5ba5134a0506dd7b27fdf1vboxsyncDEBIAN=%DEBIAN%
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncMODPROBE=/sbin/modprobe
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
49ad08ab15f3b96d2d5ba5134a0506dd7b27fdf1vboxsyncif $MODPROBE -c | grep -q '^allow_unsupported_modules *0'; then
49ad08ab15f3b96d2d5ba5134a0506dd7b27fdf1vboxsync MODPROBE="$MODPROBE --allow-unsupported-modules"
49ad08ab15f3b96d2d5ba5134a0506dd7b27fdf1vboxsyncfi
f22cba796fd7499bf85058671a1af7cbe491c622vboxsync
f22cba796fd7499bf85058671a1af7cbe491c622vboxsync[ -f /lib/lsb/init-functions ] || NOLSB=yes
f22cba796fd7499bf85058671a1af7cbe491c622vboxsync[ -f /etc/vbox/vbox.cfg ] && . /etc/vbox/vbox.cfg
f22cba796fd7499bf85058671a1af7cbe491c622vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncif [ -n "$INSTALL_DIR" ]; then
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync VBOXMANAGE="$INSTALL_DIR/VBoxManage"
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync DODKMS="$INSTALL_DIR/src/vboxhost/do_dkms"
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync BUILDVBOXDRV="$INSTALL_DIR/src/vboxhost/vboxdrv/build_in_tmp"
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync BUILDVBOXNETFLT="$INSTALL_DIR/src/vboxhost/vboxnetflt/build_in_tmp"
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync BUILDVBOXNETADP="$INSTALL_DIR/src/vboxhost/vboxnetadp/build_in_tmp"
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncelse
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync VBOXMANAGE="/usr/lib/%PACKAGE%/VBoxManage"
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync DODKMS="/usr/share/%PACKAGE%/src/vboxhost/do_dkms"
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync BUILDVBOXDRV="/usr/share/%PACKAGE%/src/vboxhost/vboxdrv/build_in_tmp"
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync BUILDVBOXNETFLT="/usr/share/%PACKAGE%/src/vboxhost/vboxnetflt/build_in_tmp"
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync BUILDVBOXNETADP="/usr/share/%PACKAGE%/src/vboxhost/vboxnetadp/build_in_tmp"
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncfi
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# silently exit if the package was uninstalled but not purged,
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync# applies to Debian packages only
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync[ -z "$DEBIAN" -o -x $VBOXMANAGE -a -x $BUILDVBOXDRV ] || exit 0
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncif [ -n "$NOLSB" ]; then
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync if [ -f /etc/redhat-release ]; then
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync system=redhat
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync elif [ -f /etc/SuSE-release ]; then
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync system=suse
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync elif [ -f /etc/gentoo-release ]; then
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync system=gentoo
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync fi
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncfi
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync[ -r /etc/default/%PACKAGE% ] && . /etc/default/%PACKAGE%
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncif [ -z "$NOLSB" ]; then
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync . /lib/lsb/init-functions
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync fail_msg() {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync echo ""
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync log_failure_msg "$1"
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync }
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync succ_msg() {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync log_end_msg 0
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync }
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync begin_msg() {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync log_daemon_msg "$@"
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync }
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncelse
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync if [ "$system" = "redhat" ]; then
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync . /etc/init.d/functions
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync fail_msg() {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync echo -n " "
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync echo_failure
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync echo
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync echo " ($1)"
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync }
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync succ_msg() {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync echo -n " "
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync echo_success
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync echo
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync }
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync elif [ "$system" = "suse" ]; then
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync . /etc/rc.status
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync fail_msg() {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync rc_failed 1
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync rc_status -v
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync echo " ($1)"
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync }
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync succ_msg() {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync rc_reset
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync rc_status -v
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync }
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync elif [ "$system" = "gentoo" ]; then
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync if [ -f /sbin/functions.sh ]; then
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync . /sbin/functions.sh
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync elif [ -f /etc/init.d/functions.sh ]; then
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync . /etc/init.d/functions.sh
bf5c9a8b30aeef751e52cfe8a66250caab2fd6a2vboxsync fi
71c944130c141978d2c36c720ff831a2105a63d0vboxsync fail_msg() {
71c944130c141978d2c36c720ff831a2105a63d0vboxsync eerror "$1"
bf5c9a8b30aeef751e52cfe8a66250caab2fd6a2vboxsync }
7e032664d31552364e83b411950d6e7c96b0b880vboxsync succ_msg() {
f22cba796fd7499bf85058671a1af7cbe491c622vboxsync eend "$?"
f22cba796fd7499bf85058671a1af7cbe491c622vboxsync }
71c944130c141978d2c36c720ff831a2105a63d0vboxsync begin_msg() {
f22cba796fd7499bf85058671a1af7cbe491c622vboxsync ebegin "$1"
ca551aca153d6df494985b5281c573ba2e3eb474vboxsync }
10183353efa12aa94e0f3dc78e0f1c47f7928e8dvboxsync if [ "`which $0`" = "/sbin/rc" ]; then
ca551aca153d6df494985b5281c573ba2e3eb474vboxsync shift
af8d59d05d72f134aeea62712f1286b369807d52vboxsync fi
49ad08ab15f3b96d2d5ba5134a0506dd7b27fdf1vboxsync else
af8d59d05d72f134aeea62712f1286b369807d52vboxsync fail_msg() {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync 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 modules"
if [ -d /proc/xen ]; then
failure "Running VirtualBox in a Xen environment is not supported"
fi
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 :%GROUP% $DEVICE 2>/dev/null; then
rmmod vboxnetadp 2>/dev/null
rmmod vboxnetflt 2>/dev/null
rmmod vboxdrv 2>/dev/null
failure "Cannot change group %GROUP% 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
succ_msg
}
stop()
{
begin_msg "Stopping VirtualBox kernel modules"
if running vboxnetadp; then
if ! rmmod vboxnetadp 2>/dev/null; then
failure "Cannot unload module vboxnetadp"
fi
fi
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 | sed -e 's/^".*".*{\(.*\)}/\1/' 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_script
setup()
{
stop
begin_msg "Uninstalling old VirtualBox DKMS kernel modules"
$DODKMS uninstall > $LOG
succ_msg
if find /lib/modules/`uname -r` -name "vboxnetadp\.*" 2>/dev/null|grep -q vboxnetadp; then
begin_msg "Removing old VirtualBox netadp kernel module"
find /lib/modules/`uname -r` -name "vboxnetadp\.*" 2>/dev/null|xargs rm -f 2>/dev/null
succ_msg
fi
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 "Trying to register the VirtualBox kernel modules using DKMS"
if ! $DODKMS install >> $LOG; then
fail_msg "Failed, trying without DKMS"
begin_msg "Recompiling VirtualBox kernel modules"
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 ! $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
if ! $BUILDVBOXNETADP \
--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
rm -f /etc/vbox/module_not_compiled
succ_msg
start
}
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=`$VBOXMANAGE --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/' 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