diff --git a/factory_linux.go b/factory_linux.go index ecd3dd5c..a2d3bec7 100644 --- a/factory_linux.go +++ b/factory_linux.go @@ -10,7 +10,9 @@ import ( "os/exec" "path/filepath" "regexp" + "syscall" + "github.com/docker/docker/pkg/mount" "github.com/docker/libcontainer/cgroups" "github.com/docker/libcontainer/cgroups/fs" "github.com/docker/libcontainer/cgroups/systemd" @@ -78,6 +80,20 @@ func Cgroupfs(l *LinuxFactory) error { return nil } +// TmpfsRoot is an option func to mount LinuxFactory.Root to tmpfs. +func TmpfsRoot(l *LinuxFactory) error { + mounted, err := mount.Mounted(l.Root) + if err != nil { + return err + } + if !mounted { + if err := syscall.Mount("tmpfs", l.Root, "tmpfs", 0, ""); err != nil { + return err + } + } + return nil +} + // New returns a linux based container factory based in the root directory and // configures the factory with the provided option funcs. func New(root string, options ...func(*LinuxFactory) error) (Factory, error) { diff --git a/factory_linux_test.go b/factory_linux_test.go index 968f6a96..00e39739 100644 --- a/factory_linux_test.go +++ b/factory_linux_test.go @@ -9,6 +9,7 @@ import ( "path/filepath" "testing" + "github.com/docker/docker/pkg/mount" "github.com/docker/libcontainer/configs" ) @@ -17,9 +18,6 @@ func newTestRoot() (string, error) { if err != nil { return "", err } - if err := os.MkdirAll(dir, 0700); err != nil { - return "", err - } return dir, nil } @@ -49,6 +47,58 @@ func TestFactoryNew(t *testing.T) { } } +func TestFactoryNewTmpfs(t *testing.T) { + root, rerr := newTestRoot() + if rerr != nil { + t.Fatal(rerr) + } + defer os.RemoveAll(root) + factory, err := New(root, Cgroupfs, TmpfsRoot) + if err != nil { + t.Fatal(err) + } + if factory == nil { + t.Fatal("factory should not be nil") + } + lfactory, ok := factory.(*LinuxFactory) + if !ok { + t.Fatal("expected linux factory returned on linux based systems") + } + if lfactory.Root != root { + t.Fatalf("expected factory root to be %q but received %q", root, lfactory.Root) + } + + if factory.Type() != "libcontainer" { + t.Fatalf("unexpected factory type: %q, expected %q", factory.Type(), "libcontainer") + } + mounted, err := mount.Mounted(lfactory.Root) + if err != nil { + t.Fatal(err) + } + if !mounted { + t.Fatalf("Factory Root is not mounted") + } + mounts, err := mount.GetMounts() + if err != nil { + t.Fatal(err) + } + var found bool + for _, m := range mounts { + if m.Mountpoint == lfactory.Root { + if m.Fstype != "tmpfs" { + t.Fatalf("Fstype of root: %s, expected %s", m.Fstype, "tmpfs") + } + if m.Source != "tmpfs" { + t.Fatalf("Source of root: %s, expected %s", m.Source, "tmpfs") + } + found = true + } + } + if !found { + t.Fatalf("Factory Root is not listed in mounts list") + } +} + func TestFactoryLoadNotExists(t *testing.T) { root, rerr := newTestRoot() if rerr != nil { diff --git a/rootfs_linux.go b/rootfs_linux.go index cea629ab..6caa07a0 100644 --- a/rootfs_linux.go +++ b/rootfs_linux.go @@ -24,7 +24,7 @@ func setupRootfs(config *configs.Config, console *linuxConsole) (err error) { return newSystemError(err) } for _, m := range config.Mounts { - if err := mount(m, config.Rootfs, config.MountLabel); err != nil { + if err := mountToRootfs(m, config.Rootfs, config.MountLabel); err != nil { return newSystemError(err) } } @@ -62,7 +62,7 @@ func setupRootfs(config *configs.Config, console *linuxConsole) (err error) { return nil } -func mount(m *configs.Mount, rootfs, mountLabel string) error { +func mountToRootfs(m *configs.Mount, rootfs, mountLabel string) error { var ( dest = m.Destination data = label.FormatMountLabel(m.Data, mountLabel)