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