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