From 2924761826677361fbc8368ea7a354197a507587 Mon Sep 17 00:00:00 2001 From: reubenmiller Date: Mon, 9 Dec 2024 09:50:57 +0100 Subject: [PATCH] feat: fallback to mac address of first adapter for the serial number --- src/scripts.d/50_c8y_Hardware | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/scripts.d/50_c8y_Hardware b/src/scripts.d/50_c8y_Hardware index 54b6215..63f159b 100755 --- a/src/scripts.d/50_c8y_Hardware +++ b/src/scripts.d/50_c8y_Hardware @@ -68,6 +68,45 @@ hardware_info_container() { echo "serialNumber=\"$SERIAL\"" } +get_mac_address() { + SCN=/sys/class/net + min=65535 + arphrd_ether=1 + ifdev= + + # find iface with lowest ifindex, skip non ARPHRD_ETHER types (lo, sit ...) + for dev in "$SCN"/*; do + if [ ! -f "$dev/type" ]; then + continue + fi + + iftype=$(cat "$dev/type") + if [ "$iftype" -ne $arphrd_ether ]; then + continue + fi + + # Skip dummy interfaces + if echo "$dev" | grep -q "$SCN/dummy" 2>/dev/null; then + continue + fi + + idx=$(cat "$dev/ifindex") + if [ "$idx" -lt "$min" ]; then + min=$idx + ifdev=$dev + fi + done + + if [ -z "$ifdev" ]; then + echo "no suitable interfaces found" >&2 + exit 1 + else + echo "using interface $ifdev" >&2 + # grab MAC address + cat "$ifdev/address" + fi +} + hardware_info_device() { MODEL=$(grep Model /proc/cpuinfo | cut -d: -f2- | xargs) HARDWARE=$(grep "^Hardware" /proc/cpuinfo | cut -d: -f2- | xargs) @@ -81,6 +120,12 @@ hardware_info_device() { fi fi + if [ -z "$SERIAL" ]; then + # TODO: not all cpus have a serial number /proc/cpuinfo + # Fallback to using the mac address + SERIAL="$(get_mac_address 2>/dev/null)" + fi + echo "model=\"$MODEL\"" echo "revision=\"$HARDWARE-$REVISION\"" echo "serialNumber=\"$SERIAL\""