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