Skip to content

Commit

Permalink
Fix handling of /dev/shm mounting inside of containers
Browse files Browse the repository at this point in the history
Add test to make sure /dev/shm is shared between containers in CRI-O

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
  • Loading branch information
rhatdan committed May 2, 2018
1 parent 4e4f1e2 commit f83d074
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 2 deletions.
5 changes: 4 additions & 1 deletion server/container_create.go
Expand Up @@ -1105,10 +1105,13 @@ func (s *Server) createSandboxContainer(ctx context.Context, containerID string,
specgen.AddAnnotation(annotations.ImageRef, imageRef)
specgen.AddAnnotation(annotations.IP, sb.IP())

// Remove the default /dev/shm mount to ensure we overwrite it
specgen.RemoveMount("/dev/shm")

mnt = rspec.Mount{
Type: "bind",
Source: sb.ShmPath(),
Destination: "/etc/shm",
Destination: "/dev/shm",
Options: []string{"rw", "bind"},
}
// bind mount the pod shm
Expand Down
14 changes: 13 additions & 1 deletion server/sandbox_run.go
Expand Up @@ -292,6 +292,9 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
g.SetProcessSelinuxLabel(processLabel)
g.SetLinuxMountLabel(mountLabel)

// Remove the default /dev/shm mount to ensure we overwrite it
g.RemoveMount("/dev/shm")

// create shm mount for the pod containers.
var shmPath string
if securityContext.GetNamespaceOptions().GetIpc() == pb.NamespaceMode_NODE {
Expand All @@ -310,6 +313,15 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
}()
}

mnt := runtimespec.Mount{
Type: "bind",
Source: shmPath,
Destination: "/dev/shm",
Options: []string{"rw", "bind"},
}
// bind mount the pod shm
g.AddMount(mnt)

err = s.setPodSandboxMountLabel(id, mountLabel)
if err != nil {
return nil, err
Expand Down Expand Up @@ -506,7 +518,7 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
if err := label.Relabel(hostnamePath, mountLabel, true); err != nil && err != unix.ENOTSUP {
return nil, err
}
mnt := runtimespec.Mount{
mnt = runtimespec.Mount{
Type: "bind",
Source: hostnamePath,
Destination: "/etc/hostname",
Expand Down
40 changes: 40 additions & 0 deletions test/shm.bats
@@ -0,0 +1,40 @@
#!/usr/bin/env bats

load helpers

function teardown() {
cleanup_test
}

@test "ctr check shared /dev/shm" {
start_crio
run crictl runp "$TESTDATA"/sandbox_config.json
echo "$output"
[ "$status" -eq 0 ]
pod_id="$output"
run crictl create "$pod_id" "$TESTDIR"/container_sleep.json "$TESTDATA"/sandbox_config.json
echo "$output"
[ "$status" -eq 0 ]
ctr_id="$output"
run crictl start "$ctr_id"
echo "$output"
[ "$status" -eq 0 ]
run crictl exec --sync "$ctr_id" "touch /dev/shm/testdata"
echo "$output"
[ "$status" -eq 0 ]

run crictl create "$pod_id" "$TESTDIR"/container_redis.json "$TESTDATA"/sandbox_config.json
echo "$output"
[ "$status" -eq 0 ]
ctr_id="$output"
run crictl start "$ctr_id"
echo "$output"
[ "$status" -eq 0 ]
run crictl exec --sync "$ctr_id" "ls /dev/shm/testdata"
echo "$output"
[ "$status" -eq 0 ]

cleanup_ctrs
cleanup_pods
stop_crio
}
56 changes: 56 additions & 0 deletions test/testdata/container_sleep.json
@@ -0,0 +1,56 @@
{
"metadata": {
"name": "podsandbox-sleep"
},
"image": {
"image": "quay.io/crio/redis:alpine"
},
"command": [
"/bin/sleep", "6000"
],
"args": [
"6000"
],
"working_dir": "/",
"envs": [
{
"key": "PATH",
"value": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
},
{
"key": "TERM",
"value": "xterm"
},
{
"key": "REDIS_VERSION",
"value": "3.2.3"
},
{
"key": "REDIS_DOWNLOAD_URL",
"value": "http://download.redis.io/releases/redis-3.2.3.tar.gz"
},
{
"key": "REDIS_DOWNLOAD_SHA1",
"value": "92d6d93ef2efc91e595c8bf578bf72baff397507"
}
],
"labels": {
"tier": "backend"
},
"annotations": {
"pod": "podsandbox1"
},
"readonly_rootfs": false,
"log_path": "",
"stdin": false,
"stdin_once": false,
"tty": false,
"linux": {
"resources": {
"cpu_period": 10000,
"cpu_quota": 20000,
"cpu_shares": 512,
"oom_score_adj": 30
}
}
}

0 comments on commit f83d074

Please sign in to comment.