VirtualBox

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

Last change on this file since 25624 was 23891, checked in by vboxsync, 15 years ago

3rd try to fix r53634

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