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