10_indicate_preinit 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # Copyright (C) 2006 OpenWrt.org
  2. # Copyright (C) 2010 Vertical Communications
  3. preinit_ip_config() {
  4. local netdev vid
  5. netdev=${1%\.*}
  6. vid=${1#*\.}
  7. if [ "$vid" = "$netdev" ]; then
  8. vid=
  9. fi
  10. grep -q "$netdev" /proc/net/dev || return
  11. if [ -n "$vid" ]; then
  12. ip link add link $netdev name $1 type vlan id $vid
  13. fi
  14. ip link set dev $netdev up
  15. if [ -n "$vid" ]; then
  16. ip link set dev $1 up
  17. fi
  18. ip -4 address add $pi_ip/$pi_netmask broadcast $pi_broadcast dev $1
  19. }
  20. preinit_config_switch() {
  21. local role roles ports device enable reset
  22. local name=$1
  23. local lan_if=$2
  24. json_select switch
  25. json_select $name
  26. json_get_vars enable reset
  27. if [ "$reset" -eq "1" ]; then
  28. swconfig dev $name set reset
  29. fi
  30. swconfig dev $name set enable_vlan $enable
  31. if json_is_a roles array; then
  32. json_get_keys roles roles
  33. json_select roles
  34. for role in $roles; do
  35. json_select "$role"
  36. json_get_vars ports device
  37. json_select ..
  38. if [ "$device" = "$lan_if" ]; then
  39. swconfig dev $name vlan $role set ports "$ports"
  40. fi
  41. done
  42. json_select ..
  43. fi
  44. swconfig dev $name set apply
  45. json_select ..
  46. json_select ..
  47. }
  48. preinit_config_port() {
  49. local original
  50. local dev_port
  51. local netdev="$1"
  52. local path="$2"
  53. local port="$3"
  54. [ -d "/sys/devices/$path/net" ] || return
  55. if [ -z "$port" ]; then
  56. original="$(ls "/sys/devices/$path/net" | head -1)"
  57. else
  58. for device in /sys/devices/$path/net/*; do
  59. dev_port="$(cat "$device/dev_port")"
  60. if [ "$dev_port" = "$port" ]; then
  61. original="${device##*/}"
  62. break
  63. fi
  64. done
  65. [ -z "$original" ] && return
  66. fi
  67. [ "$netdev" = "$original" ] && return
  68. ip link set "$original" name "$netdev"
  69. }
  70. preinit_config_board() {
  71. /bin/board_detect /tmp/board.json
  72. [ -f "/tmp/board.json" ] || return
  73. . /usr/share/libubox/jshn.sh
  74. json_init
  75. json_load "$(cat /tmp/board.json)"
  76. # Find the current highest eth*
  77. max_eth=$(grep -o '^ *eth[0-9]*:' /proc/net/dev | tr -dc '[0-9]\n' | sort -n | tail -1)
  78. # Find and move netdevs using eth*s we are configuring
  79. json_get_keys keys "network_device"
  80. for netdev in $keys; do
  81. json_select "network_device"
  82. json_select "$netdev"
  83. json_get_vars path path
  84. if [ -n "$path" -a -h "/sys/class/net/$netdev" ]; then
  85. ip link set "$netdev" down
  86. ip link set "$netdev" name eth$((++max_eth))
  87. fi
  88. json_select ..
  89. json_select ..
  90. done
  91. # Move interfaces by path to their netdev name
  92. json_get_keys keys "network_device"
  93. for netdev in $keys; do
  94. json_select "network_device"
  95. json_select "$netdev"
  96. json_get_vars path path
  97. json_get_vars port port
  98. [ -n "$path" ] && preinit_config_port "$netdev" "$path" "$port"
  99. json_select ..
  100. json_select ..
  101. done
  102. json_select network
  103. json_select "lan"
  104. json_get_vars device
  105. json_get_values ports ports
  106. json_select ..
  107. json_select ..
  108. [ -n "$device" -o -n "$ports" ] || return
  109. # swconfig uses $device and DSA uses ports
  110. [ -z "$ports" ] && {
  111. ports="$device"
  112. }
  113. # only use the first one
  114. ifname=${ports%% *}
  115. if [ -x /sbin/swconfig ]; then
  116. # configure the switch, if present
  117. json_get_keys keys switch
  118. for key in $keys; do
  119. preinit_config_switch $key $ifname
  120. done
  121. else
  122. # trim any vlan ids
  123. ifname=${ifname%\.*}
  124. # trim any vlan modifiers like :t
  125. ifname=${ifname%\:*}
  126. fi
  127. pi_ifname=$ifname
  128. preinit_ip_config $pi_ifname
  129. }
  130. preinit_ip() {
  131. [ "$pi_preinit_no_failsafe" = "y" ] && return
  132. # if the preinit interface isn't specified and ifname is set in
  133. # preinit.arch use that interface
  134. if [ -z "$pi_ifname" ]; then
  135. pi_ifname=$ifname
  136. fi
  137. if [ -n "$pi_ifname" ]; then
  138. preinit_ip_config $pi_ifname
  139. elif [ -d "/etc/board.d/" ]; then
  140. preinit_config_board
  141. fi
  142. preinit_net_echo "Doing OpenWrt Preinit\n"
  143. }
  144. preinit_ip_deconfig() {
  145. [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && {
  146. local netdev vid
  147. netdev=${pi_ifname%\.*}
  148. vid=${pi_ifname#*\.}
  149. if [ "$vid" = "$netdev" ]; then
  150. vid=
  151. fi
  152. ip -4 address flush dev $pi_ifname
  153. ip link set dev $netdev down
  154. if [ -n "$vid" ]; then
  155. ip link delete $pi_ifname
  156. fi
  157. }
  158. }
  159. preinit_net_echo() {
  160. [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && {
  161. {
  162. [ "$pi_preinit_net_messages" = "y" ] || {
  163. [ "$pi_failsafe_net_message" = "true" ] &&
  164. [ "$pi_preinit_no_failsafe_netmsg" != "y" ]
  165. }
  166. } && netmsg $pi_broadcast "$1"
  167. }
  168. }
  169. pi_indicate_preinit() {
  170. set_state preinit
  171. }
  172. boot_hook_add preinit_main preinit_ip
  173. boot_hook_add preinit_main pi_indicate_preinit