Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.

Commit a74e194

Browse files
committed
shm: handle shm mount backed by empty-dir memory volumes
Check if shm mount is backed by empty-dir memory based volume. If so let the logic to handle epehemeral volumes take care of this mount, so that shm mount within the container is backed by tmpfs mount within the the container in the VM. Fixes #2631 Signed-off-by: Archana Shinde <archana.m.shinde@intel.com> (cherry picked from commit 3c4fe03)
1 parent 673b471 commit a74e194

2 files changed

Lines changed: 57 additions & 30 deletions

File tree

virtcontainers/kata_agent.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,22 +1088,32 @@ func (k *kataAgent) constraintGRPCSpec(grpcSpec *grpc.Spec, passSeccomp bool) {
10881088
grpcSpec.Linux.Devices = linuxDevices
10891089
}
10901090

1091-
func (k *kataAgent) handleShm(grpcSpec *grpc.Spec, sandbox *Sandbox) {
1092-
for idx, mnt := range grpcSpec.Mounts {
1091+
func (k *kataAgent) handleShm(mounts []specs.Mount, sandbox *Sandbox) {
1092+
for idx, mnt := range mounts {
10931093
if mnt.Destination != "/dev/shm" {
10941094
continue
10951095
}
10961096

1097+
// If /dev/shm for a container is backed by an ephemeral volume, skip
1098+
// bind-mounting it to the sandbox shm.
1099+
// A later call to handleEphemeralStorage should take care of setting up /dev/shm correctly.
1100+
if mnt.Type == KataEphemeralDevType {
1101+
continue
1102+
}
1103+
1104+
// A container shm mount is shared with sandbox shm mount.
10971105
if sandbox.shmSize > 0 {
1098-
grpcSpec.Mounts[idx].Type = "bind"
1099-
grpcSpec.Mounts[idx].Options = []string{"rbind"}
1100-
grpcSpec.Mounts[idx].Source = filepath.Join(kataGuestSandboxDir(), shmDir)
1106+
mounts[idx].Type = "bind"
1107+
mounts[idx].Options = []string{"rbind"}
1108+
mounts[idx].Source = filepath.Join(kataGuestSandboxDir(), shmDir)
11011109
k.Logger().WithField("shm-size", sandbox.shmSize).Info("Using sandbox shm")
11021110
} else {
1111+
// This should typically not happen, as a sandbox shm mount is always set up by the
1112+
// upper stack.
11031113
sizeOption := fmt.Sprintf("size=%d", DefaultShmSize)
1104-
grpcSpec.Mounts[idx].Type = "tmpfs"
1105-
grpcSpec.Mounts[idx].Source = "shm"
1106-
grpcSpec.Mounts[idx].Options = []string{"noexec", "nosuid", "nodev", "mode=1777", sizeOption}
1114+
mounts[idx].Type = "tmpfs"
1115+
mounts[idx].Source = "shm"
1116+
mounts[idx].Options = []string{"noexec", "nosuid", "nodev", "mode=1777", sizeOption}
11071117
k.Logger().WithField("shm-size", sizeOption).Info("Setting up a separate shm for container")
11081118
}
11091119
}
@@ -1332,6 +1342,8 @@ func (k *kataAgent) createContainer(sandbox *Sandbox, c *Container) (p *Process,
13321342
return nil, err
13331343
}
13341344

1345+
k.handleShm(ociSpec.Mounts, sandbox)
1346+
13351347
epheStorages := k.handleEphemeralStorage(ociSpec.Mounts)
13361348
ctrStorages = append(ctrStorages, epheStorages...)
13371349

@@ -1382,8 +1394,6 @@ func (k *kataAgent) createContainer(sandbox *Sandbox, c *Container) (p *Process,
13821394
// irrelevant information to the agent.
13831395
k.constraintGRPCSpec(grpcSpec, passSeccomp)
13841396

1385-
k.handleShm(grpcSpec, sandbox)
1386-
13871397
req := &grpc.CreateContainerRequest{
13881398
ContainerId: c.id,
13891399
ExecId: c.id,

virtcontainers/kata_agent_test.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -664,33 +664,50 @@ func TestHandleShm(t *testing.T) {
664664
shmSize: 8192,
665665
}
666666

667-
g := &pb.Spec{
668-
Hooks: &pb.Hooks{},
669-
Mounts: []pb.Mount{
670-
{Destination: "/dev/shm"},
671-
},
667+
var ociMounts []specs.Mount
668+
669+
mount := specs.Mount{
670+
Type: "bind",
671+
Destination: "/dev/shm",
672672
}
673673

674-
k.handleShm(g, sandbox)
674+
ociMounts = append(ociMounts, mount)
675+
k.handleShm(ociMounts, sandbox)
675676

676-
assert.Len(g.Mounts, 1)
677-
assert.NotEmpty(g.Mounts[0].Destination)
678-
assert.Equal(g.Mounts[0].Destination, "/dev/shm")
679-
assert.Equal(g.Mounts[0].Type, "bind")
680-
assert.NotEmpty(g.Mounts[0].Source, filepath.Join(kataGuestSharedDir(), shmDir))
681-
assert.Equal(g.Mounts[0].Options, []string{"rbind"})
677+
assert.Len(ociMounts, 1)
678+
assert.NotEmpty(ociMounts[0].Destination)
679+
assert.Equal(ociMounts[0].Destination, "/dev/shm")
680+
assert.Equal(ociMounts[0].Type, "bind")
681+
assert.NotEmpty(ociMounts[0].Source, filepath.Join(kataGuestSharedDir(), shmDir))
682+
assert.Equal(ociMounts[0].Options, []string{"rbind"})
682683

683684
sandbox.shmSize = 0
684-
k.handleShm(g, sandbox)
685-
686-
assert.Len(g.Mounts, 1)
687-
assert.NotEmpty(g.Mounts[0].Destination)
688-
assert.Equal(g.Mounts[0].Destination, "/dev/shm")
689-
assert.Equal(g.Mounts[0].Type, "tmpfs")
690-
assert.Equal(g.Mounts[0].Source, "shm")
685+
k.handleShm(ociMounts, sandbox)
691686

687+
assert.Len(ociMounts, 1)
688+
assert.Equal(ociMounts[0].Destination, "/dev/shm")
689+
assert.Equal(ociMounts[0].Type, "tmpfs")
690+
assert.Equal(ociMounts[0].Source, "shm")
692691
sizeOption := fmt.Sprintf("size=%d", DefaultShmSize)
693-
assert.Equal(g.Mounts[0].Options, []string{"noexec", "nosuid", "nodev", "mode=1777", sizeOption})
692+
assert.Equal(ociMounts[0].Options, []string{"noexec", "nosuid", "nodev", "mode=1777", sizeOption})
693+
694+
// In case the type of mount is ephemeral, the container mount is not
695+
// shared with the sandbox shm.
696+
ociMounts[0].Type = KataEphemeralDevType
697+
mountSource := "/tmp/mountPoint"
698+
ociMounts[0].Source = mountSource
699+
k.handleShm(ociMounts, sandbox)
700+
701+
assert.Len(ociMounts, 1)
702+
assert.Equal(ociMounts[0].Type, KataEphemeralDevType)
703+
assert.NotEmpty(ociMounts[0].Source, mountSource)
704+
705+
epheStorages := k.handleEphemeralStorage(ociMounts)
706+
epheMountPoint := epheStorages[0].GetMountPoint()
707+
expected := filepath.Join(ephemeralPath(), filepath.Base(mountSource))
708+
assert.Equal(epheMountPoint, expected,
709+
"Ephemeral mount point didn't match: got %s, expecting %s", epheMountPoint, expected)
710+
694711
}
695712

696713
func testIsPidNamespacePresent(grpcSpec *pb.Spec) bool {

0 commit comments

Comments
 (0)