Skip to content

Commit

Permalink
libnetwork/netavark: do not create config dir in init
Browse files Browse the repository at this point in the history
Podman creates/initializes the network backend for every command. Howver
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] containers#2265

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Dec 9, 2024
1 parent a2b7fc0 commit 7097b3f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions libnetwork/netavark/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 15 additions & 4 deletions libnetwork/netavark/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,6 @@ func NewNetworkInterface(conf *InitConfig) (types.ContainerNetwork, error) {
return nil, err
}

if err := os.MkdirAll(conf.NetworkRunDir, 0o755); err != nil {
return nil, err
}

defaultSubnetPools := conf.Config.Network.DefaultSubnetPools
if defaultSubnetPools == nil {
defaultSubnetPools = config.DefaultSubnetPools
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 7097b3f

Please sign in to comment.