I would like all of the physical interfaces on my system to live within a specific network namespace called "physical". It's easy enough to move devices over manually using commands like ip link set enp2s0 netns physical
and iw phy phy0 set netns name physical
, but I would like this to happen automatically both at boot and for any devices connected at runtime.
It seems like the best way to accomplish this would be via udev rules, or possibly some systemd magic.
My first thought was just to write a udev rule that runs the appropriate command, but I ran into a couple of questions I haven't been able to answer via my searches:
- How do I distinguish physical interfaces from virtual interfaces?
- How do I tell WLAN interfaces apart so I can issue the
iw
command instead of theip
command? - How do I get the name of a WLAN phy so I can pass it to the
iw
command?
I was hoping the above would be relatively straightforward, but I don't see any obvious distinguishing factors in udevadm info
.
Best Answer
Most of these informations are retrievable from
/sys
:1a.
/sys/class/net/
: list of network devices, all types included1b.
/sys/devices/virtual/net/
: list of virtual network devices: includeslo
, tunnels, veth, bridges ... so if it's in the former but not this one, it should be physical.if a device is a modern wireless device (driver), it will have the entry
/sys/class/net/<device>/phy82011/name
, eg:So by running the adequate script from
udev
environment and comparing with those directories and files from/sys
, you should have all the needed informations to do it.Just as a side note, if later, to work on those devices, you change only the network namespace (eg using
nsenter --net=/var/run/netns/physical
),/sys
will still be in the host's mount namespace, and won't reflect the arrival of those devices but will instead show them missing. Usingip netns exec physical command
is fine, it does change mount namespace and remounts/sys
for you.