VirtualBox

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

Last change on this file since 11982 was 9789, checked in by vboxsync, 16 years ago

Installer/linux: fixed a bug in the creation of tap host interfaces belonging to groups and cleaned up that code slightly

File size: 10.4 KB
Line 
1#! /bin/sh
2# Sun xVM VirtualBox
3# Linux static host networking interface initialization
4#
5
6#
7# Copyright (C) 2007 Sun Microsystems, Inc.
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: 2 3 4 5
26# Default-Stop: 0 1 6
27# Description: VirtualBox permanent host networking setup
28### END INIT INFO
29
30PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
31CONFIG="/etc/vbox/interfaces"
32VARDIR="/var/run/VirtualBox"
33VARFILE="/var/run/VirtualBox/vboxnet"
34TAPDEV="/dev/net/tun"
35NOLSB=%NOLSB%
36
37[ -f /lib/lsb/init-functions ] || NOLSB=yes
38
39if [ -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
47fi
48
49if [ -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 }
61else
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
117fi
118
119failure()
120{
121 fail_msg "$1"
122 # never return with exit code != 0
123 exit 0
124}
125
126running()
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.
135start_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 # Name our parameters, to make this script slightly less unreadable
183 interface=$1
184 user=$2
185 bridge=$3
186 # As the very first thing, try delete the interface. Might already
187 # exist with different configuration. Ignore errors.
188 VBoxTunctl -d $interface > /dev/null 2>&1
189 case $user in
190 +*)
191 group=`echo $user | cut -c2-`
192 cmd="VBoxTunctl -t $interface -g $group"
193 ;;
194 *)
195 cmd="VBoxTunctl -t $interface -u $user"
196 ;;
197 esac
198 # Try to create the interface
199 if $cmd > /dev/null 2>&1; then
200 # On SUSE Linux Enterprise Server, the interface does not
201 # appear immediately, so we loop trying to bring it up.
202 i=1
203 while [ $i -le 10 ]; do
204 ifconfig "$interface" up 2> /dev/null
205 if ifconfig | grep "$interface" > /dev/null; then
206 # Add the interface to a bridge if one was specified
207 if [ -n "$bridge" ]; then
208 if brctl addif "$bridge" "$interface" 2> /dev/null; then
209 echo "$interface $user $bridge" >> "$VARFILE"
210 else
211 echo "$interface $user" >> "$VARFILE"
212 echo "Warning - failed to add interface $interface to the bridge $bridge"
213 fi
214 else
215 echo "$interface $user" >> "$VARFILE"
216 fi
217 i=20
218 else
219 i=`expr $i + 1`
220 sleep .1
221 fi
222 done
223 if [ $i -ne 20 ]; then
224 echo "Warning - failed to bring up the interface $interface"
225 fi
226 else
227 echo "Warning - failed to create the interface $interface for the user $user"
228 fi
229 else
230 echo "Warning - invalid line in $CONFIG:"
231 echo " $line"
232 fi
233 fi
234 done < "$CONFIG"
235 # Set /dev/net/tun to belong to the group vboxusers if it exists and does
236 # yet belong to a group.
237 if ls -g "$TAPDEV" 2>/dev/null | grep root > /dev/null; then
238 chgrp vboxusers "$TAPDEV"
239 chmod 0660 "$TAPDEV"
240 fi
241 return 0
242}
243
244# Shut down VirtualBox host networking and remove all permanent TAP
245# interfaces. This action will fail if some interfaces could not be removed.
246stop_network()
247{
248 begin_msg "Shutting down VirtualBox host networking"
249 # If there is no runtime record file, assume that the service is not
250 # running.
251 if [ ! -f "$VARFILE" ]; then
252 succ_msg
253 return 0
254 fi
255 # Fail if we can't read our runtime record file or write to the
256 # folder it is located in
257 if [ ! -r "$VARFILE" -o ! -w "$VARDIR" ]; then
258 failure "Failed to read $VARFILE or to write $VARDIR"
259 fi
260 # Fail if we don't have tunctl
261 if ! VBoxTunctl -h 2>&1 | grep VBoxTunctl > /dev/null; then
262 failure "VBoxTunctl not found"
263 fi
264 # Read the runtime record file entries line by line and delete the
265 # interfaces. The format of the runtime record file is not checked for
266 # errors.
267 while read line; do
268 set ""$line
269 # Remove the interface from a bridge if it is part of one
270 if [ -n "$3" ]; then
271 brctl delif "$3" "$1" 2> /dev/null
272 fi
273 # Remove the interface. Roll back everything and fail if this is not
274 # possible
275 if (! ifconfig "$1" down 2> /dev/null ||
276 ! VBoxTunctl -d "$1" > /dev/null 2>&1); then
277 while read line; do
278 set ""$line
279 VBoxTunctl -t "$1" -u "$2" > /dev/null 2>&1
280 ifconfig "$1" up 2> /dev/null
281 if [ -n "$3" ]; then
282 brctl addif "$3" "$1"
283 fi
284 done < "$VARFILE"
285 fi
286 done < "$VARFILE"
287 rm -f "$VARFILE" 2> /dev/null
288 succ_msg
289 return 0
290}
291
292# Shut down VirtualBox host networking and remove all permanent TAP
293# interfaces. This action will succeed even if not all interfaces could be
294# removed. It is only intended for exceptional circumstances such as
295# uninstalling VirtualBox.
296force_stop_network()
297{
298 begin_msg "Shutting down VirtualBox host networking"
299 # If there is no runtime record file, assume that the service is not
300 # running.
301 if [ ! -f "$VARFILE" ]; then
302 succ_msg
303 return 0
304 fi
305 # Fail if we can't read our runtime record file or write to the
306 # folder it is located in
307 if [ ! -r "$VARFILE" -o ! -w "$VARDIR" ]; then
308 failure "Failed to read $VARFILE or to write $VARDIR"
309 fi
310 # Fail if we don't have tunctl
311 if ! VBoxTunctl -h 2>&1 | grep VBoxTunctl > /dev/null; then
312 failure "VBoxTunctl not found"
313 fi
314 # Read the runtime record file entries line by line and delete the
315 # interfaces. The format of the runtime record file is not checked for
316 # errors.
317 while read line; do
318 set ""$line
319 # Remove the interface from a bridge if it is part of one
320 if [ -n "$3" ]; then
321 brctl delif "$3" "$1" 2> /dev/null
322 fi
323 # Remove the interface.
324 ifconfig "$1" down 2> /dev/null
325 VBoxTunctl -d "$1" > /dev/null 2>&1
326 done < "$VARFILE"
327 rm -f "$VARFILE" 2> /dev/null
328 succ_msg
329 return 0
330}
331
332case "$1" in
333start)
334 start_network
335 ;;
336stop)
337 stop_network
338 ;;
339restart|reload)
340 stop_network && start_network
341 ;;
342force-reload)
343 stop_network
344 start_network
345 ;;
346force-stop)
347 force_stop_network
348 ;;
349status)
350 if running; then
351 echo "VirtualBox host networking is loaded."
352 else
353 echo "VirtualBox host networking is not loaded."
354 fi
355 ;;
356*)
357 echo "Usage: `basename $0` {start|stop|force-stop|restart|force-reload|status}"
358 exit 1
359esac
360
361exit 0
Note: See TracBrowser for help on using the repository browser.

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