base-files: rename hostname with EUI of mac address
authorRosy Song <rosysong@rosinson.com>
Mon, 4 Nov 2019 19:44:41 +0000 (20:44 +0100)
committerAdrian Schmutzler <freifunk@adrianschmutzler.de>
Thu, 7 Nov 2019 16:13:18 +0000 (17:13 +0100)
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 <rosysong@rosinson.com>
[merged with commit introducing macaddr_geteui, rebased on updated
label MAC address storage, extended commit message]
Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
package/base-files/files/bin/config_generate
package/base-files/files/lib/functions/system.sh

index 3ca035ca8b20d460644698636a025b6b5e3e2e5d..b473eba9e9e4cc2746d904f1486a892c29f26521 100755 (executable)
@@ -3,6 +3,7 @@
 CFG=/etc/board.json
 
 . /usr/share/libubox/jshn.sh
+. /lib/functions/system.sh
 
 [ -s $CFG ] || /bin/board_detect || exit 1
 [ -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'
        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
                json_select system
-                       local hostname
                        if json_get_var hostname hostname; then
                                uci -q set "system.@system[-1].hostname=$hostname"
                        fi
index be7efe4e2eed88ae5bc2b9db0ba8258e28acd8d9..c4dc3cc3a9bdd3a9174c400d5b2d5e113de4928c 100644 (file)
@@ -144,6 +144,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