VirtualBox

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

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

Installer/linux: move host udev rule generation to run-time (and SELinux permission setting to routines.sh).

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