Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit c802f93

Browse files
authored
Merge pull request containerd#298 from AkihiroSuda/fix-297
rootless: fix --pid=host
2 parents 5e311ef + 747cb2e commit c802f93

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

cmd/nerdctl/run.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"net/url"
2525
"os"
26+
"path"
2627
"path/filepath"
2728
"strings"
2829

@@ -48,6 +49,7 @@ import (
4849
"github.com/containerd/nerdctl/pkg/netutil"
4950
"github.com/containerd/nerdctl/pkg/netutil/nettype"
5051
"github.com/containerd/nerdctl/pkg/portutil"
52+
"github.com/containerd/nerdctl/pkg/rootlessutil"
5153
"github.com/containerd/nerdctl/pkg/strutil"
5254
"github.com/containerd/nerdctl/pkg/taskutil"
5355
"github.com/docker/cli/opts"
@@ -495,6 +497,9 @@ func runAction(clicontext *cli.Context) error {
495497
return fmt.Errorf("Invalid pid namespace. Set --pid=host to enable host pid namespace.")
496498
} else {
497499
opts = append(opts, oci.WithHostNamespace(specs.PIDNamespace))
500+
if rootlessutil.IsRootless() {
501+
opts = append(opts, withBindMountHostProcfs)
502+
}
498503
}
499504
}
500505

@@ -669,6 +674,36 @@ func generateRootfsOpts(ctx context.Context, client *containerd.Client, cliconte
669674
return opts, cOpts, ensured, nil
670675
}
671676

677+
// withBindMountHostProcfs replaces procfs mount with rbind.
678+
// Required for --pid=host on rootless.
679+
//
680+
// https://github.com/moby/moby/pull/41893/files
681+
// https://github.com/containers/podman/blob/v3.0.0-rc1/pkg/specgen/generate/oci.go#L248-L257
682+
func withBindMountHostProcfs(_ context.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error {
683+
for i, m := range s.Mounts {
684+
if path.Clean(m.Destination) == "/proc" {
685+
newM := specs.Mount{
686+
Destination: "/proc",
687+
Type: "bind",
688+
Source: "/proc",
689+
Options: []string{"rbind", "nosuid", "noexec", "nodev"},
690+
}
691+
s.Mounts[i] = newM
692+
}
693+
}
694+
695+
// Remove ReadonlyPaths for /proc/*
696+
newROP := s.Linux.ReadonlyPaths[:0]
697+
for _, x := range s.Linux.ReadonlyPaths {
698+
x = path.Clean(x)
699+
if !strings.HasPrefix(x, "/proc/") {
700+
newROP = append(newROP, x)
701+
}
702+
}
703+
s.Linux.ReadonlyPaths = newROP
704+
return nil
705+
}
706+
672707
func withCustomResolvConf(src string) func(context.Context, oci.Client, *containers.Container, *oci.Spec) error {
673708
return func(_ context.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error {
674709
s.Mounts = append(s.Mounts, specs.Mount{

cmd/nerdctl/run_mount.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import (
3939
"github.com/urfave/cli/v2"
4040
)
4141

42+
// generateMountOpts generates volume-related mount opts.
43+
// Other mounts such as procfs mount are not handled here.
4244
func generateMountOpts(clicontext *cli.Context, ctx context.Context, client *containerd.Client, ensuredImage *imgutil.EnsuredImage) ([]oci.SpecOpts, []string, error) {
4345
volStore, err := getVolumeStore(clicontext)
4446
if err != nil {

cmd/nerdctl/run_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io/ioutil"
2222
"os"
2323
"path/filepath"
24+
"strconv"
2425
"strings"
2526
"testing"
2627

@@ -181,3 +182,10 @@ func TestRunEnvFile(t *testing.T) {
181182
base.Cmd("run", "--rm", "--env-file", path1, "--env-file", path2, testutil.AlpineImage, "sh", "-c", "echo $TESTKEY1").AssertOutContains("TESTVAL1")
182183
base.Cmd("run", "--rm", "--env-file", path1, "--env-file", path2, testutil.AlpineImage, "sh", "-c", "echo $TESTKEY2").AssertOutContains("TESTVAL2")
183184
}
185+
186+
func TestRunPidHost(t *testing.T) {
187+
base := testutil.NewBase(t)
188+
pid := os.Getpid()
189+
190+
base.Cmd("run", "--rm", "--pid=host", testutil.AlpineImage, "ps", "auxw").AssertOutContains(strconv.Itoa(pid))
191+
}

0 commit comments

Comments
 (0)