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

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

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.

I think that allowing users to configure p2p interfaces through LXD would be a very useful feature in LXD.

For example, I’m writing an LXD backend to NixOps, a tool used for declarative management and deployment of NixOS systems. This backend can only use the LXD API, as it is agnostic of the host where LXD is running. Currently, the only way to deploy containers with isolated networks is by defining a bridge for each container with a /31 network, which is very cumbersome. If LXD could be used to configure P2P interfaces as it does with bridges, it would be possible to deploy to network-isolated containers without having to waste an IP address for each container.

I don’t know if it’s appropriate to submit feature requests in the GitHub issue tracker. Sadly, I’m not experienced enough with Go and LXD’s codebase to implement this feature myself at the moment.

Have you looked at the routed nic type, it sounds like it may suit your purposes.

Oh right ! I forgot checking the 4.0 docs to see if it had been implemented. Thanks a lot @tomp. Sorry for the noise.

I solved the original issue with systemd-networkd and the following link and network files. It has been working very well on Ubuntu 18.04.

# cat /etc/systemd/network/50-veth0.link 
[Match]
OriginalName=veth0

[Link]
MACAddress=xxxx

# cat /etc/systemd/network/50-veth0.network 
[Match]
Name=veth0

[Network]
Description=Router
DHCP=yes
IPv6PrivacyExtensions=true