diff mbox series

[meta-oe] udev-extraconf: fix ifupdown for non hotplug devices

Message ID 20250407172925.1190126-2-jeroen@myspectrum.nl
State New
Headers show
Series [meta-oe] udev-extraconf: fix ifupdown for non hotplug devices | expand

Commit Message

Jeroen Hofstee April 7, 2025, 5:29 p.m. UTC
Commit 160f713917 (udev-extraconf: fix network.sh script did not
configure hotplugged interfaces, 2024-10-18) fixed ifupdown for
hotplug devices, but also calls it for non hotplug devices. That
can cause issue, since they might not expect the ifupdown from
udev, since it wasn't called before mentioned patch got merged.

For util-linux this can simply be fixed by adding --allow=hotplug.
Unfortunately busybox doesn't have that option, so a function is
added to check if the device is marked as allow-hotplug.

Since wilcards are supported adding 'allow-hotplug *' allows to
restore behaviour of mentioned patch, while this restores the
original behaviour.

Signed-off-by: Jeroen Hofstee <jhofstee@victronenergy.com>
Cc: Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>
---
 .../udev/udev-extraconf/network.sh            | 28 +++++++++++++++++++
 1 file changed, 28 insertions(+)
diff mbox series

Patch

diff --git a/meta/recipes-core/udev/udev-extraconf/network.sh b/meta/recipes-core/udev/udev-extraconf/network.sh
index 500e60ae61..da79f00d5a 100644
--- a/meta/recipes-core/udev/udev-extraconf/network.sh
+++ b/meta/recipes-core/udev/udev-extraconf/network.sh
@@ -6,6 +6,34 @@  echo "$INTERFACE" | grep -q wifi && exit 0
 # udevd does clearenv(). Export shell PATH to children.
 export PATH
 
+# udev should only trigger ifupdown for interfaces marked as allow-hotplug
+# and with util-linux that is as simple as adding --allow=hotplug.
+# Busybox unfortunately doesn't have this option.
+# allow-hotplug is a pattern like eth0 /eth* /eth*/1 /eth*=eth.
+# This function checks if INTERFACE matches an allow-hotplug pattern.
+
+allow_hotplug() {
+    allow_hotplug="$(sed -n -e 's/^allow-hotplug \+\([^= ]*\).*/\1/p' /etc/network/interfaces)"
+    for pattern in $allow_hotplug; do
+        options="$(echo $pattern | sed -n -e 's,^/\?[^ /]\+/\(.*\),\1,p')"
+        value="$(echo $pattern | sed -n -e 's,^/\?\([^ /]\+\).*,\1,p')"
+        interfaces="$(ls -d /sys/class/net/$value 2>/dev/null | xargs -r -n 1 basename)"
+        if [ "$options" != "" ]; then
+            interfaces="$(echo $interfaces | awk -v n=$options '{print $n }')"
+        fi
+        echo "$interfaces" | grep -w -q "$INTERFACE"
+        if [ $? -eq 0 ]; then
+            return 0
+        fi
+    done
+
+    return 1
+}
+
+if ! allow_hotplug; then
+    exit 0
+fi
+
 # if this interface has an entry in /etc/network/interfaces, let ifupdown
 # handle it
 if grep -q "iface \+$INTERFACE" /etc/network/interfaces; then