Working around the lack of udev for NICs, giving devices specific names

I would like a NIC with specific MAC address to have a particular name, something I've usually done in the past with a udev rule. I want to avoid eth0 and eth1 coming up in a random order. This is on an x64 virtual machine.

I was going to do something like this:

ETH1_MAC_ADDRESS=$(cat /sys/class/net/eth1/address)
if [ "$ETH1_MAC_ADDRESS" != "<expected mac>" ]; then
    echo "Interfaces are in the wrong order"
    # Swap the two NICs round.
    /usr/sbin/ip link set eth0 name ethtmp0
    /usr/sbin/ip link set eth1 name eth0
    /usr/sbin/ip link set ethtmp0 name eth1

But it's not very elegant, wondered if there's a better way more in tune with the OpenWrt network config.

yes... that's basically how it's done...

  • random order means there is no guarantee the 'second' nic will be initialized / parsable when this code runs ( unless you detect hotplug complete state and run outside of hotplug )
  • if you want to update uci... ( at the hotplug stage ) then you may need two handlers ( one for net one of iface to properly handle the logical side of things...

a better approach is to somehow hook into the proto-handlers or similar @ netifd-ubus or something... and implement some sort of "option sys-walk 'XYZ'" || "option deviceid 'ABC'" etc. etc. ... ( aka... there is no cleaner way short of hw-level init tweaks or changes ... well... you could remove the secondary module from modules.d/boot and probe it at runtime I suppose or place it right at the end of the module stack )

What virtualization type are you using?
It should not be random, but sorted by PCI bus + slot ID.

Yes, it's very likely to be bus order. But this is not guaranteed, so not good enough. I'd like this to work with any virtualization type.

Also, it may be affected by BIOS: Affecting interface name by mac address

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.