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

Commit

Permalink
virtcontainers: Handle regular files in /dev
Browse files Browse the repository at this point in the history
The k8s test creates a log file in /dev under
/dev/termination-log, which is not the right place to create
logs, but we need to handle this. With this commit, we handle
regular files under /dev by passing them as 9p shares. All other
special files including device files and directories
are not passed as 9p shares as these are specific to the host.
Any operations on these in the guest would fail anyways.

Signed-off-by: Archana Shinde <[email protected]>
  • Loading branch information
amshinde committed Apr 19, 2018
1 parent 70c3fe9 commit 71c7a9c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
7 changes: 7 additions & 0 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,13 @@ func (c *Container) mountSharedDirMounts(hostSharedDir, guestSharedDir string) (
continue
}

// Ignore /dev, directories and all other device files. We handle
// only regular files in /dev. It does not make sense to pass the host
// device nodes to the guest.
if isHostDevice(m.Destination) {
continue
}

randBytes, err := generateRandomBytes(8)
if err != nil {
return nil, err
Expand Down
28 changes: 28 additions & 0 deletions virtcontainers/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,34 @@ func isSystemMount(m string) bool {
return false
}

func isHostDevice(m string) bool {
if m == "/dev" {
return true
}

if strings.HasPrefix(m, "/dev/") {
// Check if regular file
s, err := os.Stat(m)

// This should not happen. In case file does not exist let the
// error be handled by the agent, simply return false here.
if err != nil {
return false
}

if s.Mode().IsRegular() {
return false
}

// This is not a regular file in /dev. It is either a
// device file, directory or any other special file which is
// specific to the host system.
return true
}

return false
}

func major(dev uint64) int {
return int((dev >> 8) & 0xfff)
}
Expand Down
35 changes: 35 additions & 0 deletions virtcontainers/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,41 @@ func TestIsSystemMount(t *testing.T) {
}
}

func TestIsHostDevice(t *testing.T) {
tests := []struct {
mnt string
expected bool
}{
{"/dev", true},
{"/dev/zero", true},
{"/dev/block", true},
{"/mnt/dev/block", false},
}

for _, test := range tests {
result := isHostDevice(test.mnt)
if result != test.expected {
t.Fatalf("Expected result for path %s : %v, got %v", test.mnt, test.expected, result)
}
}

// Create regular file in /dev
path := "/dev/foobar"
f, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
f.Close()

if isHostDevice(path) != false {
t.Fatalf("Expected result for path %s : %v, got %v", path, false, true)
}

if err := os.Remove(path); err != nil {
t.Fatal(err)
}
}

func TestMajorMinorNumber(t *testing.T) {
devices := []string{"/dev/zero", "/dev/net/tun"}

Expand Down

0 comments on commit 71c7a9c

Please sign in to comment.