Configure IP address, NTP, SSH

- Build userspace (Buildroot) in Thumb2 mode
- Set MAC address from EEPROM on boot
- Load SSH host key from SD card
- Enable DHCP
- Configure NTP (using DHCP if possible)
This commit is contained in:
Joris van Rantwijk 2024-01-13 16:01:49 +01:00
parent bddfa86b28
commit a8ceeddc83
9 changed files with 139 additions and 2 deletions

View File

@ -2,7 +2,7 @@
set -e
BUILDROOT_DIR="buildroot-2023.02.8"
. script_env
make -C "$BUILDROOT_DIR" distclean
@ -10,4 +10,3 @@ cp -a config/buildroot_puzzlefw_defconfig "$BUILDROOT_DIR/.config"
make -C "$BUILDROOT_DIR" olddefconfig
make -C "$BUILDROOT_DIR"
ln -sf "$BUILDROOT_DIR" buildroot

View File

@ -28,3 +28,8 @@ cp -a "$BUILDROOT_DIR/output/images/rootfs.cpio.uboot" "$SDCARD_DIR"
# Wrap U-Boot script in image file
$MKIMAGE -A arm -T script -d config/uboot_script.txt "$SDCARD_DIR/boot.scr"
# Create SSH host key for embedded system
if [ ! -f "$SDCARD_DIR/dropbear_ed25519_host_key" ]; then
dropbearkey -t ed25519 -f "$SDCARD_DIR/dropbear_ed25519_host_key"
fi

View File

@ -0,0 +1,3 @@
include /etc/chrony_dhcp.conf
pool pool.ntp.org iburst
makestep 0.1 3

View File

@ -0,0 +1,44 @@
#!/bin/sh
#
# Set Ethernet MAC address based on EEPROM settings.
#
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=}"
# Check that ethaddr variable is well-formed.
if [ -n "${ethaddr##[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]}" ]; then
echo "ERROR: Invalid ethaddr found in EEPROM enviroment" >&2
exit 1
fi
echo "Setting MAC address ${ethaddr}"
# Configure MAC address
ip link set dev eth0 addr ${ethaddr}
}
case "$1" in
start)
start
;;
stop|restart|reload)
;;
*)
echo "Usage: $0 start"
exit 1
esac

View File

@ -0,0 +1,44 @@
#!/bin/sh
#
# Load SSH host key from SD card.
#
start() {
# If host key exists, do nothing.
[ -f /etc/dropbear/dropbear_ed25519_host_key ] && return
# If /etc/dropbear is a symlink, delete it.
[ -L /etc/dropbear ] && rm /etc/dropbear
# Create directory /etc/dropbear if it does not exist.
mkdir -p /etc/dropbear
# Try to copy SSH host key from SD card.
mkdir -p /mnt/tmp_sdcard
mount -t vfat -o fmask=0177 -r /dev/mmcblk0p1 /mnt/tmp_sdcard
if ! cp -p /mnt/tmp_sdcard/dropbear_ed25519_host_key /etc/dropbear ; then
echo "WARNING: Failed to load SSH host key from SD card" >&2
umount /mnt/tmp_sdcard
rmdir /mnt/tmp_sdcard
exit 1
fi
umount /mnt/tmp_sdcard
rmdir /mnt/tmp_sdcard
chmod 0600 /etc/dropbear/dropbear_ed25519_host_key
}
case "$1" in
start)
start
;;
stop|restart|reload)
;;
*)
echo "Usage: $0 start"
exit 1
esac

View File

@ -0,0 +1,6 @@
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp

View File

@ -0,0 +1,29 @@
#!/bin/sh
#
# Configure chrony to use NTP server advertised by DHCP server.
#
configure_ntp() {
# Do nothing if the DHCP server does not advertise any NTP server.
[ -z "$ntpsrv" ] && return
# Write config snippet with NTP servers.
for srv in $ntpsrv ; do
echo "server $srv iburst"
done > /etc/chrony_dhcp.conf.new
# Compare to currently configured NTP servers.
if ! diff -q /etc/chrony_dhcp.conf /etc/chrony_dhcp.conf.new >/dev/null ; then
# Update configuration and restart Chrony.
mv /etc/chrony_dhcp.conf.new /etc/chrony_dhcp.conf
/etc/init.d/S49chrony reload
fi
}
case "$1" in
renew|bound)
configure_ntp
;;
esac

View File

@ -2,18 +2,24 @@ BR2_arm=y
BR2_cortex_a9=y
BR2_ARM_ENABLE_NEON=y
BR2_ARM_ENABLE_VFP=y
BR2_ARM_INSTRUCTIONS_THUMB2=y
BR2_TOOLCHAIN_BUILDROOT_UCLIBC=y
BR2_TOOLCHAIN_BUILDROOT_WCHAR=y
BR2_BINUTILS_VERSION_2_39_X=y
BR2_GCC_VERSION_12_X=y
BR2_TOOLCHAIN_BUILDROOT_CXX=y
BR2_TARGET_GENERIC_HOSTNAME="pitaya"
BR2_TARGET_GENERIC_ISSUE="Welcome to Red Pitaya PuzzleFW"
BR2_TARGET_GENERIC_ROOT_PASSWD="root"
BR2_ROOTFS_OVERLAY="../buildroot_overlay"
BR2_PACKAGE_XZ=y
BR2_PACKAGE_ZIP=y
BR2_PACKAGE_MEMSTAT=y
BR2_PACKAGE_STRACE=y
BR2_PACKAGE_TINYMEMBENCH=y
BR2_PACKAGE_DOSFSTOOLS=y
BR2_PACKAGE_DOSFSTOOLS_FSCK_FAT=y
BR2_PACKAGE_DOSFSTOOLS_MKFS_FAT=y
BR2_PACKAGE_E2FSPROGS=y
BR2_PACKAGE_LIBUIO=y
BR2_PACKAGE_LSUIO=y
@ -46,6 +52,7 @@ BR2_PACKAGE_NGINX=y
# BR2_PACKAGE_NGINX_HTTP_UPSTREAM_KEEPALIVE_MODULE is not set
# BR2_PACKAGE_NGINX_HTTP_UPSTREAM_RANDOM_MODULE is not set
# BR2_PACKAGE_NGINX_HTTP_UPSTREAM_ZONE_MODULE is not set
BR2_PACKAGE_PHYTOOL=y
# BR2_PACKAGE_URANDOM_SCRIPTS is not set
BR2_PACKAGE_XXHASH=y
BR2_PACKAGE_ATOP=y