From eb544d2c93053878800ee045a80d6f0a9e72829f Mon Sep 17 00:00:00 2001 From: Joris van Rantwijk Date: Tue, 24 Sep 2024 19:28:13 +0200 Subject: [PATCH] Set unique hostname at boot Set hostname to rp-xxxxxx based on the last 6 digits of the MAC address. This is the same as used by the official Red Pitaya software. --- .../etc/init.d/S03set_hostname.sh | 45 +++++++++++++++++++ .../opt/puzzlefw/lib/functions.sh | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100755 sw/buildroot_overlay/etc/init.d/S03set_hostname.sh diff --git a/sw/buildroot_overlay/etc/init.d/S03set_hostname.sh b/sw/buildroot_overlay/etc/init.d/S03set_hostname.sh new file mode 100755 index 0000000..99e3658 --- /dev/null +++ b/sw/buildroot_overlay/etc/init.d/S03set_hostname.sh @@ -0,0 +1,45 @@ +#!/bin/sh +# +# Set unique host name rp-xxxxxx based on MAC address. +# + +start() { + + # Read "ethaddr" variable from U-Boot environment in EEPROM. + # + # Environment starts at address 0x1800 and is 1024 bytes long. + # The environment consists of "key=value" pairs, separated by 0x00 bytes. + # + ethaddr_pair="$( dd if=/sys/bus/i2c/devices/0-0050/eeprom bs=1k skip=6 count=1 status=none | tr '\000' '\n' | grep ^ethaddr= )" + + if [ -z "${ethaddr_pair}" ]; then + echo "ERROR: ethaddr not found in EEPROM enviroment" >&2 + exit 1 + fi + + ethaddr="${ethaddr_pair#ethaddr=}" + + suffix="$( echo $ethaddr | cut -d: -f 4- | tr -d : | tr ABCEDF abcdef )" + hostname="rp-${suffix}" + + echo "Setting hostname ${hostname}" + + echo $hostname > /etc/hostname + /bin/hostname -F /etc/hostname + + grep -v '^127\.0\.1\.1' /etc/hosts > /etc/hosts.new + echo "127.0.1.1 $hostname" >>/etc/hosts.new + mv /etc/hosts.new /etc/hosts +} + +case "$1" in + start) + start + ;; + stop|restart|reload) + ;; + *) + echo "Usage: $0 start" + exit 1 +esac + diff --git a/sw/buildroot_overlay/opt/puzzlefw/lib/functions.sh b/sw/buildroot_overlay/opt/puzzlefw/lib/functions.sh index 2a2ab2b..acfb7a2 100644 --- a/sw/buildroot_overlay/opt/puzzlefw/lib/functions.sh +++ b/sw/buildroot_overlay/opt/puzzlefw/lib/functions.sh @@ -53,7 +53,7 @@ sync_config() { # Atomically replace the old file on the SD card. mv ${CONFIG_MOUNTPOINT}/${fname}.new ${CONFIG_MOUNTPOINT}/${fname} - # Replace the old file in te RAM filesystem. + # Replace the old file in the RAM filesystem. mv ${CONFIG_DIR}/${fname}.new ${CONFIG_DIR}/${fname} done