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.
This commit is contained in:
Joris van Rantwijk 2024-09-24 19:28:13 +02:00
parent 762097017d
commit eb544d2c93
2 changed files with 46 additions and 1 deletions

View File

@ -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

View File

@ -53,7 +53,7 @@ sync_config() {
# Atomically replace the old file on the SD card. # Atomically replace the old file on the SD card.
mv ${CONFIG_MOUNTPOINT}/${fname}.new ${CONFIG_MOUNTPOINT}/${fname} 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} mv ${CONFIG_DIR}/${fname}.new ${CONFIG_DIR}/${fname}
done done