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 | # Short-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 | # Fail if we don't have the kernel tun device
|
---|
116 | # Make sure that the tun module is loaded (Ubuntu 7.10 needs this)
|
---|
117 | modprobe tun > /dev/null 2>&1
|
---|
118 | if ! cat /proc/misc 2>/dev/null | grep tun > /dev/null
|
---|
119 | then
|
---|
120 | failure "Linux tun/tap subsystem not available"
|
---|
121 | fi
|
---|
122 | # Read the configuration file entries line by line and create the
|
---|
123 | # interfaces
|
---|
124 | while read line; do
|
---|
125 | set ""$line
|
---|
126 | # If the line is a comment then ignore it
|
---|
127 | if ((! expr match "$1" "#" > /dev/null) && (! test -z "$1")); then
|
---|
128 | # Check that the line is correctly formed (an interface name plus one
|
---|
129 | # or two non-comment entries, possibly followed by a comment).
|
---|
130 | if ((! expr match "$2" "#" > /dev/null) &&
|
---|
131 | (test -z "$4" || expr match "$4" "#" > /dev/null) &&
|
---|
132 | (valid_ifname "$1"))
|
---|
133 | then
|
---|
134 | # Try to create the interface
|
---|
135 | if VBoxTunctl -t "$1" -u "$2" > /dev/null 2>&1
|
---|
136 | then
|
---|
137 | # On SUSE Linux Enterprise Server, the interface does not
|
---|
138 | # appear immediately, so we loop trying to bring it up.
|
---|
139 | i=1
|
---|
140 | while [ $i -le 10 ]
|
---|
141 | do
|
---|
142 | ifconfig "$1" up 2> /dev/null
|
---|
143 | if ifconfig | grep "$1" > /dev/null 2>&1
|
---|
144 | then
|
---|
145 | # Add the interface to a bridge if one was specified
|
---|
146 | if [ ! -z "$3" ]
|
---|
147 | then
|
---|
148 | if brctl addif "$3" "$1" 2> /dev/null
|
---|
149 | then
|
---|
150 | echo "$1 $2 $3" > "$VARFILE"
|
---|
151 | else
|
---|
152 | echo "$1 $2" > "$VARFILE"
|
---|
153 | echo "Warning - failed to add interface $1 to the bridge $3"
|
---|
154 | fi
|
---|
155 | else
|
---|
156 | echo "$1 $2" > $VARFILE
|
---|
157 | fi
|
---|
158 | i=20
|
---|
159 | else
|
---|
160 | i=`expr $i + 1`
|
---|
161 | sleep .1
|
---|
162 | fi
|
---|
163 | done
|
---|
164 | if [ $i -ne 20 ]
|
---|
165 | then
|
---|
166 | echo "Warning - failed to bring up the interface $1"
|
---|
167 | fi
|
---|
168 | else
|
---|
169 | echo "Warning - failed to create the interface $1 for the user $2"
|
---|
170 | fi
|
---|
171 | else
|
---|
172 | echo
|
---|
173 | echo "Warning - invalid line in $CONFIG:"
|
---|
174 | echo " $line"
|
---|
175 | fi
|
---|
176 | fi
|
---|
177 | done < "$CONFIG"
|
---|
178 | # Set /dev/net/tun to belong to the group vboxusers if it exists and does
|
---|
179 | # yet belong to a group.
|
---|
180 | if ls -g "$TAPDEV" 2>/dev/null | grep -q root; then
|
---|
181 | chgrp vboxusers "$TAPDEV"
|
---|
182 | chmod 0660 "$TAPDEV"
|
---|
183 | fi
|
---|
184 | log_action_end_msg 0
|
---|
185 | return 0
|
---|
186 | }
|
---|
187 |
|
---|
188 | # Shut down VirtualBox host networking and remove all permanent TAP
|
---|
189 | # interfaces. This action will fail if some interfaces could not be removed.
|
---|
190 | stop_network() {
|
---|
191 | log_action_begin_msg "Shutting down VirtualBox host networking"
|
---|
192 | # If there is no runtime record file, assume that the service is not
|
---|
193 | # running.
|
---|
194 | if [ ! -f "$VARFILE" ]; then
|
---|
195 | log_action_end_msg 0
|
---|
196 | return 0
|
---|
197 | fi
|
---|
198 | # Fail if we can't read our runtime record file or write to the
|
---|
199 | # folder it is located in
|
---|
200 | if [ ! -r "$VARFILE" -o ! -w "$VARDIR" ]; then
|
---|
201 | failure "Failed to read $VARFILE or to write $VARDIR"
|
---|
202 | fi
|
---|
203 | # Fail if we don't have tunctl
|
---|
204 | if ! VBoxTunctl -h 2>&1 | grep -q VBoxTunctl > /dev/null; then
|
---|
205 | failure "VBoxTunctl not found"
|
---|
206 | fi
|
---|
207 | # Read the runtime record file entries line by line and delete the
|
---|
208 | # interfaces. The format of the runtime record file is not checked for
|
---|
209 | # errors.
|
---|
210 | while read line; do
|
---|
211 | set ""$line
|
---|
212 | # Remove the interface from a bridge if it is part of one
|
---|
213 | if [ ! -z "$3" ]; then
|
---|
214 | brctl delif "$3" "$1" 2> /dev/null
|
---|
215 | fi
|
---|
216 | # Remove the interface. Roll back everything and fail if this is not
|
---|
217 | # possible
|
---|
218 | if (! ifconfig "$1" down 2> /dev/null ||
|
---|
219 | ! VBoxTunctl -d "$1" > /dev/null 2>&1)
|
---|
220 | then
|
---|
221 | while read line; do
|
---|
222 | set ""$line
|
---|
223 | VBoxTunctl -t "$1" -u "$2" > /dev/null 2>&1
|
---|
224 | ifconfig "$1" up 2> /dev/null
|
---|
225 | if [ ! -z "$3" ]; then
|
---|
226 | brctl addif "$3" "$1"
|
---|
227 | fi
|
---|
228 | done < "$VARFILE"
|
---|
229 | failure "Removing of interface failed"
|
---|
230 | fi
|
---|
231 | done < "$VARFILE"
|
---|
232 | rm -f "$VARFILE" 2> /dev/null
|
---|
233 | log_action_end_msg 0
|
---|
234 | return 0
|
---|
235 | }
|
---|
236 |
|
---|
237 | # Shut down VirtualBox host networking and remove all permanent TAP
|
---|
238 | # interfaces. This action will succeed even if not all interfaces could be
|
---|
239 | # removed. It is only intended for exceptional circumstances such as
|
---|
240 | # uninstalling VirtualBox.
|
---|
241 | force_stop_network() {
|
---|
242 | log_action_begin_msg "Shutting down VirtualBox host networking"
|
---|
243 | # If there is no runtime record file, assume that the service is not
|
---|
244 | # running.
|
---|
245 | if [ ! -f "$VARFILE" ]; then
|
---|
246 | log_action_end_msg 0
|
---|
247 | return 0
|
---|
248 | fi
|
---|
249 | # Fail if we can't read our runtime record file or write to the
|
---|
250 | # folder it is located in
|
---|
251 | if [ ! -r "$VARFILE" -o ! -w "$VARDIR" ]; then
|
---|
252 | failure "Failed to read $VARFILE or to write $VARDIR"
|
---|
253 | fi
|
---|
254 | # Fail if we don't have tunctl
|
---|
255 | if ! VBoxTunctl -h 2>&1 | grep -q VBoxTunctl > /dev/null; then
|
---|
256 | failure "VBoxTunctl not found"
|
---|
257 | fi
|
---|
258 | # Read the runtime record file entries line by line and delete the
|
---|
259 | # interfaces. The format of the runtime record file is not checked for
|
---|
260 | # errors.
|
---|
261 | while read line; do
|
---|
262 | set ""$line
|
---|
263 | # Remove the interface from a bridge if it is part of one
|
---|
264 | if [ ! -z "$3" ]; then
|
---|
265 | brctl delif "$3" "$1" 2> /dev/null
|
---|
266 | fi
|
---|
267 | # Remove the interface.
|
---|
268 | ifconfig "$1" down 2> /dev/null
|
---|
269 | VBoxTunctl -d "$1" > /dev/null 2>&1
|
---|
270 | done < "$VARFILE"
|
---|
271 | rm -f "$VARFILE" 2> /dev/null
|
---|
272 | log_action_end_msg 0
|
---|
273 | return 0
|
---|
274 | }
|
---|
275 |
|
---|
276 | case "$1" in
|
---|
277 | start)
|
---|
278 | start_network
|
---|
279 | ;;
|
---|
280 | stop)
|
---|
281 | stop_network
|
---|
282 | ;;
|
---|
283 | restart|reload|force-reload)
|
---|
284 | stop_network
|
---|
285 | start_network
|
---|
286 | ;;
|
---|
287 | force-stop)
|
---|
288 | force_stop_network
|
---|
289 | ;;
|
---|
290 | status)
|
---|
291 | if running; then
|
---|
292 | log_success_msg "VirtualBox host networking is loaded."
|
---|
293 | else
|
---|
294 | log_failure_msg "VirtualBox host networking is not loaded."
|
---|
295 | fi
|
---|
296 | ;;
|
---|
297 | *)
|
---|
298 | echo "Usage: `basename $0` {start|stop|force-stop|restart|force-reload|status}"
|
---|
299 | exit 1
|
---|
300 | esac
|
---|
301 |
|
---|
302 | exit
|
---|