Annotation of /trunk/initscripts/systemd/units/scripts/network.sh
Parent Directory | Revision Log
Revision 1378 -
(hide annotations)
(download)
(as text)
Sat Jun 4 22:39:03 2011 UTC (12 years, 11 months ago) by niro
File MIME type: application/x-sh
File size: 11771 byte(s)
Sat Jun 4 22:39:03 2011 UTC (12 years, 11 months ago) by niro
File MIME type: application/x-sh
File size: 11771 byte(s)
-added scripts dir
1 | niro | 1378 | #!/bin/bash |
2 | # $Id$ | ||
3 | # Magellan network configuration script for systemd | ||
4 | |||
5 | # read values from files | ||
6 | read_value() | ||
7 | { | ||
8 | local var="$1" | ||
9 | local file="$2" | ||
10 | local value | ||
11 | |||
12 | # local all possible vars | ||
13 | # global | ||
14 | local ONBOOT | ||
15 | local NETWORKING | ||
16 | |||
17 | # static | ||
18 | local IP | ||
19 | local NETMASK | ||
20 | local BROADCAST | ||
21 | local NETWORKING | ||
22 | local FORCE_MAC_TO | ||
23 | |||
24 | # dhcp | ||
25 | local DHCP_PROG | ||
26 | local DHCP_START | ||
27 | local DHCP_STOP | ||
28 | |||
29 | # default gw | ||
30 | local GATEWAY | ||
31 | local GATEWAY_IF | ||
32 | |||
33 | # wireless extensions | ||
34 | local WIRELESS_AP | ||
35 | local WIRELESS_AUTH_MODE | ||
36 | local WIRELESS_BITRATE | ||
37 | local WIRELESS_CHANNEL | ||
38 | local WIRELESS_DEFAULT_KEY | ||
39 | local WIRELESS_ESSID | ||
40 | local WIRELESS_FREQUENCY | ||
41 | local WIRELESS_KEY | ||
42 | local WIRELESS_KEY_ASCII | ||
43 | local WIRELESS_KEY_0 | ||
44 | local WIRELESS_KEY_1 | ||
45 | local WIRELESS_KEY_2 | ||
46 | local WIRELESS_KEY_3 | ||
47 | local WIRELESS_KEY_LENGTH | ||
48 | local WIRELESS_MODE | ||
49 | local WIRELESS_NICK | ||
50 | local WIRELESS_NWID | ||
51 | local WIRELESS_POWER | ||
52 | local WIRELESS_WPA_DRIVER | ||
53 | |||
54 | local BRIDGE_INTERFACES | ||
55 | local BRIDGE_STP | ||
56 | local BRIDGE_AGEING_TIME | ||
57 | local BRIDGE_PRIORITY | ||
58 | local BRIDGE_FORWARD_DELAY | ||
59 | local BRIDGE_HELLO_TIME | ||
60 | local BRIDGE_MAX_MESSAGE_AGE | ||
61 | local BRIDGE_PATH_COST | ||
62 | local BRIDGE_PORT_PRIORITY | ||
63 | |||
64 | source ${file} | ||
65 | eval value=\$$(echo ${var}) | ||
66 | echo "${value}" | ||
67 | } | ||
68 | |||
69 | checkconfig() | ||
70 | { | ||
71 | if [[ -z ${NETWORKING} ]] | ||
72 | then | ||
73 | echo "NETWORKING missing in net.${iface}, aborted" | ||
74 | exit 1 | ||
75 | fi | ||
76 | |||
77 | case "${NETWORKING}" in | ||
78 | static) | ||
79 | if [[ -z ${IP} ]] | ||
80 | then | ||
81 | echo "IP missing in net.${iface}, aborted" | ||
82 | exit 1 | ||
83 | fi | ||
84 | |||
85 | if [[ -z ${NETMASK} ]] | ||
86 | then | ||
87 | echo -n "NETMASK missing in net.${iface}, " | ||
88 | echo "using 255.255.255.0" | ||
89 | NETMASK=255.255.255.0 | ||
90 | fi | ||
91 | |||
92 | if [[ -z ${BROADCAST} ]] | ||
93 | then | ||
94 | echo -n "BROADCAST missing in net.${iface}, " | ||
95 | echo "using default address" | ||
96 | fi | ||
97 | ;; | ||
98 | |||
99 | dhcp) | ||
100 | if [[ -z ${DHCP_PROG} ]] | ||
101 | then | ||
102 | echo -n "DHCP_PROG missing in net.${iface}," | ||
103 | echo "using default programm /sbin/dhcpcd" | ||
104 | DHCP_PROG="/sbin/dhcpcd" | ||
105 | fi | ||
106 | [[ -z ${DHCP_START} ]] && DHCP_START="-t 10" | ||
107 | [[ -z ${DHCP_STOP} ]] && DHCP_STOP="-k" | ||
108 | ;; | ||
109 | |||
110 | esac | ||
111 | } | ||
112 | |||
113 | # onboot_interface_list /path/to/files* | ||
114 | onboot_interface_list() | ||
115 | { | ||
116 | local file | ||
117 | local devices | ||
118 | local iface | ||
119 | |||
120 | # get list of all devices | ||
121 | for file in $@ | ||
122 | do | ||
123 | iface="$(basename ${file} | sed s/net.//)" | ||
124 | |||
125 | # exclude backup files and exclude net.routes and net.sample too | ||
126 | case "${iface}" in | ||
127 | *~) continue ;; | ||
128 | routes) continue ;; | ||
129 | sample) continue ;; | ||
130 | esac | ||
131 | |||
132 | if [[ $(read_value ONBOOT ${file}) = yes ]] | ||
133 | then | ||
134 | devices="${devices} ${iface}" | ||
135 | fi | ||
136 | done | ||
137 | |||
138 | echo "${devices}" | ||
139 | } | ||
140 | |||
141 | config_wireless_wep() | ||
142 | { | ||
143 | local iface="$1" | ||
144 | |||
145 | if [[ -z ${iface} ]] | ||
146 | then | ||
147 | echo "WEP: no \$iface given. Aborting setup." | ||
148 | return 1 | ||
149 | fi | ||
150 | |||
151 | iwconfig "${iface}" enc on | ||
152 | [[ -n ${WIRELESS_KEY_LENGTH} ]] && iwconfig "${iface}" enc "${WIRELESS_KEY_LENGTH}" | ||
153 | [[ -n ${WIRELESS_KEY} ]] && iwconfig "${iface}" key "${WIRELESS_KEY}" | ||
154 | [[ -n ${WIRELESS_KEY_ASCII} ]] && iwconfig "${iface}" key s:"${WIRELESS_KEY_ASCII}" | ||
155 | } | ||
156 | |||
157 | config_wireless_wpa() | ||
158 | { | ||
159 | local iface="$1" | ||
160 | |||
161 | if [[ -z ${iface} ]] | ||
162 | then | ||
163 | echo "WPA: no \$iface given. Aborting setup." | ||
164 | return 1 | ||
165 | fi | ||
166 | |||
167 | if [ ! -x /sbin/wpa_supplicant ] | ||
168 | then | ||
169 | echo "WPA: wpa_supplicant not installed. Aborting setup." | ||
170 | return 1 | ||
171 | fi | ||
172 | |||
173 | # get default settings | ||
174 | [[ -f /etc/conf.d/wpa_supplicant ]] && source /etc/conf.d/wpa_supplicant | ||
175 | |||
176 | # check the configuration | ||
177 | [[ -z ${WIRELESS_WPA_CONFIG} ]] && WIRELESS_WPA_CONFIG=/etc/wpa_supplicant.auto | ||
178 | [[ -z ${WIRELESS_WPA_SKEL} ]] && WIRELESS_WPA_SKEL=/etc/conf.d/wpa_supplicant.skel | ||
179 | |||
180 | # use wext as default driver, do not abort here anymore | ||
181 | [[ -z ${WIRELESS_WPA_DRIVER} ]] && WIRELESS_WPA_DRIVER=wext | ||
182 | |||
183 | # write a config with the settings from net.${iface} | ||
184 | # only wpa-psk ! all other needs manual setup | ||
185 | if [[ ${WIRELESS_WPA_AUTOCONF} = true ]] | ||
186 | then | ||
187 | # write default cfg from skeleton | ||
188 | cat ${WIRELESS_WPA_SKEL} > ${WIRELESS_WPA_CONFIG} | ||
189 | |||
190 | local wpa_proto | ||
191 | case ${WIRELESS_AUTH_MODE} in | ||
192 | wpa) wpa_proto="WPA" ;; | ||
193 | wpa2) wpa_proto="WPA2" ;; | ||
194 | esac | ||
195 | |||
196 | # setup the network entry | ||
197 | sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \ | ||
198 | -e "s:@WIRELESS_KEY@:${WIRELESS_KEY_ASCII}:g" \ | ||
199 | -e "s:@WIRELESS_AUTH_MODE@:${wpa_proto}:g" \ | ||
200 | ${WIRELESS_WPA_CONFIG} | ||
201 | fi | ||
202 | |||
203 | # remove old state dir | ||
204 | [ -d /var/run/wpa_supplicant ] && rm -rf /var/run/wpa_supplicant | ||
205 | |||
206 | # now run the wpa_supplicant dameon | ||
207 | wpa_supplicant -B \ | ||
208 | -D"${WIRELESS_WPA_DRIVER}" \ | ||
209 | -c"${WIRELESS_WPA_CONFIG}" \ | ||
210 | -i"${iface}" \ | ||
211 | ${WIRELESS_WPA_OPTS} | ||
212 | |||
213 | # echo wait 5 seconds | ||
214 | echo " Waiting 5 seconds to retrieve authentification reply ... " | ||
215 | sleep 5 | ||
216 | } | ||
217 | |||
218 | setup_wireless_extensions() | ||
219 | { | ||
220 | local iface="$1" | ||
221 | |||
222 | if [[ -z ${iface} ]] | ||
223 | then | ||
224 | echo "WIRELESS_EXTENSIONS: no \$iface given. Aborting setup." | ||
225 | return 1 | ||
226 | fi | ||
227 | |||
228 | [[ -n ${WIRELESS_BITRATE} ]] && iwconfig "${iface}" rate "${WIRELESS_BITRATE}" | ||
229 | [[ -n ${WIRELESS_CHANNEL} ]] && iwconfig "${iface}" channel "${WIRELESS_CHANNEL}" | ||
230 | [[ -n ${WIRELESS_ESSID} ]] && iwconfig "${iface}" essid "${WIRELESS_ESSID}" | ||
231 | [[ -n ${WIRELESS_FREQUENCY} ]] && iwconfig "${iface}" freq "${WIRELESS_FREQUENCY}" | ||
232 | [[ -n ${WIRELESS_MODE} ]] && iwconfig "${iface}" mode "${WIRELESS_MODE}" | ||
233 | [[ -n ${WIRELESS_NICK} ]] && iwconfig "${iface}" nick "${WIRELESS_NICK}" | ||
234 | |||
235 | case "${WIRELESS_AUTH_MODE}" in | ||
236 | wpa|wpa2) config_wireless_wpa "${iface}" ;; | ||
237 | wep|on) config_wireless_wep "${iface}" ;; | ||
238 | off) iwconfig "${iface}" enc off ;; | ||
239 | esac | ||
240 | } | ||
241 | |||
242 | config_bridge_options() | ||
243 | { | ||
244 | local iface="$1" | ||
245 | local i | ||
246 | local port | ||
247 | local cost | ||
248 | local prio | ||
249 | |||
250 | # enable spanning-tree protocol | ||
251 | case ${BRIDGE_STP} in | ||
252 | on|off) brctl stp "${iface}" "${BRIDGE_STP}" ;; | ||
253 | *) echo "BRIDGE: unkown value \$BRIDGE_STP='$BRIDGE_STP'."; return 1 ;; | ||
254 | esac | ||
255 | |||
256 | # configure ageing time | ||
257 | if [[ ! -z ${BRIDGE_AGEING_TIME} ]] | ||
258 | then | ||
259 | brctl setageing "${iface}" "${BRIDGE_AGEING_TIME}" | ||
260 | fi | ||
261 | |||
262 | # configure bridge priority | ||
263 | if [[ ! -z ${BRIDGE_PRIORITY} ]] | ||
264 | then | ||
265 | brctl setbridgeprio "${iface}" "${BRIDGE_PRIORITY}" | ||
266 | fi | ||
267 | |||
268 | # configure forward delay | ||
269 | if [[ ! -z ${BRIDGE_FORWARD_DELAY} ]] | ||
270 | then | ||
271 | brctl setfd "${iface}" "${BRIDGE_FORWARD_DELAY}" | ||
272 | fi | ||
273 | |||
274 | # configure hello time | ||
275 | if [[ ! -z ${BRIDGE_HELLO_TIME} ]] | ||
276 | then | ||
277 | brctl sethello "${iface}" "${BRIDGE_HELLO_TIME}" | ||
278 | fi | ||
279 | |||
280 | # configure maximal message age | ||
281 | if [[ ! -z ${BRIDGE_MAX_MESSAGE_AGE} ]] | ||
282 | then | ||
283 | brctl setmaxage "${iface}" "${BRIDGE_MAX_MESSAGE_AGE}" | ||
284 | fi | ||
285 | |||
286 | # configure path cost for every port | ||
287 | if [[ ! -z ${BRIDGE_PATH_COST} ]] | ||
288 | then | ||
289 | for i in ${BRIDGE_PATH_COST} | ||
290 | do | ||
291 | port="${i%=*}" | ||
292 | cost="${i#*=}" | ||
293 | [[ ! -z ${port} ]] && brctl pathcost "${iface}" "${port}" "${cost}" | ||
294 | done | ||
295 | fi | ||
296 | |||
297 | # configure port priority for every port | ||
298 | if [[ ! -z ${BRIDGE_PORT_PRIORITY} ]] | ||
299 | then | ||
300 | for i in ${BRIDGE_PORT_PRIORITY} | ||
301 | do | ||
302 | port="${i%=*}" | ||
303 | prio="${i#*=}" | ||
304 | [[ ! -z ${port} ]] && brctl setportprio "${iface}" "${port}" "${prio}" | ||
305 | done | ||
306 | fi | ||
307 | } | ||
308 | |||
309 | config_bridge_devices() | ||
310 | { | ||
311 | local iface="$1" | ||
312 | local method="$2" | ||
313 | local bport | ||
314 | |||
315 | if [[ -z ${iface} ]] | ||
316 | then | ||
317 | echo "BRIDGE: no \$iface given. Aborting setup." | ||
318 | return 1 | ||
319 | fi | ||
320 | |||
321 | if [[ -z ${method} ]] | ||
322 | then | ||
323 | echo "BRIDGE: no \$method given. Aborting setup." | ||
324 | return 1 | ||
325 | fi | ||
326 | |||
327 | # first check for brctl | ||
328 | if [[ -z $(which brctl) ]] | ||
329 | then | ||
330 | echo "brctl not found! Please install 'net-misc/bridge-utils'." | ||
331 | return 1 | ||
332 | fi | ||
333 | |||
334 | # check the config | ||
335 | if [[ -z ${BRIDGE_INTERFACES} ]] | ||
336 | then | ||
337 | echo "BRIDGE: no \$BRIDGE_INTERFACES given. Aborting setup." | ||
338 | return 1 | ||
339 | fi | ||
340 | |||
341 | case ${method} in | ||
342 | add) | ||
343 | # setup the bridge device | ||
344 | brctl addbr "${iface}" | ||
345 | for bport in ${BRIDGE_INTERFACES} | ||
346 | do | ||
347 | # enter promiscous mode | ||
348 | ifconfig "${bport}" 0.0.0.0 promisc | ||
349 | # now setup the bridge | ||
350 | brctl addif "${iface}" "${bport}" | ||
351 | done | ||
352 | # configure all other options | ||
353 | config_bridge_options "${iface}" | ||
354 | ;; | ||
355 | |||
356 | remove) | ||
357 | for bport in ${BRIDGE_INTERFACE} | ||
358 | do | ||
359 | # bring the interface down | ||
360 | ifconfig "${bport}" down | ||
361 | # remove the interface from the bridge | ||
362 | brctl delif "${iface}" "${bport}" | ||
363 | done | ||
364 | # bring the bridge down | ||
365 | brctl delbr "${iface}" | ||
366 | ;; | ||
367 | esac | ||
368 | |||
369 | # unset the bridge variable to be safe | ||
370 | unset BRIDGE_INTERFACES | ||
371 | # continue to setup generic networking | ||
372 | } | ||
373 | |||
374 | config_routes() | ||
375 | { | ||
376 | local method="$1" | ||
377 | local message | ||
378 | |||
379 | # only add and del are allowed | ||
380 | case ${method} in | ||
381 | add) message="Adding" ;; | ||
382 | del) message="Removing" ;; | ||
383 | *) | ||
384 | echo "config_routes: unsupported \$method '${method}'." | ||
385 | exit 1 | ||
386 | ;; | ||
387 | esac | ||
388 | |||
389 | # adds/delete user routes | ||
390 | if [[ -f /etc/conf.d/net.routes ]] | ||
391 | then | ||
392 | ( cat /etc/conf.d/net.routes; echo ) | # make sure there is a LF at the end | ||
393 | while read route | ||
394 | do | ||
395 | case "${route}" in | ||
396 | \#*|"") continue ;; | ||
397 | esac | ||
398 | # do not esacpe ${route} or it breaks! | ||
399 | route "${method}" ${route} | ||
400 | done | ||
401 | fi | ||
402 | } | ||
403 | |||
404 | networking_start() | ||
405 | { | ||
406 | local iface dns routes ALL_INTERFACES | ||
407 | |||
408 | if [[ -z $1 ]] | ||
409 | then | ||
410 | ALL_INTERFACES=$(onboot_interface_list /etc/conf.d/net.*) | ||
411 | else | ||
412 | if [[ -e /etc/conf.d/net.$1 ]] | ||
413 | then | ||
414 | ALL_INTERFACES="$1" | ||
415 | else | ||
416 | echo "Interface $1 does not exist. Aborting" | ||
417 | exit 1 | ||
418 | fi | ||
419 | fi | ||
420 | |||
421 | # get list of all devices | ||
422 | for iface in ${ALL_INTERFACES} | ||
423 | do | ||
424 | # checkconfig | ||
425 | source /etc/conf.d/net.${iface} || exit 1 | ||
426 | checkconfig | ||
427 | |||
428 | # setup mac | ||
429 | if [[ -n ${FORCE_MAC_TO} ]] | ||
430 | then | ||
431 | ifconfig "${iface}" hw ether "${FORCE_MAC_TO}" | ||
432 | fi | ||
433 | |||
434 | # setup bridges | ||
435 | if [[ ${iface} = br[0-9]* ]] | ||
436 | then | ||
437 | config_bridge_devices "${iface}" add | ||
438 | fi | ||
439 | |||
440 | # now configure wireless_extensions | ||
441 | [ -x /usr/sbin/iwconfig ] && setup_wireless_extensions "${iface}" | ||
442 | |||
443 | # activate the interface | ||
444 | ifconfig "${iface}" up | ||
445 | |||
446 | # setup static or dhcp | ||
447 | case ${NETWORKING} in | ||
448 | dhcp|DHCP) | ||
449 | ${DHCP_PROG} ${DHCP_START} "${iface}" ;; | ||
450 | static|STATIC) | ||
451 | ifconfig "${iface}" "${IP}" netmask "${NETMASK}" broadcast "${BROADCAST}" ;; | ||
452 | esac | ||
453 | |||
454 | # setup def gw | ||
455 | if [[ -n ${GATEWAY} ]] | ||
456 | then | ||
457 | route add default gateway "${GATEWAY}" metric 1 dev "${iface}" | ||
458 | unset GATEWAY | ||
459 | fi | ||
460 | |||
461 | # setup /etc/resolv.conf | ||
462 | # add given nameserver | ||
463 | if [[ -n ${NAMESERVER} ]] | ||
464 | then | ||
465 | # whipe out the old one | ||
466 | echo "# Generated by the magellan-initscripts for ${iface}" > /etc/resolv.conf | ||
467 | # include head | ||
468 | if [ -f /etc/resolv.conf.head ] | ||
469 | then | ||
470 | cat /etc/resolv.conf.head >> /etc/resolv.conf | ||
471 | else | ||
472 | echo "# /etc/resolv.conf.head can replace this line" >> /etc/resolv.conf | ||
473 | fi | ||
474 | |||
475 | for dns in ${NAMESERVER} | ||
476 | do | ||
477 | echo "nameserver ${dns}" >> /etc/resolv.conf | ||
478 | done | ||
479 | |||
480 | # include tail | ||
481 | if [ -f /etc/resolv.conf.tail ] | ||
482 | then | ||
483 | cat /etc/resolv.conf.tail >> /etc/resolv.conf | ||
484 | else | ||
485 | echo "# /etc/resolv.conf.tail can replace this line" >> /etc/resolv.conf | ||
486 | fi | ||
487 | |||
488 | unset NAMESERVER | ||
489 | fi | ||
490 | done | ||
491 | |||
492 | # setup user routes | ||
493 | config_routes add | ||
494 | } | ||
495 | |||
496 | networking_stop() | ||
497 | { | ||
498 | if [[ -z $1 ]] | ||
499 | then | ||
500 | ALL_INTERFACES=$(onboot_interface_list /etc/conf.d/net.*) | ||
501 | else | ||
502 | if [[ -e /etc/conf.d/net.$1 ]] | ||
503 | then | ||
504 | ALL_INTERFACES="$1" | ||
505 | else | ||
506 | echo "Interface $1 does not exist. Aborting" | ||
507 | exit 1 | ||
508 | fi | ||
509 | fi | ||
510 | |||
511 | # get list of all devices | ||
512 | for iface in ${ALL_INTERFACES} | ||
513 | do | ||
514 | source /etc/conf.d/net.${iface} || exit 1 | ||
515 | checkconfig | ||
516 | |||
517 | if [[ -n ${GATEWAY} ]] | ||
518 | then | ||
519 | route del -net default | ||
520 | fi | ||
521 | |||
522 | ifconfig "${iface}" down | ||
523 | |||
524 | # remove bridges | ||
525 | if [[ ${iface} = br[0-9]* ]] | ||
526 | then | ||
527 | config_bridge_devices "${iface}" remove | ||
528 | fi | ||
529 | |||
530 | # shutdown dhcp-daemon | ||
531 | if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof $(basename ${DHCP_PROG})) ]] | ||
532 | then | ||
533 | if [[ -z ${DHCP_STOP} ]] | ||
534 | then | ||
535 | killall -15 ${DHCP_PROG} | ||
536 | sleep 1 | ||
537 | # try harder | ||
538 | if [[ -n $(pidof $(basename ${DHCP_PROG})) ]] | ||
539 | then | ||
540 | killall -9 ${DHCP_PROG} | ||
541 | fi | ||
542 | else | ||
543 | ${DHCP_PROG} ${DHCP_STOP} "${iface}" | ||
544 | fi | ||
545 | fi | ||
546 | |||
547 | # shutdown wpa_supplicant daemon | ||
548 | if [[ -n $(pidof wpa_supplicant) ]] | ||
549 | then | ||
550 | killall wpa_supplicant | ||
551 | fi | ||
552 | done | ||
553 | |||
554 | # remove state dir | ||
555 | if [ -d /var/run/wpa_supplicant ] | ||
556 | then | ||
557 | rm -rf /var/run/wpa_supplicant | ||
558 | fi | ||
559 | |||
560 | # delete user routes | ||
561 | config_routes del | ||
562 | } | ||
563 | |||
564 | case $1 in | ||
565 | start) | ||
566 | networking_start $2 ;; | ||
567 | |||
568 | stop) | ||
569 | networking_stop $2 ;; | ||
570 | |||
571 | restart) | ||
572 | $0 stop | ||
573 | sleep 1 | ||
574 | $0 start | ||
575 | ;; | ||
576 | |||
577 | *) | ||
578 | echo "Usage: $0 {start|stop|restart} [interface]" | ||
579 | exit 1 | ||
580 | ;; | ||
581 | esac |