Skip to content

Commit

Permalink
Stop trying to use mount for image based drivers
Browse files Browse the repository at this point in the history
Fixes #2178 and allows using Docker and other image based drivers even
when nomad is run as a non-root user.

`client/allocdir` tests can be run as a non-root user to ensure this
behavior and tests that rely on root or non-root users properly detect
their effective user and skip instead of fail.
  • Loading branch information
schmichael committed Jan 13, 2017
1 parent b82c698 commit 0785109
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
10 changes: 5 additions & 5 deletions client/allocdir/alloc_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ func TestAllocDir_Snapshot(t *testing.T) {

// Build 2 task dirs
td1 := d.NewTaskDir(t1.Name)
if err := td1.Build(nil, cstructs.FSIsolationNone); err != nil {
if err := td1.Build(nil, cstructs.FSIsolationImage); err != nil {
t.Fatalf("error build task=%q dir: %v", t1.Name, err)
}
td2 := d.NewTaskDir(t2.Name)
if err := td2.Build(nil, cstructs.FSIsolationNone); err != nil {
if err := td2.Build(nil, cstructs.FSIsolationImage); err != nil {
t.Fatalf("error build task=%q dir: %v", t2.Name, err)
}

Expand Down Expand Up @@ -224,12 +224,12 @@ func TestAllocDir_Move(t *testing.T) {
defer d2.Destroy()

td1 := d1.NewTaskDir(t1.Name)
if err := td1.Build(nil, cstructs.FSIsolationNone); err != nil {
if err := td1.Build(nil, cstructs.FSIsolationImage); err != nil {
t.Fatalf("TaskDir.Build() faild: %v", err)
}

td2 := d2.NewTaskDir(t1.Name)
if err := td2.Build(nil, cstructs.FSIsolationNone); err != nil {
if err := td2.Build(nil, cstructs.FSIsolationImage); err != nil {
t.Fatalf("TaskDir.Build() faild: %v", err)
}

Expand Down Expand Up @@ -322,7 +322,7 @@ func TestAllocDir_ReadAt_SecretDir(t *testing.T) {
defer d.Destroy()

td := d.NewTaskDir(t1.Name)
if err := td.Build(nil, cstructs.FSIsolationNone); err != nil {
if err := td.Build(nil, cstructs.FSIsolationImage); err != nil {
t.Fatalf("TaskDir.Build() failed: %v", err)
}

Expand Down
11 changes: 6 additions & 5 deletions client/allocdir/task_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ func (t *TaskDir) Build(chroot map[string]string, fsi cstructs.FSIsolation) erro
}
}

// Always link the shared task directory even though image based
// filesystem isolalation doesn't require it. This way we have a
// consistent task dir.
if err := linkDir(t.SharedAllocDir, t.SharedTaskDir); err != nil {
return fmt.Errorf("Failed to mount shared directory for task: %v", err)
// Only link alloc dir into task dir for no and chroot fs isolation.
// Image based isolation will bind the shared alloc dir in the driver.
if fsi == cstructs.FSIsolationNone || fsi == cstructs.FSIsolationChroot {
if err := linkDir(t.SharedAllocDir, t.SharedTaskDir); err != nil {
return fmt.Errorf("Failed to mount shared directory for task: %v", err)
}
}

// Create the secret directory
Expand Down
25 changes: 25 additions & 0 deletions client/allocdir/task_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"path/filepath"
"testing"

cstructs "github.com/hashicorp/nomad/client/structs"
)

// Test that building a chroot will skip nonexistent directories.
Expand Down Expand Up @@ -82,3 +84,26 @@ func TestTaskDir_EmbedDirs(t *testing.T) {
}
}
}

// Test that task dirs for image based isolation don't require root.
func TestTaskDir_NonRoot(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("test should be run as non-root user")
}
tmp, err := ioutil.TempDir("", "AllocDir")
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}
defer os.RemoveAll(tmp)

d := NewAllocDir(testLogger(), tmp)
defer d.Destroy()
td := d.NewTaskDir(t1.Name)
if err := d.Build(); err != nil {
t.Fatalf("Build() failed: %v", err)
}

if err := td.Build(nil, cstructs.FSIsolationImage); err != nil {
t.Fatalf("TaskDir.Build failed: %v", err)
}
}

0 comments on commit 0785109

Please sign in to comment.