Detecting whether netplan is managing network config (shell)

I would like to figure out in a shell script in an LXD container, whether the container is being managed by Netplan. This can be a bit of an unknown as I have a mix of 16.04 systems 18.04 systems and upgraded 18.04 systems.

In order to write correct network config to the container, I need to detect if the config is static in /etc/network/interfaces or in a Netplan yaml file.

I could really use something like netplan status but since that command does not exist I need alternatives.

I have asked around a fair bit, and there seems to be no official way.The suggestions I have had so far:

  1. cat /etc/network/interfaces and look for the Netplan info message there. I don't like this approach, as this would obviously be prone to breakage.
  2. Someone suggested I could just look for /etc/netplan - but that will not work on upgraded systems as there Netplan is disabled but still installed.
  3. Another suggestion was the inverse: that I could check if ifupdown or systemd is managing the network. Problem is, I am not sure how to do this in a practical sense.

You may be able to check for the ifstate file from ifupdown, which will tell you whether that's what was used to configure the network:

/run/network/ifstate

If that's there and it contains text, you can expect that ifupdown is in use. If it's not present, the system is likely managed via netplan (but that won't tell you whether it's NetworkManager or systemd)

Then, if you want to know what backend is used for a particular interface, you can ask netplan, and parse the output to get the information you need:

netplan generate --mapping enp3s0

It outputs text like this:

id=enp3s0, backend=NetworkManager, set_name=(null), match_name=enp3s0, match_mac=(null), match_driver=(null)

That way you can also know using what matching rules the interface was configured (you use the "final" name of an interface to check the mapping, it tells you what matching rule is in the netplan YAML that matches for it).