This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 376
mounts: Add check for system volumes #1418
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -326,3 +326,69 @@ func bindUnmountAllRootfs(ctx context.Context, sharedDir string, sandbox *Sandbo | |
} | ||
} | ||
} | ||
|
||
const ( | ||
dockerVolumePrefix = "/var/lib/docker/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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. magic number |
||
if storageType == K8sEmptyDir { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ import ( | |
"bytes" | ||
"context" | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
@@ -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 | ||
|
@@ -282,3 +289,46 @@ func TestIsDeviceMapper(t *testing.T) { | |
t.Fatal() | ||
} | ||
} | ||
|
||
func TestIsDockerVolume(t *testing.T) { | ||
path := "/var/lib/docker/volumes/00da1347c7cf4f15db35f/_data" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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