base-files: generate config files with correct permissions
[openwrt/staging/wigyori.git] / package / base-files / files / bin / config_generate
1 #!/bin/sh
2
3 CFG=/etc/board.json
4
5 . /usr/share/libubox/jshn.sh
6
7 [ -s $CFG ] || /bin/board_detect || exit 1
8 [ -s /etc/config/network -a -s /etc/config/system ] && exit 0
9
10 generate_static_network() {
11 uci -q batch <<-EOF
12 delete network.loopback
13 set network.loopback='interface'
14 set network.loopback.ifname='lo'
15 set network.loopback.proto='static'
16 set network.loopback.ipaddr='127.0.0.1'
17 set network.loopback.netmask='255.0.0.0'
18 EOF
19 [ -e /proc/sys/net/ipv6 ] && {
20 uci -q batch <<-EOF
21 delete network.globals
22 set network.globals='globals'
23 set network.globals.ula_prefix='auto'
24 EOF
25 }
26
27 if json_is_a dsl object; then
28 json_select dsl
29 if json_is_a atmbridge object; then
30 json_select atmbridge
31 local vpi vci encaps payload nameprefix
32 json_get_vars vpi vci encaps payload nameprefix
33 uci -q batch <<-EOF
34 delete network.atm
35 set network.atm='atm-bridge'
36 set network.atm.vpi='$vpi'
37 set network.atm.vci='$vci'
38 set network.atm.encaps='$encaps'
39 set network.atm.payload='$payload'
40 set network.atm.nameprefix='$nameprefix'
41 EOF
42 json_select ..
43 fi
44
45 if json_is_a modem object; then
46 json_select modem
47 local type annex firmware tone xfer_mode
48 json_get_vars type annex firmware tone xfer_mode
49 uci -q batch <<-EOF
50 delete network.dsl
51 set network.dsl='dsl'
52 set network.dsl.annex='$annex'
53 set network.dsl.firmware='$firmware'
54 set network.dsl.tone='$tone'
55 set network.dsl.xfer_mode='$xfer_mode'
56 EOF
57 json_select ..
58 fi
59 json_select ..
60 fi
61 }
62
63 addr_offset=2
64 generate_network() {
65 local ifname macaddr protocol type ipaddr netmask
66
67 json_select network
68 json_select "$1"
69 json_get_vars ifname macaddr protocol ipaddr netmask
70 json_select ..
71 json_select ..
72
73 [ -n "$ifname" ] || return
74
75 # force bridge for multi-interface devices (and lan)
76 case "$1:$ifname" in
77 *\ * | lan:*) type="bridge" ;;
78 esac
79
80 uci -q batch <<-EOF
81 delete network.$1
82 set network.$1='interface'
83 set network.$1.type='$type'
84 set network.$1.ifname='$ifname'
85 set network.$1.proto='none'
86 EOF
87
88 if [ -n "$macaddr" ]; then
89 for name in $ifname; do
90 uci -q batch <<-EOF
91 delete network.$1_${name/./_}_dev
92 set network.$1_${name/./_}_dev='device'
93 set network.$1_${name/./_}_dev.name='$name'
94 set network.$1_${name/./_}_dev.macaddr='$macaddr'
95 EOF
96 done
97 fi
98
99 case "$protocol" in
100 static)
101 local ipad
102 case "$1" in
103 lan) ipad=${ipaddr:-"192.168.1.1"} ;;
104 *) ipad=${ipaddr:-"192.168.$((addr_offset++)).1"} ;;
105 esac
106
107 netm=${netmask:-"255.255.255.0"}
108
109 uci -q batch <<-EOF
110 set network.$1.proto='static'
111 set network.$1.ipaddr='$ipad'
112 set network.$1.netmask='$netm'
113 EOF
114 [ -e /proc/sys/net/ipv6 ] && uci set network.$1.ip6assign='60'
115 ;;
116
117 dhcp)
118 # fixup IPv6 slave interface if parent is a bridge
119 [ "$type" = "bridge" ] && ifname="br-$1"
120
121 uci set network.$1.proto='dhcp'
122 [ -e /proc/sys/net/ipv6 ] && {
123 uci -q batch <<-EOF
124 delete network.${1}6
125 set network.${1}6='interface'
126 set network.${1}6.ifname='$ifname'
127 set network.${1}6.proto='dhcpv6'
128 EOF
129 }
130 ;;
131
132 pppoe)
133 uci -q batch <<-EOF
134 set network.$1.proto='pppoe'
135 set network.$1.username='username'
136 set network.$1.password='password'
137 EOF
138 [ -e /proc/sys/net/ipv6 ] && {
139 uci -q batch <<-EOF
140 set network.$1.ipv6='1'
141 delete network.${1}6
142 set network.${1}6='interface'
143 set network.${1}6.ifname='@${1}'
144 set network.${1}6.proto='dhcpv6'
145 EOF
146 }
147 ;;
148 esac
149 }
150
151 generate_switch_vlans_ports() {
152 local switch="$1"
153 local port ports role roles num attr val
154
155 #
156 # autogenerate vlans
157 #
158
159 if json_is_a roles array; then
160 json_get_keys roles roles
161 json_select roles
162
163 for role in $roles; do
164 json_select "$role"
165 json_get_vars ports
166 json_select ..
167
168 uci -q batch <<-EOF
169 add network switch_vlan
170 set network.@switch_vlan[-1].device='$switch'
171 set network.@switch_vlan[-1].vlan='$role'
172 set network.@switch_vlan[-1].ports='$ports'
173 EOF
174 done
175
176 json_select ..
177 fi
178
179
180 #
181 # write port specific settings
182 #
183
184 if json_is_a ports array; then
185 json_get_keys ports ports
186 json_select ports
187
188 for port in $ports; do
189 json_select "$port"
190 json_get_vars num
191
192 if json_is_a attr object; then
193 json_get_keys attr attr
194 json_select attr
195 uci -q batch <<-EOF
196 add network switch_port
197 set network.@switch_port[-1].device='$switch'
198 set network.@switch_port[-1].port=$num
199 EOF
200
201 for attr in $attr; do
202 json_get_var val "$attr"
203 uci -q set network.@switch_port[-1].$attr="$val"
204 done
205 json_select ..
206 fi
207 json_select ..
208 done
209
210 json_select ..
211 fi
212 }
213
214 generate_switch() {
215 local key="$1"
216 local vlans
217
218 json_select switch
219 json_select "$key"
220 json_get_vars enable reset blinkrate cpu_port \
221 ar8xxx_mib_type ar8xxx_mib_poll_interval
222
223 uci -q batch <<-EOF
224 add network switch
225 set network.@switch[-1].name='$key'
226 set network.@switch[-1].reset='$reset'
227 set network.@switch[-1].enable_vlan='$enable'
228 set network.@switch[-1].blinkrate='$blinkrate'
229 set network.@switch[-1].ar8xxx_mib_type='$ar8xxx_mib_type'
230 set network.@switch[-1].ar8xxx_mib_poll_interval='$ar8xxx_mib_poll_interval'
231 EOF
232
233 generate_switch_vlans_ports "$1"
234
235 json_select ..
236 json_select ..
237 }
238
239
240 generate_static_system() {
241 uci -q batch <<-EOF
242 delete system.@system[0]
243 add system system
244 set system.@system[-1].hostname='OpenWrt'
245 set system.@system[-1].timezone='UTC'
246 set system.@system[-1].ttylogin='0'
247 set system.@system[-1].log_size='64'
248 set system.@system[-1].urandom_seed='0'
249
250 delete system.ntp
251 set system.ntp='timeserver'
252 set system.ntp.enabled='1'
253 set system.ntp.enable_server='0'
254 add_list system.ntp.server='0.openwrt.pool.ntp.org'
255 add_list system.ntp.server='1.openwrt.pool.ntp.org'
256 add_list system.ntp.server='2.openwrt.pool.ntp.org'
257 add_list system.ntp.server='3.openwrt.pool.ntp.org'
258 EOF
259
260 if json_is_a system object; then
261 json_select system
262 local hostname
263 if json_get_var hostname hostname; then
264 uci -q set "system.@system[-1].hostname=$hostname"
265 fi
266
267 if json_is_a ntpserver array; then
268 local keys key
269 json_get_keys keys ntpserver
270 json_select ntpserver
271 uci -q delete "system.ntp.server"
272
273 for key in $keys; do
274 local server
275 if json_get_var server "$key"; then
276 uci -q add_list "system.ntp.server=$server"
277 fi
278 done
279 json_select ..
280 fi
281 json_select ..
282 fi
283 }
284
285 generate_rssimon() {
286 local key="$1"
287 local cfg="rssid_$key"
288 local refresh threshold
289
290 json_select rssimon
291 json_select "$key"
292 json_get_vars refresh threshold
293 json_select ..
294 json_select ..
295
296 uci -q batch <<-EOF
297 delete system.$cfg
298 set system.$cfg='rssid'
299 set system.$cfg.dev='$key'
300 set system.$cfg.refresh='$refresh'
301 set system.$cfg.threshold='$threshold'
302 EOF
303 }
304
305 generate_led() {
306 local key="$1"
307 local cfg="led_$key"
308
309 json_select led
310 json_select "$key"
311 json_get_vars name sysfs type trigger default
312
313 uci -q batch <<-EOF
314 delete system.$cfg
315 set system.$cfg='led'
316 set system.$cfg.name='$name'
317 set system.$cfg.sysfs='$sysfs'
318 set system.$cfg.trigger='$trigger'
319 set system.$cfg.default='$default'
320 EOF
321
322 case "$type" in
323 gpio)
324 local gpio inverted
325 json_get_vars gpio inverted
326 uci -q batch <<-EOF
327 set system.$cfg.trigger='gpio'
328 set system.$cfg.gpio='$gpio'
329 set system.$cfg.inverted='$inverted'
330 EOF
331 ;;
332
333 netdev)
334 local device mode
335 json_get_vars device mode
336 uci -q batch <<-EOF
337 set system.$cfg.trigger='netdev'
338 set system.$cfg.mode='$mode'
339 set system.$cfg.dev='$device'
340 EOF
341 ;;
342
343 usb)
344 local device
345 json_get_vars device
346 uci -q batch <<-EOF
347 set system.$cfg.trigger='usbdev'
348 set system.$cfg.interval='50'
349 set system.$cfg.dev='$device'
350 EOF
351 ;;
352
353 usbport)
354 local ports port
355 json_get_values ports ports
356 uci set system.$cfg.trigger='usbport'
357 for port in $ports; do
358 uci add_list system.$cfg.port=$port
359 done
360 ;;
361
362 rssi)
363 local iface minq maxq offset factor
364 json_get_vars iface minq maxq offset factor
365 uci -q batch <<-EOF
366 set system.$cfg.trigger='rssi'
367 set system.$cfg.iface='rssid_$iface'
368 set system.$cfg.minq='$minq'
369 set system.$cfg.maxq='$maxq'
370 set system.$cfg.offset='$offset'
371 set system.$cfg.factor='$factor'
372 EOF
373 ;;
374
375 switch)
376 local port_mask speed_mask mode
377 json_get_vars port_mask speed_mask mode
378 uci -q batch <<-EOF
379 set system.$cfg.port_mask='$port_mask'
380 set system.$cfg.speed_mask='$speed_mask'
381 set system.$cfg.mode='$mode'
382 EOF
383 ;;
384
385 portstate)
386 local port_state
387 json_get_vars port_state
388 uci -q batch <<-EOF
389 set system.$cfg.port_state='$port_state'
390 EOF
391 ;;
392
393 timer|oneshot)
394 local delayon delayoff
395 json_get_vars delayon delayoff
396 uci -q batch <<-EOF
397 set system.$cfg.trigger='$type'
398 set system.$cfg.delayon='$delayon'
399 set system.$cfg.delayoff='$delayoff'
400 EOF
401 ;;
402 esac
403
404 json_select ..
405 json_select ..
406 }
407
408 generate_gpioswitch() {
409 local cfg="$1"
410
411 json_select gpioswitch
412 json_select "$cfg"
413 local name pin default
414 json_get_vars name pin default
415 uci -q batch <<-EOF
416 delete system.$cfg
417 set system.$cfg='gpio_switch'
418 set system.$cfg.name='$name'
419 set system.$cfg.gpio_pin='$pin'
420 set system.$cfg.value='$default'
421 EOF
422 json_select ..
423 json_select ..
424 }
425
426 json_init
427 json_load "$(cat ${CFG})"
428
429 umask 077
430
431 if [ ! -s /etc/config/network ]; then
432 touch /etc/config/network
433 generate_static_network
434
435 json_get_keys keys network
436 for key in $keys; do generate_network $key; done
437
438 json_get_keys keys switch
439 for key in $keys; do generate_switch $key; done
440 fi
441
442 if [ ! -s /etc/config/system ]; then
443 touch /etc/config/system
444 generate_static_system
445
446 json_get_keys keys rssimon
447 for key in $keys; do generate_rssimon $key; done
448
449 json_get_keys keys gpioswitch
450 for key in $keys; do generate_gpioswitch $key; done
451
452 json_get_keys keys led
453 for key in $keys; do generate_led $key; done
454 fi
455 uci commit