From 13a21efd28777ddfe7578ec7506e98d248878e67 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Mon, 9 Dec 2024 14:01:21 +0100 Subject: [PATCH] libnetwork/netavark: do not create config dir in init Podman creates/initializes the network backend for every command. However most commands will not need it so we should keep the required actions we do to a minimum. In this case the config directory /etc/containers/networks by default as root may not exists and then we try to create it which can fail, i.e. when /etc is read only[1]. The code here are a bit more changes then I would have liked but we must make sure the default in memory network always exists and do not create the directory there. [1] https://github.com/containers/common/pull/2265 Signed-off-by: Paul Holzinger --- libnetwork/netavark/config.go | 3 +++ libnetwork/netavark/network.go | 19 +++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/libnetwork/netavark/config.go b/libnetwork/netavark/config.go index 33ef26acf..3305258b6 100644 --- a/libnetwork/netavark/config.go +++ b/libnetwork/netavark/config.go @@ -30,6 +30,9 @@ func sliceRemoveDuplicates(strList []string) []string { } func (n *netavarkNetwork) commitNetwork(network *types.Network) error { + if err := os.MkdirAll(n.networkConfigDir, 0o755); err != nil { + return nil + } confPath := filepath.Join(n.networkConfigDir, network.Name+".json") f, err := os.Create(confPath) if err != nil { diff --git a/libnetwork/netavark/network.go b/libnetwork/netavark/network.go index 6ec4a9d15..985d0db2d 100644 --- a/libnetwork/netavark/network.go +++ b/libnetwork/netavark/network.go @@ -135,10 +135,6 @@ func NewNetworkInterface(conf *InitConfig) (types.ContainerNetwork, error) { return nil, fmt.Errorf("failed to parse default subnet: %w", err) } - if err := os.MkdirAll(conf.NetworkConfigDir, 0o755); err != nil { - return nil, err - } - if err := os.MkdirAll(conf.NetworkRunDir, 0o755); err != nil { return nil, err } @@ -187,6 +183,21 @@ func (n *netavarkNetwork) loadNetworks() error { // check the mod time of the config dir f, err := os.Stat(n.networkConfigDir) if err != nil { + // the directory may not exists which is fine. It will be created on the first network create + if errors.Is(err, os.ErrNotExist) { + // networks are already loaded + if n.networks != nil { + return nil + } + networks := make(map[string]*types.Network, 1) + networkInfo, err := n.createDefaultNetwork() + if err != nil { + return fmt.Errorf("failed to create default network %s: %w", n.defaultNetwork, err) + } + networks[n.defaultNetwork] = networkInfo + n.networks = networks + return nil + } return err } modTime := f.ModTime()