10_indicate_preinit 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 netdev="$1"
  51. local path="$2"
  52. [ -d "/sys/devices/$path/net" ] || return
  53. original="$(ls "/sys/devices/$path/net" | head -1)"
  54. [ "$netdev" = "$original" ] && return
  55. ip link set "$original" name "$netdev"
  56. }
  57. preinit_config_board() {
  58. /bin/board_detect /tmp/board.json
  59. [ -f "/tmp/board.json" ] || return
  60. . /usr/share/libubox/jshn.sh
  61. json_init
  62. json_load "$(cat /tmp/board.json)"
  63. # Find the current highest eth*
  64. max_eth=$(grep -o '^ *eth[0-9]*:' /proc/net/dev | tr -dc '[0-9]\n' | sort -n | tail -1)
  65. # Find and move netdevs using eth*s we are configuring
  66. json_get_keys keys "network_device"
  67. for netdev in $keys; do
  68. json_select "network_device"
  69. json_select "$netdev"
  70. json_get_vars path path
  71. if [ -n "$path" -a -h "/sys/class/net/$netdev" ]; then
  72. ip link set "$netdev" down
  73. ip link set "$netdev" name eth$((++max_eth))
  74. fi
  75. json_select ..
  76. json_select ..
  77. done
  78. # Move interfaces by path to their netdev name
  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. [ -n "$path" ] && preinit_config_port "$netdev" "$path"
  85. json_select ..
  86. json_select ..
  87. done
  88. json_select network
  89. json_select "lan"
  90. json_get_vars device
  91. json_get_values ports ports
  92. json_select ..
  93. json_select ..
  94. [ -n "$device" -o -n "$ports" ] || return
  95. # swconfig uses $device and DSA uses ports
  96. [ -z "$ports" ] && {
  97. ports="$device"
  98. }
  99. # only use the first one
  100. ifname=${ports%% *}
  101. if [ -x /sbin/swconfig ]; then
  102. # configure the switch, if present
  103. json_get_keys keys switch
  104. for key in $keys; do
  105. preinit_config_switch $key $ifname
  106. done
  107. else
  108. # trim any vlan ids
  109. ifname=${ifname%\.*}
  110. # trim any vlan modifiers like :t
  111. ifname=${ifname%\:*}
  112. fi
  113. pi_ifname=$ifname
  114. preinit_ip_config $pi_ifname
  115. }
  116. preinit_ip() {
  117. [ "$pi_preinit_no_failsafe" = "y" ] && return
  118. # if the preinit interface isn't specified and ifname is set in
  119. # preinit.arch use that interface
  120. if [ -z "$pi_ifname" ]; then
  121. pi_ifname=$ifname
  122. fi
  123. if [ -n "$pi_ifname" ]; then
  124. preinit_ip_config $pi_ifname
  125. elif [ -d "/etc/board.d/" ]; then
  126. preinit_config_board
  127. fi
  128. preinit_net_echo "Doing OpenWrt Preinit\n"
  129. }
  130. preinit_ip_deconfig() {
  131. [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && {
  132. local netdev vid
  133. netdev=${pi_ifname%\.*}
  134. vid=${pi_ifname#*\.}
  135. if [ "$vid" = "$netdev" ]; then
  136. vid=
  137. fi
  138. ip -4 address flush dev $pi_ifname
  139. ip link set dev $netdev down
  140. if [ -n "$vid" ]; then
  141. ip link delete $pi_ifname
  142. fi
  143. }
  144. }
  145. preinit_net_echo() {
  146. [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && {
  147. {
  148. [ "$pi_preinit_net_messages" = "y" ] || {
  149. [ "$pi_failsafe_net_message" = "true" ] &&
  150. [ "$pi_preinit_no_failsafe_netmsg" != "y" ]
  151. }
  152. } && netmsg $pi_broadcast "$1"
  153. }
  154. }
  155. pi_indicate_preinit() {
  156. set_state preinit
  157. }
  158. boot_hook_add preinit_main preinit_ip
  159. boot_hook_add preinit_main pi_indicate_preinit