config_generate 9.6 KB

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