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