1
0

config_generate 12 KB

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