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