Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 2 additions & 31 deletions cmd/nerdctl/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/containerd/nerdctl/pkg/clientutil"
"github.com/containerd/nerdctl/pkg/cmd/container"
"github.com/containerd/nerdctl/pkg/cmd/image"
"github.com/containerd/nerdctl/pkg/containerutil"
"github.com/containerd/nerdctl/pkg/defaults"
"github.com/containerd/nerdctl/pkg/errutil"
"github.com/containerd/nerdctl/pkg/idgen"
Expand Down Expand Up @@ -922,36 +923,6 @@ func withBindMountHostIPC(_ context.Context, _ oci.Client, _ *containers.Contain
return nil
}

// withBindMountHostProcfs replaces procfs mount with rbind.
// Required for --pid=host on rootless.
//
// https://github.com/moby/moby/pull/41893/files
// https://github.com/containers/podman/blob/v3.0.0-rc1/pkg/specgen/generate/oci.go#L248-L257
func withBindMountHostProcfs(_ context.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error {
for i, m := range s.Mounts {
if path.Clean(m.Destination) == "/proc" {
newM := specs.Mount{
Destination: "/proc",
Type: "bind",
Source: "/proc",
Options: []string{"rbind", "nosuid", "noexec", "nodev"},
}
s.Mounts[i] = newM
}
}

// Remove ReadonlyPaths for /proc/*
newROP := s.Linux.ReadonlyPaths[:0]
for _, x := range s.Linux.ReadonlyPaths {
x = path.Clean(x)
if !strings.HasPrefix(x, "/proc/") {
newROP = append(newROP, x)
}
}
s.Linux.ReadonlyPaths = newROP
return nil
}

func generateLogURI(dataStore string) (*url.URL, error) {
selfExe, err := os.Executable()
if err != nil {
Expand Down Expand Up @@ -1296,7 +1267,7 @@ func generateSharingPIDOpts(ctx context.Context, targetCon containerd.Container)
if isHost {
opts = append(opts, oci.WithHostNamespace(specs.PIDNamespace))
if rootlessutil.IsRootless() {
opts = append(opts, withBindMountHostProcfs)
opts = append(opts, containerutil.WithBindMountHostProcfs)
}
} else {
ns := specs.LinuxNamespace{
Expand Down
3 changes: 2 additions & 1 deletion cmd/nerdctl/run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/containerd/containerd/pkg/userns"
"github.com/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/bypass4netnsutil"
"github.com/containerd/nerdctl/pkg/containerutil"
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
"github.com/containerd/nerdctl/pkg/rootlessutil"
"github.com/containerd/nerdctl/pkg/strutil"
Expand Down Expand Up @@ -280,7 +281,7 @@ func generatePIDOpts(ctx context.Context, client *containerd.Client, pid string)
case "host":
opts = append(opts, oci.WithHostNamespace(specs.PIDNamespace))
if rootlessutil.IsRootless() {
opts = append(opts, withBindMountHostProcfs)
opts = append(opts, containerutil.WithBindMountHostProcfs)
}
default: // container:<id|name>
parsed := strings.Split(pid, ":")
Expand Down
34 changes: 34 additions & 0 deletions pkg/containerutil/containerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ import (
"context"
"fmt"
"io"
"path"
"strconv"
"strings"
"time"

"github.com/containerd/containerd"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/oci"
"github.com/containerd/containerd/runtime/restart"
"github.com/containerd/nerdctl/pkg/portutil"
"github.com/opencontainers/runtime-spec/specs-go"
)

// PrintHostPort writes to `writer` the public (HostIP:HostPort) of a given `containerPort/protocol` in a container.
Expand Down Expand Up @@ -95,3 +99,33 @@ func UpdateExplicitlyStoppedLabel(ctx context.Context, container containerd.Cont
})
return container.Update(ctx, containerd.UpdateContainerOpts(opt))
}

// WithBindMountHostProcfs replaces procfs mount with rbind.
// Required for --pid=host on rootless.
//
// https://github.com/moby/moby/pull/41893/files
// https://github.com/containers/podman/blob/v3.0.0-rc1/pkg/specgen/generate/oci.go#L248-L257
func WithBindMountHostProcfs(_ context.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error {
for i, m := range s.Mounts {
if path.Clean(m.Destination) == "/proc" {
newM := specs.Mount{
Destination: "/proc",
Type: "bind",
Source: "/proc",
Options: []string{"rbind", "nosuid", "noexec", "nodev"},
}
s.Mounts[i] = newM
}
}

// Remove ReadonlyPaths for /proc/*
newROP := s.Linux.ReadonlyPaths[:0]
for _, x := range s.Linux.ReadonlyPaths {
x = path.Clean(x)
if !strings.HasPrefix(x, "/proc/") {
newROP = append(newROP, x)
}
}
s.Linux.ReadonlyPaths = newROP
return nil
}