VirtualBox

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

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

Installers/linux: cleaner script for getting shell script location folders.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 17.4 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
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 "${SCRIPTNAME}: ${1}."
94}
95
96succ_msg()
97{
98 logger "${SCRIPTNAME}: done."
99}
100
101fail_msg()
102{
103 echo "${SCRIPTNAME}: failed: ${1}." >&2
104 logger "${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
313}
314
315stop()
316{
317 begin_msg "Stopping VirtualBox services" console
318 # Remove udev description file
319 rm -f /etc/udev/rules.d/60-vboxdrv.rules
320 rm -f /etc/udev/rules.d/10-vboxdrv.rules
321
322 # Remove our USB device tree
323 rm -rf /dev/vboxusb
324
325 if running vboxpci; then
326 if ! rmmod vboxpci 2>/dev/null; then
327 failure "Cannot unload module vboxpci"
328 fi
329 fi
330 if running vboxnetadp; then
331 if ! rmmod vboxnetadp 2>/dev/null; then
332 failure "Cannot unload module vboxnetadp"
333 fi
334 fi
335 if running vboxdrv; then
336 if running vboxnetflt; then
337 if ! rmmod vboxnetflt 2>/dev/null; then
338 failure "Cannot unload module vboxnetflt"
339 fi
340 fi
341 if ! rmmod vboxdrv 2>/dev/null; then
342 failure "Cannot unload module vboxdrv"
343 fi
344 if ! rm -f $DEVICE; then
345 failure "Cannot unlink $DEVICE"
346 fi
347 fi
348 succ_msg
349}
350
351# enter the following variables in /etc/default/virtualbox:
352# SHUTDOWN_USERS="foo bar"
353# check for running VMs of user foo and user bar
354# SHUTDOWN=poweroff
355# SHUTDOWN=acpibutton
356# SHUTDOWN=savestate
357# select one of these shutdown methods for running VMs
358stop_vms()
359{
360 wait=0
361 for i in $SHUTDOWN_USERS; do
362 # don't create the ipcd directory with wrong permissions!
363 if [ -d /tmp/.vbox-$i-ipc ]; then
364 export VBOX_IPC_SOCKETID="$i"
365 VMS=`$VBOXMANAGE --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/' 2>/dev/null`
366 if [ -n "$VMS" ]; then
367 if [ "$SHUTDOWN" = "poweroff" ]; then
368 begin_msg "Powering off remaining VMs"
369 for v in $VMS; do
370 $VBOXMANAGE --nologo controlvm $v poweroff
371 done
372 succ_msg
373 elif [ "$SHUTDOWN" = "acpibutton" ]; then
374 begin_msg "Sending ACPI power button event to remaining VMs"
375 for v in $VMS; do
376 $VBOXMANAGE --nologo controlvm $v acpipowerbutton
377 wait=30
378 done
379 succ_msg
380 elif [ "$SHUTDOWN" = "savestate" ]; then
381 begin_msg "Saving state of remaining VMs"
382 for v in $VMS; do
383 $VBOXMANAGE --nologo controlvm $v savestate
384 done
385 succ_msg
386 fi
387 fi
388 fi
389 done
390 # wait for some seconds when doing ACPI shutdown
391 if [ "$wait" -ne 0 ]; then
392 begin_msg "Waiting for $wait seconds for VM shutdown"
393 sleep $wait
394 succ_msg
395 fi
396}
397
398cleanup()
399{
400 for i in /lib/modules/*; do
401 # We could just do "rm -f", but we only want to try deleting folders if
402 # we are sure they were ours, i.e. they had our modules in beforehand.
403 if test -e "${i}/misc/vboxdrv.ko" \
404 || test -e "${i}/misc/vboxnetadp.ko" \
405 || test -e "${i}/misc/vboxnetflt.ko" \
406 || test -e "${i}/misc/vboxpci.ko"; then
407 rm -f "${i}/misc/vboxdrv.ko" "${i}/misc/vboxnetadp.ko" \
408 "${i}/misc/vboxnetflt.ko" "${i}/misc/vboxpci.ko"
409 # Remove the kernel version folder if it was empty except for us.
410 test "`echo ${i}/misc/* ${i}/misc/.?* ${i}/* ${i}/.?*`" \
411 = "${i}/misc/* ${i}/misc/.. ${i}/misc ${i}/.." &&
412 rmdir "${i}/misc" "${i}" # We used to leave empty folders.
413 version=`expr "${i}" : "/lib/modules/\(.*\)"`
414 depmod -a "${version}"
415 fi
416 done
417}
418
419# setup_script
420setup()
421{
422 begin_msg "Building VirtualBox kernel modules" console
423 cleanup
424 if ! $BUILDINTMP \
425 --save-module-symvers /tmp/vboxdrv-Module.symvers \
426 --module-source "$MODULE_SRC/vboxdrv" \
427 --no-print-directory install >> $LOG 2>&1; then
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
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 ;;
496restart)
497 stop && start
498 ;;
499setup)
500 stop && setup && start
501 ;;
502cleanup)
503 stop && cleanup
504 ;;
505force-reload)
506 stop
507 start
508 ;;
509status)
510 dmnstatus
511 ;;
512*)
513 echo "Usage: $0 {start|stop|stop_vms|restart|force-reload|status}"
514 exit 1
515esac
516
517exit 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