From ea8778f53b5a02d0be58d70d874554ce5f363453 Mon Sep 17 00:00:00 2001
From: Alessandro Rubini <rubini@gnudd.com>
Date: Thu, 27 Nov 2014 20:02:00 +0100
Subject: [PATCH] dot-config: retrieve dot-config at run time, if so configured

Now /etc/init.d/S20dot-config is able to download a new dot-config
file if the user specified an URL at configuration time.

We support tftp, http and ftp.  Full documentation in next commit.

Signed-off-by: Alessandro Rubini <rubini@gnudd.com>
---
 Kconfig                                       | 14 ++++++
 .../rootfs_override/etc/init.d/S20dot-config  | 47 ++++++++++++++++++-
 2 files changed, 60 insertions(+), 1 deletion(-)

diff --git a/Kconfig b/Kconfig
index 54f91b8cd..713781f9b 100644
--- a/Kconfig
+++ b/Kconfig
@@ -21,6 +21,20 @@ config PPSI
 
 menu "Local configuration"
 
+config DOTCONF_URL
+
+	string "URL for a run-time replacement of dot-config"
+	help
+	  The White Rabbit Switch is configured at run-time,
+	  according to a dot-config .config file. The file
+	  is the same .config you are defining now.  If you
+	  select an empty string, dot-config is not replaced
+	  at run time.  Otherwise, tftp://, ftp:// or http://
+	  URLs are allowed. Names are allowed if you configured
+	  a DNS server.  The special strings IPADDR and MACADDR
+	   are substituted before retrieving the file.
+	  Example:  "tftp//morgana/wrs-config-IPADDR"
+
 config NTP_SERVER
 	string "IP address of local NTP server (empty for none)"
 	help
diff --git a/userspace/rootfs_override/etc/init.d/S20dot-config b/userspace/rootfs_override/etc/init.d/S20dot-config
index fbacbe48c..4e7993fba 100755
--- a/userspace/rootfs_override/etc/init.d/S20dot-config
+++ b/userspace/rootfs_override/etc/init.d/S20dot-config
@@ -5,4 +5,49 @@
 # could edit dot-config and run /wr/bin/apply_dot-config like we do here,
 # without the need to rember wheter this is S20 or S10 during boot.
 
-. /wr/bin/apply_dot-config
\ No newline at end of file
+# First, read dot-config to get the new location, if any.
+if [ -f /wr/etc/dot-config ]; then
+    . /wr/etc/dot-config
+fi
+
+# Create /etc/resolv.conf, so we can use it. /etc is ramdisk anyways
+if [ ! -z "$CONFIG_DNS_SERVER" ]; then
+    echo "nameserver $CONFIG_DNS_SERVER" > /etc/resolv.conf
+    if [ ! -z "$CONFIG_DNS_DOMAIN" ]; then
+        echo "domain $CONFIG_DNS_DOMAIN" >> /etc/resolv.conf
+    fi
+fi
+
+# If we are expected to get a new dot-conf, do it
+if [ -n "$CONFIG_DOTCONF_URL" ]; then
+    # replace IPADDR and MACADDR, to have a device-specific name
+    macaddr=$(cat /sys/class/net/eth0/address)
+    ipaddr=$(ifconfig eth0 | grep inet | cut -d: -f 2 | cut '-d '  -f 1)
+    URL=$(echo $CONFIG_DOTCONF_URL | \
+	sed -e s/MACADDR/$macaddr/ -e s/IPADDR/$ipaddr/)
+    # split the parts, as we need to handle tftp by hand
+    proto=$(echo $URL | cut -d: -f 1)
+    host=$(echo $URL | cut -d/ -f 3)
+    filename=$(echo $URL | cut -d/ -f 4-)
+
+    rm -f /tmp/dot-config
+    case $proto in
+	http|ftp)
+	    wget $URL -O /tmp/dot-config
+	    ;;
+	tftp)
+	    tftp -g -r "$filename" -l /tmp/dot-config $host
+	    ;;
+	*)
+	    echo "Invalid URL for dot-config: \"$URL\"" >& 2
+	    ;;
+    esac
+    # If it exists and it is not empty or awfully small, trust it
+    if [ -f /tmp/dot-config ] &&
+	[ $(cat /tmp/dot-config | wc -c) -gt 200 ]; then
+	mv /tmp/dot-config /wr/etc
+    fi
+fi
+
+# Finally, apply what we have, be it old or new
+. /wr/bin/apply_dot-config
-- 
GitLab