config_generate 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/bin/sh
  2. CFG=/etc/board.json
  3. . /usr/share/libubox/jshn.sh
  4. [ -f $CFG ] || exit 1
  5. generate_static_network() {
  6. uci -q batch <<EOF
  7. delete network.loopback
  8. set network.loopback='interface'
  9. set network.loopback.ifname='lo'
  10. set network.loopback.proto='static'
  11. set network.loopback.ipaddr='127.0.0.1'
  12. set network.loopback.netmask='255.0.0.0'
  13. delete network.globals
  14. set network.globals='globals'
  15. set network.globals.ula_prefix='auto'
  16. EOF
  17. }
  18. next_vlan=3
  19. generate_network() {
  20. local vlan
  21. json_select network
  22. json_select $1
  23. json_get_vars ifname create_vlan macaddr
  24. json_select ..
  25. json_select ..
  26. [ -n "$ifname" ] || return
  27. [ "$create_vlan" -eq 1 ] && case $1 in
  28. lan) vlan=1;;
  29. wan) vlan=2;;
  30. *)
  31. vlan=$next_vlan
  32. next_vlan=$((next_vlan + 1))
  33. ;;
  34. esac
  35. [ -n "$vlan" ] && ifname=${ifname}.${vlan}
  36. uci -q batch <<EOF
  37. delete network.$1
  38. set network.$1='interface'
  39. set network.$1.ifname='$ifname'
  40. set network.$1.force_link=1
  41. set network.$1.proto='none'
  42. set network.$1.macaddr='$macaddr'
  43. EOF
  44. case $1 in
  45. lan) uci -q batch <<EOF
  46. set network.$1.type='bridge'
  47. set network.$1.proto='static'
  48. set network.$1.ipaddr='192.168.1.1'
  49. set network.$1.netmask='255.255.255.0'
  50. set network.$1.ip6assign='60'
  51. EOF
  52. ;;
  53. wan) uci -q batch <<EOF
  54. set network.$1.proto='dhcp'
  55. delete network.wan6
  56. set network.wan6='interface'
  57. set network.wan6.ifname='$ifname'
  58. set network.wan6.proto='dhcpv6'
  59. EOF
  60. ;;
  61. esac
  62. }
  63. generate_switch_vlan() {
  64. local device=$1
  65. local vlan=$2
  66. local cpu_port=$3
  67. case $vlan in
  68. lan) vlan=1;;
  69. wan) vlan=2;;
  70. *) vlan=${vlan##vlan};;
  71. esac
  72. json_select vlans
  73. json_select $2
  74. json_get_values ports
  75. json_select ..
  76. json_select ..
  77. uci -q batch <<EOF
  78. add network switch_vlan
  79. set network.@switch_vlan[-1].device='$device'
  80. set network.@switch_vlan[-1].vlan='$vlan'
  81. set network.@switch_vlan[-1].ports='$ports ${cpu_port}t'
  82. EOF
  83. }
  84. generate_switch() {
  85. local key=$1
  86. local vlans
  87. json_select switch
  88. json_select $key
  89. json_get_vars enable reset blinkrate cpu_port
  90. uci -q batch <<EOF
  91. add network switch
  92. set network.@switch[-1].name='$key'
  93. set network.@switch[-1].reset='$reset'
  94. set network.@switch[-1].enable_vlan='$enable'
  95. set network.@switch[-1].blinkrate='$blinkrate'
  96. EOF
  97. [ -n "$cpu_port" ] && {
  98. json_get_keys vlans vlans
  99. for vlan in $vlans; do generate_switch_vlan $1 $vlan $cpu_port; done
  100. }
  101. json_select ..
  102. json_select ..
  103. }
  104. generate_led() {
  105. local key=$1
  106. local cfg="led_$key"
  107. json_select led
  108. json_select $key
  109. json_get_vars name sysfs type trigger device interface default
  110. json_select ..
  111. json_select ..
  112. uci -q batch <<EOF
  113. delete system.$cfg
  114. set system.$cfg='led'
  115. set system.$cfg.name='$name'
  116. set system.$cfg.sysfs='$sysfs'
  117. set system.$cfg.dev='$device'
  118. set system.$cfg.trigger='$trigger'
  119. set system.$cfg.port_mask='$port_mask'
  120. set system.$cfg.default='$default'
  121. EOF
  122. case $type in
  123. netdev)
  124. uci -q batch <<EOF
  125. set system.$cfg.trigger='netdev'
  126. set system.$cfg.mode='link tx rx'
  127. EOF
  128. ;;
  129. usb)
  130. uci -q batch <<EOF
  131. set system.$cfg.trigger='usbdev'
  132. set system.$cfg.interval='50'
  133. EOF
  134. ;;
  135. esac
  136. }
  137. json_init
  138. json_load "$(cat ${CFG})"
  139. generate_static_network
  140. json_get_keys keys network
  141. for key in $keys; do generate_network $key; done
  142. json_get_keys keys switch
  143. for key in $keys; do generate_switch $key; done
  144. json_get_keys keys led
  145. for key in $keys; do generate_led $key; done
  146. uci commit