VirtualBox

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

Last change on this file since 37549 was 36888, checked in by vboxsync, 14 years ago

Installer/Linux: vboxpci adaptions

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