Mikrotik HAP AC2 "RBD52G-5HacD2HnD-TC" MAC address issue

I'm having a problem with MAC address automatically randomizing in my builds and not using the default mac address. I'm not including a MAC address in /etc/config/network and it will randomize the MAC address after each boot. This is not the desired behavior that I'm looking for. How does openwrt set this on first boot on the default firmware? Would it be that the first boot I've set the WAN interface to be PPPoE and not DHCP and the interface isn't up during the first configuration?

I observe the same behavior as well on my unit. It has something to do with the EDMA using random MACs at boot.

I find that the official images make this works so it must be something that I am doing wrong in the build process. I'm not against using tools to extract it from mtd and using sed to replace a defualt value. However i've not figured out how to do that yet. Also since the wlan is only a few macs off its possible that i can just have a script to hex add or subract the correct offset to find out what it would be.

Just doing some digging around I've found that the MAC address for the board are in this file /overlay/upper/etc/board.json
So doing something like this awk '/label_macaddr/{print $NF}' /overlay/upper/etc/board.json | sed 's/"//g'
Will get you the MAC address for eth0

Last I remember:
netifd, the network manager for OpenWrt, reads those MAC addresses from /etc/config/network. For hap ac2, they are not set (in Linux) anywhere else, so they are randomised at boot.

For this device, when the image first boots, the /etc/board.json file is built, using these rules to set the MAC addresses for the LAN and WAN devices: https://github.com/openwrt/openwrt/blob/master/target/linux/ipq40xx/base-files/etc/board.d/02_network#L211=
Then the uci /etc/config/network files are then generated based on board.json

If you really don't like this, and you are building your own images, you could set the MAC addresses at linux level like these other ipq40xx devices do: https://github.com/openwrt/openwrt/blob/master/target/linux/ipq40xx/base-files/lib/preinit/05_set_iface_mac_ipq40xx.sh

Current best practice is using nvmem to set MAC addresses, but we cannot do this with RouterBoot devices as our RouterBoot config reader (which reads the MAC addresses from RouterBoot tags on NOR) does not support nvmem yet.

This is exactly what I was looking for..
cat /sys/firmware/mikrotik/hard_config/mac_base
instead of how I was doing it...
awk '/label_macaddr/{print $NF}' /overlay/upper/etc/board.json | sed 's/"//g')
which worked but hey why reinvent the wheel.

So this is the script that I'm running at boot.

#!/bin/sh
echo 'Getting WAN Mac Address'
PPPoE_Username=$(awk '/label_macaddr/{print $NF}' /overlay/upper/etc/board.json | sed 's/"//g')
echo $PPPoE_Username
echo 'Configuring WAN link for PPPoE'
uci set network.wan.macaddr=$PPPoE_Username
uci set network.wan.proto=pppoe
uci set network.wan.username=$PPPoE_Username
uci set network.wan.password=XXXXXXXXXXXXXXXXXXXX
echo 'Commiting Changes'
uci commit network
echo 'Restarting Network'
service network reload

and that gets me

config interface 'wan'
        option device 'eth1'
        option proto 'pppoe'
        option ipv6 'auto'
        option username '48:8f:5a:2b:38:8e'
        option password 'XXXXXXXXXXXXXXXXXXXXXXXX'
        option macaddr '48:8f:5a:2b:38:8e'

and that mostly gets me where I need to be so now I've got to figure out the UCI commands to set

config device
        option name 'eth1'

To

config device
        option name 'eth1'
        option macaddr '48:8f:5a:2b:38:8e'

So I can keep track of the proper MAC address on the eth1 interface.

If anyone can help me with this last bit it would be most helpful

Well, I wasn't thinking so sorry for asking before putting some thought into it...

I set the option that I wanted and then did a uci show network to get this

network.@device[2]=device
network.@device[2].name='eth1'
network.@device[2].macaddr='48:8f:5a:2b:38:8e'

and then I was able to figure out that the proper command is

uci set network.@device[2].macaddr='48:8f:5a:2b:38:8e'

Thank you @johnth for the help in getting me going... Newish to OpenWrt so every little bit helps.

What I ended up with for setting the MAC's at boot using rc.local to run this script before I run my PPPoE setup scripts.

#!/bin/sh
# This script is to setup the correct MAC address for a Mikrotik routerboad as they are random on boot in my custom image

. /lib/functions/uci-defaults.sh
. /lib/functions/system.sh

wan_mac=$(cat /sys/firmware/mikrotik/hard_config/mac_base)
echo $wan_mac
lan_mac=$(macaddr_add $wan_mac 1)
echo $lan_mac
#
echo 'Setting WAN MAC'
uci set network.@device[2].macaddr=$wan_mac
echo 'Setting LAN MAC'
uci set network.@device[1].macaddr=$lan_mac
#
echo 'Commiting Changes'
uci commit network
echo 'Restarting Network'
service network reload
1 Like