VirtualBox

source: vbox/trunk/debian/vboxnet.init.in@ 8583

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

sync debian

File size: 10.1 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: 3 5
26# Default-Stop:
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 # 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.
242stop_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 fi
282 done < "$VARFILE"
283 rm -f "$VARFILE" 2> /dev/null
284 succ_msg
285 return 0
286}
287
288# Shut down VirtualBox host networking and remove all permanent TAP
289# interfaces. This action will succeed even if not all interfaces could be
290# removed. It is only intended for exceptional circumstances such as
291# uninstalling VirtualBox.
292force_stop_network()
293{
294 begin_msg "Shutting down VirtualBox host networking"
295 # If there is no runtime record file, assume that the service is not
296 # running.
297 if [ ! -f "$VARFILE" ]; then
298 succ_msg
299 return 0
300 fi
301 # Fail if we can't read our runtime record file or write to the
302 # folder it is located in
303 if [ ! -r "$VARFILE" -o ! -w "$VARDIR" ]; then
304 failure "Failed to read $VARFILE or to write $VARDIR"
305 fi
306 # Fail if we don't have tunctl
307 if ! VBoxTunctl -h 2>&1 | grep VBoxTunctl > /dev/null; then
308 failure "VBoxTunctl not found"
309 fi
310 # Read the runtime record file entries line by line and delete the
311 # interfaces. The format of the runtime record file is not checked for
312 # errors.
313 while read line; do
314 set ""$line
315 # Remove the interface from a bridge if it is part of one
316 if [ -n "$3" ]; then
317 brctl delif "$3" "$1" 2> /dev/null
318 fi
319 # Remove the interface.
320 ifconfig "$1" down 2> /dev/null
321 VBoxTunctl -d "$1" > /dev/null 2>&1
322 done < "$VARFILE"
323 rm -f "$VARFILE" 2> /dev/null
324 succ_msg
325 return 0
326}
327
328case "$1" in
329start)
330 start_network
331 ;;
332stop)
333 stop_network
334 ;;
335restart|reload)
336 stop_network && start_network
337 ;;
338force-reload)
339 stop_network
340 start_network
341 ;;
342force-stop)
343 force_stop_network
344 ;;
345status)
346 if running; then
347 echo "VirtualBox host networking is loaded."
348 else
349 echo "VirtualBox host networking is not loaded."
350 fi
351 ;;
352*)
353 echo "Usage: `basename $0` {start|stop|force-stop|restart|force-reload|status}"
354 exit 1
355esac
356
357exit 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