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