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