meta-netboot: don't use 'ip' command to detect boot interface 54/22754/2
authorStephane Desneux <stephane.desneux@iot.bzh>
Wed, 23 Oct 2019 10:05:21 +0000 (10:05 +0000)
committerJan-Simon Moeller <jsmoeller@linuxfoundation.org>
Wed, 23 Oct 2019 14:13:25 +0000 (14:13 +0000)
This patch changes the method to detect the current network interface
used for netboot. Instead of using the 'ip' command, it parses the content
of /sys/class/net/* to detect the first running ethernet interface.

Two new messages are now visible on the console in initramfs phase:
--------------------------------------------------
/sbin/init[1]: find_active_interface: first active interface is eth0
/sbin/init[1]: Adjusting Connman command line. Will be: 'connmand -r -n -I eth0'
--------------------------------------------------

Background:

When booting using netboot, it's necessary to update connman command line
options to ignore the network interface used for NBD connection.
For this, the initramfs script tries to detect the interface in use by
running 'ip -o link show state'.

'ip' command comes with iproute2 package but for various reasons, some AGL
images like m3ulcb-nogfx don't have iproute2 installed. In this case, the
'ip' command is implemented by busybox, which doesn't support the '-o' option.
This leads to issues when running connman as the command line is not properly
updated.

Bug-AGL: SPEC-2921

Change-Id: I5691f04ab462a148219b741d235247a2bfbc2e24
Signed-off-by: Stephane Desneux <stephane.desneux@iot.bzh>
meta-netboot/recipes-core/initramfs-netboot/files/init.sh

index 7b8fbb5..50d6247 100644 (file)
@@ -57,6 +57,29 @@ check_debug() {
        esac
 }
 
+find_active_interface() {
+       [[ ! -d /sys/class/net ]] && { log_error "find_active_interface: /sys/class/net doesn't exist"; return 2; }
+       local iface
+       for x in $(ls -d /sys/class/net/* 2>/dev/null); do
+               iface=$(basename $x)
+               # find interfaces with:
+               # - type == 1 (ethernet)
+               # - not wireless
+               # - with state up
+
+               [[ $(cat $x/type) != 1 ]] && continue
+               [[ -d $x/wireless ]] && continue
+               [[ $(cat $x/operstate) != "up" ]] && continue
+
+               log_info "find_active_interface: first active interface is $iface"
+               echo $iface
+               return 0
+       done
+
+       log_error "Unable to find any active network interface."
+       return 1
+}
+
 # -------------------------------------------
 
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin
@@ -131,9 +154,12 @@ pivot_root . boot/initramfs || bail_out "pivot_root failed."
 
 # workaround for connman (avoid bringing down the network interface used for booting, disable DNS proxy)
 if [[ -f /lib/systemd/system/connman.service ]]; then
-       log_info "Adjusting Connman configuration"
-       iface=$(ip -o link show up | tr ':' ' ' | awk '{print $2}' | grep -v -e "^lo$" | head -1)
-       sed -i "s|connmand -n\$|connmand -r -n -I $iface|g" /lib/systemd/system/connman.service
+       newopts="-r -n"
+       iface=$(find_active_interface)
+       [[ -n "$iface" ]] && newopts="$newopts -I $iface"
+
+       log_info "Adjusting Connman command line. Will be: 'connmand $newopts'"
+       sed -i "s|connmand -n\$|connmand $newopts|g" /lib/systemd/system/connman.service
 fi
 
 # also use /proc/net/pnp to generate /etc/resolv.conf