VirtualBox

source: vbox/trunk/src/VBox/Installer/linux/vboxdrv.sh.in@ 41883

Last change on this file since 41883 was 39358, checked in by vboxsync, 13 years ago

Installers/linux: make do_dkms take arguments instead of requiring patching.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 12.2 KB
Line 
1#! /bin/sh
2# Oracle VM VirtualBox
3# Linux kernel module init script
4
5#
6# Copyright (C) 2006-2010 Oracle Corporation
7#
8# This file is part of VirtualBox Open Source Edition (OSE), as
9# available from http://www.virtualbox.org. This file is free software;
10# you can redistribute it and/or modify it under the terms of the GNU
11# General Public License (GPL) as published by the Free Software
12# Foundation, in version 2 as it comes in the "COPYING" file of the
13# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15#
16
17# chkconfig: 35 20 80
18# description: VirtualBox Linux kernel module
19#
20### BEGIN INIT INFO
21# Provides: vboxdrv
22# Required-Start: $syslog
23# Required-Stop:
24# Default-Start: 2 3 4 5
25# Default-Stop: 0 1 6
26# Short-Description: VirtualBox Linux kernel module
27### END INIT INFO
28
29PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
30DEVICE=/dev/vboxdrv
31LOG="/var/log/vbox-install.log"
32NOLSB=%NOLSB%
33DEBIAN=%DEBIAN%
34VERSION=%VERSION%
35MODPROBE=/sbin/modprobe
36
37if $MODPROBE -c | grep -q '^allow_unsupported_modules *0'; then
38 MODPROBE="$MODPROBE --allow-unsupported-modules"
39fi
40
41[ -f /lib/lsb/init-functions ] || NOLSB=yes
42[ -f /etc/vbox/vbox.cfg ] && . /etc/vbox/vbox.cfg
43export BUILD_TYPE
44export USERNAME
45export USER=$USERNAME
46
47if [ -n "$INSTALL_DIR" ]; then
48 VBOXMANAGE="$INSTALL_DIR/VBoxManage"
49 MODULE_SRC="$INSTALL_DIR/src/vboxhost"
50else
51 VBOXMANAGE="/usr/lib/%PACKAGE%/VBoxManage"
52 MODULE_SRC="/usr/share/%PACKAGE%/src/vboxhost"
53fi
54BUILDINTMP="$MODULE_SRC/build_in_tmp"
55DODKMS="$MODULE_SRC/do_dkms"
56
57# silently exit if the package was uninstalled but not purged,
58# applies to Debian packages only
59[ -z "$DEBIAN" -o -x $VBOXMANAGE -a -x $BUILDINTMP ] || exit 0
60
61if [ -n "$NOLSB" ]; then
62 if [ -f /etc/redhat-release ]; then
63 system=redhat
64 elif [ -f /etc/SuSE-release ]; then
65 system=suse
66 elif [ -f /etc/gentoo-release ]; then
67 system=gentoo
68 fi
69fi
70
71[ -r /etc/default/%PACKAGE% ] && . /etc/default/%PACKAGE%
72
73if [ -z "$NOLSB" ]; then
74 . /lib/lsb/init-functions
75 fail_msg() {
76 echo ""
77 log_failure_msg "$1"
78 }
79 succ_msg() {
80 log_end_msg 0
81 }
82 begin_msg() {
83 log_daemon_msg "$@"
84 }
85else
86 if [ "$system" = "redhat" ]; then
87 . /etc/init.d/functions
88 fail_msg() {
89 echo -n " "
90 echo_failure
91 echo
92 echo " ($1)"
93 }
94 succ_msg() {
95 echo -n " "
96 echo_success
97 echo
98 }
99 elif [ "$system" = "suse" ]; then
100 . /etc/rc.status
101 fail_msg() {
102 rc_failed 1
103 rc_status -v
104 echo " ($1)"
105 }
106 succ_msg() {
107 rc_reset
108 rc_status -v
109 }
110 elif [ "$system" = "gentoo" ]; then
111 if [ -f /sbin/functions.sh ]; then
112 . /sbin/functions.sh
113 elif [ -f /etc/init.d/functions.sh ]; then
114 . /etc/init.d/functions.sh
115 fi
116 fail_msg() {
117 eerror "$1"
118 }
119 succ_msg() {
120 eend "$?"
121 }
122 begin_msg() {
123 ebegin "$1"
124 }
125 if [ "`which $0`" = "/sbin/rc" ]; then
126 shift
127 fi
128 else
129 fail_msg() {
130 echo " ...failed!"
131 echo " ($1)"
132 }
133 succ_msg() {
134 echo " ...done."
135 }
136 fi
137 if [ "$system" != "gentoo" ]; then
138 begin_msg() {
139 [ -z "${1:-}" ] && return 1
140 if [ -z "${2:-}" ]; then
141 echo -n "$1"
142 else
143 echo -n "$1: $2"
144 fi
145 }
146 fi
147fi
148
149failure()
150{
151 fail_msg "$1"
152 exit 0
153}
154
155running()
156{
157 lsmod | grep -q "$1[^_-]"
158}
159
160start()
161{
162 begin_msg "Starting VirtualBox kernel modules"
163 if [ -d /proc/xen ]; then
164 failure "Running VirtualBox in a Xen environment is not supported"
165 fi
166 if ! running vboxdrv; then
167 if ! rm -f $DEVICE; then
168 failure "Cannot remove $DEVICE"
169 fi
170 if ! $MODPROBE vboxdrv > /dev/null 2>&1; then
171 failure "modprobe vboxdrv failed. Please use 'dmesg' to find out why"
172 fi
173 sleep .2
174 fi
175 # ensure the character special exists
176 if [ ! -c $DEVICE ]; then
177 MAJOR=`sed -n 's;\([0-9]\+\) vboxdrv;\1;p' /proc/devices`
178 if [ ! -z "$MAJOR" ]; then
179 MINOR=0
180 else
181 MINOR=`sed -n 's;\([0-9]\+\) vboxdrv;\1;p' /proc/misc`
182 if [ ! -z "$MINOR" ]; then
183 MAJOR=10
184 fi
185 fi
186 if [ -z "$MAJOR" ]; then
187 rmmod vboxdrv 2>/dev/null
188 failure "Cannot locate the VirtualBox device"
189 fi
190 if ! mknod -m 0660 $DEVICE c $MAJOR $MINOR 2>/dev/null; then
191 rmmod vboxdrv 2>/dev/null
192 failure "Cannot create device $DEVICE with major $MAJOR and minor $MINOR"
193 fi
194 fi
195 # ensure permissions
196 if ! chown :%GROUP% $DEVICE 2>/dev/null; then
197 rmmod vboxpci 2>/dev/null
198 rmmod vboxnetadp 2>/dev/null
199 rmmod vboxnetflt 2>/dev/null
200 rmmod vboxdrv 2>/dev/null
201 failure "Cannot change group %GROUP% for device $DEVICE"
202 fi
203 if ! $MODPROBE vboxnetflt > /dev/null 2>&1; then
204 failure "modprobe vboxnetflt failed. Please use 'dmesg' to find out why"
205 fi
206 if ! $MODPROBE vboxnetadp > /dev/null 2>&1; then
207 failure "modprobe vboxnetadp failed. Please use 'dmesg' to find out why"
208 fi
209 if ! $MODPROBE vboxpci > /dev/null 2>&1; then
210 failure "modprobe vboxpci failed. Please use 'dmesg' to find out why"
211 fi
212 # Create the /dev/vboxusb directory if the host supports that method
213 # of USB access. The USB code checks for the existance of that path.
214 if grep -q usb_device /proc/devices; then
215 mkdir -p -m 0750 /dev/vboxusb 2>/dev/null
216 chown root:vboxusers /dev/vboxusb 2>/dev/null
217 fi
218 succ_msg
219}
220
221stop()
222{
223 begin_msg "Stopping VirtualBox kernel modules"
224 if running vboxpci; then
225 if ! rmmod vboxpci 2>/dev/null; then
226 failure "Cannot unload module vboxpci"
227 fi
228 fi
229 if running vboxnetadp; then
230 if ! rmmod vboxnetadp 2>/dev/null; then
231 failure "Cannot unload module vboxnetadp"
232 fi
233 fi
234 if running vboxdrv; then
235 if running vboxnetflt; then
236 if ! rmmod vboxnetflt 2>/dev/null; then
237 failure "Cannot unload module vboxnetflt"
238 fi
239 fi
240 if ! rmmod vboxdrv 2>/dev/null; then
241 failure "Cannot unload module vboxdrv"
242 fi
243 if ! rm -f $DEVICE; then
244 failure "Cannot unlink $DEVICE"
245 fi
246 fi
247 succ_msg
248}
249
250# enter the following variables in /etc/default/%PACKAGE%:
251# SHUTDOWN_USERS="foo bar"
252# check for running VMs of user foo and user bar
253# SHUTDOWN=poweroff
254# SHUTDOWN=acpibutton
255# SHUTDOWN=savestate
256# select one of these shutdown methods for running VMs
257stop_vms()
258{
259 wait=0
260 for i in $SHUTDOWN_USERS; do
261 # don't create the ipcd directory with wrong permissions!
262 if [ -d /tmp/.vbox-$i-ipc ]; then
263 export VBOX_IPC_SOCKETID="$i"
264 VMS=`$VBOXMANAGE --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/' 2>/dev/null`
265 if [ -n "$VMS" ]; then
266 if [ "$SHUTDOWN" = "poweroff" ]; then
267 begin_msg "Powering off remaining VMs"
268 for v in $VMS; do
269 $VBOXMANAGE --nologo controlvm $v poweroff
270 done
271 succ_msg
272 elif [ "$SHUTDOWN" = "acpibutton" ]; then
273 begin_msg "Sending ACPI power button event to remaining VMs"
274 for v in $VMS; do
275 $VBOXMANAGE --nologo controlvm $v acpipowerbutton
276 wait=30
277 done
278 succ_msg
279 elif [ "$SHUTDOWN" = "savestate" ]; then
280 begin_msg "Saving state of remaining VMs"
281 for v in $VMS; do
282 $VBOXMANAGE --nologo controlvm $v savestate
283 done
284 succ_msg
285 fi
286 fi
287 fi
288 done
289 # wait for some seconds when doing ACPI shutdown
290 if [ "$wait" -ne 0 ]; then
291 begin_msg "Waiting for $wait seconds for VM shutdown"
292 sleep $wait
293 succ_msg
294 fi
295}
296
297# setup_script
298setup()
299{
300 stop
301 begin_msg "Uninstalling old VirtualBox DKMS kernel modules"
302 $DODKMS uninstall vboxhost vboxdrv vboxnetflt vboxnetadp > $LOG
303 succ_msg
304 if find /lib/modules/`uname -r` -name "vboxpci\.*" 2>/dev/null|grep -q vboxpci; then
305 begin_msg "Removing old VirtualBox pci kernel module"
306 find /lib/modules/`uname -r` -name "vboxpci\.*" 2>/dev/null|xargs rm -f 2>/dev/null
307 succ_msg
308 fi
309 if find /lib/modules/`uname -r` -name "vboxnetadp\.*" 2>/dev/null|grep -q vboxnetadp; then
310 begin_msg "Removing old VirtualBox netadp kernel module"
311 find /lib/modules/`uname -r` -name "vboxnetadp\.*" 2>/dev/null|xargs rm -f 2>/dev/null
312 succ_msg
313 fi
314 if find /lib/modules/`uname -r` -name "vboxnetflt\.*" 2>/dev/null|grep -q vboxnetflt; then
315 begin_msg "Removing old VirtualBox netflt kernel module"
316 find /lib/modules/`uname -r` -name "vboxnetflt\.*" 2>/dev/null|xargs rm -f 2>/dev/null
317 succ_msg
318 fi
319 if find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|grep -q vboxdrv; then
320 begin_msg "Removing old VirtualBox kernel module"
321 find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|xargs rm -f 2>/dev/null
322 succ_msg
323 fi
324 begin_msg "Trying to register the VirtualBox kernel modules using DKMS"
325 if ! $DODKMS install vboxhost $VERSION >> $LOG; then
326 fail_msg "Failed, trying without DKMS"
327 begin_msg "Recompiling VirtualBox kernel modules"
328 if ! $BUILDINTMP \
329 --save-module-symvers /tmp/vboxdrv-Module.symvers \
330 --module-source "$MODULE_SRC/vboxdrv" \
331 --no-print-directory install >> $LOG 2>&1; then
332 failure "Look at $LOG to find out what went wrong"
333 fi
334 if ! $BUILDINTMP \
335 --use-module-symvers /tmp/vboxdrv-Module.symvers \
336 --module-source "$MODULE_SRC/vboxnetflt" \
337 --no-print-directory install >> $LOG 2>&1; then
338 failure "Look at $LOG to find out what went wrong"
339 fi
340 if ! $BUILDINTMP \
341 --use-module-symvers /tmp/vboxdrv-Module.symvers \
342 --module-source "$MODULE_SRC/vboxnetadp" \
343 --no-print-directory install >> $LOG 2>&1; then
344 failure "Look at $LOG to find out what went wrong"
345 fi
346 if ! $BUILDINTMP \
347 --use-module-symvers /tmp/vboxdrv-Module.symvers \
348 --module-source "$MODULE_SRC/vboxpci" \
349 --no-print-directory install >> $LOG 2>&1; then
350 failure "Look at $LOG to find out what went wrong"
351 fi
352 fi
353 rm -f /etc/vbox/module_not_compiled
354 succ_msg
355 start
356}
357
358dmnstatus()
359{
360 if running vboxdrv; then
361 str="vboxdrv"
362 if running vboxnetflt; then
363 str="$str, vboxnetflt"
364 if running vboxnetadp; then
365 str="$str, vboxnetadp"
366 fi
367 fi
368 if running vboxpci; then
369 str="$str, vboxpci"
370 fi
371 echo "VirtualBox kernel modules ($str) are loaded."
372 for i in $SHUTDOWN_USERS; do
373 # don't create the ipcd directory with wrong permissions!
374 if [ -d /tmp/.vbox-$i-ipc ]; then
375 export VBOX_IPC_SOCKETID="$i"
376 VMS=`$VBOXMANAGE --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/' 2>/dev/null`
377 if [ -n "$VMS" ]; then
378 echo "The following VMs are currently running:"
379 for v in $VMS; do
380 echo " $v"
381 done
382 fi
383 fi
384 done
385 else
386 echo "VirtualBox kernel module is not loaded."
387 fi
388}
389
390case "$1" in
391start)
392 start
393 ;;
394stop)
395 stop_vms
396 stop
397 ;;
398stop_vms)
399 stop_vms
400 ;;
401restart)
402 stop && start
403 ;;
404force-reload)
405 stop
406 start
407 ;;
408setup)
409 setup
410 ;;
411status)
412 dmnstatus
413 ;;
414*)
415 echo "Usage: $0 {start|stop|stop_vms|restart|force-reload|status|setup}"
416 exit 1
417esac
418
419exit 0
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette