瀏覽代碼

base-files: rename hostname with EUI of mac address

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.

Since it's no good idea to nest json_* functions, this code does
not use get_mac_label directly, but only get_mac_label_dt as
external resource.

Signed-off-by: Rosy Song <[email protected]>
[merged with commit introducing macaddr_geteui, rebased on updated
label MAC address storage, extended commit message]
Signed-off-by: Adrian Schmutzler <[email protected]>
Rosy Song 6 年之前
父節點
當前提交
6170c46b47
共有 2 個文件被更改,包括 22 次插入1 次删除
  1. 15 1
      package/base-files/files/bin/config_generate
  2. 7 0
      package/base-files/files/lib/functions/system.sh

+ 15 - 1
package/base-files/files/bin/config_generate

@@ -3,6 +3,7 @@
 CFG=/etc/board.json
 CFG=/etc/board.json
 
 
 . /usr/share/libubox/jshn.sh
 . /usr/share/libubox/jshn.sh
+. /lib/functions/system.sh
 
 
 [ -s $CFG ] || /bin/board_detect || exit 1
 [ -s $CFG ] || /bin/board_detect || exit 1
 [ -s /etc/config/network -a -s /etc/config/system ] && exit 0
 [ -s /etc/config/network -a -s /etc/config/system ] && exit 0
@@ -253,9 +254,22 @@ generate_static_system() {
 		add_list system.ntp.server='3.openwrt.pool.ntp.org'
 		add_list system.ntp.server='3.openwrt.pool.ntp.org'
 	EOF
 	EOF
 
 
+	local label_macaddr=$(get_mac_label_dt)
+	local hostname
+
+	if json_is_a system object; then
+		json_select system
+			[ -n "$label_macaddr" ] || json_get_var label_macaddr label_macaddr
+		json_select ..
+	fi
+
+	if [ -n "$label_macaddr" ]; then
+		hostname="OpenWrt-$(macaddr_geteui $label_macaddr)"
+		uci -q set "system.@system[-1].hostname=$hostname"
+	fi
+
 	if json_is_a system object; then
 	if json_is_a system object; then
 		json_select system
 		json_select system
-			local hostname
 			if json_get_var hostname hostname; then
 			if json_get_var hostname hostname; then
 				uci -q set "system.@system[-1].hostname=$hostname"
 				uci -q set "system.@system[-1].hostname=$hostname"
 			fi
 			fi

+ 7 - 0
package/base-files/files/lib/functions/system.sh

@@ -144,6 +144,13 @@ macaddr_add() {
 	echo $oui:$nic
 	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() {
 macaddr_setbit_la() {
 	local mac=$1
 	local mac=$1