Skip to content

Commit 7f9587d

Browse files
authored
Merge pull request #497 from AkihiroSuda/refactor-unshare
detach-netns: simplify unshare helper
2 parents d38d0a3 + 0dfe464 commit 7f9587d

File tree

3 files changed

+6
-74
lines changed

3 files changed

+6
-74
lines changed

cmd/rootlesskit/main.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/sirupsen/logrus"
1616
"github.com/urfave/cli/v2"
1717

18-
"github.com/rootless-containers/rootlesskit/v2/cmd/rootlesskit/unshare"
1918
"github.com/rootless-containers/rootlesskit/v2/pkg/child"
2019
"github.com/rootless-containers/rootlesskit/v2/pkg/common"
2120
"github.com/rootless-containers/rootlesskit/v2/pkg/copyup/tmpfssymlink"
@@ -42,10 +41,6 @@ const (
4241
)
4342

4443
func main() {
45-
if checkUnshareHelper() {
46-
unshare.Main()
47-
return
48-
}
4944
iAmActivationHelper := checkActivationHelper()
5045
iAmChild := os.Getenv(pipeFDEnvKey) != ""
5146
id := "parent"
@@ -706,7 +701,3 @@ func createActivationOpts(clicontext *cli.Context) (activation.Opt, error) {
706701
}
707702
return opt, nil
708703
}
709-
710-
func checkUnshareHelper() bool {
711-
return filepath.Base(os.Args[0]) == "unshare"
712-
}

cmd/rootlesskit/unshare/unshare.go

Lines changed: 0 additions & 53 deletions
This file was deleted.

pkg/child/child.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -583,19 +583,13 @@ func NewNetNsWithPathWithoutEnter(p string) error {
583583
if err := os.WriteFile(p, nil, 0400); err != nil {
584584
return err
585585
}
586-
selfExe, err := os.Executable()
586+
tempNS, err := ns.TempNetNS()
587587
if err != nil {
588588
return err
589589
}
590-
// this is hard (not impossible though) to reimplement in Go: https://github.com/cloudflare/slirpnetstack/commit/d7766a8a77f0093d3cb7a94bd0ccbe3f67d411ba
591-
cmd := exec.Command("unshare", "-n", "mount", "--bind", "/proc/self/ns/net", p)
592-
// Use our own implementation of unshare that is embedded in RootlessKit, so as to
593-
// avoid /etc/apparmor.d/unshare-userns-restrict on Ubuntu 25.04.
594-
// https://github.com/rootless-containers/rootlesskit/issues/494
595-
cmd.Path = selfExe
596-
out, err := cmd.CombinedOutput()
597-
if err != nil {
598-
return fmt.Errorf("failed to execute %v: %w (out=%q)", cmd.Args, err, string(out))
599-
}
600-
return nil
590+
defer tempNS.Close()
591+
tempNSPath := tempNS.Path()
592+
return ns.WithNetNSPath(tempNSPath, func(_ ns.NetNS) error {
593+
return unix.Mount(tempNSPath, p, "", unix.MS_BIND, "")
594+
})
601595
}

0 commit comments

Comments
 (0)