base-files: Do not break on non-eth ports
[openwrt/staging/nbd.git] / package / base-files / files / lib / preinit / 10_indicate_preinit
index 3c59929790168c12d1745c395f171b08b6d06f8a..a8f7758c841fe59d84e74ab4289e685046d0f4ec 100644 (file)
@@ -18,6 +18,9 @@ preinit_ip_config() {
        fi
 
        ip link set dev $netdev up
+       if [ -n "$vid" ]; then
+               ip link set dev $1 up
+       fi
        ip -4 address add $pi_ip/$pi_netmask broadcast $pi_broadcast dev $1
 }
 
@@ -60,6 +63,20 @@ preinit_config_switch() {
        json_select ..
 }
 
+preinit_config_port() {
+       local original
+
+       local netdev="$1"
+       local path="$2"
+
+       [ -d "/sys/devices/$path/net" ] || return
+       original="$(ls "/sys/devices/$path/net" | head -1)"
+
+       [ "$netdev" = "$original" ] && return
+
+       ip link set "$original" name "$netdev"
+}
+
 preinit_config_board() {
        /bin/board_detect /tmp/board.json
 
@@ -70,16 +87,49 @@ preinit_config_board() {
        json_init
        json_load "$(cat /tmp/board.json)"
 
+       # Find the current highest eth*
+       max_eth=$(grep -o '^ *eth[0-9]*:' /proc/net/dev | tr -dc '[0-9]\n' | sort -n | tail -1)
+       # Find and move netdevs using eth*s we are configuring
+       json_get_keys keys "network_device"
+       for netdev in $keys; do
+               json_select "network_device"
+                       json_select "$netdev"
+                               json_get_vars path path
+                               if [ -n "$path" -a -h "/sys/class/net/$netdev" ]; then
+                                       ip link set "$netdev" down
+                                       ip link set "$netdev" name eth$((++max_eth))
+                               fi
+                       json_select ..
+               json_select ..
+       done
+
+       # Move interfaces by path to their netdev name
+       json_get_keys keys "network_device"
+       for netdev in $keys; do
+               json_select "network_device"
+                       json_select "$netdev"
+                               json_get_vars path path
+                               [ -n "$path" ] && preinit_config_port "$netdev" "$path"
+                       json_select ..
+               json_select ..
+       done
+
        json_select network
                json_select "lan"
-                       json_get_vars ifname
+                       json_get_vars device
+                       json_get_values ports ports
                json_select ..
        json_select ..
 
-       [ -n "$ifname" ] || return
+       [ -n "$device" -o -n "$ports" ] || return
+
+       # swconfig uses $device and DSA uses ports
+       [ -z "$ports" ] && {
+               ports="$device"
+       }
 
        # only use the first one
-       ifname=${ifname%% *}
+       ifname=${ports%% *}
 
        if [ -x /sbin/swconfig ]; then
                # configure the switch, if present
@@ -91,6 +141,8 @@ preinit_config_board() {
        else
                # trim any vlan ids
                ifname=${ifname%\.*}
+               # trim any vlan modifiers like :t
+               ifname=${ifname%\:*}
        fi
 
        pi_ifname=$ifname