config_generate 9.2 KB

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