VirtualBox

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

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

sync deb

File size: 8.8 KB
Line 
1#! /bin/sh
2#
3# innotek VirtualBox
4# Linux static host networking interface initialization
5#
6
7#
8# Copyright (C) 2007 innotek GmbH
9#
10# This file is part of VirtualBox Open Source Edition (OSE), as
11# available from http://www.virtualbox.org. This file is free software;
12# you can redistribute it and/or modify it under the terms of the GNU
13# General Public License (GPL) as published by the Free Software
14# Foundation, in version 2 as it comes in the "COPYING" file of the
15# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17#
18
19# chkconfig: 35 30 60
20# description: VirtualBox permanent host networking setup
21#
22### BEGIN INIT INFO
23# Provides: vboxnet
24# Required-Start: $network
25# Required-Stop:
26# Default-Start: 3 5
27# Default-Stop:
28# Short-Description: VirtualBox permanent host networking setup
29### END INIT INFO
30
31PATH=$PATH:/bin:/sbin:/usr/sbin
32CONFIG="/etc/vbox/interfaces"
33VARDIR="/var/run/VirtualBox"
34VARFILE="/var/run/VirtualBox/vboxnet"
35TAPDEV="/dev/net/tun"
36NOLSB=%LSB%
37
38if [ -z "$NOLSB" -a -f /lib/lsb/init-functions ]; then
39 . /lib/lsb/init-functions
40else
41 log_action_begin_msg()
42 {
43 echo -n "$@..."
44 }
45 log_action_end_msg()
46 {
47 if [ -z "${2:-}" ]; then
48 end="."
49 else
50 end=" ($2)."
51 fi
52 if [ $1 -eq 0 ]; then
53 echo "done${end}"
54 else
55 echo "failed${end}"
56 fi
57 }
58 log_success_msg()
59 {
60 echo "$@"
61 }
62 log_failure_msg()
63 {
64 echo "$@"
65 }
66fi
67
68failure()
69{
70 log_action_end_msg 1 "$1"
71 exit 0
72}
73
74running()
75{
76 test -f "$VARFILE"
77}
78
79valid_ifname()
80{
81 if expr match "$1" "vbox[0-9][0-9]*$" > /dev/null 2>&1; then
82 return 0
83 else
84 return 1
85 fi
86}
87
88# Create all permanent TAP devices registered on the system, add them to a
89# bridge if required and keep a record of proceedings in the file
90# /var/run/VirtualBox/vboxnet. If this file already exists, assume that the
91# script has already been started and do nothing.
92start_network() {
93 log_action_begin_msg "Starting VirtualBox host networking"
94 # If the service is already running, return successfully.
95 if [ -f "$VARFILE" ]; then
96 log_action_end_msg 0
97 return 0
98 fi
99 # Fail if we can't create our runtime record file
100 if [ ! -d "$VARDIR" ]; then
101 if ! mkdir "$VARDIR" 2> /dev/null; then
102 failure "Cannot create $VARDIR"
103 fi
104 fi
105 if ! touch "$VARFILE" 2> /dev/null; then
106 failure "Cannot create $VARFILE"
107 fi
108 # If there is no configuration file, report success
109 if [ ! -f "$CONFIG" ]; then
110 log_action_end_msg 0
111 return 0
112 fi
113 # Fail if we can't read our configuration
114 if [ ! -r "$CONFIG" ]; then
115 failure "Cannot read $CONFIG"
116 fi
117 # Fail if we don't have tunctl
118 if ! VBoxTunctl -h 2>&1 | grep -q VBoxTunctl > /dev/null; then
119 failure "VBoxTunctl not found"
120 fi
121 # Fail if we don't have the kernel tun device
122 # Make sure that the tun module is loaded (Ubuntu 7.10 needs this)
123 modprobe tun > /dev/null 2>&1
124 if ! cat /proc/misc 2>/dev/null | grep tun > /dev/null
125 then
126 failure "Linux tun/tap subsystem not available"
127 fi
128 # Read the configuration file entries line by line and create the
129 # interfaces
130 while read line; do
131 set ""$line
132 # If the line is a comment then ignore it
133 if ((! expr match "$1" "#" > /dev/null) && (! test -z "$1")); then
134 # Check that the line is correctly formed (an interface name plus one
135 # or two non-comment entries, possibly followed by a comment).
136 if ((! expr match "$2" "#" > /dev/null) &&
137 (test -z "$4" || expr match "$4" "#" > /dev/null) &&
138 (valid_ifname "$1"))
139 then
140 # Try to create the interface
141 if VBoxTunctl -t "$1" -u "$2" > /dev/null 2>&1
142 then
143 # On SUSE Linux Enterprise Server, the interface does not
144 # appear immediately, so we loop trying to bring it up.
145 i=1
146 while [ $i -le 10 ]
147 do
148 ifconfig "$1" up 2> /dev/null
149 if ifconfig | grep "$1" > /dev/null 2>&1
150 then
151 # Add the interface to a bridge if one was specified
152 if [ ! -z "$3" ]
153 then
154 if brctl addif "$3" "$1" 2> /dev/null
155 then
156 echo "$1 $2 $3" > "$VARFILE"
157 else
158 echo "$1 $2" > "$VARFILE"
159 echo "Warning - failed to add interface $1 to the bridge $3"
160 fi
161 else
162 echo "$1 $2" > $VARFILE
163 fi
164 i=20
165 else
166 i=`expr $i + 1`
167 sleep .1
168 fi
169 done
170 if [ $i -ne 20 ]
171 then
172 echo "Warning - failed to bring up the interface $1"
173 fi
174 else
175 echo "Warning - failed to create the interface $1 for the user $2"
176 fi
177 else
178 echo
179 echo "Warning - invalid line in $CONFIG:"
180 echo " $line"
181 fi
182 fi
183 done < "$CONFIG"
184 # Set /dev/net/tun to belong to the group vboxusers if it exists and does
185 # yet belong to a group.
186 if ls -g "$TAPDEV" 2>/dev/null | grep -q root; then
187 chgrp vboxusers "$TAPDEV"
188 chmod 0660 "$TAPDEV"
189 fi
190 log_action_end_msg 0
191 return 0
192}
193
194# Shut down VirtualBox host networking and remove all permanent TAP
195# interfaces. This action will fail if some interfaces could not be removed.
196stop_network() {
197 log_action_begin_msg "Shutting down VirtualBox host networking"
198 # If there is no runtime record file, assume that the service is not
199 # running.
200 if [ ! -f "$VARFILE" ]; then
201 log_action_end_msg 0
202 return 0
203 fi
204 # Fail if we can't read our runtime record file or write to the
205 # folder it is located in
206 if [ ! -r "$VARFILE" -o ! -w "$VARDIR" ]; then
207 failure "Failed to read $VARFILE or to write $VARDIR"
208 fi
209 # Fail if we don't have tunctl
210 if ! VBoxTunctl -h 2>&1 | grep -q VBoxTunctl > /dev/null; then
211 failure "VBoxTunctl not found"
212 fi
213 # Read the runtime record file entries line by line and delete the
214 # interfaces. The format of the runtime record file is not checked for
215 # errors.
216 while read line; do
217 set ""$line
218 # Remove the interface from a bridge if it is part of one
219 if [ ! -z "$3" ]; then
220 brctl delif "$3" "$1" 2> /dev/null
221 fi
222 # Remove the interface. Roll back everything and fail if this is not
223 # possible
224 if (! ifconfig "$1" down 2> /dev/null ||
225 ! VBoxTunctl -d "$1" > /dev/null 2>&1)
226 then
227 while read line; do
228 set ""$line
229 VBoxTunctl -t "$1" -u "$2" > /dev/null 2>&1
230 ifconfig "$1" up 2> /dev/null
231 if [ ! -z "$3" ]; then
232 brctl addif "$3" "$1"
233 fi
234 done < "$VARFILE"
235 failure "Removing of interface failed"
236 fi
237 done < "$VARFILE"
238 rm -f "$VARFILE" 2> /dev/null
239 log_action_end_msg 0
240 return 0
241}
242
243# Shut down VirtualBox host networking and remove all permanent TAP
244# interfaces. This action will succeed even if not all interfaces could be
245# removed. It is only intended for exceptional circumstances such as
246# uninstalling VirtualBox.
247force_stop_network() {
248 log_action_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 log_action_end_msg 0
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 -q 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 [ ! -z "$3" ]; then
271 brctl delif "$3" "$1" 2> /dev/null
272 fi
273 # Remove the interface.
274 ifconfig "$1" down 2> /dev/null
275 VBoxTunctl -d "$1" > /dev/null 2>&1
276 done < "$VARFILE"
277 rm -f "$VARFILE" 2> /dev/null
278 log_action_end_msg 0
279 return 0
280}
281
282case "$1" in
283start)
284 start_network
285 ;;
286stop)
287 stop_network
288 ;;
289restart|reload|force-reload)
290 stop_network
291 start_network
292 ;;
293force-stop)
294 force_stop_network
295 ;;
296status)
297 if running; then
298 log_success_msg "VirtualBox host networking is loaded."
299 else
300 log_failure_msg "VirtualBox host networking is not loaded."
301 fi
302 ;;
303*)
304 echo "Usage: `basename $0` {start|stop|force-stop|restart|force-reload|status}"
305 exit 1
306esac
307
308exit
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