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

mounts: Add check for system volumes #1418

Merged
merged 2 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/katautils/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func HandleFactory(ctx context.Context, vci vc.VC, runtimeConfig *oci.RuntimeCon
// of the same pod the already existing volume is reused.
func SetEphemeralStorageType(ociSpec oci.CompatOCISpec) oci.CompatOCISpec {
for idx, mnt := range ociSpec.Mounts {
if IsEphemeralStorage(mnt.Source) {
if vc.IsEphemeralStorage(mnt.Source) {
ociSpec.Mounts[idx].Type = "ephemeral"
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/katautils/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestSetEphemeralStorageType(t *testing.T) {
}
defer os.RemoveAll(dir)

ephePath := filepath.Join(dir, k8sEmptyDir, "tmp-volume")
ephePath := filepath.Join(dir, vc.K8sEmptyDir, "tmp-volume")
err = os.MkdirAll(ephePath, testDirMode)
assert.Nil(err)

Expand Down
28 changes: 0 additions & 28 deletions pkg/katautils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import (
"path/filepath"
"strings"
"syscall"

vc "github.com/kata-containers/runtime/virtcontainers"
)

const (
k8sEmptyDir = "kubernetes.io~empty-dir"
)

// FileExists test is a file exiting or not
Expand All @@ -31,28 +25,6 @@ func FileExists(path string) bool {
return true
}

// IsEphemeralStorage returns true if the given path
// to the storage belongs to kubernetes ephemeral storage
//
// This method depends on a specific path used by k8s
// to detect if it's of type ephemeral. As of now,
// this is a very k8s specific solution that works
// but in future there should be a better way for this
// method to determine if the path is for ephemeral
// volume type
func IsEphemeralStorage(path string) bool {
splitSourceSlice := strings.Split(path, "/")
if len(splitSourceSlice) > 1 {
storageType := splitSourceSlice[len(splitSourceSlice)-2]
if storageType == k8sEmptyDir {
if _, fsType, _ := vc.GetDevicePathAndFsType(path); fsType == "tmpfs" {
return true
}
}
}
return false
}

// ResolvePath returns the fully resolved and expanded value of the
// specified path.
func ResolvePath(path string) (string, error) {
Expand Down
31 changes: 0 additions & 31 deletions pkg/katautils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,34 +366,3 @@ func TestGetFileContents(t *testing.T) {
assert.Equal(t, contents, d.contents)
}
}

func TestIsEphemeralStorage(t *testing.T) {
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")
}

sampleEphePath = "/var/lib/kubelet/pods/366c3a75-4869-11e8-b479-507b9ddd5ce4/volumes/cache-volume"
isEphe = IsEphemeralStorage(sampleEphePath)
if isEphe {
t.Fatalf("Unable to correctly determine volume type")
}
}
8 changes: 7 additions & 1 deletion virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,13 @@ func (c *Container) mountSharedDirMounts(hostSharedDir, guestSharedDir string) (
var sharedDirMounts []Mount
var ignoredMounts []Mount
for idx, m := range c.mounts {
if isSystemMount(m.Destination) || m.Type != "bind" {
if isSystemMount(m.Destination) {
if !(IsDockerVolume(m.Source) || Isk8sHostEmptyDir(m.Source)) {
continue
}
}

if m.Type != "bind" {
continue
}

Expand Down
66 changes: 66 additions & 0 deletions virtcontainers/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,69 @@ func bindUnmountAllRootfs(ctx context.Context, sharedDir string, sandbox *Sandbo
}
}
}

const (
dockerVolumePrefix = "/var/lib/docker/volumes"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to check where is defined this is docker code just to add as a comment reference in case it changes in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var/lib/docker is configurable at docker startup I may check only from volumes

dockerVolumeSuffix = "_data"
)

// IsDockerVolume returns true if the given source path is
// a docker volume.
// This uses a very specific path that is used by docker.
func IsDockerVolume(path string) bool {
if strings.HasPrefix(path, dockerVolumePrefix) && filepath.Base(path) == dockerVolumeSuffix {
return true
}
return false
}

const (
// K8sEmptyDir is the k8s specific path for `empty-dir` volumes
K8sEmptyDir = "kubernetes.io~empty-dir"
)

// IsEphemeralStorage returns true if the given path
// to the storage belongs to kubernetes ephemeral storage
//
// This method depends on a specific path used by k8s
// to detect if it's of type ephemeral. As of now,
// this is a very k8s specific solution that works
// but in future there should be a better way for this
// method to determine if the path is for ephemeral
// volume type
func IsEphemeralStorage(path string) bool {
if !isEmptyDir(path) {
return false
}

if _, fsType, _ := GetDevicePathAndFsType(path); fsType == "tmpfs" {
return true
}

return false
}

// Isk8sHostEmptyDir returns true if the given path
// to the storage belongs to kubernetes empty-dir of medium "default"
// i.e volumes that are directories on the host.
func Isk8sHostEmptyDir(path string) bool {
if !isEmptyDir(path) {
return false
}

if _, fsType, _ := GetDevicePathAndFsType(path); fsType != "tmpfs" {
return true
}
return false
}

func isEmptyDir(path string) bool {
splitSourceSlice := strings.Split(path, "/")
if len(splitSourceSlice) > 1 {
storageType := splitSourceSlice[len(splitSourceSlice)-2]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic number

if storageType == K8sEmptyDir {
return true
}
}
return false
}
50 changes: 50 additions & 0 deletions virtcontainers/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"bytes"
"context"
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -18,6 +20,11 @@ import (
"testing"
)

const (
testDisabledNeedRoot = "Test disabled as requires root user"
testDirMode = os.FileMode(0750)
)

func TestIsSystemMount(t *testing.T) {
tests := []struct {
mnt string
Expand Down Expand Up @@ -282,3 +289,46 @@ func TestIsDeviceMapper(t *testing.T) {
t.Fatal()
}
}

func TestIsDockerVolume(t *testing.T) {
path := "/var/lib/docker/volumes/00da1347c7cf4f15db35f/_data"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dockerVolumePrefix

isDockerVolume := IsDockerVolume(path)
assert.True(t, isDockerVolume)

path = "/var/lib/testdir"
isDockerVolume = IsDockerVolume(path)
assert.False(t, isDockerVolume)
}

func TestIsEphemeralStorage(t *testing.T) {
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)
assert.True(t, isEphe)

isHostEmptyDir := Isk8sHostEmptyDir(sampleEphePath)
assert.False(t, isHostEmptyDir)

sampleEphePath = "/var/lib/kubelet/pods/366c3a75-4869-11e8-b479-507b9ddd5ce4/volumes/cache-volume"
isEphe = IsEphemeralStorage(sampleEphePath)
assert.False(t, isEphe)

isHostEmptyDir = Isk8sHostEmptyDir(sampleEphePath)
assert.False(t, isHostEmptyDir)
}