VirtualBox

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

Last change on this file since 77807 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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