Using uci-defaults scripts
-
I have used uci-defaults script in my image creation to setup some parameters on my onion. However one setup set that requires the devices MAC has me stumped. I need to echo the MAC into a mosquitto mqtt configuration file so I have setup a uci script called "52_mosquitto_config"
#!/bin/sh # Script to update MAC in mosquitto configuration echo "Get uppercase MAC address without colons from system config" MAC=$(cat /sys/class/net/ra0/address | sed s/://g | sed y/[abcdef]/[ABCDEF]/) echo "Configuring Mosquitto (/etc/mosquitto/mosquitto.conf) with $MAC" #replace the placeholder <MAC> in the file with the actual MAC address sed -i "s/<MAC>/$MAC/" /etc/mosquitto/mosquitto.conf exit 0
however this always results in a my placeholder "<MAC>" being replaced with "000000000000".
This led me to the conclusion that the file /sys/class/net/ra0/address is returning 00:00:00:00:00:00 at the point that uci is running.
However in uci-defaults/ file "13_hostname" they clearly use the same file to set hostname:
m1=$(cat /sys/class/net/ra0/address | awk -F':' '{ print $5 }' | awk '{print toupper($0)}') m2=$(cat /sys/class/net/ra0/address | awk -F':' '{ print $6 }' | awk '{print toupper($0)}')
My understanding of uci-defaults is that they are run sequentially based on name.
Why does mac return a valid response for script 13_hostname and not for me in 52_mosquitto_config?
Any help always appreciated.
Thanks.
-
@UFD I hit this issue some time ago, I use a hotplug script to achieve a similar outcome to what you require.
Here is my script:
#!/bin/sh
File: 40-setup
Author: chris
Created on 24 Jun. 2019, 1:07:07 am
#!/bin/sh
Place me in /etc/hotplug.d/iface/40-setup
WIFI_INTERFACE="wwan"
m1=$(cat /sys/class/net/ra0/address | awk -F':' '{ print $3$4$5$6 }' | awk '{print toupper($0)}')echo "Starting interace hotplug ${DEVICE} Action: ${ACTION} Interface: ${INTERFACE}" >> /tmp/cblog
if [ "$ACTION" = "ifup" -a "$INTERFACE" = "wwan" ]; then
echo "IFUP ${DEVICE}" >> /tmp/cblog
rm -f /etc/hotplug.d/iface/40-setup
echo "Run script for device: ${DEVICE}" >> /tmp/cblog
echo "nameserver $(ip -o -4 a s apcli0 | awk '{ print $4 }' | cut -d/ -f1)" >> /etc/resolv.conf//## Insert command to use MAC
fi
-
Thanks @crispyoz.
I have tried this workaround but i must have done something wrong as my file did not seem to want to run.
Another workaround I did however findis to use the ethernet mac address and derive the wifi one in the uci script. Seems the ra0 interface mac is only available once the wifi drivers are loaded which seems to be after uci-defaults scripts. however ethernet MAC is available and wifi mac can be derived by subtracting 1.
My updated script as shared before is now:
#!/bin/sh # Script to update MAC in mosquitto configuration echo "Get uppercase WiFi MAC address without colons from system config" EMAC=$(cat /sys/class/net/eth0/address | sed s/://g | sed y/[abcdef]/[ABCDEF]/) WMAC=$(printf "%X" $((0x${EMAC} - 1))) echo "Configuring Mosquitto (/etc/mosquitto/mosquitto.conf) with $WMAC" #replace the placeholder <MAC> in the file with the actual WMAC address sed -i "s/<MAC>/$WMAC/" /etc/mosquitto/mosquitto.conf exit 0
It will however continue to bug me as to why the original uci script did not work... I wonder how the omega onion official 13_hostname is actually doing anything when I overwrite this file with a debug version to print its mac also shows up as 00:00:00:00:00:00.
Maybe we will never know.
Thanks.
-
For the interested:
I found that MAC can also be obtained on a more lower level:hexdump -s 4 -n 6 /dev/mtd2 | sed -n '1p' | awk '{print substr($2,3) substr($2,1,2) substr($3,3) substr($3,1,2) substr($4,3) substr($4,1,2)}'
as seen in the ethernet-mode init.d script