VirtualBox

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

Last change on this file since 58261 was 58261, checked in by vboxsync, 9 years ago

Installer/linux (host): do not forget depmod, but get it right.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 16.8 KB
Line 
1#! /bin/sh
2# Oracle VM VirtualBox
3# Linux kernel module init script
4
5#
6# Copyright (C) 2006-2015 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: 345 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"
32MODPROBE=/sbin/modprobe
33SCRIPTNAME=vboxdrv
34# This is GNU-specific, sorry Solaris. It fails on directories ending in '\n'.
35SCRIPT_DIR="$(dirname $(readlink -f -- "${0}"))"
36
37if $MODPROBE -c | grep -q '^allow_unsupported_modules *0'; then
38 MODPROBE="$MODPROBE --allow-unsupported-modules"
39fi
40
41[ -f /etc/vbox/vbox.cfg ] && . /etc/vbox/vbox.cfg
42export BUILD_TYPE
43export USERNAME
44export USER=$USERNAME
45
46if test -n "${INSTALL_DIR}" && test -x "${INSTALL_DIR}/VirtualBox"; then
47 MODULE_SRC="${INSTALL_DIR}/src/vboxhost"
48elif test -x /usr/lib/virtualbox/VirtualBox; then
49 INSTALL_DIR=/usr/lib/virtualbox
50 MODULE_SRC="/usr/share/virtualbox/src/vboxhost"
51elif test -x "${SCRIPT_DIR}/VirtualBox"; then
52 # Executing from the build directory
53 INSTALL_DIR="${SCRIPT_DIR}"
54 MODULE_SRC="${INSTALL_DIR}/src"
55else
56 # Silently exit if the package was uninstalled but not purged.
57 # Applies to Debian packages only (but shouldn't hurt elsewhere)
58 exit 0
59fi
60VIRTUALBOX="${INSTALL_DIR}/VirtualBox"
61VBOXMANAGE="${INSTALL_DIR}/VBoxManage"
62BUILDINTMP="${MODULE_SRC}/build_in_tmp"
63if test -u "${VIRTUALBOX}"; then
64 GROUP=root
65 DEVICE_MODE=0600
66else
67 GROUP=vboxusers
68 DEVICE_MODE=0660
69fi
70
71[ -r /etc/default/virtualbox ] && . /etc/default/virtualbox
72
73# Preamble for Gentoo
74if [ "`which $0`" = "/sbin/rc" ]; then
75 shift
76fi
77
78begin_msg()
79{
80 test -n "${2}" && echo "${SCRIPTNAME}: ${1}."
81 logger "${SCRIPTNAME}: ${1}."
82}
83
84succ_msg()
85{
86 logger "${SCRIPTNAME}: done."
87}
88
89fail_msg()
90{
91 echo "${SCRIPTNAME}: failed: ${1}." >&2
92 logger "${SCRIPTNAME}: failed: ${1}."
93}
94
95failure()
96{
97 fail_msg "$1"
98 exit 0
99}
100
101running()
102{
103 lsmod | grep -q "$1[^_-]"
104}
105
106## Output the vboxdrv part of our udev rule. This is redirected to the right file.
107udev_write_vboxdrv() {
108 VBOXDRV_GRP="$1"
109 VBOXDRV_MODE="$2"
110
111 echo "KERNEL==\"vboxdrv\", NAME=\"vboxdrv\", OWNER=\"root\", GROUP=\"$VBOXDRV_GRP\", MODE=\"$VBOXDRV_MODE\""
112 echo "KERNEL==\"vboxdrvu\", NAME=\"vboxdrvu\", OWNER=\"root\", GROUP=\"root\", MODE=\"0666\""
113 echo "KERNEL==\"vboxnetctl\", NAME=\"vboxnetctl\", OWNER=\"root\", GROUP=\"$VBOXDRV_GRP\", MODE=\"$VBOXDRV_MODE\""
114}
115
116## Output the USB part of our udev rule. This is redirected to the right file.
117udev_write_usb() {
118 INSTALLATION_DIR="$1"
119 USB_GROUP="$2"
120
121 echo "SUBSYSTEM==\"usb_device\", ACTION==\"add\", RUN+=\"$INSTALLATION_DIR/VBoxCreateUSBNode.sh \$major \$minor \$attr{bDeviceClass}${USB_GROUP}\""
122 echo "SUBSYSTEM==\"usb\", ACTION==\"add\", ENV{DEVTYPE}==\"usb_device\", RUN+=\"$INSTALLATION_DIR/VBoxCreateUSBNode.sh \$major \$minor \$attr{bDeviceClass}${USB_GROUP}\""
123 echo "SUBSYSTEM==\"usb_device\", ACTION==\"remove\", RUN+=\"$INSTALLATION_DIR/VBoxCreateUSBNode.sh --remove \$major \$minor\""
124 echo "SUBSYSTEM==\"usb\", ACTION==\"remove\", ENV{DEVTYPE}==\"usb_device\", RUN+=\"$INSTALLATION_DIR/VBoxCreateUSBNode.sh --remove \$major \$minor\""
125}
126
127## Generate our udev rule file. This takes a change in udev rule syntax in
128## version 55 into account. It only creates rules for USB for udev versions
129## recent enough to support USB device nodes.
130generate_udev_rule() {
131 VBOXDRV_GRP="$1" # The group owning the vboxdrv device
132 VBOXDRV_MODE="$2" # The access mode for the vboxdrv device
133 INSTALLATION_DIR="$3" # The directory VirtualBox is installed in
134 USB_GROUP="$4" # The group that has permission to access USB devices
135 NO_INSTALL="$5" # Set this to "1" to remove but not re-install rules
136 UDEV_STRING="$6" # The output of the udev version command
137
138 # Extra space!
139 case "$USB_GROUP" in ?*) USB_GROUP=" $USB_GROUP" ;; esac
140 case "$NO_INSTALL" in
141 "1") ;;
142 *)
143 udev_ver=`expr "$UDEV_STRING" : '[^0-9]*\([0-9]*\)'`
144 udev_fix=""
145 test "$udev_ver" = "" -o "$udev_ver" -lt 55 &&
146 udev_fix="1"
147 udev_do_usb=""
148 test "$udev_ver" -ge 59 &&
149 udev_do_usb="1"
150 case "$udev_fix" in
151 "1")
152 udev_write_vboxdrv "$VBOXDRV_GRP" "$VBOXDRV_MODE" |
153 sed 's/\([^+=]*\)[+=]*\([^"]*"[^"]*"\)/\1=\2/g'
154 ;;
155 *)
156 udev_write_vboxdrv "$VBOXDRV_GRP" "$VBOXDRV_MODE"
157 case "$udev_do_usb" in "1")
158 udev_write_usb "$INSTALLATION_DIR" "$USB_GROUP" ;;
159 esac
160 ;;
161 esac
162 ;;
163 esac
164}
165
166## Install udev rule (disable with INSTALL_NO_UDEV=1 in
167## /etc/default/virtualbox).
168install_udev() {
169 VBOXDRV_GRP="$1" # The group owning the vboxdrv device
170 VBOXDRV_MODE="$2" # The access mode for the vboxdrv device
171 INSTALLATION_DIR="$3" # The directory VirtualBox is installed in
172 USB_GROUP="$4" # The group that has permission to access USB devices
173 NO_INSTALL="$5" # Set this to "1" to remove but not re-install rules
174
175 if test -d /etc/udev/rules.d; then
176 udev_out="`udevadm version 2>/dev/null || udevinfo -V 2>/dev/null`"
177 generate_udev_rule "$VBOXDRV_GRP" "$VBOXDRV_MODE" "$INSTALLATION_DIR" \
178 "$USB_GROUP" "$NO_INSTALL" "$udev_out"
179 fi
180 # Remove old udev description file
181 rm -f /etc/udev/rules.d/10-vboxdrv.rules 2> /dev/null
182}
183
184## Create a usb device node for a given sysfs path to a USB device.
185install_create_usb_node_for_sysfs() {
186 path="$1" # sysfs path for the device
187 usb_createnode="$2" # Path to the USB device node creation script
188 usb_group="$3" # The group to give ownership of the node to
189 if test -r "${path}/dev"; then
190 dev="`cat "${path}/dev" 2> /dev/null`"
191 major="`expr "$dev" : '\(.*\):' 2> /dev/null`"
192 minor="`expr "$dev" : '.*:\(.*\)' 2> /dev/null`"
193 class="`cat ${path}/bDeviceClass 2> /dev/null`"
194 sh "${usb_createnode}" "$major" "$minor" "$class" \
195 "${usb_group}" 2>/dev/null
196 fi
197}
198
199udev_rule_file=/etc/udev/rules.d/60-vboxdrv.rules
200sysfs_usb_devices="/sys/bus/usb/devices/*"
201
202## Install udev rules and create device nodes for usb access
203install_device_node_setup() {
204 VBOXDRV_GRP="$1" # The group that should own /dev/vboxdrv
205 VBOXDRV_MODE="$2" # The mode to be used for /dev/vboxdrv
206 INSTALLATION_DIR="$3" # The directory VirtualBox is installed in
207 USB_GROUP="$4" # The group that should own the /dev/vboxusb device
208 # nodes unless INSTALL_NO_GROUP=1 in
209 # /etc/default/virtualbox. Optional.
210 usb_createnode="$INSTALLATION_DIR/VBoxCreateUSBNode.sh"
211 # install udev rule (disable with INSTALL_NO_UDEV=1 in
212 # /etc/default/virtualbox)
213 if [ "$INSTALL_NO_GROUP" != "1" ]; then
214 usb_group=$USB_GROUP
215 vboxdrv_group=$VBOXDRV_GRP
216 else
217 usb_group=root
218 vboxdrv_group=root
219 fi
220 install_udev "${vboxdrv_group}" "$VBOXDRV_MODE" \
221 "$INSTALLATION_DIR" "${usb_group}" \
222 "$INSTALL_NO_UDEV" > ${udev_rule_file}
223 # Build our device tree
224 for i in ${sysfs_usb_devices}; do # This line intentionally without quotes.
225 install_create_usb_node_for_sysfs "$i" "${usb_createnode}" \
226 "${usb_group}"
227 done
228}
229
230start()
231{
232 begin_msg "Starting VirtualBox services" console
233 # Create udev rule and USB device nodes.
234 ## todo Wouldn't it make more sense to install the rule to /lib/udev? This
235 ## is not a user-created configuration file after all.
236 ## todo Do we need a udev rule to create /dev/vboxdrv[u] at all? We have
237 ## working fall-back code here anyway, and the "right" code is more complex
238 ## than the fall-back. Unnecessary duplication?
239 install_device_node_setup "$GROUP" "$DEVICE_MODE" "$INSTALL_DIR"
240 if [ -d /proc/xen ]; then
241 failure "Running VirtualBox in a Xen environment is not supported"
242 fi
243 if ! running vboxdrv; then
244 if ! rm -f $DEVICE; then
245 failure "Cannot remove $DEVICE"
246 fi
247 if ! $MODPROBE vboxdrv > /dev/null 2>&1; then
248 setup
249 if ! $MODPROBE vboxdrv > /dev/null 2>&1; then
250 failure "modprobe vboxdrv failed. Please use 'dmesg' to find out why"
251 fi
252 fi
253 sleep .2
254 fi
255 # ensure the character special exists
256 if [ ! -c $DEVICE ]; then
257 MAJOR=`sed -n 's;\([0-9]\+\) vboxdrv$;\1;p' /proc/devices`
258 if [ ! -z "$MAJOR" ]; then
259 MINOR=0
260 else
261 MINOR=`sed -n 's;\([0-9]\+\) vboxdrv$;\1;p' /proc/misc`
262 if [ ! -z "$MINOR" ]; then
263 MAJOR=10
264 fi
265 fi
266 if [ -z "$MAJOR" ]; then
267 rmmod vboxdrv 2>/dev/null
268 failure "Cannot locate the VirtualBox device"
269 fi
270 if ! mknod -m 0660 $DEVICE c $MAJOR $MINOR 2>/dev/null; then
271 rmmod vboxdrv 2>/dev/null
272 failure "Cannot create device $DEVICE with major $MAJOR and minor $MINOR"
273 fi
274 fi
275 # ensure permissions
276 if ! chown :"${GROUP}" $DEVICE 2>/dev/null; then
277 rmmod vboxpci 2>/dev/null
278 rmmod vboxnetadp 2>/dev/null
279 rmmod vboxnetflt 2>/dev/null
280 rmmod vboxdrv 2>/dev/null
281 failure "Cannot change group ${GROUP} for device $DEVICE"
282 fi
283 if ! $MODPROBE vboxnetflt > /dev/null 2>&1; then
284 failure "modprobe vboxnetflt failed. Please use 'dmesg' to find out why"
285 fi
286 if ! $MODPROBE vboxnetadp > /dev/null 2>&1; then
287 failure "modprobe vboxnetadp failed. Please use 'dmesg' to find out why"
288 fi
289 if ! $MODPROBE vboxpci > /dev/null 2>&1; then
290 failure "modprobe vboxpci failed. Please use 'dmesg' to find out why"
291 fi
292 # Create the /dev/vboxusb directory if the host supports that method
293 # of USB access. The USB code checks for the existance of that path.
294 if grep -q usb_device /proc/devices; then
295 mkdir -p -m 0750 /dev/vboxusb 2>/dev/null
296 chown root:vboxusers /dev/vboxusb 2>/dev/null
297 fi
298 succ_msg
299}
300
301stop()
302{
303 begin_msg "Stopping VirtualBox services" console
304 # Remove udev description file
305 rm -f /etc/udev/rules.d/60-vboxdrv.rules
306 rm -f /etc/udev/rules.d/10-vboxdrv.rules
307
308 # Remove our USB device tree
309 rm -rf /dev/vboxusb
310
311 if running vboxpci; then
312 if ! rmmod vboxpci 2>/dev/null; then
313 failure "Cannot unload module vboxpci"
314 fi
315 fi
316 if running vboxnetadp; then
317 if ! rmmod vboxnetadp 2>/dev/null; then
318 failure "Cannot unload module vboxnetadp"
319 fi
320 fi
321 if running vboxdrv; then
322 if running vboxnetflt; then
323 if ! rmmod vboxnetflt 2>/dev/null; then
324 failure "Cannot unload module vboxnetflt"
325 fi
326 fi
327 if ! rmmod vboxdrv 2>/dev/null; then
328 failure "Cannot unload module vboxdrv"
329 fi
330 if ! rm -f $DEVICE; then
331 failure "Cannot unlink $DEVICE"
332 fi
333 fi
334 succ_msg
335}
336
337# enter the following variables in /etc/default/virtualbox:
338# SHUTDOWN_USERS="foo bar"
339# check for running VMs of user foo and user bar
340# SHUTDOWN=poweroff
341# SHUTDOWN=acpibutton
342# SHUTDOWN=savestate
343# select one of these shutdown methods for running VMs
344stop_vms()
345{
346 wait=0
347 for i in $SHUTDOWN_USERS; do
348 # don't create the ipcd directory with wrong permissions!
349 if [ -d /tmp/.vbox-$i-ipc ]; then
350 export VBOX_IPC_SOCKETID="$i"
351 VMS=`$VBOXMANAGE --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/' 2>/dev/null`
352 if [ -n "$VMS" ]; then
353 if [ "$SHUTDOWN" = "poweroff" ]; then
354 begin_msg "Powering off remaining VMs"
355 for v in $VMS; do
356 $VBOXMANAGE --nologo controlvm $v poweroff
357 done
358 succ_msg
359 elif [ "$SHUTDOWN" = "acpibutton" ]; then
360 begin_msg "Sending ACPI power button event to remaining VMs"
361 for v in $VMS; do
362 $VBOXMANAGE --nologo controlvm $v acpipowerbutton
363 wait=30
364 done
365 succ_msg
366 elif [ "$SHUTDOWN" = "savestate" ]; then
367 begin_msg "Saving state of remaining VMs"
368 for v in $VMS; do
369 $VBOXMANAGE --nologo controlvm $v savestate
370 done
371 succ_msg
372 fi
373 fi
374 fi
375 done
376 # wait for some seconds when doing ACPI shutdown
377 if [ "$wait" -ne 0 ]; then
378 begin_msg "Waiting for $wait seconds for VM shutdown"
379 sleep $wait
380 succ_msg
381 fi
382}
383
384cleanup()
385{
386 for i in /lib/modules/*; do
387 # We could just do "rm -f", but we only want to try deleting folders if
388 # we are sure they were ours, i.e. they had our modules in beforehand.
389 if test -e "${i}/misc/vboxdrv.ko" \
390 || test -e "${i}/misc/vboxnetadp.ko" \
391 || test -e "${i}/misc/vboxnetflt.ko" \
392 || test -e "${i}/misc/vboxpci.ko"; then
393 rm -f "${i}/misc/vboxdrv.ko" "${i}/misc/vboxnetadp.ko" \
394 "${i}/misc/vboxnetflt.ko" "${i}/misc/vboxpci.ko"
395 # Remove the kernel version folder if it was empty except for us.
396 test "`echo ${i}/misc/* ${i}/misc/.?* ${i}/* ${i}/.?*`" \
397 = "${i}/misc/* ${i}/misc/.. ${i}/misc ${i}/.." &&
398 rmdir "${i}/misc" "${i}" # We used to leave empty folders.
399 version=`expr "${i}" : "/lib/modules/\(.*\)"`
400 depmod -a "${version}"
401 fi
402 done
403}
404
405# setup_script
406setup()
407{
408 begin_msg "Building VirtualBox kernel modules" console
409 cleanup
410 if ! $BUILDINTMP \
411 --save-module-symvers /tmp/vboxdrv-Module.symvers \
412 --module-source "$MODULE_SRC/vboxdrv" \
413 --no-print-directory install >> $LOG 2>&1; then
414 failure "Look at $LOG to find out what went wrong"
415 fi
416 if ! $BUILDINTMP \
417 --use-module-symvers /tmp/vboxdrv-Module.symvers \
418 --module-source "$MODULE_SRC/vboxnetflt" \
419 --no-print-directory install >> $LOG 2>&1; then
420 failure "Look at $LOG to find out what went wrong"
421 fi
422 if ! $BUILDINTMP \
423 --use-module-symvers /tmp/vboxdrv-Module.symvers \
424 --module-source "$MODULE_SRC/vboxnetadp" \
425 --no-print-directory install >> $LOG 2>&1; then
426 failure "Look at $LOG to find out what went wrong"
427 fi
428 if ! $BUILDINTMP \
429 --use-module-symvers /tmp/vboxdrv-Module.symvers \
430 --module-source "$MODULE_SRC/vboxpci" \
431 --no-print-directory install >> $LOG 2>&1; then
432 failure "Look at $LOG to find out what went wrong"
433 fi
434 rm -f /etc/vbox/module_not_compiled
435 depmod -a
436 succ_msg
437}
438
439dmnstatus()
440{
441 if running vboxdrv; then
442 str="vboxdrv"
443 if running vboxnetflt; then
444 str="$str, vboxnetflt"
445 if running vboxnetadp; then
446 str="$str, vboxnetadp"
447 fi
448 fi
449 if running vboxpci; then
450 str="$str, vboxpci"
451 fi
452 echo "VirtualBox kernel modules ($str) are loaded."
453 for i in $SHUTDOWN_USERS; do
454 # don't create the ipcd directory with wrong permissions!
455 if [ -d /tmp/.vbox-$i-ipc ]; then
456 export VBOX_IPC_SOCKETID="$i"
457 VMS=`$VBOXMANAGE --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/' 2>/dev/null`
458 if [ -n "$VMS" ]; then
459 echo "The following VMs are currently running:"
460 for v in $VMS; do
461 echo " $v"
462 done
463 fi
464 fi
465 done
466 else
467 echo "VirtualBox kernel module is not loaded."
468 fi
469}
470
471case "$1" in
472start)
473 start
474 ;;
475stop)
476 stop_vms
477 stop
478 ;;
479stop_vms)
480 stop_vms
481 ;;
482restart)
483 stop && start
484 ;;
485setup)
486 stop && setup && start
487 ;;
488cleanup)
489 stop && cleanup
490 ;;
491force-reload)
492 stop
493 start
494 ;;
495status)
496 dmnstatus
497 ;;
498*)
499 echo "Usage: $0 {start|stop|stop_vms|restart|force-reload|status}"
500 exit 1
501esac
502
503exit 0
Note: See TracBrowser for help on using the repository browser.

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