Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit 870119e

Browse files
committed
Add TmpfsRoot option
Signed-off-by: Alexander Morozov <[email protected]>
1 parent 14a7d2f commit 870119e

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

Diff for: factory_linux.go

+16
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"os/exec"
1111
"path/filepath"
1212
"regexp"
13+
"syscall"
1314

15+
"github.com/docker/docker/pkg/mount"
1416
"github.com/docker/libcontainer/cgroups"
1517
"github.com/docker/libcontainer/cgroups/fs"
1618
"github.com/docker/libcontainer/cgroups/systemd"
@@ -78,6 +80,20 @@ func Cgroupfs(l *LinuxFactory) error {
7880
return nil
7981
}
8082

83+
// TmpfsRoot is an option func to mount LinuxFactory.Root to tmpfs.
84+
func TmpfsRoot(l *LinuxFactory) error {
85+
mounted, err := mount.Mounted(l.Root)
86+
if err != nil {
87+
return err
88+
}
89+
if !mounted {
90+
if err := syscall.Mount("tmpfs", l.Root, "tmpfs", 0, ""); err != nil {
91+
return err
92+
}
93+
}
94+
return nil
95+
}
96+
8197
// New returns a linux based container factory based in the root directory and
8298
// configures the factory with the provided option funcs.
8399
func New(root string, options ...func(*LinuxFactory) error) (Factory, error) {

Diff for: factory_linux_test.go

+53-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"path/filepath"
1010
"testing"
1111

12+
"github.com/docker/docker/pkg/mount"
1213
"github.com/docker/libcontainer/configs"
1314
)
1415

@@ -17,9 +18,6 @@ func newTestRoot() (string, error) {
1718
if err != nil {
1819
return "", err
1920
}
20-
if err := os.MkdirAll(dir, 0700); err != nil {
21-
return "", err
22-
}
2321
return dir, nil
2422
}
2523

@@ -49,6 +47,58 @@ func TestFactoryNew(t *testing.T) {
4947
}
5048
}
5149

50+
func TestFactoryNewTmpfs(t *testing.T) {
51+
root, rerr := newTestRoot()
52+
if rerr != nil {
53+
t.Fatal(rerr)
54+
}
55+
defer os.RemoveAll(root)
56+
factory, err := New(root, Cgroupfs, TmpfsRoot)
57+
if err != nil {
58+
t.Fatal(err)
59+
}
60+
if factory == nil {
61+
t.Fatal("factory should not be nil")
62+
}
63+
lfactory, ok := factory.(*LinuxFactory)
64+
if !ok {
65+
t.Fatal("expected linux factory returned on linux based systems")
66+
}
67+
if lfactory.Root != root {
68+
t.Fatalf("expected factory root to be %q but received %q", root, lfactory.Root)
69+
}
70+
71+
if factory.Type() != "libcontainer" {
72+
t.Fatalf("unexpected factory type: %q, expected %q", factory.Type(), "libcontainer")
73+
}
74+
mounted, err := mount.Mounted(lfactory.Root)
75+
if err != nil {
76+
t.Fatal(err)
77+
}
78+
if !mounted {
79+
t.Fatalf("Factory Root is not mounted")
80+
}
81+
mounts, err := mount.GetMounts()
82+
if err != nil {
83+
t.Fatal(err)
84+
}
85+
var found bool
86+
for _, m := range mounts {
87+
if m.Mountpoint == lfactory.Root {
88+
if m.Fstype != "tmpfs" {
89+
t.Fatalf("Fstype of root: %s, expected %s", m.Fstype, "tmpfs")
90+
}
91+
if m.Source != "tmpfs" {
92+
t.Fatalf("Source of root: %s, expected %s", m.Source, "tmpfs")
93+
}
94+
found = true
95+
}
96+
}
97+
if !found {
98+
t.Fatalf("Factory Root is not listed in mounts list")
99+
}
100+
}
101+
52102
func TestFactoryLoadNotExists(t *testing.T) {
53103
root, rerr := newTestRoot()
54104
if rerr != nil {

Diff for: rootfs_linux.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func setupRootfs(config *configs.Config, console *linuxConsole) (err error) {
2424
return newSystemError(err)
2525
}
2626
for _, m := range config.Mounts {
27-
if err := mount(m, config.Rootfs, config.MountLabel); err != nil {
27+
if err := mountToRootfs(m, config.Rootfs, config.MountLabel); err != nil {
2828
return newSystemError(err)
2929
}
3030
}
@@ -62,7 +62,7 @@ func setupRootfs(config *configs.Config, console *linuxConsole) (err error) {
6262
return nil
6363
}
6464

65-
func mount(m *configs.Mount, rootfs, mountLabel string) error {
65+
func mountToRootfs(m *configs.Mount, rootfs, mountLabel string) error {
6666
var (
6767
dest = m.Destination
6868
data = label.FormatMountLabel(m.Data, mountLabel)

0 commit comments

Comments
 (0)