Skip to content

Commit

Permalink
Merge pull request #16 from thin-edge/feat-serial-from-macaddr
Browse files Browse the repository at this point in the history
feat: fallback to mac address of first adapter for the serial number
  • Loading branch information
reubenmiller authored Dec 9, 2024
2 parents a8da9de + 2924761 commit 8f9b79a
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/scripts.d/50_c8y_Hardware
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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\""
Expand Down

0 comments on commit 8f9b79a

Please sign in to comment.