VirtualBox

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

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

bugref:8525: Installers/Linux: reduce duplication between different installers: added a todo for removing duplication to vboxdrv.sh.

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