diff mbox series

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

Message ID 20191103204606.7929-2-freifunk@adrianschmutzler.de
State Superseded
Headers show
Series [OpenWrt-Devel,RFC,1/2] base-files: always store label MAC address in uci system config | expand

Commit Message

Adrian Schmutzler Nov. 3, 2019, 8:46 p.m. UTC
From: Rosy Song <rosysong@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@rosinson.com>
[merged with commit introducing macaddr_geteui, rebased on updated
label MAC address storage]
Signed-off-by: Adrian Schmutzler <freifunk@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 mbox series

Patch

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.@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.@system[-1].label_macaddr=$label_macaddr"
 			fi
 
+			local hostname
+			if json_get_var hostname hostname; then
+				uci -q set "system.@system[-1].hostname=$hostname"
+			elif [ -n "$label_macaddr" ]; then
+				uci -q set "system.@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