VirtualBox

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

Last change on this file since 36713 was 36417, checked in by vboxsync, 14 years ago

Installer/Linux and Main: USB enumeration: fix detection of USB via sysfs/usb_device

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 11.5 KB
Line 
1#! /bin/sh
2# Oracle VM VirtualBox
3# Linux kernel module init script
4
5#
6# Copyright (C) 2006-2010 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: 35 30 70
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: 1
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"
32NOLSB=%NOLSB%
33DEBIAN=%DEBIAN%
34MODPROBE=/sbin/modprobe
35
36if $MODPROBE -c | grep -q '^allow_unsupported_modules *0'; then
37 MODPROBE="$MODPROBE --allow-unsupported-modules"
38fi
39
40[ -f /lib/lsb/init-functions ] || NOLSB=yes
41[ -f /etc/vbox/vbox.cfg ] && . /etc/vbox/vbox.cfg
42
43if [ -n "$INSTALL_DIR" ]; then
44 VBOXMANAGE="$INSTALL_DIR/VBoxManage"
45 DODKMS="$INSTALL_DIR/src/vboxhost/do_dkms"
46 BUILDVBOXDRV="$INSTALL_DIR/src/vboxhost/vboxdrv/build_in_tmp"
47 BUILDVBOXNETFLT="$INSTALL_DIR/src/vboxhost/vboxnetflt/build_in_tmp"
48 BUILDVBOXNETADP="$INSTALL_DIR/src/vboxhost/vboxnetadp/build_in_tmp"
49else
50 VBOXMANAGE="/usr/lib/%PACKAGE%/VBoxManage"
51 DODKMS="/usr/share/%PACKAGE%/src/vboxhost/do_dkms"
52 BUILDVBOXDRV="/usr/share/%PACKAGE%/src/vboxhost/vboxdrv/build_in_tmp"
53 BUILDVBOXNETFLT="/usr/share/%PACKAGE%/src/vboxhost/vboxnetflt/build_in_tmp"
54 BUILDVBOXNETADP="/usr/share/%PACKAGE%/src/vboxhost/vboxnetadp/build_in_tmp"
55fi
56
57# silently exit if the package was uninstalled but not purged,
58# applies to Debian packages only
59[ -z "$DEBIAN" -o -x $VBOXMANAGE -a -x $BUILDVBOXDRV ] || exit 0
60
61if [ -n "$NOLSB" ]; then
62 if [ -f /etc/redhat-release ]; then
63 system=redhat
64 elif [ -f /etc/SuSE-release ]; then
65 system=suse
66 elif [ -f /etc/gentoo-release ]; then
67 system=gentoo
68 fi
69fi
70
71[ -r /etc/default/%PACKAGE% ] && . /etc/default/%PACKAGE%
72
73if [ -z "$NOLSB" ]; then
74 . /lib/lsb/init-functions
75 fail_msg() {
76 echo ""
77 log_failure_msg "$1"
78 }
79 succ_msg() {
80 log_end_msg 0
81 }
82 begin_msg() {
83 log_daemon_msg "$@"
84 }
85else
86 if [ "$system" = "redhat" ]; then
87 . /etc/init.d/functions
88 fail_msg() {
89 echo -n " "
90 echo_failure
91 echo
92 echo " ($1)"
93 }
94 succ_msg() {
95 echo -n " "
96 echo_success
97 echo
98 }
99 elif [ "$system" = "suse" ]; then
100 . /etc/rc.status
101 fail_msg() {
102 rc_failed 1
103 rc_status -v
104 echo " ($1)"
105 }
106 succ_msg() {
107 rc_reset
108 rc_status -v
109 }
110 elif [ "$system" = "gentoo" ]; then
111 if [ -f /sbin/functions.sh ]; then
112 . /sbin/functions.sh
113 elif [ -f /etc/init.d/functions.sh ]; then
114 . /etc/init.d/functions.sh
115 fi
116 fail_msg() {
117 eerror "$1"
118 }
119 succ_msg() {
120 eend "$?"
121 }
122 begin_msg() {
123 ebegin "$1"
124 }
125 if [ "`which $0`" = "/sbin/rc" ]; then
126 shift
127 fi
128 else
129 fail_msg() {
130 echo " ...failed!"
131 echo " ($1)"
132 }
133 succ_msg() {
134 echo " ...done."
135 }
136 fi
137 if [ "$system" != "gentoo" ]; then
138 begin_msg() {
139 [ -z "${1:-}" ] && return 1
140 if [ -z "${2:-}" ]; then
141 echo -n "$1"
142 else
143 echo -n "$1: $2"
144 fi
145 }
146 fi
147fi
148
149failure()
150{
151 fail_msg "$1"
152 exit 0
153}
154
155running()
156{
157 lsmod | grep -q "$1[^_-]"
158}
159
160start()
161{
162 begin_msg "Starting VirtualBox kernel modules"
163 if [ -d /proc/xen ]; then
164 failure "Running VirtualBox in a Xen environment is not supported"
165 fi
166 if ! running vboxdrv; then
167 if ! rm -f $DEVICE; then
168 failure "Cannot remove $DEVICE"
169 fi
170 if ! $MODPROBE vboxdrv > /dev/null 2>&1; then
171 failure "modprobe vboxdrv failed. Please use 'dmesg' to find out why"
172 fi
173 sleep .2
174 fi
175 # ensure the character special exists
176 if [ ! -c $DEVICE ]; then
177 MAJOR=`sed -n 's;\([0-9]\+\) vboxdrv;\1;p' /proc/devices`
178 if [ ! -z "$MAJOR" ]; then
179 MINOR=0
180 else
181 MINOR=`sed -n 's;\([0-9]\+\) vboxdrv;\1;p' /proc/misc`
182 if [ ! -z "$MINOR" ]; then
183 MAJOR=10
184 fi
185 fi
186 if [ -z "$MAJOR" ]; then
187 rmmod vboxdrv 2>/dev/null
188 failure "Cannot locate the VirtualBox device"
189 fi
190 if ! mknod -m 0660 $DEVICE c $MAJOR $MINOR 2>/dev/null; then
191 rmmod vboxdrv 2>/dev/null
192 failure "Cannot create device $DEVICE with major $MAJOR and minor $MINOR"
193 fi
194 fi
195 # ensure permissions
196 if ! chown :%GROUP% $DEVICE 2>/dev/null; then
197 rmmod vboxnetadp 2>/dev/null
198 rmmod vboxnetflt 2>/dev/null
199 rmmod vboxdrv 2>/dev/null
200 failure "Cannot change group %GROUP% for device $DEVICE"
201 fi
202 if ! $MODPROBE vboxnetflt > /dev/null 2>&1; then
203 failure "modprobe vboxnetflt failed. Please use 'dmesg' to find out why"
204 fi
205 if ! $MODPROBE vboxnetadp > /dev/null 2>&1; then
206 failure "modprobe vboxnetadp failed. Please use 'dmesg' to find out why"
207 fi
208 # Create the /dev/vboxusb directory if the host supports that method
209 # of USB access. The USB code checks for the existance of that path.
210 if grep -q usb_device /proc/devices; then
211 mkdir -p -m 0640 /dev/vboxusb 2>/dev/null
212 chown root:vboxusers /dev/vboxusb 2>/dev/null
213 fi
214 succ_msg
215}
216
217stop()
218{
219 begin_msg "Stopping VirtualBox kernel modules"
220 if running vboxnetadp; then
221 if ! rmmod vboxnetadp 2>/dev/null; then
222 failure "Cannot unload module vboxnetadp"
223 fi
224 fi
225 if running vboxdrv; then
226 if running vboxnetflt; then
227 if ! rmmod vboxnetflt 2>/dev/null; then
228 failure "Cannot unload module vboxnetflt"
229 fi
230 fi
231 if ! rmmod vboxdrv 2>/dev/null; then
232 failure "Cannot unload module vboxdrv"
233 fi
234 if ! rm -f $DEVICE; then
235 failure "Cannot unlink $DEVICE"
236 fi
237 fi
238 succ_msg
239}
240
241# enter the following variables in /etc/default/%PACKAGE%:
242# SHUTDOWN_USERS="foo bar"
243# check for running VMs of user foo and user bar
244# SHUTDOWN=poweroff
245# SHUTDOWN=acpibutton
246# SHUTDOWN=savestate
247# select one of these shutdown methods for running VMs
248stop_vms()
249{
250 wait=0
251 for i in $SHUTDOWN_USERS; do
252 # don't create the ipcd directory with wrong permissions!
253 if [ -d /tmp/.vbox-$i-ipc ]; then
254 export VBOX_IPC_SOCKETID="$i"
255 VMS=`$VBOXMANAGE --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/' 2>/dev/null`
256 if [ -n "$VMS" ]; then
257 if [ "$SHUTDOWN" = "poweroff" ]; then
258 begin_msg "Powering off remaining VMs"
259 for v in $VMS; do
260 $VBOXMANAGE --nologo controlvm $v poweroff
261 done
262 succ_msg
263 elif [ "$SHUTDOWN" = "acpibutton" ]; then
264 begin_msg "Sending ACPI power button event to remaining VMs"
265 for v in $VMS; do
266 $VBOXMANAGE --nologo controlvm $v acpipowerbutton
267 wait=30
268 done
269 succ_msg
270 elif [ "$SHUTDOWN" = "savestate" ]; then
271 begin_msg "Saving state of remaining VMs"
272 for v in $VMS; do
273 $VBOXMANAGE --nologo controlvm $v savestate
274 done
275 succ_msg
276 fi
277 fi
278 fi
279 done
280 # wait for some seconds when doing ACPI shutdown
281 if [ "$wait" -ne 0 ]; then
282 begin_msg "Waiting for $wait seconds for VM shutdown"
283 sleep $wait
284 succ_msg
285 fi
286}
287
288# setup_script
289setup()
290{
291 stop
292 begin_msg "Uninstalling old VirtualBox DKMS kernel modules"
293 $DODKMS uninstall > $LOG
294 succ_msg
295 if find /lib/modules/`uname -r` -name "vboxnetadp\.*" 2>/dev/null|grep -q vboxnetadp; then
296 begin_msg "Removing old VirtualBox netadp kernel module"
297 find /lib/modules/`uname -r` -name "vboxnetadp\.*" 2>/dev/null|xargs rm -f 2>/dev/null
298 succ_msg
299 fi
300 if find /lib/modules/`uname -r` -name "vboxnetflt\.*" 2>/dev/null|grep -q vboxnetflt; then
301 begin_msg "Removing old VirtualBox netflt kernel module"
302 find /lib/modules/`uname -r` -name "vboxnetflt\.*" 2>/dev/null|xargs rm -f 2>/dev/null
303 succ_msg
304 fi
305 if find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|grep -q vboxdrv; then
306 begin_msg "Removing old VirtualBox kernel module"
307 find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|xargs rm -f 2>/dev/null
308 succ_msg
309 fi
310 begin_msg "Trying to register the VirtualBox kernel modules using DKMS"
311 if ! $DODKMS install >> $LOG; then
312 fail_msg "Failed, trying without DKMS"
313 begin_msg "Recompiling VirtualBox kernel modules"
314 if ! $BUILDVBOXDRV \
315 --save-module-symvers /tmp/vboxdrv-Module.symvers \
316 --no-print-directory install >> $LOG 2>&1; then
317 failure "Look at $LOG to find out what went wrong"
318 fi
319 if ! $BUILDVBOXNETFLT \
320 --use-module-symvers /tmp/vboxdrv-Module.symvers \
321 --no-print-directory install >> $LOG 2>&1; then
322 failure "Look at $LOG to find out what went wrong"
323 fi
324 if ! $BUILDVBOXNETADP \
325 --use-module-symvers /tmp/vboxdrv-Module.symvers \
326 --no-print-directory install >> $LOG 2>&1; then
327 failure "Look at $LOG to find out what went wrong"
328 fi
329 fi
330 rm -f /etc/vbox/module_not_compiled
331 succ_msg
332 start
333}
334
335dmnstatus()
336{
337 if running vboxdrv; then
338 if running vboxnetflt; then
339 if running vboxnetadp; then
340 echo "VirtualBox kernel modules (vboxdrv, vboxnetflt and vboxnetadp) are loaded."
341 else
342 echo "VirtualBox kernel modules (vboxdrv and vboxnetflt) are loaded."
343 fi
344 else
345 echo "VirtualBox kernel module is loaded."
346 fi
347 for i in $SHUTDOWN_USERS; do
348 # don't create the ipcd directory with wrong permissions!
349 if [ -d /tmp/.vbox-$i-ipc ]; then
350 export VBOX_IPC_SOCKETID="$i"
351 VMS=`$VBOXMANAGE --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/' 2>/dev/null`
352 if [ -n "$VMS" ]; then
353 echo "The following VMs are currently running:"
354 for v in $VMS; do
355 echo " $v"
356 done
357 fi
358 fi
359 done
360 else
361 echo "VirtualBox kernel module is not loaded."
362 fi
363}
364
365case "$1" in
366start)
367 start
368 ;;
369stop)
370 stop_vms
371 stop
372 ;;
373stop_vms)
374 stop_vms
375 ;;
376restart)
377 stop && start
378 ;;
379force-reload)
380 stop
381 start
382 ;;
383setup)
384 setup
385 ;;
386status)
387 dmnstatus
388 ;;
389*)
390 echo "Usage: $0 {start|stop|stop_vms|restart|force-reload|status|setup}"
391 exit 1
392esac
393
394exit 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