Skip to content

Commit 7882001

Browse files
authored
Merge pull request #4718 from weiyuhang2011/fix-ipc-share
fix: shared IPC ns when setting shareable
2 parents d135c67 + 83807d3 commit 7882001

2 files changed

Lines changed: 96 additions & 10 deletions

File tree

cmd/nerdctl/container/container_run_linux_test.go

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func TestRunIpcHost(t *testing.T) {
189189
base := testutil.NewBase(t)
190190
testFilePath := filepath.Join("/dev/shm",
191191
fmt.Sprintf("%s-%d-%s", testutil.Identifier(t), os.Geteuid(), base.Target))
192-
err := os.WriteFile(testFilePath, []byte(""), 0644)
192+
err := os.WriteFile(testFilePath, []byte(""), 0o644)
193193
assert.NilError(base.T, err)
194194
defer os.Remove(testFilePath)
195195

@@ -203,7 +203,7 @@ func TestRunAddHost(t *testing.T) {
203203
var found bool
204204
sc := bufio.NewScanner(bytes.NewBufferString(stdout))
205205
for sc.Scan() {
206-
//removing spaces and tabs separating items
206+
// removing spaces and tabs separating items
207207
line := strings.ReplaceAll(sc.Text(), " ", "")
208208
line = strings.ReplaceAll(line, "\t", "")
209209
if strings.Contains(line, "10.0.0.1testing.example.com") {
@@ -219,7 +219,7 @@ func TestRunAddHost(t *testing.T) {
219219
var found int
220220
sc := bufio.NewScanner(bytes.NewBufferString(stdout))
221221
for sc.Scan() {
222-
//removing spaces and tabs separating items
222+
// removing spaces and tabs separating items
223223
line := strings.ReplaceAll(sc.Text(), " ", "")
224224
line = strings.ReplaceAll(line, "\t", "")
225225
if strutil.InStringSlice([]string{"10.0.0.1test", "10.0.0.1test1"}, line) {
@@ -252,7 +252,7 @@ func TestRunAddHostWithCustomHostGatewayIP(t *testing.T) {
252252
var found bool
253253
sc := bufio.NewScanner(bytes.NewBufferString(stdout))
254254
for sc.Scan() {
255-
//removing spaces and tabs separating items
255+
// removing spaces and tabs separating items
256256
line := strings.ReplaceAll(sc.Text(), " ", "")
257257
line = strings.ReplaceAll(line, "\t", "")
258258
if strings.Contains(line, "192.168.5.2test") {
@@ -449,7 +449,7 @@ func TestRunSigProxy(t *testing.T) {
449449
func TestRunWithFluentdLogDriver(t *testing.T) {
450450
base := testutil.NewBase(t)
451451
tempDirectory := t.TempDir()
452-
err := os.Chmod(tempDirectory, 0777)
452+
err := os.Chmod(tempDirectory, 0o777)
453453
assert.NilError(t, err)
454454

455455
containerName := testutil.Identifier(t)
@@ -478,7 +478,7 @@ func TestRunWithFluentdLogDriver(t *testing.T) {
478478
func TestRunWithFluentdLogDriverWithLogOpt(t *testing.T) {
479479
base := testutil.NewBase(t)
480480
tempDirectory := t.TempDir()
481-
err := os.Chmod(tempDirectory, 0777)
481+
err := os.Chmod(tempDirectory, 0o777)
482482
assert.NilError(t, err)
483483

484484
containerName := testutil.Identifier(t)
@@ -510,7 +510,7 @@ func TestRunWithOOMScoreAdj(t *testing.T) {
510510
}
511511
t.Parallel()
512512
base := testutil.NewBase(t)
513-
var score = "-42"
513+
score := "-42"
514514

515515
base.Cmd("run", "--rm", "--oom-score-adj", score, testutil.AlpineImage, "cat", "/proc/self/oom_score_adj").AssertOutContains(score)
516516
}
@@ -712,7 +712,7 @@ devices:
712712
tomlPath := filepath.Join(t.TempDir(), "nerdctl.toml")
713713
err := os.WriteFile(tomlPath, []byte(fmt.Sprintf(`
714714
cdi_spec_dirs = ["%s"]
715-
`, cdiSpecDir)), 0400)
715+
`, cdiSpecDir)), 0o400)
716716
assert.NilError(t, err)
717717

718718
base := testutil.NewBase(t)
@@ -854,6 +854,7 @@ devices:
854854
env:
855855
- FOO=injected
856856
`
857+
857858
tmpDir := t.TempDir()
858859
writeTestCDISpec(t, amdSpec, "amd.yaml", tmpDir)
859860
writeTestCDISpec(t, vendor1Spec, "vendor1.yaml", tmpDir)
@@ -878,9 +879,89 @@ devices:
878879
}
879880

880881
func writeTestCDISpec(t *testing.T, spec string, fileName string, cdiSpecDir string) {
881-
err := os.MkdirAll(cdiSpecDir, 0700)
882+
err := os.MkdirAll(cdiSpecDir, 0o700)
882883
assert.NilError(t, err)
883884
cdiSpecPath := filepath.Join(cdiSpecDir, fileName)
884-
err = os.WriteFile(cdiSpecPath, []byte(spec), 0400)
885+
err = os.WriteFile(cdiSpecPath, []byte(spec), 0o400)
885886
assert.NilError(t, err)
886887
}
888+
889+
func TestSharedIpcSetup(t *testing.T) {
890+
nerdtest.Setup()
891+
testCase := &test.Case{
892+
Require: require.Not(require.Windows),
893+
Setup: func(data test.Data, helpers test.Helpers) {
894+
data.Labels().Set("container1", data.Identifier("container1"))
895+
helpers.Ensure("run", "-d", "--name", data.Identifier("container1"), "--ipc=shareable",
896+
testutil.CommonImage, "sleep", "inf")
897+
nerdtest.EnsureContainerStarted(helpers, data.Identifier("container1"))
898+
},
899+
Cleanup: func(data test.Data, helpers test.Helpers) {
900+
helpers.Anyhow("rm", "-f", data.Identifier("container1"))
901+
},
902+
SubTests: []*test.Case{
903+
{
904+
Description: "Test ipc is shared",
905+
NoParallel: true, // The validation involves starting of the main container: container1
906+
Cleanup: func(data test.Data, helpers test.Helpers) {
907+
helpers.Anyhow("rm", "-f", data.Identifier("container2"))
908+
},
909+
Setup: func(data test.Data, helpers test.Helpers) {
910+
helpers.Ensure(
911+
"run", "-d", "--name", data.Identifier("container2"),
912+
"--ipc=container:"+data.Labels().Get("container1"),
913+
testutil.NginxAlpineImage)
914+
data.Labels().Set("container2", data.Identifier("container2"))
915+
nerdtest.EnsureContainerStarted(helpers, data.Identifier("container2"))
916+
},
917+
SubTests: []*test.Case{
918+
{
919+
NoParallel: true,
920+
Description: "Test ipc is shared",
921+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
922+
return helpers.Command("exec", data.Labels().Get("container2"), "readlink", "/proc/1/ns/ipc")
923+
},
924+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
925+
return &test.Expected{
926+
ExitCode: 0,
927+
Output: expect.All(
928+
func(stdout string, t tig.T) {
929+
container1IPC := strings.TrimSpace(helpers.Capture("exec", data.Labels().Get("container1"), "readlink", "/proc/1/ns/ipc"))
930+
container2IPC := strings.TrimSpace(stdout)
931+
assert.Equal(t, container1IPC, container2IPC)
932+
},
933+
),
934+
}
935+
},
936+
},
937+
{
938+
NoParallel: true,
939+
Description: "Test ipc is shared after restart",
940+
Setup: func(data test.Data, helpers test.Helpers) {
941+
helpers.Ensure("restart", data.Labels().Get("container1"))
942+
helpers.Ensure("stop", "--time=1", data.Labels().Get("container2"))
943+
helpers.Ensure("start", data.Labels().Get("container2"))
944+
nerdtest.EnsureContainerStarted(helpers, data.Labels().Get("container2"))
945+
},
946+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
947+
return helpers.Command("exec", data.Labels().Get("container2"), "readlink", "/proc/1/ns/ipc")
948+
},
949+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
950+
return &test.Expected{
951+
ExitCode: 0,
952+
Output: expect.All(
953+
func(stdout string, t tig.T) {
954+
container1IPC := strings.TrimSpace(helpers.Capture("exec", data.Labels().Get("container1"), "readlink", "/proc/1/ns/ipc"))
955+
container2IPC := strings.TrimSpace(stdout)
956+
assert.Equal(t, container1IPC, container2IPC)
957+
},
958+
),
959+
}
960+
},
961+
},
962+
},
963+
},
964+
},
965+
}
966+
testCase.Run(t)
967+
}

pkg/ipcutil/ipcutil.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ func GenerateIPCOpts(ctx context.Context, ipc IPC, client *containerd.Client) ([
207207
}
208208

209209
opts = append(opts, withBindMountHostOtherSourceIPC(*targetConIPC.HostShmPath))
210+
ns := specs.LinuxNamespace{
211+
Type: specs.IPCNamespace,
212+
Path: fmt.Sprintf("/proc/%d/ns/ipc", task.Pid()),
213+
}
214+
opts = append(opts, oci.WithLinuxNamespace(ns))
210215
}
211216

212217
return opts, nil

0 commit comments

Comments
 (0)