config_generate 9.3 KB

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