How to automatically configure a p2p network device on the host?

networking

#1

I’m trying to use a p2p virtual device pair connected to a containerized router as my main network interface on the host (Ubuntu 18.04). Currently I have to run a script manually after (re)starting the router. How can the configuration be done automatically, what’s the best practice?

This is my small script that’s working but needs to be run manually:

sudo ip link set address xxxx dev veth0
nmcli d set veth0 managed yes

I use the following profile in addition to the default:

config: {}
description: Router LXD profile
devices:
  eth1:
    host_name: veth0
    name: eth1
    nictype: p2p
    type: nic
  eth2:
    name: eth2
    nictype: macvlan
    parent: enp1s0f1
    type: nic
name: router
used_by:
- /1.0/containers/router

(Stéphane Graber) #2

You should be able to use a udev script on the host to have it trigger running your script whenever that network device shows up.

If on a systemd system, I think you can also do that with a systemd unit and a condition that makes it trigger when a veth0 device appears.

At the LXD level, you could write an events API client that will trigger when the startup event for your container triggers, but that’s very likely overkill for this use case, the udev or systemd approaches should be much easier.