redpitaya-puzzlefw/sw/buildroot_overlay/etc/init.d/S03set_hostname.sh

46 lines
1.1 KiB
Bash
Raw Normal View History

#!/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