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: 0 1 6
|
---|
26 | # Short-Description: VirtualBox Linux kernel module
|
---|
27 | ### END INIT INFO
|
---|
28 |
|
---|
29 | PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
|
---|
30 | DEVICE=/dev/vboxdrv
|
---|
31 | GROUPNAME=vboxusers
|
---|
32 | LOG="/var/log/vbox-install.log"
|
---|
33 | NOLSB=%NOLSB%
|
---|
34 | DEBIAN=%DEBIAN%
|
---|
35 | MODPROBE=/sbin/modprobe
|
---|
36 |
|
---|
37 | if $MODPROBE -c | grep -q '^allow_unsupported_modules *0'; then
|
---|
38 | MODPROBE="$MODPROBE --allow-unsupported-modules"
|
---|
39 | fi
|
---|
40 |
|
---|
41 | [ -f /lib/lsb/init-functions ] || NOLSB=yes
|
---|
42 | [ -f /etc/vbox/vbox.cfg ] && . /etc/vbox/vbox.cfg
|
---|
43 |
|
---|
44 | if [ -n "$INSTALL_DIR" ]; then
|
---|
45 | VBOXMANAGE="$INSTALL_DIR/VBoxManage"
|
---|
46 | BUILDVBOXDRV="$INSTALL_DIR/src/vboxdrv/build_in_tmp"
|
---|
47 | BUILDVBOXNETFLT="$INSTALL_DIR/src/vboxnetflt/build_in_tmp"
|
---|
48 | BUILDVBOXNETADP="$INSTALL_DIR/src/vboxnetadp/build_in_tmp"
|
---|
49 | else
|
---|
50 | VBOXMANAGE="/usr/lib/%PACKAGE%/VBoxManage"
|
---|
51 | BUILDVBOXDRV="/usr/share/%PACKAGE%/src/vboxdrv/build_in_tmp"
|
---|
52 | BUILDVBOXNETFLT="/usr/share/%PACKAGE%/src/vboxnetflt/build_in_tmp"
|
---|
53 | BUILDVBOXNETADP="/usr/share/%PACKAGE%/src/vboxnetadp/build_in_tmp"
|
---|
54 | fi
|
---|
55 |
|
---|
56 | # silently exit if the package was uninstalled but not purged,
|
---|
57 | # applies to Debian packages only
|
---|
58 | [ -z "$DEBIAN" -o -x $VBOXMANAGE -a -x $BUILDVBOXDRV ] || exit 0
|
---|
59 |
|
---|
60 | if [ -n "$NOLSB" ]; then
|
---|
61 | if [ -f /etc/redhat-release ]; then
|
---|
62 | system=redhat
|
---|
63 | elif [ -f /etc/SuSE-release ]; then
|
---|
64 | system=suse
|
---|
65 | elif [ -f /etc/gentoo-release ]; then
|
---|
66 | system=gentoo
|
---|
67 | fi
|
---|
68 | fi
|
---|
69 |
|
---|
70 | [ -r /etc/default/%PACKAGE% ] && . /etc/default/%PACKAGE%
|
---|
71 |
|
---|
72 | if [ -z "$NOLSB" ]; then
|
---|
73 | . /lib/lsb/init-functions
|
---|
74 | fail_msg() {
|
---|
75 | echo ""
|
---|
76 | log_failure_msg "$1"
|
---|
77 | }
|
---|
78 | succ_msg() {
|
---|
79 | log_success_msg " done."
|
---|
80 | }
|
---|
81 | begin_msg() {
|
---|
82 | log_daemon_msg "$@"
|
---|
83 | }
|
---|
84 | else
|
---|
85 | if [ "$system" = "redhat" ]; then
|
---|
86 | . /etc/init.d/functions
|
---|
87 | fail_msg() {
|
---|
88 | echo -n " "
|
---|
89 | echo_failure
|
---|
90 | echo
|
---|
91 | echo " ($1)"
|
---|
92 | }
|
---|
93 | succ_msg() {
|
---|
94 | echo -n " "
|
---|
95 | echo_success
|
---|
96 | echo
|
---|
97 | }
|
---|
98 | elif [ "$system" = "suse" ]; then
|
---|
99 | . /etc/rc.status
|
---|
100 | fail_msg() {
|
---|
101 | rc_failed 1
|
---|
102 | rc_status -v
|
---|
103 | echo " ($1)"
|
---|
104 | }
|
---|
105 | succ_msg() {
|
---|
106 | rc_reset
|
---|
107 | rc_status -v
|
---|
108 | }
|
---|
109 | elif [ "$system" = "gentoo" ]; then
|
---|
110 | if [ -f /sbin/functions.sh ]; then
|
---|
111 | . /sbin/functions.sh
|
---|
112 | elif [ -f /etc/init.d/functions.sh ]; then
|
---|
113 | . /etc/init.d/functions.sh
|
---|
114 | fi
|
---|
115 | fail_msg() {
|
---|
116 | eerror "$1"
|
---|
117 | }
|
---|
118 | succ_msg() {
|
---|
119 | eend "$?"
|
---|
120 | }
|
---|
121 | begin_msg() {
|
---|
122 | ebegin "$1"
|
---|
123 | }
|
---|
124 | if [ "`which $0`" = "/sbin/rc" ]; then
|
---|
125 | shift
|
---|
126 | fi
|
---|
127 | else
|
---|
128 | fail_msg() {
|
---|
129 | echo " ...failed!"
|
---|
130 | echo " ($1)"
|
---|
131 | }
|
---|
132 | succ_msg() {
|
---|
133 | echo " ...done."
|
---|
134 | }
|
---|
135 | fi
|
---|
136 | if [ "$system" != "gentoo" ]; then
|
---|
137 | begin_msg() {
|
---|
138 | [ -z "${1:-}" ] && return 1
|
---|
139 | if [ -z "${2:-}" ]; then
|
---|
140 | echo -n "$1"
|
---|
141 | else
|
---|
142 | echo -n "$1: $2"
|
---|
143 | fi
|
---|
144 | }
|
---|
145 | fi
|
---|
146 | fi
|
---|
147 |
|
---|
148 | failure()
|
---|
149 | {
|
---|
150 | fail_msg "$1"
|
---|
151 | exit 0
|
---|
152 | }
|
---|
153 |
|
---|
154 | running()
|
---|
155 | {
|
---|
156 | lsmod | grep -q "$1[^_-]"
|
---|
157 | }
|
---|
158 |
|
---|
159 | start()
|
---|
160 | {
|
---|
161 | begin_msg "Starting VirtualBox kernel module"
|
---|
162 | if ! running vboxdrv; then
|
---|
163 | if ! rm -f $DEVICE; then
|
---|
164 | failure "Cannot remove $DEVICE"
|
---|
165 | fi
|
---|
166 | # HACK: disable the hardware performance counter framework
|
---|
167 | if [ -e /proc/sys/kernel/perf_counter_paranoid ]; then
|
---|
168 | echo 2 > /proc/sys/kernel/perf_counter_paranoid
|
---|
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 :$GROUPNAME $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 owner $GROUPNAME 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 | succ_msg
|
---|
209 | }
|
---|
210 |
|
---|
211 | stop()
|
---|
212 | {
|
---|
213 | begin_msg "Stopping VirtualBox kernel module"
|
---|
214 | if running vboxnetadp; then
|
---|
215 | if ! rmmod vboxnetadp 2>/dev/null; then
|
---|
216 | failure "Cannot unload module vboxnetadp"
|
---|
217 | fi
|
---|
218 | fi
|
---|
219 | if running vboxdrv; then
|
---|
220 | if running vboxnetflt; then
|
---|
221 | if ! rmmod vboxnetflt 2>/dev/null; then
|
---|
222 | failure "Cannot unload module vboxnetflt"
|
---|
223 | fi
|
---|
224 | fi
|
---|
225 | if ! rmmod vboxdrv 2>/dev/null; then
|
---|
226 | failure "Cannot unload module vboxdrv"
|
---|
227 | fi
|
---|
228 | if ! rm -f $DEVICE; then
|
---|
229 | failure "Cannot unlink $DEVICE"
|
---|
230 | fi
|
---|
231 | fi
|
---|
232 | succ_msg
|
---|
233 | }
|
---|
234 |
|
---|
235 | # enter the following variables in /etc/default/%PACKAGE%:
|
---|
236 | # SHUTDOWN_USERS="foo bar"
|
---|
237 | # check for running VMs of user foo and user bar
|
---|
238 | # SHUTDOWN=poweroff
|
---|
239 | # SHUTDOWN=acpibutton
|
---|
240 | # SHUTDOWN=savestate
|
---|
241 | # select one of these shutdown methods for running VMs
|
---|
242 | stop_vms()
|
---|
243 | {
|
---|
244 | wait=0
|
---|
245 | for i in $SHUTDOWN_USERS; do
|
---|
246 | # don't create the ipcd directory with wrong permissions!
|
---|
247 | if [ -d /tmp/.vbox-$i-ipc ]; then
|
---|
248 | export VBOX_IPC_SOCKETID="$i"
|
---|
249 | VMS=`$VBOXMANAGE --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/' 2>/dev/null`
|
---|
250 | if [ -n "$VMS" ]; then
|
---|
251 | if [ "$SHUTDOWN" = "poweroff" ]; then
|
---|
252 | begin_msg "Powering off remaining VMs"
|
---|
253 | for v in $VMS; do
|
---|
254 | $VBOXMANAGE --nologo controlvm $v poweroff
|
---|
255 | done
|
---|
256 | succ_msg
|
---|
257 | elif [ "$SHUTDOWN" = "acpibutton" ]; then
|
---|
258 | begin_msg "Sending ACPI power button event to remaining VMs"
|
---|
259 | for v in $VMS; do
|
---|
260 | $VBOXMANAGE --nologo controlvm $v acpipowerbutton
|
---|
261 | wait=30
|
---|
262 | done
|
---|
263 | succ_msg
|
---|
264 | elif [ "$SHUTDOWN" = "savestate" ]; then
|
---|
265 | begin_msg "Saving state of remaining VMs"
|
---|
266 | for v in $VMS; do
|
---|
267 | $VBOXMANAGE --nologo controlvm $v savestate
|
---|
268 | done
|
---|
269 | succ_msg
|
---|
270 | fi
|
---|
271 | fi
|
---|
272 | fi
|
---|
273 | done
|
---|
274 | # wait for some seconds when doing ACPI shutdown
|
---|
275 | if [ "$wait" -ne 0 ]; then
|
---|
276 | begin_msg "Waiting for $wait seconds for VM shutdown"
|
---|
277 | sleep $wait
|
---|
278 | succ_msg
|
---|
279 | fi
|
---|
280 | }
|
---|
281 |
|
---|
282 | setup()
|
---|
283 | {
|
---|
284 | stop
|
---|
285 | if find /lib/modules/`uname -r` -name "vboxnetadp\.*" 2>/dev/null|grep -q vboxnetadp; then
|
---|
286 | begin_msg "Removing old VirtualBox netadp kernel module"
|
---|
287 | find /lib/modules/`uname -r` -name "vboxnetadp\.*" 2>/dev/null|xargs rm -f 2>/dev/null
|
---|
288 | succ_msg
|
---|
289 | fi
|
---|
290 | if find /lib/modules/`uname -r` -name "vboxnetflt\.*" 2>/dev/null|grep -q vboxnetflt; then
|
---|
291 | begin_msg "Removing old VirtualBox netflt kernel module"
|
---|
292 | find /lib/modules/`uname -r` -name "vboxnetflt\.*" 2>/dev/null|xargs rm -f 2>/dev/null
|
---|
293 | succ_msg
|
---|
294 | fi
|
---|
295 | if find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|grep -q vboxdrv; then
|
---|
296 | begin_msg "Removing old VirtualBox kernel module"
|
---|
297 | find /lib/modules/`uname -r` -name "vboxdrv\.*" 2>/dev/null|xargs rm -f 2>/dev/null
|
---|
298 | succ_msg
|
---|
299 | fi
|
---|
300 | begin_msg "Recompiling VirtualBox kernel module"
|
---|
301 | if ! $BUILDVBOXDRV \
|
---|
302 | --save-module-symvers /tmp/vboxdrv-Module.symvers \
|
---|
303 | --no-print-directory install > $LOG 2>&1; then
|
---|
304 | failure "Look at $LOG to find out what went wrong"
|
---|
305 | fi
|
---|
306 | if ! $BUILDVBOXNETFLT \
|
---|
307 | --use-module-symvers /tmp/vboxdrv-Module.symvers \
|
---|
308 | --no-print-directory install >> $LOG 2>&1; then
|
---|
309 | failure "Look at $LOG to find out what went wrong"
|
---|
310 | fi
|
---|
311 | if ! $BUILDVBOXNETADP \
|
---|
312 | --use-module-symvers /tmp/vboxdrv-Module.symvers \
|
---|
313 | --no-print-directory install >> $LOG 2>&1; then
|
---|
314 | failure "Look at $LOG to find out what went wrong"
|
---|
315 | fi
|
---|
316 | rm -f /etc/vbox/module_not_compiled
|
---|
317 | succ_msg
|
---|
318 | start
|
---|
319 | }
|
---|
320 |
|
---|
321 | dmnstatus()
|
---|
322 | {
|
---|
323 | if running vboxdrv; then
|
---|
324 | if running vboxnetflt; then
|
---|
325 | if running vboxnetadp; then
|
---|
326 | echo "VirtualBox kernel modules (vboxdrv, vboxnetflt and vboxnetadp) are loaded."
|
---|
327 | else
|
---|
328 | echo "VirtualBox kernel modules (vboxdrv and vboxnetflt) are loaded."
|
---|
329 | fi
|
---|
330 | else
|
---|
331 | echo "VirtualBox kernel module is loaded."
|
---|
332 | fi
|
---|
333 | for i in $SHUTDOWN_USERS; do
|
---|
334 | # don't create the ipcd directory with wrong permissions!
|
---|
335 | if [ -d /tmp/.vbox-$i-ipc ]; then
|
---|
336 | export VBOX_IPC_SOCKETID="$i"
|
---|
337 | VMS=`$VBOXMANAGE --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/' 2>/dev/null`
|
---|
338 | if [ -n "$VMS" ]; then
|
---|
339 | echo "The following VMs are currently running:"
|
---|
340 | for v in $VMS; do
|
---|
341 | echo " $v"
|
---|
342 | done
|
---|
343 | fi
|
---|
344 | fi
|
---|
345 | done
|
---|
346 | else
|
---|
347 | echo "VirtualBox kernel module is not loaded."
|
---|
348 | fi
|
---|
349 | }
|
---|
350 |
|
---|
351 | case "$1" in
|
---|
352 | start)
|
---|
353 | start
|
---|
354 | ;;
|
---|
355 | stop)
|
---|
356 | stop_vms
|
---|
357 | stop
|
---|
358 | ;;
|
---|
359 | stop_vms)
|
---|
360 | stop_vms
|
---|
361 | ;;
|
---|
362 | restart)
|
---|
363 | stop && start
|
---|
364 | ;;
|
---|
365 | force-reload)
|
---|
366 | stop
|
---|
367 | start
|
---|
368 | ;;
|
---|
369 | setup)
|
---|
370 | setup
|
---|
371 | ;;
|
---|
372 | status)
|
---|
373 | dmnstatus
|
---|
374 | ;;
|
---|
375 | *)
|
---|
376 | echo "Usage: $0 {start|stop|stop_vms|restart|force-reload|status|setup}"
|
---|
377 | exit 1
|
---|
378 | esac
|
---|
379 |
|
---|
380 | exit 0
|
---|