config_generate 9.8 KB

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