tools/patchelf: update to 0.18.0
[openwrt/staging/dedeckeh.git] / package / base-files / files / lib / preinit / 10_indicate_preinit
1 # Copyright (C) 2006 OpenWrt.org
2 # Copyright (C) 2010 Vertical Communications
3
4 preinit_ip_config() {
5 local netdev vid
6
7 netdev=${1%\.*}
8 vid=${1#*\.}
9
10 if [ "$vid" = "$netdev" ]; then
11 vid=
12 fi
13
14 grep -q "$netdev" /proc/net/dev || return
15
16 if [ -n "$vid" ]; then
17 ip link add link $netdev name $1 type vlan id $vid
18 fi
19
20 ip link set dev $netdev up
21 if [ -n "$vid" ]; then
22 ip link set dev $1 up
23 fi
24 ip -4 address add $pi_ip/$pi_netmask broadcast $pi_broadcast dev $1
25 }
26
27 preinit_config_switch() {
28 local role roles ports device enable reset
29
30 local name=$1
31 local lan_if=$2
32
33 json_select switch
34 json_select $name
35
36 json_get_vars enable reset
37
38 if [ "$reset" -eq "1" ]; then
39 swconfig dev $name set reset
40 fi
41 swconfig dev $name set enable_vlan $enable
42
43 if json_is_a roles array; then
44 json_get_keys roles roles
45 json_select roles
46
47 for role in $roles; do
48 json_select "$role"
49 json_get_vars ports device
50 json_select ..
51
52 if [ "$device" = "$lan_if" ]; then
53 swconfig dev $name vlan $role set ports "$ports"
54 fi
55 done
56
57 json_select ..
58 fi
59
60 swconfig dev $name set apply
61
62 json_select ..
63 json_select ..
64 }
65
66 preinit_config_port() {
67 local original
68
69 local netdev="$1"
70 local path="$2"
71
72 [ -d "/sys/devices/$path/net" ] || return
73 original="$(ls "/sys/devices/$path/net" | head -1)"
74
75 [ "$netdev" = "$original" ] && return
76
77 ip link set "$original" name "$netdev"
78 }
79
80 preinit_config_board() {
81 /bin/board_detect /tmp/board.json
82
83 [ -f "/tmp/board.json" ] || return
84
85 . /usr/share/libubox/jshn.sh
86
87 json_init
88 json_load "$(cat /tmp/board.json)"
89
90 # Find the current highest eth*
91 max_eth=$(grep -o '^ *eth[0-9]*:' /proc/net/dev | tr -dc '[0-9]\n' | sort -n | tail -1)
92 # Find and move netdevs using eth*s we are configuring
93 json_get_keys keys "network_device"
94 for netdev in $keys; do
95 json_select "network_device"
96 json_select "$netdev"
97 json_get_vars path path
98 next_eth="$(echo "$netdev" | grep 'eth[0-9]*' | tr -dc '[0-9]')"
99 [ "$next_eth" -gt "$max_eth" ] && max_eth=$next_eth
100 if [ -n "$path" -a -h "/sys/class/net/$netdev" ]; then
101 ip link set "$netdev" down
102 ip link set "$netdev" name eth$((++max_eth))
103 fi
104 json_select ..
105 json_select ..
106 done
107
108 # Move interfaces by path to their netdev name
109 json_get_keys keys "network_device"
110 for netdev in $keys; do
111 json_select "network_device"
112 json_select "$netdev"
113 json_get_vars path path
114 [ -n "$path" ] && preinit_config_port "$netdev" "$path"
115 json_select ..
116 json_select ..
117 done
118
119 json_select network
120 json_select "lan"
121 json_get_vars device
122 json_get_values ports ports
123 json_select ..
124 json_select ..
125
126 [ -n "$device" -o -n "$ports" ] || return
127
128 # swconfig uses $device and DSA uses ports
129 [ -z "$ports" ] && {
130 ports="$device"
131 }
132
133 # only use the first one
134 ifname=${ports%% *}
135
136 if [ -x /sbin/swconfig ]; then
137 # configure the switch, if present
138
139 json_get_keys keys switch
140 for key in $keys; do
141 preinit_config_switch $key $ifname
142 done
143 else
144 # trim any vlan ids
145 ifname=${ifname%\.*}
146 # trim any vlan modifiers like :t
147 ifname=${ifname%\:*}
148 fi
149
150 pi_ifname=$ifname
151
152 preinit_ip_config $pi_ifname
153 }
154
155 preinit_ip() {
156 [ "$pi_preinit_no_failsafe" = "y" ] && return
157
158 # if the preinit interface isn't specified and ifname is set in
159 # preinit.arch use that interface
160 if [ -z "$pi_ifname" ]; then
161 pi_ifname=$ifname
162 fi
163
164 if [ -n "$pi_ifname" ]; then
165 preinit_ip_config $pi_ifname
166 elif [ -d "/etc/board.d/" ]; then
167 preinit_config_board
168 fi
169
170 preinit_net_echo "Doing OpenWrt Preinit\n"
171 }
172
173 preinit_ip_deconfig() {
174 [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && {
175 local netdev vid
176
177 netdev=${pi_ifname%\.*}
178 vid=${pi_ifname#*\.}
179
180 if [ "$vid" = "$netdev" ]; then
181 vid=
182 fi
183
184 ip -4 address flush dev $pi_ifname
185 ip link set dev $netdev down
186
187 if [ -n "$vid" ]; then
188 ip link delete $pi_ifname
189 fi
190 }
191 }
192
193 preinit_net_echo() {
194 [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && {
195 {
196 [ "$pi_preinit_net_messages" = "y" ] || {
197 [ "$pi_failsafe_net_message" = "true" ] &&
198 [ "$pi_preinit_no_failsafe_netmsg" != "y" ]
199 }
200 } && netmsg $pi_broadcast "$1"
201 }
202 }
203
204 pi_indicate_preinit() {
205 set_state preinit
206 }
207
208 boot_hook_add preinit_main preinit_ip
209 boot_hook_add preinit_main pi_indicate_preinit