VirtualBox

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

Last change on this file since 23760 was 23473, checked in by vboxsync, 16 years ago

OSE header fixes

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette