diff --git a/pkg/katautils/create_test.go b/pkg/katautils/create_test.go index 77701838e9..3caa54d3e2 100644 --- a/pkg/katautils/create_test.go +++ b/pkg/katautils/create_test.go @@ -16,6 +16,7 @@ import ( "path" "path/filepath" "strings" + "syscall" "testing" vc "github.com/kata-containers/runtime/virtcontainers" @@ -177,12 +178,30 @@ func findLastParam(key string, params []vc.Param) (string, error) { } func TestSetEphemeralStorageType(t *testing.T) { + if os.Geteuid() != 0 { + t.Skip(testDisabledNeedRoot) + } + assert := assert.New(t) + dir, err := ioutil.TempDir(testDir, "foo") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + + ephePath := filepath.Join(dir, k8sEmptyDir, "tmp-volume") + err = os.MkdirAll(ephePath, testDirMode) + assert.Nil(err) + + err = syscall.Mount("tmpfs", ephePath, "tmpfs", 0, "") + assert.Nil(err) + defer syscall.Unmount(ephePath, 0) + ociSpec := oci.CompatOCISpec{} var ociMounts []specs.Mount mount := specs.Mount{ - Source: "/var/lib/kubelet/pods/366c3a77-4869-11e8-b479-507b9ddd5ce4/volumes/kubernetes.io~empty-dir/cache-volume", + Source: ephePath, } ociMounts = append(ociMounts, mount) diff --git a/pkg/katautils/utils.go b/pkg/katautils/utils.go index a8c1ca96f4..ae3ccecd36 100644 --- a/pkg/katautils/utils.go +++ b/pkg/katautils/utils.go @@ -14,6 +14,8 @@ import ( "path/filepath" "strings" "syscall" + + vc "github.com/kata-containers/runtime/virtcontainers" ) const ( @@ -43,7 +45,9 @@ func IsEphemeralStorage(path string) bool { if len(splitSourceSlice) > 1 { storageType := splitSourceSlice[len(splitSourceSlice)-2] if storageType == k8sEmptyDir { - return true + if _, fsType, _ := vc.GetDevicePathAndFsType(path); fsType == "tmpfs" { + return true + } } } return false diff --git a/pkg/katautils/utils_test.go b/pkg/katautils/utils_test.go index d061e6dc0d..4a1d7a5f11 100644 --- a/pkg/katautils/utils_test.go +++ b/pkg/katautils/utils_test.go @@ -368,7 +368,24 @@ func TestGetFileContents(t *testing.T) { } func TestIsEphemeralStorage(t *testing.T) { - sampleEphePath := "/var/lib/kubelet/pods/366c3a75-4869-11e8-b479-507b9ddd5ce4/volumes/kubernetes.io~empty-dir/cache-volume" + if os.Geteuid() != 0 { + t.Skip(testDisabledNeedRoot) + } + + dir, err := ioutil.TempDir(testDir, "foo") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + + sampleEphePath := filepath.Join(dir, k8sEmptyDir, "tmp-volume") + err = os.MkdirAll(sampleEphePath, testDirMode) + assert.Nil(t, err) + + err = syscall.Mount("tmpfs", sampleEphePath, "tmpfs", 0, "") + assert.Nil(t, err) + defer syscall.Unmount(sampleEphePath, 0) + isEphe := IsEphemeralStorage(sampleEphePath) if !isEphe { t.Fatalf("Unable to correctly determine volume type") diff --git a/virtcontainers/container.go b/virtcontainers/container.go index f11fe997b8..991ebe5d1a 100644 --- a/virtcontainers/container.go +++ b/virtcontainers/container.go @@ -1138,7 +1138,7 @@ func (c *Container) hotplugDrive() error { } // If device mapper device, then fetch the full path of the device - devicePath, fsType, err := getDevicePathAndFsType(dev.mountPoint) + devicePath, fsType, err := GetDevicePathAndFsType(dev.mountPoint) if err != nil { return err } diff --git a/virtcontainers/mount.go b/virtcontainers/mount.go index bd279dfde7..5ffa7c5713 100644 --- a/virtcontainers/mount.go +++ b/virtcontainers/mount.go @@ -164,7 +164,9 @@ const ( procTypeIndex ) -func getDevicePathAndFsType(mountPoint string) (devicePath, fsType string, err error) { +// GetDevicePathAndFsType gets the device for the mount point and the file system type +// of the mount. +func GetDevicePathAndFsType(mountPoint string) (devicePath, fsType string, err error) { if mountPoint == "" { err = fmt.Errorf("Mount point cannot be empty") return diff --git a/virtcontainers/mount_test.go b/virtcontainers/mount_test.go index 3b67c96887..820fd7447c 100644 --- a/virtcontainers/mount_test.go +++ b/virtcontainers/mount_test.go @@ -238,7 +238,7 @@ func TestGetDeviceForPathBindMount(t *testing.T) { } func TestGetDevicePathAndFsTypeEmptyMount(t *testing.T) { - _, _, err := getDevicePathAndFsType("") + _, _, err := GetDevicePathAndFsType("") if err == nil { t.Fatal() @@ -246,7 +246,7 @@ func TestGetDevicePathAndFsTypeEmptyMount(t *testing.T) { } func TestGetDevicePathAndFsTypeSuccessful(t *testing.T) { - path, fstype, err := getDevicePathAndFsType("/proc") + path, fstype, err := GetDevicePathAndFsType("/proc") if err != nil { t.Fatal(err)