diff options
Diffstat (limited to 'libpod/networking_linux.go')
-rw-r--r-- | libpod/networking_linux.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/libpod/networking_linux.go b/libpod/networking_linux.go index dbc68e04b..2186b4ac9 100644 --- a/libpod/networking_linux.go +++ b/libpod/networking_linux.go @@ -6,9 +6,11 @@ import ( "crypto/rand" "fmt" "os" + "os/exec" "path/filepath" "strconv" "strings" + "syscall" cnitypes "github.com/containernetworking/cni/pkg/types/current" "github.com/containernetworking/plugins/pkg/ns" @@ -93,6 +95,42 @@ func (r *Runtime) createNetNS(ctr *Container) (err error) { return r.configureNetNS(ctr, ctrNS) } +// Configure the network namespace for a rootless container +func (r *Runtime) setupRootlessNetNS(ctr *Container) (err error) { + defer ctr.rootlessSlirpSyncR.Close() + defer ctr.rootlessSlirpSyncW.Close() + + path, err := exec.LookPath("slirp4netns") + if err != nil { + logrus.Errorf("could not find slirp4netns, the network namespace won't be configured: %v", err) + return nil + } + + syncR, syncW, err := os.Pipe() + if err != nil { + return errors.Wrapf(err, "failed to open pipe") + } + defer syncR.Close() + defer syncW.Close() + + cmd := exec.Command(path, "-c", "-e", "3", "-r", "4", fmt.Sprintf("%d", ctr.state.PID), "tap0") + + cmd.SysProcAttr = &syscall.SysProcAttr{ + Setpgid: true, + } + cmd.ExtraFiles = append(cmd.ExtraFiles, ctr.rootlessSlirpSyncR, syncW) + + if err := cmd.Start(); err != nil { + return errors.Wrapf(err, "failed to start process") + } + + b := make([]byte, 16) + if _, err := syncR.Read(b); err != nil { + return errors.Wrapf(err, "failed to read from sync pipe") + } + return nil +} + // Configure the network namespace using the container process func (r *Runtime) setupNetNS(ctr *Container) (err error) { nsProcess := fmt.Sprintf("/proc/%d/ns/net", ctr.state.PID) |