Skip to content

Commit 4123170

Browse files
committed
*: export RemoveVolatileOption for CRI image volumes
Remove volatile option when CRI prepares image volumes. Signed-off-by: Wei Fu <[email protected]>
1 parent eb8b3de commit 4123170

File tree

5 files changed

+93
-7
lines changed

5 files changed

+93
-7
lines changed

core/mount/mount_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func TestRemoveVolatileTempMount(t *testing.T) {
250250

251251
for _, tc := range testCases {
252252
original := copyMounts(tc.input)
253-
actual := removeVolatileTempMount(tc.input)
253+
actual := RemoveVolatileOption(tc.input)
254254
if !reflect.DeepEqual(actual, tc.expected) {
255255
t.Fatalf("incorrectly modified mounts: %s.\n\n Expected: %v\n\n, Actual: %v", tc.desc, tc.expected, actual)
256256
}

core/mount/temp.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ var tempMountLocation = getTempDir()
2828

2929
// WithTempMount mounts the provided mounts to a temp dir, and pass the temp dir to f.
3030
// The mounts are valid during the call to the f.
31-
// The volatile option of overlayfs doesn't allow to mount again using the same upper / work dirs. Since it's a temp mount, avoid using that option here if found.
3231
// Finally we will unmount and remove the temp dir regardless of the result of f.
32+
//
33+
// NOTE: The volatile option of overlayfs doesn't allow to mount again using the
34+
// same upper / work dirs. Since it's a temp mount, avoid using that option here
35+
// if found.
3336
func WithTempMount(ctx context.Context, mounts []Mount, f func(root string) error) (err error) {
3437
root, uerr := os.MkdirTemp(tempMountLocation, "containerd-mount")
3538
if uerr != nil {
@@ -60,7 +63,7 @@ func WithTempMount(ctx context.Context, mounts []Mount, f func(root string) erro
6063
}
6164
}()
6265

63-
if uerr = All(removeVolatileTempMount(mounts), root); uerr != nil {
66+
if uerr = All(RemoveVolatileOption(mounts), root); uerr != nil {
6467
return fmt.Errorf("failed to mount %s: %w", root, uerr)
6568
}
6669
if err := f(root); err != nil {
@@ -69,12 +72,15 @@ func WithTempMount(ctx context.Context, mounts []Mount, f func(root string) erro
6972
return nil
7073
}
7174

72-
// removeVolatileTempMount The volatile option of overlayfs doesn't allow to mount again using the
73-
// same upper / work dirs. Since it's a temp mount, avoid using that
74-
// option here. Reference: https://docs.kernel.org/filesystems/overlayfs.html#volatile-mount
75+
// RemoveVolatileOption copies and remove the volatile option for overlay
76+
// type, since overlayfs doesn't allow to mount again using the same upper/work
77+
// dirs.
78+
//
79+
// REF: https://docs.kernel.org/filesystems/overlayfs.html#volatile-mount
80+
//
7581
// TODO: Make this logic conditional once the kernel supports reusing
7682
// overlayfs volatile mounts.
77-
func removeVolatileTempMount(mounts []Mount) []Mount {
83+
func RemoveVolatileOption(mounts []Mount) []Mount {
7884
var out []Mount
7985
for i, m := range mounts {
8086
if m.Type != "overlay" {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package integration
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
"syscall"
23+
"testing"
24+
"time"
25+
26+
"github.com/containerd/containerd/v2/integration/images"
27+
"github.com/containerd/containerd/v2/pkg/kernelversion"
28+
"github.com/stretchr/testify/require"
29+
criruntime "k8s.io/cri-api/pkg/apis/runtime/v1"
30+
)
31+
32+
func TestRunContainerWithVolatileOption(t *testing.T) {
33+
if ok, err := kernelversion.GreaterEqualThan(kernelversion.KernelVersion{
34+
Kernel: 5,
35+
Major: 10,
36+
}); !ok {
37+
t.Skipf("Only test it when kernel >= 5.10: %v", err)
38+
}
39+
40+
workDir := t.TempDir()
41+
42+
t.Log("Prepare containerd config with volatile option")
43+
cfgPath := filepath.Join(workDir, "config.toml")
44+
err := os.WriteFile(
45+
cfgPath,
46+
[]byte(`
47+
version = 3
48+
49+
[plugins.'io.containerd.internal.v1.cri']
50+
ignore_image_defined_volumes = false
51+
52+
[plugins."io.containerd.snapshotter.v1.overlayfs"]
53+
mount_options = ["volatile"]
54+
`),
55+
0600)
56+
require.NoError(t, err)
57+
58+
t.Logf("Starting containerd")
59+
currentProc := newCtrdProc(t, "containerd", workDir)
60+
require.NoError(t, currentProc.isReady())
61+
t.Cleanup(func() {
62+
t.Log("Cleanup all the pods")
63+
cleanupPods(t, currentProc.criRuntimeService(t))
64+
65+
t.Log("Stopping containerd process")
66+
require.NoError(t, currentProc.kill(syscall.SIGTERM))
67+
require.NoError(t, currentProc.wait(5*time.Minute))
68+
})
69+
70+
imageName := images.Get(images.VolumeOwnership)
71+
pullImagesByCRI(t, currentProc.criImageService(t), imageName)
72+
73+
podCtx := newPodTCtx(t, currentProc.criRuntimeService(t), "running-pod", "sandbox")
74+
75+
podCtx.createContainer("running", imageName,
76+
criruntime.ContainerState_CONTAINER_RUNNING,
77+
WithCommand("sleep", "1d"))
78+
}

integration/images/volume-ownership/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
FROM ubuntu
1616
RUN mkdir -p /test_dir && \
1717
chown -R nobody:nogroup /test_dir
18+
# NOTE: The volume is required by ../../container_volume_linux_test.go#TestRunContainerWithVolatileOption.
1819
VOLUME /test_dir

internal/cri/opts/container.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func WithVolumes(volumeMounts map[string]string, platform imagespec.Platform) co
7474
if len(mounts) == 1 && mounts[0].Type == "overlay" {
7575
mounts[0].Options = append(mounts[0].Options, "ro")
7676
}
77+
mounts = mount.RemoveVolatileOption(mounts)
7778

7879
root, err := os.MkdirTemp("", "ctd-volume")
7980
if err != nil {

0 commit comments

Comments
 (0)