config_generate 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. # fixup IPv6 slave interface
  105. ifname="pppoe-$1"
  106. uci -q batch <<-EOF
  107. set network.$1.proto='pppoe'
  108. set network.$1.username='username'
  109. set network.$1.password='password'
  110. set network.$1.ipv6='auto'
  111. delete network.${1}6
  112. set network.${1}6='interface'
  113. set network.${1}6.ifname='$ifname'
  114. set network.${1}6.proto='dhcpv6'
  115. EOF
  116. ;;
  117. esac
  118. }
  119. generate_switch_vlans_ports() {
  120. local switch="$1"
  121. local port ports role roles num attr val
  122. #
  123. # autogenerate vlans
  124. #
  125. if json_is_a roles array; then
  126. json_get_keys roles roles
  127. json_select roles
  128. for role in $roles; do
  129. json_select "$role"
  130. json_get_vars ports
  131. json_select ..
  132. uci -q batch <<-EOF
  133. add network switch_vlan
  134. set network.@switch_vlan[-1].device='$switch'
  135. set network.@switch_vlan[-1].vlan='$role'
  136. set network.@switch_vlan[-1].ports='$ports'
  137. EOF
  138. done
  139. json_select ..
  140. fi
  141. #
  142. # write port specific settings
  143. #
  144. if json_is_a ports array; then
  145. json_get_keys ports ports
  146. json_select ports
  147. for port in $ports; do
  148. json_select "$port"
  149. json_get_vars num
  150. if json_is_a attr object; then
  151. json_get_keys attr attr
  152. json_select attr
  153. uci -q batch <<-EOF
  154. add network switch_port
  155. set network.@switch_port[-1].device='$switch'
  156. set network.@switch_port[-1].port=$num
  157. EOF
  158. for attr in $attr; do
  159. json_get_var val "$attr"
  160. uci -q set network.@switch_port[-1].$attr="$val"
  161. done
  162. json_select ..
  163. fi
  164. json_select ..
  165. done
  166. json_select ..
  167. fi
  168. }
  169. generate_switch() {
  170. local key="$1"
  171. local vlans
  172. json_select switch
  173. json_select "$key"
  174. json_get_vars enable reset blinkrate cpu_port
  175. uci -q batch <<-EOF
  176. add network switch
  177. set network.@switch[-1].name='$key'
  178. set network.@switch[-1].reset='$reset'
  179. set network.@switch[-1].enable_vlan='$enable'
  180. set network.@switch[-1].blinkrate='$blinkrate'
  181. EOF
  182. generate_switch_vlans_ports "$1"
  183. json_select ..
  184. json_select ..
  185. }
  186. generate_static_system() {
  187. uci -q batch <<-EOF
  188. delete system.@system[0]
  189. add system system
  190. set system.@system[-1].hostname='libreCMC'
  191. set system.@system[-1].timezone='UTC'
  192. set system.@system[-1].ttylogin='0'
  193. set system.@system[-1].log_size='64'
  194. set system.@system[-1].urandom_seed='0'
  195. delete system.ntp
  196. set system.ntp='timeserver'
  197. set system.ntp.enabled='1'
  198. set system.ntp.enable_server='0'
  199. add_list system.ntp.server='0.lede.pool.ntp.org'
  200. add_list system.ntp.server='1.lede.pool.ntp.org'
  201. add_list system.ntp.server='2.lede.pool.ntp.org'
  202. add_list system.ntp.server='3.lede.pool.ntp.org'
  203. EOF
  204. if json_is_a system object; then
  205. json_select system
  206. local hostname
  207. if json_get_var hostname hostname; then
  208. uci -q set "system.@system[-1].hostname=$hostname"
  209. fi
  210. if json_is_a ntpserver array; then
  211. local keys key
  212. json_get_keys keys ntpserver
  213. json_select ntpserver
  214. uci -q delete "system.ntp.server"
  215. for key in $keys; do
  216. local server
  217. if json_get_var server "$key"; then
  218. uci -q add_list "system.ntp.server=$server"
  219. fi
  220. done
  221. json_select ..
  222. fi
  223. json_select ..
  224. fi
  225. }
  226. generate_rssimon() {
  227. local key="$1"
  228. local cfg="rssid_$key"
  229. local refresh threshold
  230. json_select rssimon
  231. json_select "$key"
  232. json_get_vars refresh threshold
  233. json_select ..
  234. json_select ..
  235. uci -q batch <<-EOF
  236. delete system.$cfg
  237. set system.$cfg='rssid'
  238. set system.$cfg.dev='$key'
  239. set system.$cfg.refresh='$refresh'
  240. set system.$cfg.threshold='$threshold'
  241. EOF
  242. }
  243. generate_led() {
  244. local key="$1"
  245. local cfg="led_$key"
  246. json_select led
  247. json_select "$key"
  248. json_get_vars name sysfs type trigger default
  249. uci -q batch <<-EOF
  250. delete system.$cfg
  251. set system.$cfg='led'
  252. set system.$cfg.name='$name'
  253. set system.$cfg.sysfs='$sysfs'
  254. set system.$cfg.trigger='$trigger'
  255. set system.$cfg.default='$default'
  256. EOF
  257. case "$type" in
  258. gpio)
  259. local gpio inverted
  260. json_get_vars gpio inverted
  261. uci -q batch <<-EOF
  262. set system.$cfg.trigger='gpio'
  263. set system.$cfg.gpio='$gpio'
  264. set system.$cfg.inverted='$inverted'
  265. EOF
  266. ;;
  267. netdev)
  268. local device mode
  269. json_get_vars device mode
  270. uci -q batch <<-EOF
  271. set system.$cfg.trigger='netdev'
  272. set system.$cfg.mode='$mode'
  273. set system.$cfg.dev='$device'
  274. EOF
  275. ;;
  276. usb)
  277. local device
  278. json_get_vars device
  279. uci -q batch <<-EOF
  280. set system.$cfg.trigger='usbdev'
  281. set system.$cfg.interval='50'
  282. set system.$cfg.dev='$device'
  283. EOF
  284. ;;
  285. usbport)
  286. local ports port
  287. json_get_values ports ports
  288. uci set system.$cfg.trigger='usbport'
  289. for port in $ports; do
  290. uci add_list system.$cfg.port=$port
  291. done
  292. ;;
  293. rssi)
  294. local iface minq maxq offset factor
  295. json_get_vars iface minq maxq offset factor
  296. uci -q batch <<-EOF
  297. set system.$cfg.trigger='rssi'
  298. set system.$cfg.iface='rssid_$iface'
  299. set system.$cfg.minq='$minq'
  300. set system.$cfg.maxq='$maxq'
  301. set system.$cfg.offset='$offset'
  302. set system.$cfg.factor='$factor'
  303. EOF
  304. ;;
  305. switch)
  306. local port_mask speed_mask
  307. json_get_vars port_mask speed_mask
  308. uci -q batch <<-EOF
  309. set system.$cfg.port_mask='$port_mask'
  310. set system.$cfg.speed_mask='$speed_mask'
  311. EOF
  312. ;;
  313. portstate)
  314. local port_state
  315. json_get_vars port_state
  316. uci -q batch <<-EOF
  317. set system.$cfg.port_state='$port_state'
  318. EOF
  319. ;;
  320. timer|oneshot)
  321. local delayon delayoff
  322. json_get_vars delayon delayoff
  323. uci -q batch <<-EOF
  324. set system.$cfg.trigger='$type'
  325. set system.$cfg.delayon='$delayon'
  326. set system.$cfg.delayoff='$delayoff'
  327. EOF
  328. ;;
  329. esac
  330. json_select ..
  331. json_select ..
  332. }
  333. generate_gpioswitch() {
  334. local cfg="$1"
  335. json_select gpioswitch
  336. json_select "$cfg"
  337. local name pin default
  338. json_get_vars name pin default
  339. uci -q batch <<-EOF
  340. delete system.$cfg
  341. set system.$cfg='gpio_switch'
  342. set system.$cfg.name='$name'
  343. set system.$cfg.gpio_pin='$pin'
  344. set system.$cfg.value='$default'
  345. EOF
  346. json_select ..
  347. json_select ..
  348. }
  349. json_init
  350. json_load "$(cat ${CFG})"
  351. if [ ! -s /etc/config/network ]; then
  352. touch /etc/config/network
  353. generate_static_network
  354. json_get_keys keys network
  355. for key in $keys; do generate_network $key; done
  356. json_get_keys keys switch
  357. for key in $keys; do generate_switch $key; done
  358. fi
  359. if [ ! -s /etc/config/system ]; then
  360. touch /etc/config/system
  361. generate_static_system
  362. json_get_keys keys rssimon
  363. for key in $keys; do generate_rssimon $key; done
  364. json_get_keys keys gpioswitch
  365. for key in $keys; do generate_gpioswitch $key; done
  366. json_get_keys keys led
  367. for key in $keys; do generate_led $key; done
  368. fi
  369. uci commit