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

Adrian Schmutzler freifunk at adrianschmutzler.de
Mon Nov 4 05:43:48 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     | 4 +++-
 package/base-files/files/lib/functions/system.sh | 7 +++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/package/base-files/files/bin/config_generate b/package/base-files/files/bin/config_generate
index bb6117e6dc..2c33bca09a 100755
--- a/package/base-files/files/bin/config_generate
+++ b/package/base-files/files/bin/config_generate
@@ -255,6 +255,7 @@ generate_static_system() {
 	EOF
 
 	local label_macaddr=$(get_mac_label_dt)
+	local hostname
 
 	if json_is_a system object; then
 		json_select system
@@ -264,11 +265,12 @@ generate_static_system() {
 
 	if [ -n "$label_macaddr" ]; then
 		uci -q set "system. at system[-1].label_macaddr=$label_macaddr"
+		hostname="OpenWrt-$(macaddr_geteui $label_macaddr)"
+		uci -q set "system. at system[-1].hostname=$hostname"
 	fi
 
 	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
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