[OpenWrt-Devel,v2,2/2] base-files: rename hostname with EUI of mac address
diff mbox series

Message ID 20191104104348.63115-2-freifunk@adrianschmutzler.de
State Superseded
Delegated to: Adrian Schmutzler
Headers show
Series
  • [OpenWrt-Devel,v2,1/2] base-files: always store label MAC address in uci system config
Related show

Commit Message

Adrian Schmutzler Nov. 4, 2019, 10:43 a.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     | 4 +++-
 package/base-files/files/lib/functions/system.sh | 7 +++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

Patch
diff mbox series

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.@system[-1].label_macaddr=$label_macaddr"
+		hostname="OpenWrt-$(macaddr_geteui $label_macaddr)"
+		uci -q set "system.@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.@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