1 | #! /bin/sh
|
---|
2 | # innotek VirtualBox
|
---|
3 | # Linux static host networking interface initialization
|
---|
4 | #
|
---|
5 |
|
---|
6 | #
|
---|
7 | # Copyright (C) 2007 innotek GmbH
|
---|
8 | #
|
---|
9 | # This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | # available from http://www.virtualbox.org. This file is free software;
|
---|
11 | # you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | # General Public License as published by the Free Software Foundation,
|
---|
13 | # in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | # distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | # be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 |
|
---|
17 | # chkconfig: 35 30 60
|
---|
18 | # description: VirtualBox permanent host networking setup
|
---|
19 | #
|
---|
20 | ### BEGIN INIT INFO
|
---|
21 | # Provides: vboxnet
|
---|
22 | # Required-Start: $network
|
---|
23 | # Required-Stop:
|
---|
24 | # Default-Start: 3 5
|
---|
25 | # Default-Stop:
|
---|
26 | # Description: VirtualBox permanent host networking setup
|
---|
27 | ### END INIT INFO
|
---|
28 |
|
---|
29 | PATH=$PATH:/bin:/sbin:/usr/sbin
|
---|
30 | CONFIG="/etc/vbox/interfaces"
|
---|
31 | VARDIR="/var/run/VirtualBox"
|
---|
32 | VARFILE="/var/run/VirtualBox/vboxnet"
|
---|
33 | TAPDEV="/dev/net/tun"
|
---|
34 |
|
---|
35 | if [ -f /etc/redhat-release ]; then
|
---|
36 | system=redhat
|
---|
37 | elif [ -f /etc/SuSE-release ]; then
|
---|
38 | system=suse
|
---|
39 | elif [ -f /etc/gentoo-release ]; then
|
---|
40 | system=gentoo
|
---|
41 | else
|
---|
42 | system=other
|
---|
43 | fi
|
---|
44 |
|
---|
45 | if [ "$system" = "redhat" ]; then
|
---|
46 | . /etc/init.d/functions
|
---|
47 | fail_msg() {
|
---|
48 | echo_failure
|
---|
49 | echo
|
---|
50 | }
|
---|
51 |
|
---|
52 | succ_msg() {
|
---|
53 | echo_success
|
---|
54 | echo
|
---|
55 | }
|
---|
56 |
|
---|
57 | begin() {
|
---|
58 | echo -n "$1"
|
---|
59 | }
|
---|
60 | fi
|
---|
61 |
|
---|
62 | if [ "$system" = "suse" ]; then
|
---|
63 | . /etc/rc.status
|
---|
64 | fail_msg() {
|
---|
65 | rc_failed 1
|
---|
66 | rc_status -v
|
---|
67 | }
|
---|
68 |
|
---|
69 | succ_msg() {
|
---|
70 | rc_reset
|
---|
71 | rc_status -v
|
---|
72 | }
|
---|
73 |
|
---|
74 | begin() {
|
---|
75 | echo -n "$1"
|
---|
76 | }
|
---|
77 | fi
|
---|
78 |
|
---|
79 | if [ "$system" = "gentoo" ]; then
|
---|
80 | . /sbin/functions.sh
|
---|
81 | fail_msg() {
|
---|
82 | eend 1
|
---|
83 | }
|
---|
84 |
|
---|
85 | succ_msg() {
|
---|
86 | eend $?
|
---|
87 | }
|
---|
88 |
|
---|
89 | begin() {
|
---|
90 | ebegin $1
|
---|
91 | }
|
---|
92 |
|
---|
93 | if [ "`which $0`" = "/sbin/rc" ]; then
|
---|
94 | shift
|
---|
95 | fi
|
---|
96 | fi
|
---|
97 |
|
---|
98 | if [ "$system" = "other" ]; then
|
---|
99 | fail_msg() {
|
---|
100 | echo " ...fail!"
|
---|
101 | }
|
---|
102 |
|
---|
103 | succ_msg() {
|
---|
104 | echo " ...done."
|
---|
105 | }
|
---|
106 |
|
---|
107 | begin() {
|
---|
108 | echo -n $1
|
---|
109 | }
|
---|
110 | fi
|
---|
111 |
|
---|
112 | fail() {
|
---|
113 | if [ "$system" = "gentoo" ]; then
|
---|
114 | eerror $1
|
---|
115 | exit 1
|
---|
116 | fi
|
---|
117 | fail_msg
|
---|
118 | echo "($1)"
|
---|
119 | exit 1
|
---|
120 | }
|
---|
121 |
|
---|
122 | running() {
|
---|
123 | test -f "$VARFILE"
|
---|
124 | }
|
---|
125 |
|
---|
126 | valid_ifname() {
|
---|
127 | if expr match "$1" "vbox[0-9][0-9]*$" > /dev/null 2>&1
|
---|
128 | then
|
---|
129 | return 0
|
---|
130 | else
|
---|
131 | return 1
|
---|
132 | fi
|
---|
133 | }
|
---|
134 |
|
---|
135 | # Create all permanent TAP devices registered on the system, add them to a
|
---|
136 | # bridge if required and keep a record of proceedings in the file
|
---|
137 | # /var/run/VirtualBox/vboxnet. If this file already exists, assume that the
|
---|
138 | # script has already been started and do nothing.
|
---|
139 | start_network() {
|
---|
140 | begin "Starting VirtualBox host networking"
|
---|
141 | # If the service is already running, return successfully.
|
---|
142 | if [ -f "$VARFILE" ]
|
---|
143 | then
|
---|
144 | succ_msg
|
---|
145 | return 0
|
---|
146 | fi
|
---|
147 | # Fail if we can't create our runtime record file
|
---|
148 | if [ ! -d "$VARDIR" ]
|
---|
149 | then
|
---|
150 | if ! mkdir "$VARDIR" 2> /dev/null
|
---|
151 | then
|
---|
152 | fail_msg
|
---|
153 | return 1
|
---|
154 | fi
|
---|
155 | fi
|
---|
156 | if ! touch "$VARFILE" 2> /dev/null
|
---|
157 | then
|
---|
158 | fail_msg
|
---|
159 | return 1
|
---|
160 | fi
|
---|
161 | # If there is no configuration file, report success
|
---|
162 | if [ ! -f "$CONFIG" ]
|
---|
163 | then
|
---|
164 | succ_msg
|
---|
165 | return 0
|
---|
166 | fi
|
---|
167 | # Fail if we can't read our configuration
|
---|
168 | if [ ! -r "$CONFIG" ]
|
---|
169 | then
|
---|
170 | fail_msg
|
---|
171 | return 1
|
---|
172 | fi
|
---|
173 | # Fail if we don't have tunctl
|
---|
174 | if ! VBoxTunctl -h 2>&1 | grep VBoxTunctl > /dev/null
|
---|
175 | then
|
---|
176 | fail_msg
|
---|
177 | return 1
|
---|
178 | fi
|
---|
179 | # Fail if we don't have the kernel tun device
|
---|
180 | # Make sure that the tun module is loaded (Ubuntu 7.10 needs this)
|
---|
181 | modprobe tun 2>&1 > /dev/null
|
---|
182 | if ! cat /proc/misc 2>/dev/null | grep tun > /dev/null
|
---|
183 | then
|
---|
184 | fail_msg
|
---|
185 | return 1
|
---|
186 | fi
|
---|
187 | succ_msg
|
---|
188 | # Read the configuration file entries line by line and create the
|
---|
189 | # interfaces
|
---|
190 | while read line
|
---|
191 | do
|
---|
192 | set ""$line
|
---|
193 | # If the line is a comment then ignore it
|
---|
194 | if ((! expr match "$1" "#" > /dev/null) && (! test -z "$1"))
|
---|
195 | then
|
---|
196 | # Check that the line is correctly formed (an interface name plus one
|
---|
197 | # or two non-comment entries, possibly followed by a comment).
|
---|
198 | if ((! expr match "$2" "#" > /dev/null) &&
|
---|
199 | (test -z "$4" || expr match "$4" "#" > /dev/null) &&
|
---|
200 | (valid_ifname "$1"))
|
---|
201 | then
|
---|
202 | # Try to create the interface
|
---|
203 | if VBoxTunctl -t "$1" -u "$2" > /dev/null 2>&1
|
---|
204 | then
|
---|
205 | # On SUSE Linux Enterprise Server, the interface does not
|
---|
206 | # appear immediately, so we loop trying to bring it up.
|
---|
207 | i=1
|
---|
208 | while [ $i -le 10 ]
|
---|
209 | do
|
---|
210 | ifconfig "$1" up 2> /dev/null
|
---|
211 | if ifconfig | grep "$1" > /dev/null 2>&1
|
---|
212 | then
|
---|
213 | # Add the interface to a bridge if one was specified
|
---|
214 | if [ ! -z "$3" ]
|
---|
215 | then
|
---|
216 | if brctl addif "$3" "$1" 2> /dev/null
|
---|
217 | then
|
---|
218 | echo "$1 $2 $3" > "$VARFILE"
|
---|
219 | else
|
---|
220 | echo "$1 $2" > "$VARFILE"
|
---|
221 | echo "Warning - failed to add interface $1 to the bridge $3"
|
---|
222 | fi
|
---|
223 | else
|
---|
224 | echo "$1 $2" > $VARFILE
|
---|
225 | fi
|
---|
226 | i=20
|
---|
227 | else
|
---|
228 | i=`expr $i + 1`
|
---|
229 | sleep .1
|
---|
230 | fi
|
---|
231 | done
|
---|
232 | if [ $i -ne 20 ]
|
---|
233 | then
|
---|
234 | echo "Warning - failed to bring up the interface $1"
|
---|
235 | fi
|
---|
236 | else
|
---|
237 | echo "Warning - failed to create the interface $1 for the user $2"
|
---|
238 | fi
|
---|
239 | else
|
---|
240 | echo "Warning - invalid line in $CONFIG:"
|
---|
241 | echo " $line"
|
---|
242 | fi
|
---|
243 | fi
|
---|
244 | done < "$CONFIG"
|
---|
245 | # Set /dev/net/tun to belong to the group vboxusers if it exists and does
|
---|
246 | # yet belong to a group.
|
---|
247 | if ls -g "$TAPDEV" 2>/dev/null | grep root 2>&1 > /dev/null
|
---|
248 | then
|
---|
249 | chgrp vboxusers "$TAPDEV"
|
---|
250 | chmod 0660 "$TAPDEV"
|
---|
251 | fi
|
---|
252 | return 0
|
---|
253 | }
|
---|
254 |
|
---|
255 | # Shut down VirtualBox host networking and remove all permanent TAP
|
---|
256 | # interfaces. This action will fail if some interfaces could not be removed.
|
---|
257 | stop_network() {
|
---|
258 | begin "Shutting down VirtualBox host networking"
|
---|
259 | # If there is no runtime record file, assume that the service is not
|
---|
260 | # running.
|
---|
261 | if [ ! -f "$VARFILE" ]
|
---|
262 | then
|
---|
263 | succ_msg
|
---|
264 | return 0
|
---|
265 | fi
|
---|
266 | # Fail if we can't read our runtime record file or write to the
|
---|
267 | # folder it is located in
|
---|
268 | if [ ! -r "$VARFILE" -o ! -w "$VARDIR" ]
|
---|
269 | then
|
---|
270 | fail_msg
|
---|
271 | return 1
|
---|
272 | fi
|
---|
273 | # Fail if we don't have tunctl
|
---|
274 | if ! VBoxTunctl -h 2>&1 | grep VBoxTunctl > /dev/null
|
---|
275 | then
|
---|
276 | fail_msg
|
---|
277 | return 1
|
---|
278 | fi
|
---|
279 | # Read the runtime record file entries line by line and delete the
|
---|
280 | # interfaces. The format of the runtime record file is not checked for
|
---|
281 | # errors.
|
---|
282 | while read line
|
---|
283 | do
|
---|
284 | set ""$line
|
---|
285 | # Remove the interface from a bridge if it is part of one
|
---|
286 | if [ ! -z "$3" ]
|
---|
287 | then
|
---|
288 | brctl delif "$3" "$1" 2> /dev/null
|
---|
289 | fi
|
---|
290 | # Remove the interface. Roll back everything and fail if this is not
|
---|
291 | # possible
|
---|
292 | if (! ifconfig "$1" down 2> /dev/null ||
|
---|
293 | ! VBoxTunctl -d "$1" > /dev/null 2>&1)
|
---|
294 | then
|
---|
295 | while read line
|
---|
296 | do
|
---|
297 | set ""$line
|
---|
298 | VBoxTunctl -t "$1" -u "$2" > /dev/null 2>&1
|
---|
299 | ifconfig "$1" up 2> /dev/null
|
---|
300 | if [ ! -z "$3" ]
|
---|
301 | then
|
---|
302 | brctl addif "$3" "$1"
|
---|
303 | fi
|
---|
304 | done < "$VARFILE"
|
---|
305 | fail_msg
|
---|
306 | return 1
|
---|
307 | fi
|
---|
308 | done < "$VARFILE"
|
---|
309 | rm -f "$VARFILE" 2> /dev/null
|
---|
310 | succ_msg
|
---|
311 | return 0
|
---|
312 | }
|
---|
313 |
|
---|
314 | # Shut down VirtualBox host networking and remove all permanent TAP
|
---|
315 | # interfaces. This action will succeed even if not all interfaces could be
|
---|
316 | # removed. It is only intended for exceptional circumstances such as
|
---|
317 | # uninstalling VirtualBox.
|
---|
318 | force_stop_network() {
|
---|
319 | begin "Shutting down VirtualBox host networking"
|
---|
320 | # If there is no runtime record file, assume that the service is not
|
---|
321 | # running.
|
---|
322 | if [ ! -f "$VARFILE" ]
|
---|
323 | then
|
---|
324 | succ_msg
|
---|
325 | return 0
|
---|
326 | fi
|
---|
327 | # Fail if we can't read our runtime record file or write to the
|
---|
328 | # folder it is located in
|
---|
329 | if [ ! -r "$VARFILE" -o ! -w "$VARDIR" ]
|
---|
330 | then
|
---|
331 | fail_msg
|
---|
332 | return 1
|
---|
333 | fi
|
---|
334 | # Fail if we don't have tunctl
|
---|
335 | if ! VBoxTunctl -h 2>&1 | grep VBoxTunctl > /dev/null
|
---|
336 | then
|
---|
337 | fail_msg
|
---|
338 | return 1
|
---|
339 | fi
|
---|
340 | # Read the runtime record file entries line by line and delete the
|
---|
341 | # interfaces. The format of the runtime record file is not checked for
|
---|
342 | # errors.
|
---|
343 | while read line
|
---|
344 | do
|
---|
345 | set ""$line
|
---|
346 | # Remove the interface from a bridge if it is part of one
|
---|
347 | if [ ! -z "$3" ]
|
---|
348 | then
|
---|
349 | brctl delif "$3" "$1" 2> /dev/null
|
---|
350 | fi
|
---|
351 | # Remove the interface.
|
---|
352 | ifconfig "$1" down 2> /dev/null
|
---|
353 | VBoxTunctl -d "$1" > /dev/null 2>&1
|
---|
354 | done < "$VARFILE"
|
---|
355 | rm -f "$VARFILE" 2> /dev/null
|
---|
356 | succ_msg
|
---|
357 | return 0
|
---|
358 | }
|
---|
359 |
|
---|
360 | start() {
|
---|
361 | start_network
|
---|
362 | }
|
---|
363 |
|
---|
364 | stop() {
|
---|
365 | stop_network
|
---|
366 | }
|
---|
367 |
|
---|
368 | force_stop() {
|
---|
369 | force_stop_network
|
---|
370 | }
|
---|
371 |
|
---|
372 | restart() {
|
---|
373 | stop_network && start_network
|
---|
374 | }
|
---|
375 |
|
---|
376 | status() {
|
---|
377 | if running; then
|
---|
378 | echo "VirtualBox host networking is loaded."
|
---|
379 | else
|
---|
380 | echo "VirtualBox host networking is not loaded."
|
---|
381 | fi
|
---|
382 | }
|
---|
383 |
|
---|
384 | case "$1" in
|
---|
385 | start)
|
---|
386 | start
|
---|
387 | ;;
|
---|
388 | stop)
|
---|
389 | stop
|
---|
390 | ;;
|
---|
391 | restart)
|
---|
392 | restart
|
---|
393 | ;;
|
---|
394 | force-reload)
|
---|
395 | restart
|
---|
396 | ;;
|
---|
397 | force-stop)
|
---|
398 | force_stop
|
---|
399 | ;;
|
---|
400 | status)
|
---|
401 | status
|
---|
402 | ;;
|
---|
403 | *)
|
---|
404 | echo "Usage: `basename $0` {start|stop|restart|force-reload|status}"
|
---|
405 | exit 1
|
---|
406 | esac
|
---|
407 |
|
---|
408 | exit
|
---|