Skip to content

Commit 1582fc8

Browse files
authored
Merge pull request #1057 from 89luca89/feature/ipc_flag
create/run: add ipc flag to create and run commands
2 parents 50f2580 + f66b943 commit 1582fc8

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ Logging flags:
480480
- :whale: `--log-opt=max-file=<MAX-FILE>`: The maximum number of log files that can be present. If rolling the logs creates excess files, the oldest file is removed. Only effective when `max-size` is also set. A positive integer. Defaults to 1.
481481

482482
Shared memory flags:
483+
- :whale: `--ipc`: IPC namespace to use
483484
- :whale: `--shm-size`: Size of `/dev/shm`
484485

485486
GPU flags:

cmd/nerdctl/run.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ func setCreateFlags(cmd *cobra.Command) {
138138
cmd.Flags().StringP("hostname", "h", "", "Container host name")
139139
// #endregion
140140

141+
cmd.Flags().String("ipc", "", `IPC namespace to use ("host"|"private")`)
142+
cmd.RegisterFlagCompletionFunc("ipc", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
143+
return []string{"host", "private"}, cobra.ShellCompDirectiveNoFileComp
144+
})
141145
// #region cgroups, namespaces, and ulimits flags
142146
cmd.Flags().Float64("cpus", 0.0, "Number of CPUs")
143147
cmd.Flags().StringP("memory", "m", "", "Memory limit")
@@ -744,6 +748,33 @@ func generateRootfsOpts(ctx context.Context, client *containerd.Client, platform
744748
return opts, cOpts, ensured, nil
745749
}
746750

751+
// withBindMountHostIPC replaces /dev/shm and /dev/mqueue mount with rbind.
752+
// Required for --ipc=host on rootless.
753+
//
754+
func withBindMountHostIPC(_ context.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error {
755+
for i, m := range s.Mounts {
756+
if path.Clean(m.Destination) == "/dev/shm" {
757+
newM := specs.Mount{
758+
Destination: "/dev/shm",
759+
Type: "bind",
760+
Source: "/dev/shm",
761+
Options: []string{"rbind", "nosuid", "noexec", "nodev"},
762+
}
763+
s.Mounts[i] = newM
764+
}
765+
if path.Clean(m.Destination) == "/dev/mqueue" {
766+
newM := specs.Mount{
767+
Destination: "/dev/mqueue",
768+
Type: "bind",
769+
Source: "/dev/mqueue",
770+
Options: []string{"rbind", "nosuid", "noexec", "nodev"},
771+
}
772+
s.Mounts[i] = newM
773+
}
774+
}
775+
return nil
776+
}
777+
747778
// withBindMountHostProcfs replaces procfs mount with rbind.
748779
// Required for --pid=host on rootless.
749780
//

cmd/nerdctl/run_linux.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,17 @@ func setPlatformOptions(opts []oci.SpecOpts, cmd *cobra.Command, id string) ([]o
175175
opts = append(opts, oci.WithRdt(rdtClass, "", ""))
176176
}
177177

178+
ipc, err := cmd.Flags().GetString("ipc")
179+
if err != nil {
180+
return nil, err
181+
}
182+
// if nothing is specified, or if private, default to normal behavior
183+
if ipc == "host" {
184+
opts = append(opts, oci.WithHostNamespace(specs.IPCNamespace))
185+
opts = append(opts, withBindMountHostIPC)
186+
} else if ipc != "" && ipc != "private" {
187+
return nil, fmt.Errorf("error: %v", "invalid ipc value, supported values are 'private' or 'host'")
188+
}
189+
178190
return opts, nil
179191
}

cmd/nerdctl/run_linux_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ func TestRunPidHost(t *testing.T) {
7171
base.Cmd("run", "--rm", "--pid=host", testutil.AlpineImage, "ps", "auxw").AssertOutContains(strconv.Itoa(pid))
7272
}
7373

74+
func TestRunIpcHost(t *testing.T) {
75+
t.Parallel()
76+
base := testutil.NewBase(t)
77+
testFilePath := filepath.Join("/dev/shm",
78+
fmt.Sprintf("%s-%d-%s", testutil.Identifier(t), os.Geteuid(), base.Target))
79+
err := os.WriteFile(testFilePath, []byte(""), 0644)
80+
assert.NilError(base.T, err)
81+
defer os.Remove(testFilePath)
82+
83+
base.Cmd("run", "--rm", "--ipc=host", testutil.AlpineImage, "ls", testFilePath).AssertOK()
84+
}
85+
7486
func TestRunAddHost(t *testing.T) {
7587
t.Parallel()
7688
base := testutil.NewBase(t)

0 commit comments

Comments
 (0)