mirror of
https://github.com/containers/podman.git
synced 2025-10-17 03:04:21 +08:00
rootless netns, one netns per libpod tmp dir
The netns cleanup code is checking if there are running containers, this can fail if you run several libpod instances with diffrent root/runroot. To fix it we use one netns for each libpod instances. To prevent name conflicts we use a hash from the static dir as part of the name. Previously this worked because we would use the CNI files to check if the netns was still in use. but this is no longer possible with netavark. [NO NEW TESTS NEEDED] Fixes #12306 Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
@ -4,6 +4,7 @@ package libpod
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
@ -400,10 +401,7 @@ func (r *Runtime) GetRootlessNetNs(new bool) (*RootlessNetNS, error) {
|
||||
return nil, nil
|
||||
}
|
||||
var rootlessNetNS *RootlessNetNS
|
||||
runDir, err := util.GetRuntimeDir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
runDir := r.config.Engine.TmpDir
|
||||
|
||||
lfile := filepath.Join(runDir, "rootless-netns.lock")
|
||||
lock, err := lockfile.GetLockfile(lfile)
|
||||
@ -429,7 +427,15 @@ func (r *Runtime) GetRootlessNetNs(new bool) (*RootlessNetNS, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
path := filepath.Join(nsDir, rootlessNetNsName)
|
||||
|
||||
// create a hash from the static dir
|
||||
// the cleanup will check if there are running containers
|
||||
// if you run a several libpod instances with different root/runroot directories this check will fail
|
||||
// we want one netns for each libpod static dir so we use the hash to prevent name collisions
|
||||
hash := sha1.Sum([]byte(r.config.Engine.StaticDir))
|
||||
netnsName := fmt.Sprintf("%s-%x", rootlessNetNsName, hash[:10])
|
||||
|
||||
path := filepath.Join(nsDir, netnsName)
|
||||
ns, err := ns.GetNS(path)
|
||||
if err != nil {
|
||||
if !new {
|
||||
@ -437,8 +443,8 @@ func (r *Runtime) GetRootlessNetNs(new bool) (*RootlessNetNS, error) {
|
||||
return nil, errors.Wrap(err, "error getting rootless network namespace")
|
||||
}
|
||||
// create a new namespace
|
||||
logrus.Debug("creating rootless network namespace")
|
||||
ns, err = netns.NewNSWithName(rootlessNetNsName)
|
||||
logrus.Debugf("creating rootless network namespace with name %q", netnsName)
|
||||
ns, err = netns.NewNSWithName(netnsName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error creating rootless network namespace")
|
||||
}
|
||||
|
Reference in New Issue
Block a user