VirtualBox

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

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

bugref:8051: Installers: unify Linux host installers as far as possible: sometimes running vboxdrv.sh setup as called by loadall.sh caused the modules to be built twice. This was because certain work was duplicated between the call to stop && setup && start, and the implementation of start(). This change removes the duplication.

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