VirtualBox

source: vbox/trunk/src/VBox/Installer/linux/vboxnet.sh@ 7031

Last change on this file since 7031 was 7031, checked in by vboxsync, 17 years ago

removed the superflous ifname_valid check

File size: 9.5 KB
Line 
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
30PATH=$PATH:/bin:/sbin:/usr/sbin
31CONFIG="/etc/vbox/interfaces"
32VARDIR="/var/run/VirtualBox"
33VARFILE="/var/run/VirtualBox/vboxnet"
34TAPDEV="/dev/net/tun"
35
36if [ -f /etc/redhat-release ]; then
37 system=redhat
38elif [ -f /etc/SuSE-release ]; then
39 system=suse
40elif [ -f /etc/gentoo-release ]; then
41 system=gentoo
42else
43 system=other
44fi
45
46if [ "$system" = "redhat" ]; then
47 . /etc/init.d/functions
48 fail_msg() {
49 echo_failure
50 echo
51 }
52
53 succ_msg() {
54 echo_success
55 echo
56 }
57
58 begin() {
59 echo -n "$1"
60 }
61fi
62
63if [ "$system" = "suse" ]; then
64 . /etc/rc.status
65 fail_msg() {
66 rc_failed 1
67 rc_status -v
68 }
69
70 succ_msg() {
71 rc_reset
72 rc_status -v
73 }
74
75 begin() {
76 echo -n "$1"
77 }
78fi
79
80if [ "$system" = "gentoo" ]; then
81 . /sbin/functions.sh
82 fail_msg() {
83 eend 1
84 }
85
86 succ_msg() {
87 eend $?
88 }
89
90 begin() {
91 ebegin $1
92 }
93
94 if [ "`which $0`" = "/sbin/rc" ]; then
95 shift
96 fi
97fi
98
99if [ "$system" = "other" ]; then
100 fail_msg() {
101 echo " ...fail!"
102 }
103
104 succ_msg() {
105 echo " ...done."
106 }
107
108 begin() {
109 echo -n $1
110 }
111fi
112
113fail() {
114 if [ "$system" = "gentoo" ]; then
115 eerror $1
116 exit 1
117 fi
118 fail_msg
119 echo "($1)"
120 exit 1
121}
122
123running() {
124 test -f "$VARFILE"
125}
126
127# Create all permanent TAP devices registered on the system, add them to a
128# bridge if required and keep a record of proceedings in the file
129# /var/run/VirtualBox/vboxnet. If this file already exists, assume that the
130# script has already been started and do nothing.
131start_network() {
132 begin "Starting VirtualBox host networking"
133 # If the service is already running, return successfully.
134 if [ -f "$VARFILE" ]
135 then
136 succ_msg
137 return 0
138 fi
139 # Fail if we can't create our runtime record file
140 if [ ! -d "$VARDIR" ]
141 then
142 if ! mkdir "$VARDIR" 2> /dev/null
143 then
144 fail_msg
145 return 1
146 fi
147 fi
148 if ! touch "$VARFILE" 2> /dev/null
149 then
150 fail_msg
151 return 1
152 fi
153 # If there is no configuration file, report success
154 if [ ! -f "$CONFIG" ]
155 then
156 succ_msg
157 return 0
158 fi
159 # Fail if we can't read our configuration
160 if [ ! -r "$CONFIG" ]
161 then
162 fail_msg
163 return 1
164 fi
165 # Fail if we don't have tunctl
166 if ! VBoxTunctl -h 2>&1 | grep VBoxTunctl > /dev/null
167 then
168 fail_msg
169 return 1
170 fi
171 # Fail if we don't have the kernel tun device
172 # Make sure that the tun module is loaded (Ubuntu 7.10 needs this)
173 modprobe tun > /dev/null 2>&1
174 if ! cat /proc/misc 2>/dev/null | grep tun > /dev/null
175 then
176 fail_msg
177 return 1
178 fi
179 succ_msg
180 # Read the configuration file entries line by line and create the
181 # interfaces
182 while read line
183 do
184 set ""$line
185 # If the line is a comment then ignore it
186 if ((! expr match "$1" "#" > /dev/null) && (! test -z "$1"))
187 then
188 # Check that the line is correctly formed (an interface name plus one
189 # or two non-comment entries, possibly followed by a comment).
190 if ((! expr match "$2" "#" > /dev/null) &&
191 (test -z "$4" || expr match "$4" "#" > /dev/null))
192 then
193 case $user in
194 +*)
195 group=`echo $2 | cut -c2-`
196 cmd="VBoxTunctl -t $1 -g $group"
197 ;;
198 *)
199 cmd="VBoxTunctl -t $1 -u $2"
200 ;;
201 esac
202 # Try to create the interface
203 if $cmd > /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.
257stop_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.
318force_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
360start() {
361 start_network
362}
363
364stop() {
365 stop_network
366}
367
368force_stop() {
369 force_stop_network
370}
371
372restart() {
373 stop_network && start_network
374}
375
376status() {
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
384case "$1" in
385start)
386 start
387 ;;
388stop)
389 stop
390 ;;
391restart)
392 restart
393 ;;
394force-reload)
395 restart
396 ;;
397force-stop)
398 force_stop
399 ;;
400status)
401 status
402 ;;
403*)
404 echo "Usage: `basename $0` {start|stop|restart|force-reload|status}"
405 exit 1
406esac
407
408exit
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette