Naming 2 usb nics on raspberry pi 4

Hi,

I am currently running OpenWrt SNAPSHOT r14365-46abcb3ade / LuCI Master git-20.246.48839-82d4384 on a raspberry pi 4.
I am using mwan3 as fail over between a cable connection and a 4g router and it works :slight_smile:

I was wondering how do I name my USB Ethernet nics. They are the same brand and same driver (trendet usb 3.0 gigabit ethernet, kmod-usb-net-asix-ax88179). I need the main connection to be named eth1 and the 4g backup as eth2 every time it reboots. I am using the onboard eth0 as the gateway router. I am newbie to all of this so not sure if I am supposed to name them based on mac address or setup vlan. I have thought about editing /etc/config/network but I think they will just override the mac address.

Cheers.

You can have a look at the hotplug.

2 Likes

Cheers. I'll have a try

I had a look at the hotplug script and think its beyond my understanding. I assume since I am using usb nics I should add a 'usb' script to the /etc/hotplug.d. The guide names usbs by device but I need to name them by mac address

3C:8C:F8:FB:86:51 Eth1
3C:8C:F8:FB:86:2A Eth2

I was trying to follow this guide instead. Does openwrt use udev? /etc/udev/rules.d/70-persistent-net.rules. Seems like its an ubuntu/redhat thing

Noob solution if anyone is curious.

Linux seems to name Ethernet devices by ascending mac addresses. Since 2a < 51 it will always set the mac address as eth1. I ended up switching the interfaces around. I tested rebooting in multiple times and it seems to always set it to eth1.

2 Likes

fyi, this is subject to probe order... so were you to connect an additional nic (with alternate driver), that is probed earlier... your device numbering will change... just something to be mindfull of... but as you've done... testing reboot behavior is a must... glad you worked something out...

2 Likes

Thanks for the heads up. Yeah i was contemplating buying more usb nics in the future to do vlans/port mirroring etc. Might just end up buying the same brand again.

in that case here is "example" not yet fully working but enough to give you some ideas on how to begin with hotplug...

messy rename a nic based on mac hotplug
#!/bin/sh

SNAME="net-000-nicname"
SLOG="/tmp/$SNAME.log"


lognic() {
	logger -t "$SNAME-$ACTION" "$1"
	echo "$SNAME-$ACTION: $1"
	echo "$SNAME-$ACTION: $1" >> $SLOG
}


nicsetupuci() {
case "$1" in
	dumpsample)
cat <<EOF
network.$INTERFACE=interface
network.$INTERFACE.ifname="$DEVICENEWNAME"
network.$INTERFACE.proto='static'
network.$INTERFACE.hostname="router"
network.$INTERFACE.ipaddr='127.7.7.1'
network.$INTERFACE.netmask='255.255.255.0'
EOF
	;;
esac

}


nicnamemod() {
	lognic "NICNAMEMOD-init $DEVICENAME:$NICMAC:$DEVICENEWNAME:$DEVICENEWMAC"
	if [ "$DEVICENAME" = "$DEVICENEWNAME" ]; then
		lognic "SAMENAME $DEVICENAME:$NICMAC:$DEVICENEWNAME"
		return 0
	fi
	lognic "CHANGE $DEVICENAME:$NICMAC:$DEVICENEWNAME:$DEVICENEWMAC"
	ip link set ${DEVICENAME} down
	ip link set ${DEVICENAME} name $DEVICENEWNAME
	######### notquiteworkingyet
	if [ "$NICMAC" != "$DEVICENEWMAC" ]; then
		lognic "SETTINGNEWMAC $DEVICENEWNAME:$NICMAC:$DEVICENEWMAC"
		lognic "ip link set dev $DEVICENEWNAME address $DEVICENEWMAC"
		ip link set dev $DEVICENEWNAME address $DEVICENEWMAC
	fi
	
	ip link set ${DEVICENEWNAME} up

	if ! uci show network | grep -q "^network.$INTERFACE."; then
		lognic "uci:/etc/config/network [no-config-for: $INTERFACE]"
		lognic "dumping-uci-nic-sample: /tmp/$SLOG.ucinicsample"
		nicsetupuci "dumpsample" >> $SLOG.ucinicsample
	else
		lognic "uci:/etc/config/network [found-config-for: $INTERFACE]"
		lognic "uci:/etc/config/network [ifname $(uci show network | grep "network.$INTERFACE.ifname=")"
		if ! uci show network | grep -q "^network.$INTERFACE.ifname='$NICNEWNAME'"; then
			lognic "uci:/etc/config/network [updateifname: $INTERFACE:$NICNEWNAME]"
			uci -q set network.$INTERFACE.ifname="$NICNEWNAME"
			uci commit network
		fi
	fi

	if ip link | grep "^$IFINDEX" | grep -q ' DOWN '; then
			lognic "link-state $IFINDEX:$NICNEWNAME [down]"
	elif ip link | grep "^$IFINDEX" | grep -q ' UP '; then
			lognic "link-state $IFINDEX:$NICNEWNAME [up]"
	elif ip link | grep "^$IFINDEX" | grep -q " UNKNOWN "; then
			lognic "link-state $IFINDEX:$NICNEWNAME [down-unknown]"
	else
			lognic "link-state $IFINDEX:$NICNEWNAME [unknown]"
			lognic "link-state $(ip link | grep "^$IFINDEX")"
			lognic "link-state ip link | grep \"^$IFINDEX\" | grep ' UP '"
	fi
}



if [ -z "$ACTION" ]; then ACTION="unknown"; fi
NICMAC=$(ip add show $DEVICENAME | grep ether | tr -s ' ' ' ' | cut -d' ' -f3)
#if [ -z "$NICMAC" ]; then NICMAC="30:20:10:01:02:03"; fi

if [ "$ACTION" = "move" ]; then
	exit 0
fi


lognic "ACTION $ACTION"
lognic "NICMAC $NICMAC"


if [ "$ACTION" = add ]; then

	if [ "${NICMAC}" = "00:00:e8:00:44:bf" ] || [ "${NICMAC}" = "aa:22:33:44:55:66" ]; then
		DEVICENEWNAME="mgmt9"
		DEVICENEWMAC="aa:22:33:44:55:66"
		INTERFACE="spare"
		nicnamemod
	else
		lognic "NICNAMEMOD-init-NICMACNOT $DEVICENAME:$NICMAC:$DEVICENEWNAME real:${NICMAC} VS 00:00:e8:00:44:bf"
	fi
else
	lognic "DEBUGCALL-$ACTION' devicename='$DEVICENAME' interface='$INTERFACE' nicmac='$NICMAC'"
fi
2 Likes

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