base-files: properly generate the ula
[openwrt/svn-archive/archive.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 [ -f $CFG ] || exit 1
8
9 generate_static_network() {
10 uci -q batch <<EOF
11 delete network.loopback
12 set network.loopback='interface'
13 set network.loopback.ifname='lo'
14 set network.loopback.proto='static'
15 set network.loopback.ipaddr='127.0.0.1'
16 set network.loopback.netmask='255.0.0.0'
17 delete network.globals
18 set network.globals='globals'
19 set network.globals.ula_prefix='auto'
20 EOF
21 }
22
23 next_vlan=3
24 generate_network() {
25 local vlan
26
27 json_select network
28 json_select $1
29 json_get_vars ifname create_vlan macaddr
30 json_select ..
31 json_select ..
32
33 [ -n "$ifname" ] || return
34 [ "$create_vlan" -eq 1 ] && case $1 in
35 lan) vlan=1;;
36 wan) vlan=2;;
37 *)
38 vlan=$next_vlan
39 next_vlan=$((next_vlan + 1))
40 ;;
41 esac
42 [ -n "$vlan" ] && ifname=${ifname}.${vlan}
43 uci -q batch <<EOF
44 delete network.$1
45 set network.$1='interface'
46 set network.$1.ifname='$ifname'
47 set network.$1.force_link=1
48 set network.$1.proto='none'
49 set network.$1.macaddr='$macaddr'
50 EOF
51
52 case $1 in
53 lan) uci -q batch <<EOF
54 set network.$1.type='bridge'
55 set network.$1.proto='static'
56 set network.$1.ipaddr='192.168.1.1'
57 set network.$1.netmask='255.255.255.0'
58 set network.$1.ip6assign='60'
59 EOF
60 ;;
61 wan) uci -q batch <<EOF
62 set network.$1.proto='dhcp'
63 delete network.wan6
64 set network.wan6='interface'
65 set network.wan6.ifname='@wan'
66 set network.wan6.proto='dhcpv6'
67 EOF
68 ;;
69 esac
70 }
71
72 generate_switch_vlan() {
73 local device=$1
74 local vlan=$2
75 local cpu_port=$3
76
77 case $vlan in
78 lan) vlan=1;;
79 wan) vlan=2;;
80 *) vlan=${vlan##vlan};;
81 esac
82
83 json_select vlans
84 json_select $2
85 json_get_values ports
86 json_select ..
87 json_select ..
88
89 uci -q batch <<EOF
90 add network switch_vlan
91 set network.@switch_vlan[-1].device='$device'
92 set network.@switch_vlan[-1].vlan='$vlan'
93 set network.@switch_vlan[-1].ports='$ports ${cpu_port}t'
94 EOF
95 }
96
97 generate_switch() {
98 local key=$1
99 local vlans
100
101 json_select switch
102 json_select $key
103 json_get_vars enable reset blinkrate cpu_port
104
105 [ -n "$cpu_port" ] || return
106
107 uci -q batch <<EOF
108 add network switch
109 set network.@switch[-1].name='$key'
110 set network.@switch[-1].reset='$reset'
111 set network.@switch[-1].enable_vlan='$enable'
112 set network.@switch[-1].blinkrate='$blinkrate'
113 EOF
114
115 json_get_keys vlans vlans
116 for vlan in $vlans; do generate_switch_vlan $1 $vlan $cpu_port; done
117 json_select ..
118 json_select ..
119 }
120
121 generate_led() {
122 local key=$1
123 local cfg="led_$key"
124
125 json_select led
126 json_select $key
127 json_get_vars name sysfs type trigger device interface default
128 json_select ..
129 json_select ..
130
131 uci -q batch <<EOF
132 delete system.$cfg
133 set system.$cfg='led'
134 set system.$cfg.name='$name'
135 set system.$cfg.sysfs='$sysfs'
136 set system.$cfg.dev='$device'
137 set system.$cfg.trigger='$trigger'
138 set system.$cfg.port_mask='$port_mask'
139 set system.$cfg.default='$default'
140 EOF
141 case $type in
142 netdev)
143 uci -q batch <<EOF
144 set system.$cfg.trigger='netdev'
145 set system.$cfg.mode='link tx rx'
146 EOF
147 ;;
148
149 usb)
150 uci -q batch <<EOF
151 set system.$cfg.trigger='usbdev'
152 set system.$cfg.interval='50'
153 EOF
154 ;;
155
156 esac
157 }
158
159 json_init
160 json_load "$(cat ${CFG})"
161
162 generate_static_network
163
164 json_get_keys keys network
165 for key in $keys; do generate_network $key; done
166
167 json_get_keys keys switch
168 for key in $keys; do generate_switch $key; done
169
170 json_get_keys keys led
171 for key in $keys; do generate_led $key; done
172
173 uci commit