[OpenWrt-Devel] [RFC PATCH 2/2] base-files: rename hostname with EUI of mac address

Adrian Schmutzler freifunk at adrianschmutzler.de
Sun Nov 3 15:46:06 EST 2019


From: Rosy Song <rosysong at rosinson.com>

If a label MAC address is provided for device, system
will rename the hostname with OpenWrt_{eui mac address}.
This helps to distinguish between different devices.

Signed-off-by: Rosy Song <rosysong at rosinson.com>
[merged with commit introducing macaddr_geteui, rebased on updated
label MAC address storage]
Signed-off-by: Adrian Schmutzler <freifunk at adrianschmutzler.de>
---
 package/base-files/files/bin/config_generate     | 12 +++++++-----
 package/base-files/files/lib/functions/system.sh |  7 +++++++
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/package/base-files/files/bin/config_generate b/package/base-files/files/bin/config_generate
index fe35788a26..d2e1897818 100755
--- a/package/base-files/files/bin/config_generate
+++ b/package/base-files/files/bin/config_generate
@@ -256,17 +256,19 @@ generate_static_system() {
 
 	if json_is_a system object; then
 		json_select system
-			local hostname
-			if json_get_var hostname hostname; then
-				uci -q set "system. at system[-1].hostname=$hostname"
-			fi
-
 			local label_macaddr=$(get_mac_label_dt)
 			[ -n "$label_macaddr" ] || json_get_var label_macaddr label_macaddr
 			if [ -n "$label_macaddr" ]; then
 				uci -q set "system. at system[-1].label_macaddr=$label_macaddr"
 			fi
 
+			local hostname
+			if json_get_var hostname hostname; then
+				uci -q set "system. at system[-1].hostname=$hostname"
+			elif [ -n "$label_macaddr" ]; then
+				uci -q set "system. at system[-1].hostname=OpenWrt-$(macaddr_geteui $label_macaddr)"
+			fi
+
 			if json_is_a ntpserver array; then
 				local keys key
 				json_get_keys keys ntpserver
diff --git a/package/base-files/files/lib/functions/system.sh b/package/base-files/files/lib/functions/system.sh
index 5b4ced836c..f2b8f2395c 100644
--- a/package/base-files/files/lib/functions/system.sh
+++ b/package/base-files/files/lib/functions/system.sh
@@ -115,6 +115,13 @@ macaddr_add() {
 	echo $oui:$nic
 }
 
+macaddr_geteui() {
+	local mac=$1
+	local sep=$2
+
+	echo ${mac:9:2}$sep${mac:12:2}$sep${mac:15:2}
+}
+
 macaddr_setbit_la() {
 	local mac=$1
 
-- 
2.20.1


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list