Skip to content

Commit 4612201

Browse files
authored
Merge pull request containerd#9635 from Burning1020/fix-cri-mounts
cri: Stat host sandbox files before adding them
2 parents b87d78f + 5611db5 commit 4612201

File tree

2 files changed

+59
-56
lines changed

2 files changed

+59
-56
lines changed

internal/cri/server/container_create.go

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,51 +1008,65 @@ func (c *criService) linuxContainerMounts(sandboxID string, config *runtime.Cont
10081008
}
10091009

10101010
if !isInCRIMounts(etcHosts, config.GetMounts()) {
1011-
mounts = append(mounts, &runtime.Mount{
1012-
ContainerPath: etcHosts,
1013-
HostPath: c.getSandboxHosts(sandboxID),
1014-
Readonly: securityContext.GetReadonlyRootfs(),
1015-
SelinuxRelabel: true,
1016-
UidMappings: uidMappings,
1017-
GidMappings: gidMappings,
1018-
})
1011+
hostpath := c.getSandboxHosts(sandboxID)
1012+
// /etc/hosts could be delegated to remote sandbox controller. That file isn't required to be existed
1013+
// in host side for some sandbox runtimes. Skip it if we don't need it.
1014+
if _, err := c.os.Stat(hostpath); err == nil {
1015+
mounts = append(mounts, &runtime.Mount{
1016+
ContainerPath: etcHosts,
1017+
HostPath: hostpath,
1018+
Readonly: securityContext.GetReadonlyRootfs(),
1019+
SelinuxRelabel: true,
1020+
UidMappings: uidMappings,
1021+
GidMappings: gidMappings,
1022+
})
1023+
}
10191024
}
10201025

10211026
// Mount sandbox resolv.config.
10221027
// TODO: Need to figure out whether we should always mount it as read-only
10231028
if !isInCRIMounts(resolvConfPath, config.GetMounts()) {
1024-
mounts = append(mounts, &runtime.Mount{
1025-
ContainerPath: resolvConfPath,
1026-
HostPath: c.getResolvPath(sandboxID),
1027-
Readonly: securityContext.GetReadonlyRootfs(),
1028-
SelinuxRelabel: true,
1029-
UidMappings: uidMappings,
1030-
GidMappings: gidMappings,
1031-
})
1029+
hostpath := c.getResolvPath(sandboxID)
1030+
// The ownership of /etc/resolv.conf could be delegated to remote sandbox controller. That file isn't
1031+
// required to be existed in host side for some sandbox runtimes. Skip it if we don't need it.
1032+
if _, err := c.os.Stat(hostpath); err == nil {
1033+
mounts = append(mounts, &runtime.Mount{
1034+
ContainerPath: resolvConfPath,
1035+
HostPath: hostpath,
1036+
Readonly: securityContext.GetReadonlyRootfs(),
1037+
SelinuxRelabel: true,
1038+
UidMappings: uidMappings,
1039+
GidMappings: gidMappings,
1040+
})
1041+
}
10321042
}
10331043

10341044
if !isInCRIMounts(devShm, config.GetMounts()) {
10351045
sandboxDevShm := c.getSandboxDevShm(sandboxID)
10361046
if securityContext.GetNamespaceOptions().GetIpc() == runtime.NamespaceMode_NODE {
10371047
sandboxDevShm = devShm
10381048
}
1039-
mounts = append(mounts, &runtime.Mount{
1040-
ContainerPath: devShm,
1041-
HostPath: sandboxDevShm,
1042-
Readonly: false,
1043-
SelinuxRelabel: sandboxDevShm != devShm,
1044-
// XXX: tmpfs support for idmap mounts got merged in
1045-
// Linux 6.3.
1046-
// Our Ubuntu 22.04 CI runs with 5.15 kernels, so
1047-
// disabling idmap mounts for this case makes the CI
1048-
// happy (the other fs used support idmap mounts in 5.15
1049-
// kernels).
1050-
// We can enable this at a later stage, but as this
1051-
// tmpfs mount is exposed empty to the container (no
1052-
// prepopulated files) and using the hostIPC with userns
1053-
// is blocked by k8s, we can just avoid using the
1054-
// mappings and it should work fine.
1055-
})
1049+
// The ownership of /dev/shm could be delegated to remote sandbox controller. That file isn't required
1050+
// to be existed in host side for some sandbox runtimes. Skip it if we don't need it.
1051+
if _, err := c.os.Stat(sandboxDevShm); err == nil {
1052+
mounts = append(mounts, &runtime.Mount{
1053+
ContainerPath: devShm,
1054+
HostPath: sandboxDevShm,
1055+
Readonly: false,
1056+
SelinuxRelabel: sandboxDevShm != devShm,
1057+
// XXX: tmpfs support for idmap mounts got merged in
1058+
// Linux 6.3.
1059+
// Our Ubuntu 22.04 CI runs with 5.15 kernels, so
1060+
// disabling idmap mounts for this case makes the CI
1061+
// happy (the other fs used support idmap mounts in 5.15
1062+
// kernels).
1063+
// We can enable this at a later stage, but as this
1064+
// tmpfs mount is exposed empty to the container (no
1065+
// prepopulated files) and using the hostIPC with userns
1066+
// is blocked by k8s, we can just avoid using the
1067+
// mappings and it should work fine.
1068+
})
1069+
}
10561070
}
10571071
return mounts
10581072
}

internal/cri/server/container_create_test.go

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -733,32 +733,21 @@ func TestLinuxContainerMounts(t *testing.T) {
733733
expectedMounts: nil,
734734
},
735735
{
736-
desc: "should skip hostname mount if the old sandbox doesn't have hostname file",
736+
desc: "should skip sandbox mounts if the old sandbox doesn't have sandbox file",
737737
statFn: func(path string) (os.FileInfo, error) {
738-
assert.Equal(t, filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hostname"), path)
739-
return nil, errors.New("random error")
738+
sandboxRootDir := filepath.Join(testRootDir, sandboxesDir, testSandboxID)
739+
sandboxStateDir := filepath.Join(testStateDir, sandboxesDir, testSandboxID)
740+
switch path {
741+
case filepath.Join(sandboxRootDir, "hostname"), filepath.Join(sandboxRootDir, "hosts"),
742+
filepath.Join(sandboxRootDir, "resolv.conf"), filepath.Join(sandboxStateDir, "shm"):
743+
return nil, errors.New("random error")
744+
default:
745+
t.Fatalf("expected sandbox files, got: %s", path)
746+
}
747+
return nil, nil
740748
},
741749
securityContext: &runtime.LinuxContainerSecurityContext{},
742-
expectedMounts: []*runtime.Mount{
743-
{
744-
ContainerPath: "/etc/hosts",
745-
HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hosts"),
746-
Readonly: false,
747-
SelinuxRelabel: true,
748-
},
749-
{
750-
ContainerPath: resolvConfPath,
751-
HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "resolv.conf"),
752-
Readonly: false,
753-
SelinuxRelabel: true,
754-
},
755-
{
756-
ContainerPath: "/dev/shm",
757-
HostPath: filepath.Join(testStateDir, sandboxesDir, testSandboxID, "shm"),
758-
Readonly: false,
759-
SelinuxRelabel: true,
760-
},
761-
},
750+
expectedMounts: nil,
762751
},
763752
} {
764753
test := test

0 commit comments

Comments
 (0)