diff --git a/server/container_create_linux.go b/server/container_create_linux.go index 8e4c6eb238e..c3c1fc67c3a 100644 --- a/server/container_create_linux.go +++ b/server/container_create_linux.go @@ -979,7 +979,7 @@ func addOCIBindMounts(ctx context.Context, ctr ctrfactory.Container, mountLabel, log.Warnf(ctx, "Configuration specifies mounting host root to the container root. This is dangerous (especially with privileged containers) and should be avoided.") } - if isSubDirectoryOf(storageRoot, m.HostPath) { + if isSubDirectoryOf(storageRoot, m.HostPath) && m.Propagation == types.MountPropagation_PROPAGATION_PRIVATE { log.Infof(ctx, "Mount propogration for the host path %s will be set to HostToContainer as it includes the container storage root", m.HostPath) m.Propagation = types.MountPropagation_PROPAGATION_HOST_TO_CONTAINER } diff --git a/test/ctr.bats b/test/ctr.bats index 9e7af693207..96deedb4cba 100644 --- a/test/ctr.bats +++ b/test/ctr.bats @@ -1004,6 +1004,42 @@ function check_oci_annotation() { ! crictl create "$pod_id" "$TESTDIR/config" "$TESTDATA"/sandbox_config.json } +@test "ctr that mounts container storage as shared should keep shared" { + # parent of `--root`, keep in sync with test/helpers.bash + PARENT_DIR="$TESTDIR" + CTR_DIR="/host" + jq --arg path "$PARENT_DIR" --arg ctr_dir "$CTR_DIR" \ + ' .mounts = [ { + host_path: $path, + container_path: $ctr_dir, + propagation: 2 + } ]' \ + "$TESTDATA"/container_redis.json > "$TESTDIR/config" + + start_crio + + ctr_id=$(crictl run "$TESTDIR/config" "$TESTDATA"/sandbox_config.json) + crictl exec --sync "$ctr_id" findmnt -no TARGET,PROPAGATION "$CTR_DIR" | grep shared +} + +@test "ctr that mounts container storage as private should not be private" { + # parent of `--root`, keep in sync with test/helpers.bash + PARENT_DIR="$TESTDIR" + CTR_DIR="/host" + jq --arg path "$PARENT_DIR" --arg ctr_dir "$CTR_DIR" \ + ' .mounts = [ { + host_path: $path, + container_path: $ctr_dir, + propagation: 1 + } ]' \ + "$TESTDATA"/container_redis.json > "$TESTDIR/config" + + start_crio + + ctr_id=$(crictl run "$TESTDIR/config" "$TESTDATA"/sandbox_config.json) + crictl exec --sync "$ctr_id" findmnt -no TARGET,PROPAGATION "$CTR_DIR" | grep -v private +} + @test "ctr has containerenv" { start_crio pod_id=$(crictl runp "$TESTDATA"/sandbox_config.json)