VirtualBox

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

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

Linux kernel module scripts: unify output functions; mount/umount all vboxsf during boot/shutdown

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