| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,144 @@ |
| 0 |
+// +build linux |
|
| 1 |
+ |
|
| 2 |
+package mount |
|
| 3 |
+ |
|
| 4 |
+import ( |
|
| 5 |
+ "fmt" |
|
| 6 |
+ "github.com/dotcloud/docker/pkg/label" |
|
| 7 |
+ "github.com/dotcloud/docker/pkg/libcontainer" |
|
| 8 |
+ "github.com/dotcloud/docker/pkg/libcontainer/mount/nodes" |
|
| 9 |
+ "github.com/dotcloud/docker/pkg/libcontainer/security/restrict" |
|
| 10 |
+ "github.com/dotcloud/docker/pkg/system" |
|
| 11 |
+ "os" |
|
| 12 |
+ "path/filepath" |
|
| 13 |
+ "syscall" |
|
| 14 |
+) |
|
| 15 |
+ |
|
| 16 |
+// default mount point flags |
|
| 17 |
+const defaultMountFlags = syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NODEV |
|
| 18 |
+ |
|
| 19 |
+type mount struct {
|
|
| 20 |
+ source string |
|
| 21 |
+ path string |
|
| 22 |
+ device string |
|
| 23 |
+ flags int |
|
| 24 |
+ data string |
|
| 25 |
+} |
|
| 26 |
+ |
|
| 27 |
+// InitializeMountNamespace setups up the devices, mount points, and filesystems for use inside a |
|
| 28 |
+// new mount namepsace |
|
| 29 |
+func InitializeMountNamespace(rootfs, console string, container *libcontainer.Container) error {
|
|
| 30 |
+ var ( |
|
| 31 |
+ err error |
|
| 32 |
+ flag = syscall.MS_PRIVATE |
|
| 33 |
+ ) |
|
| 34 |
+ if container.NoPivotRoot {
|
|
| 35 |
+ flag = syscall.MS_SLAVE |
|
| 36 |
+ } |
|
| 37 |
+ if err := system.Mount("", "/", "", uintptr(flag|syscall.MS_REC), ""); err != nil {
|
|
| 38 |
+ return fmt.Errorf("mounting / as slave %s", err)
|
|
| 39 |
+ } |
|
| 40 |
+ if err := system.Mount(rootfs, rootfs, "bind", syscall.MS_BIND|syscall.MS_REC, ""); err != nil {
|
|
| 41 |
+ return fmt.Errorf("mouting %s as bind %s", rootfs, err)
|
|
| 42 |
+ } |
|
| 43 |
+ if err := mountSystem(rootfs, container); err != nil {
|
|
| 44 |
+ return fmt.Errorf("mount system %s", err)
|
|
| 45 |
+ } |
|
| 46 |
+ if err := setupBindmounts(rootfs, container.Mounts); err != nil {
|
|
| 47 |
+ return fmt.Errorf("bind mounts %s", err)
|
|
| 48 |
+ } |
|
| 49 |
+ if err := nodes.CopyN(rootfs, nodes.DefaultNodes); err != nil {
|
|
| 50 |
+ return fmt.Errorf("copy dev nodes %s", err)
|
|
| 51 |
+ } |
|
| 52 |
+ if restrictionPath := container.Context["restriction_path"]; restrictionPath != "" {
|
|
| 53 |
+ if err := restrict.Restrict(rootfs, restrictionPath); err != nil {
|
|
| 54 |
+ return fmt.Errorf("restrict %s", err)
|
|
| 55 |
+ } |
|
| 56 |
+ } |
|
| 57 |
+ if err := SetupPtmx(rootfs, console, container.Context["mount_label"]); err != nil {
|
|
| 58 |
+ return err |
|
| 59 |
+ } |
|
| 60 |
+ if err := system.Chdir(rootfs); err != nil {
|
|
| 61 |
+ return fmt.Errorf("chdir into %s %s", rootfs, err)
|
|
| 62 |
+ } |
|
| 63 |
+ |
|
| 64 |
+ if container.NoPivotRoot {
|
|
| 65 |
+ err = MsMoveRoot(rootfs) |
|
| 66 |
+ } else {
|
|
| 67 |
+ err = PivotRoot(rootfs) |
|
| 68 |
+ } |
|
| 69 |
+ if err != nil {
|
|
| 70 |
+ return err |
|
| 71 |
+ } |
|
| 72 |
+ |
|
| 73 |
+ if container.ReadonlyFs {
|
|
| 74 |
+ if err := SetReadonly(); err != nil {
|
|
| 75 |
+ return fmt.Errorf("set readonly %s", err)
|
|
| 76 |
+ } |
|
| 77 |
+ } |
|
| 78 |
+ |
|
| 79 |
+ system.Umask(0022) |
|
| 80 |
+ |
|
| 81 |
+ return nil |
|
| 82 |
+} |
|
| 83 |
+ |
|
| 84 |
+// mountSystem sets up linux specific system mounts like sys, proc, shm, and devpts |
|
| 85 |
+// inside the mount namespace |
|
| 86 |
+func mountSystem(rootfs string, container *libcontainer.Container) error {
|
|
| 87 |
+ for _, m := range newSystemMounts(rootfs, container.Context["mount_label"], container.Mounts) {
|
|
| 88 |
+ if err := os.MkdirAll(m.path, 0755); err != nil && !os.IsExist(err) {
|
|
| 89 |
+ return fmt.Errorf("mkdirall %s %s", m.path, err)
|
|
| 90 |
+ } |
|
| 91 |
+ if err := system.Mount(m.source, m.path, m.device, uintptr(m.flags), m.data); err != nil {
|
|
| 92 |
+ return fmt.Errorf("mounting %s into %s %s", m.source, m.path, err)
|
|
| 93 |
+ } |
|
| 94 |
+ } |
|
| 95 |
+ return nil |
|
| 96 |
+} |
|
| 97 |
+ |
|
| 98 |
+func setupBindmounts(rootfs string, bindMounts libcontainer.Mounts) error {
|
|
| 99 |
+ for _, m := range bindMounts.OfType("bind") {
|
|
| 100 |
+ var ( |
|
| 101 |
+ flags = syscall.MS_BIND | syscall.MS_REC |
|
| 102 |
+ dest = filepath.Join(rootfs, m.Destination) |
|
| 103 |
+ ) |
|
| 104 |
+ if !m.Writable {
|
|
| 105 |
+ flags = flags | syscall.MS_RDONLY |
|
| 106 |
+ } |
|
| 107 |
+ if err := system.Mount(m.Source, dest, "bind", uintptr(flags), ""); err != nil {
|
|
| 108 |
+ return fmt.Errorf("mounting %s into %s %s", m.Source, dest, err)
|
|
| 109 |
+ } |
|
| 110 |
+ if !m.Writable {
|
|
| 111 |
+ if err := system.Mount(m.Source, dest, "bind", uintptr(flags|syscall.MS_REMOUNT), ""); err != nil {
|
|
| 112 |
+ return fmt.Errorf("remounting %s into %s %s", m.Source, dest, err)
|
|
| 113 |
+ } |
|
| 114 |
+ } |
|
| 115 |
+ if m.Private {
|
|
| 116 |
+ if err := system.Mount("", dest, "none", uintptr(syscall.MS_PRIVATE), ""); err != nil {
|
|
| 117 |
+ return fmt.Errorf("mounting %s private %s", dest, err)
|
|
| 118 |
+ } |
|
| 119 |
+ } |
|
| 120 |
+ } |
|
| 121 |
+ return nil |
|
| 122 |
+} |
|
| 123 |
+ |
|
| 124 |
+func newSystemMounts(rootfs, mountLabel string, mounts libcontainer.Mounts) []mount {
|
|
| 125 |
+ devMounts := []mount{
|
|
| 126 |
+ {source: "shm", path: filepath.Join(rootfs, "dev", "shm"), device: "tmpfs", flags: defaultMountFlags, data: label.FormatMountLabel("mode=1777,size=65536k", mountLabel)},
|
|
| 127 |
+ {source: "devpts", path: filepath.Join(rootfs, "dev", "pts"), device: "devpts", flags: syscall.MS_NOSUID | syscall.MS_NOEXEC, data: label.FormatMountLabel("newinstance,ptmxmode=0666,mode=620,gid=5", mountLabel)},
|
|
| 128 |
+ } |
|
| 129 |
+ |
|
| 130 |
+ systemMounts := []mount{
|
|
| 131 |
+ {source: "proc", path: filepath.Join(rootfs, "proc"), device: "proc", flags: defaultMountFlags},
|
|
| 132 |
+ } |
|
| 133 |
+ |
|
| 134 |
+ if len(mounts.OfType("devtmpfs")) == 1 {
|
|
| 135 |
+ systemMounts = append(systemMounts, mount{source: "tmpfs", path: filepath.Join(rootfs, "dev"), device: "tmpfs", flags: syscall.MS_NOSUID | syscall.MS_STRICTATIME, data: "mode=755"})
|
|
| 136 |
+ } |
|
| 137 |
+ systemMounts = append(systemMounts, devMounts...) |
|
| 138 |
+ |
|
| 139 |
+ if len(mounts.OfType("sysfs")) == 1 {
|
|
| 140 |
+ systemMounts = append(systemMounts, mount{source: "sysfs", path: filepath.Join(rootfs, "sys"), device: "sysfs", flags: defaultMountFlags})
|
|
| 141 |
+ } |
|
| 142 |
+ return systemMounts |
|
| 143 |
+} |
| 0 | 144 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,19 @@ |
| 0 |
+// +build linux |
|
| 1 |
+ |
|
| 2 |
+package mount |
|
| 3 |
+ |
|
| 4 |
+import ( |
|
| 5 |
+ "fmt" |
|
| 6 |
+ "github.com/dotcloud/docker/pkg/system" |
|
| 7 |
+ "syscall" |
|
| 8 |
+) |
|
| 9 |
+ |
|
| 10 |
+func MsMoveRoot(rootfs string) error {
|
|
| 11 |
+ if err := system.Mount(rootfs, "/", "", syscall.MS_MOVE, ""); err != nil {
|
|
| 12 |
+ return fmt.Errorf("mount move %s into / %s", rootfs, err)
|
|
| 13 |
+ } |
|
| 14 |
+ if err := system.Chroot("."); err != nil {
|
|
| 15 |
+ return fmt.Errorf("chroot . %s", err)
|
|
| 16 |
+ } |
|
| 17 |
+ return system.Chdir("/")
|
|
| 18 |
+} |
| 0 | 19 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,49 @@ |
| 0 |
+// +build linux |
|
| 1 |
+ |
|
| 2 |
+package nodes |
|
| 3 |
+ |
|
| 4 |
+import ( |
|
| 5 |
+ "fmt" |
|
| 6 |
+ "github.com/dotcloud/docker/pkg/system" |
|
| 7 |
+ "os" |
|
| 8 |
+ "path/filepath" |
|
| 9 |
+ "syscall" |
|
| 10 |
+) |
|
| 11 |
+ |
|
| 12 |
+// Default list of device nodes to copy |
|
| 13 |
+var DefaultNodes = []string{
|
|
| 14 |
+ "null", |
|
| 15 |
+ "zero", |
|
| 16 |
+ "full", |
|
| 17 |
+ "random", |
|
| 18 |
+ "urandom", |
|
| 19 |
+ "tty", |
|
| 20 |
+} |
|
| 21 |
+ |
|
| 22 |
+// CopyN copies the device node from the host into the rootfs |
|
| 23 |
+func CopyN(rootfs string, nodesToCopy []string) error {
|
|
| 24 |
+ oldMask := system.Umask(0000) |
|
| 25 |
+ defer system.Umask(oldMask) |
|
| 26 |
+ |
|
| 27 |
+ for _, node := range nodesToCopy {
|
|
| 28 |
+ if err := Copy(rootfs, node); err != nil {
|
|
| 29 |
+ return err |
|
| 30 |
+ } |
|
| 31 |
+ } |
|
| 32 |
+ return nil |
|
| 33 |
+} |
|
| 34 |
+ |
|
| 35 |
+func Copy(rootfs, node string) error {
|
|
| 36 |
+ stat, err := os.Stat(filepath.Join("/dev", node))
|
|
| 37 |
+ if err != nil {
|
|
| 38 |
+ return err |
|
| 39 |
+ } |
|
| 40 |
+ var ( |
|
| 41 |
+ dest = filepath.Join(rootfs, "dev", node) |
|
| 42 |
+ st = stat.Sys().(*syscall.Stat_t) |
|
| 43 |
+ ) |
|
| 44 |
+ if err := system.Mknod(dest, st.Mode, int(st.Rdev)); err != nil && !os.IsExist(err) {
|
|
| 45 |
+ return fmt.Errorf("copy %s %s", node, err)
|
|
| 46 |
+ } |
|
| 47 |
+ return nil |
|
| 48 |
+} |
| 0 | 49 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,31 @@ |
| 0 |
+// +build linux |
|
| 1 |
+ |
|
| 2 |
+package mount |
|
| 3 |
+ |
|
| 4 |
+import ( |
|
| 5 |
+ "fmt" |
|
| 6 |
+ "github.com/dotcloud/docker/pkg/system" |
|
| 7 |
+ "io/ioutil" |
|
| 8 |
+ "os" |
|
| 9 |
+ "path/filepath" |
|
| 10 |
+ "syscall" |
|
| 11 |
+) |
|
| 12 |
+ |
|
| 13 |
+func PivotRoot(rootfs string) error {
|
|
| 14 |
+ pivotDir, err := ioutil.TempDir(rootfs, ".pivot_root") |
|
| 15 |
+ if err != nil {
|
|
| 16 |
+ return fmt.Errorf("can't create pivot_root dir %s", pivotDir, err)
|
|
| 17 |
+ } |
|
| 18 |
+ if err := system.Pivotroot(rootfs, pivotDir); err != nil {
|
|
| 19 |
+ return fmt.Errorf("pivot_root %s", err)
|
|
| 20 |
+ } |
|
| 21 |
+ if err := system.Chdir("/"); err != nil {
|
|
| 22 |
+ return fmt.Errorf("chdir / %s", err)
|
|
| 23 |
+ } |
|
| 24 |
+ // path to pivot dir now changed, update |
|
| 25 |
+ pivotDir = filepath.Join("/", filepath.Base(pivotDir))
|
|
| 26 |
+ if err := system.Unmount(pivotDir, syscall.MNT_DETACH); err != nil {
|
|
| 27 |
+ return fmt.Errorf("unmount pivot_root dir %s", err)
|
|
| 28 |
+ } |
|
| 29 |
+ return os.Remove(pivotDir) |
|
| 30 |
+} |
| 0 | 31 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,26 @@ |
| 0 |
+// +build linux |
|
| 1 |
+ |
|
| 2 |
+package mount |
|
| 3 |
+ |
|
| 4 |
+import ( |
|
| 5 |
+ "fmt" |
|
| 6 |
+ "github.com/dotcloud/docker/pkg/libcontainer/console" |
|
| 7 |
+ "os" |
|
| 8 |
+ "path/filepath" |
|
| 9 |
+) |
|
| 10 |
+ |
|
| 11 |
+func SetupPtmx(rootfs, consolePath, mountLabel string) error {
|
|
| 12 |
+ ptmx := filepath.Join(rootfs, "dev/ptmx") |
|
| 13 |
+ if err := os.Remove(ptmx); err != nil && !os.IsNotExist(err) {
|
|
| 14 |
+ return err |
|
| 15 |
+ } |
|
| 16 |
+ if err := os.Symlink("pts/ptmx", ptmx); err != nil {
|
|
| 17 |
+ return fmt.Errorf("symlink dev ptmx %s", err)
|
|
| 18 |
+ } |
|
| 19 |
+ if consolePath != "" {
|
|
| 20 |
+ if err := console.Setup(rootfs, consolePath, mountLabel); err != nil {
|
|
| 21 |
+ return err |
|
| 22 |
+ } |
|
| 23 |
+ } |
|
| 24 |
+ return nil |
|
| 25 |
+} |
| 0 | 26 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,12 @@ |
| 0 |
+// +build linux |
|
| 1 |
+ |
|
| 2 |
+package mount |
|
| 3 |
+ |
|
| 4 |
+import ( |
|
| 5 |
+ "github.com/dotcloud/docker/pkg/system" |
|
| 6 |
+ "syscall" |
|
| 7 |
+) |
|
| 8 |
+ |
|
| 9 |
+func SetReadonly() error {
|
|
| 10 |
+ return system.Mount("/", "/", "bind", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_REC, "")
|
|
| 11 |
+} |
| 0 | 12 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,31 @@ |
| 0 |
+// +build linux |
|
| 1 |
+ |
|
| 2 |
+package mount |
|
| 3 |
+ |
|
| 4 |
+import ( |
|
| 5 |
+ "github.com/dotcloud/docker/pkg/system" |
|
| 6 |
+ "syscall" |
|
| 7 |
+) |
|
| 8 |
+ |
|
| 9 |
+func RemountProc() error {
|
|
| 10 |
+ if err := system.Unmount("/proc", syscall.MNT_DETACH); err != nil {
|
|
| 11 |
+ return err |
|
| 12 |
+ } |
|
| 13 |
+ if err := system.Mount("proc", "/proc", "proc", uintptr(defaultMountFlags), ""); err != nil {
|
|
| 14 |
+ return err |
|
| 15 |
+ } |
|
| 16 |
+ return nil |
|
| 17 |
+} |
|
| 18 |
+ |
|
| 19 |
+func RemountSys() error {
|
|
| 20 |
+ if err := system.Unmount("/sys", syscall.MNT_DETACH); err != nil {
|
|
| 21 |
+ if err != syscall.EINVAL {
|
|
| 22 |
+ return err |
|
| 23 |
+ } |
|
| 24 |
+ } else {
|
|
| 25 |
+ if err := system.Mount("sysfs", "/sys", "sysfs", uintptr(defaultMountFlags), ""); err != nil {
|
|
| 26 |
+ return err |
|
| 27 |
+ } |
|
| 28 |
+ } |
|
| 29 |
+ return nil |
|
| 30 |
+} |
| ... | ... |
@@ -6,6 +6,7 @@ import ( |
| 6 | 6 |
"fmt" |
| 7 | 7 |
"github.com/dotcloud/docker/pkg/label" |
| 8 | 8 |
"github.com/dotcloud/docker/pkg/libcontainer" |
| 9 |
+ "github.com/dotcloud/docker/pkg/libcontainer/mount" |
|
| 9 | 10 |
"github.com/dotcloud/docker/pkg/system" |
| 10 | 11 |
"os" |
| 11 | 12 |
"path/filepath" |
| ... | ... |
@@ -63,10 +64,10 @@ func (ns *linuxNs) ExecIn(container *libcontainer.Container, nspid int, args []s |
| 63 | 63 |
if err := system.Unshare(syscall.CLONE_NEWNS); err != nil {
|
| 64 | 64 |
return -1, err |
| 65 | 65 |
} |
| 66 |
- if err := remountProc(); err != nil {
|
|
| 66 |
+ if err := mount.RemountProc(); err != nil {
|
|
| 67 | 67 |
return -1, fmt.Errorf("remount proc %s", err)
|
| 68 | 68 |
} |
| 69 |
- if err := remountSys(); err != nil {
|
|
| 69 |
+ if err := mount.RemountSys(); err != nil {
|
|
| 70 | 70 |
return -1, fmt.Errorf("remount sys %s", err)
|
| 71 | 71 |
} |
| 72 | 72 |
goto dropAndExec |
| ... | ... |
@@ -12,6 +12,7 @@ import ( |
| 12 | 12 |
"github.com/dotcloud/docker/pkg/label" |
| 13 | 13 |
"github.com/dotcloud/docker/pkg/libcontainer" |
| 14 | 14 |
"github.com/dotcloud/docker/pkg/libcontainer/capabilities" |
| 15 |
+ "github.com/dotcloud/docker/pkg/libcontainer/mount" |
|
| 15 | 16 |
"github.com/dotcloud/docker/pkg/libcontainer/network" |
| 16 | 17 |
"github.com/dotcloud/docker/pkg/libcontainer/security/apparmor" |
| 17 | 18 |
"github.com/dotcloud/docker/pkg/libcontainer/utils" |
| ... | ... |
@@ -61,7 +62,7 @@ func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, consol |
| 61 | 61 |
|
| 62 | 62 |
label.Init() |
| 63 | 63 |
ns.logger.Println("setup mount namespace")
|
| 64 |
- if err := setupNewMountNamespace(rootfs, console, container); err != nil {
|
|
| 64 |
+ if err := mount.InitializeMountNamespace(rootfs, console, container); err != nil {
|
|
| 65 | 65 |
return fmt.Errorf("setup mount namespace %s", err)
|
| 66 | 66 |
} |
| 67 | 67 |
if err := system.Sethostname(container.Hostname); err != nil {
|
| 68 | 68 |
deleted file mode 100644 |
| ... | ... |
@@ -1,261 +0,0 @@ |
| 1 |
-// +build linux |
|
| 2 |
- |
|
| 3 |
-package nsinit |
|
| 4 |
- |
|
| 5 |
-import ( |
|
| 6 |
- "fmt" |
|
| 7 |
- "github.com/dotcloud/docker/pkg/label" |
|
| 8 |
- "github.com/dotcloud/docker/pkg/libcontainer" |
|
| 9 |
- "github.com/dotcloud/docker/pkg/libcontainer/console" |
|
| 10 |
- "github.com/dotcloud/docker/pkg/libcontainer/security/restrict" |
|
| 11 |
- "github.com/dotcloud/docker/pkg/system" |
|
| 12 |
- "io/ioutil" |
|
| 13 |
- "os" |
|
| 14 |
- "path/filepath" |
|
| 15 |
- "syscall" |
|
| 16 |
-) |
|
| 17 |
- |
|
| 18 |
-// default mount point flags |
|
| 19 |
-const defaultMountFlags = syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NODEV |
|
| 20 |
- |
|
| 21 |
-type mount struct {
|
|
| 22 |
- source string |
|
| 23 |
- path string |
|
| 24 |
- device string |
|
| 25 |
- flags int |
|
| 26 |
- data string |
|
| 27 |
-} |
|
| 28 |
- |
|
| 29 |
-// setupNewMountNamespace is used to initialize a new mount namespace for an new |
|
| 30 |
-// container in the rootfs that is specified. |
|
| 31 |
-// |
|
| 32 |
-// There is no need to unmount the new mounts because as soon as the mount namespace |
|
| 33 |
-// is no longer in use, the mounts will be removed automatically |
|
| 34 |
-func setupNewMountNamespace(rootfs, console string, container *libcontainer.Container) error {
|
|
| 35 |
- flag := syscall.MS_PRIVATE |
|
| 36 |
- if container.NoPivotRoot {
|
|
| 37 |
- flag = syscall.MS_SLAVE |
|
| 38 |
- } |
|
| 39 |
- if err := system.Mount("", "/", "", uintptr(flag|syscall.MS_REC), ""); err != nil {
|
|
| 40 |
- return fmt.Errorf("mounting / as slave %s", err)
|
|
| 41 |
- } |
|
| 42 |
- if err := system.Mount(rootfs, rootfs, "bind", syscall.MS_BIND|syscall.MS_REC, ""); err != nil {
|
|
| 43 |
- return fmt.Errorf("mouting %s as bind %s", rootfs, err)
|
|
| 44 |
- } |
|
| 45 |
- if err := mountSystem(rootfs, container); err != nil {
|
|
| 46 |
- return fmt.Errorf("mount system %s", err)
|
|
| 47 |
- } |
|
| 48 |
- if err := setupBindmounts(rootfs, container.Mounts); err != nil {
|
|
| 49 |
- return fmt.Errorf("bind mounts %s", err)
|
|
| 50 |
- } |
|
| 51 |
- if err := copyDevNodes(rootfs); err != nil {
|
|
| 52 |
- return fmt.Errorf("copy dev nodes %s", err)
|
|
| 53 |
- } |
|
| 54 |
- if restrictionPath := container.Context["restriction_path"]; restrictionPath != "" {
|
|
| 55 |
- if err := restrict.Restrict(rootfs, restrictionPath); err != nil {
|
|
| 56 |
- return fmt.Errorf("restrict %s", err)
|
|
| 57 |
- } |
|
| 58 |
- } |
|
| 59 |
- if err := setupPtmx(rootfs, console, container.Context["mount_label"]); err != nil {
|
|
| 60 |
- return err |
|
| 61 |
- } |
|
| 62 |
- if err := system.Chdir(rootfs); err != nil {
|
|
| 63 |
- return fmt.Errorf("chdir into %s %s", rootfs, err)
|
|
| 64 |
- } |
|
| 65 |
- |
|
| 66 |
- if container.NoPivotRoot {
|
|
| 67 |
- if err := rootMsMove(rootfs); err != nil {
|
|
| 68 |
- return err |
|
| 69 |
- } |
|
| 70 |
- } else {
|
|
| 71 |
- if err := rootPivot(rootfs); err != nil {
|
|
| 72 |
- return err |
|
| 73 |
- } |
|
| 74 |
- } |
|
| 75 |
- |
|
| 76 |
- if container.ReadonlyFs {
|
|
| 77 |
- if err := system.Mount("/", "/", "bind", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_REC, ""); err != nil {
|
|
| 78 |
- return fmt.Errorf("mounting %s as readonly %s", rootfs, err)
|
|
| 79 |
- } |
|
| 80 |
- } |
|
| 81 |
- |
|
| 82 |
- system.Umask(0022) |
|
| 83 |
- |
|
| 84 |
- return nil |
|
| 85 |
-} |
|
| 86 |
- |
|
| 87 |
-// use a pivot root to setup the rootfs |
|
| 88 |
-func rootPivot(rootfs string) error {
|
|
| 89 |
- pivotDir, err := ioutil.TempDir(rootfs, ".pivot_root") |
|
| 90 |
- if err != nil {
|
|
| 91 |
- return fmt.Errorf("can't create pivot_root dir %s", pivotDir, err)
|
|
| 92 |
- } |
|
| 93 |
- if err := system.Pivotroot(rootfs, pivotDir); err != nil {
|
|
| 94 |
- return fmt.Errorf("pivot_root %s", err)
|
|
| 95 |
- } |
|
| 96 |
- if err := system.Chdir("/"); err != nil {
|
|
| 97 |
- return fmt.Errorf("chdir / %s", err)
|
|
| 98 |
- } |
|
| 99 |
- // path to pivot dir now changed, update |
|
| 100 |
- pivotDir = filepath.Join("/", filepath.Base(pivotDir))
|
|
| 101 |
- if err := system.Unmount(pivotDir, syscall.MNT_DETACH); err != nil {
|
|
| 102 |
- return fmt.Errorf("unmount pivot_root dir %s", err)
|
|
| 103 |
- } |
|
| 104 |
- if err := os.Remove(pivotDir); err != nil {
|
|
| 105 |
- return fmt.Errorf("remove pivot_root dir %s", err)
|
|
| 106 |
- } |
|
| 107 |
- return nil |
|
| 108 |
-} |
|
| 109 |
- |
|
| 110 |
-// use MS_MOVE and chroot to setup the rootfs |
|
| 111 |
-func rootMsMove(rootfs string) error {
|
|
| 112 |
- if err := system.Mount(rootfs, "/", "", syscall.MS_MOVE, ""); err != nil {
|
|
| 113 |
- return fmt.Errorf("mount move %s into / %s", rootfs, err)
|
|
| 114 |
- } |
|
| 115 |
- if err := system.Chroot("."); err != nil {
|
|
| 116 |
- return fmt.Errorf("chroot . %s", err)
|
|
| 117 |
- } |
|
| 118 |
- if err := system.Chdir("/"); err != nil {
|
|
| 119 |
- return fmt.Errorf("chdir / %s", err)
|
|
| 120 |
- } |
|
| 121 |
- return nil |
|
| 122 |
-} |
|
| 123 |
- |
|
| 124 |
-// copyDevNodes mknods the hosts devices so the new container has access to them |
|
| 125 |
-func copyDevNodes(rootfs string) error {
|
|
| 126 |
- oldMask := system.Umask(0000) |
|
| 127 |
- defer system.Umask(oldMask) |
|
| 128 |
- |
|
| 129 |
- for _, node := range []string{
|
|
| 130 |
- "null", |
|
| 131 |
- "zero", |
|
| 132 |
- "full", |
|
| 133 |
- "random", |
|
| 134 |
- "urandom", |
|
| 135 |
- "tty", |
|
| 136 |
- } {
|
|
| 137 |
- if err := copyDevNode(rootfs, node); err != nil {
|
|
| 138 |
- return err |
|
| 139 |
- } |
|
| 140 |
- } |
|
| 141 |
- return nil |
|
| 142 |
-} |
|
| 143 |
- |
|
| 144 |
-func copyDevNode(rootfs, node string) error {
|
|
| 145 |
- stat, err := os.Stat(filepath.Join("/dev", node))
|
|
| 146 |
- if err != nil {
|
|
| 147 |
- return err |
|
| 148 |
- } |
|
| 149 |
- var ( |
|
| 150 |
- dest = filepath.Join(rootfs, "dev", node) |
|
| 151 |
- st = stat.Sys().(*syscall.Stat_t) |
|
| 152 |
- ) |
|
| 153 |
- if err := system.Mknod(dest, st.Mode, int(st.Rdev)); err != nil && !os.IsExist(err) {
|
|
| 154 |
- return fmt.Errorf("copy %s %s", node, err)
|
|
| 155 |
- } |
|
| 156 |
- return nil |
|
| 157 |
-} |
|
| 158 |
- |
|
| 159 |
-// mountSystem sets up linux specific system mounts like sys, proc, shm, and devpts |
|
| 160 |
-// inside the mount namespace |
|
| 161 |
-func mountSystem(rootfs string, container *libcontainer.Container) error {
|
|
| 162 |
- for _, m := range newSystemMounts(rootfs, container.Context["mount_label"], container.Mounts) {
|
|
| 163 |
- if err := os.MkdirAll(m.path, 0755); err != nil && !os.IsExist(err) {
|
|
| 164 |
- return fmt.Errorf("mkdirall %s %s", m.path, err)
|
|
| 165 |
- } |
|
| 166 |
- if err := system.Mount(m.source, m.path, m.device, uintptr(m.flags), m.data); err != nil {
|
|
| 167 |
- return fmt.Errorf("mounting %s into %s %s", m.source, m.path, err)
|
|
| 168 |
- } |
|
| 169 |
- } |
|
| 170 |
- return nil |
|
| 171 |
-} |
|
| 172 |
- |
|
| 173 |
-// setupPtmx adds a symlink to pts/ptmx for /dev/ptmx and |
|
| 174 |
-// finishes setting up /dev/console |
|
| 175 |
-func setupPtmx(rootfs, consolePath, mountLabel string) error {
|
|
| 176 |
- ptmx := filepath.Join(rootfs, "dev/ptmx") |
|
| 177 |
- if err := os.Remove(ptmx); err != nil && !os.IsNotExist(err) {
|
|
| 178 |
- return err |
|
| 179 |
- } |
|
| 180 |
- if err := os.Symlink("pts/ptmx", ptmx); err != nil {
|
|
| 181 |
- return fmt.Errorf("symlink dev ptmx %s", err)
|
|
| 182 |
- } |
|
| 183 |
- if consolePath != "" {
|
|
| 184 |
- if err := console.Setup(rootfs, consolePath, mountLabel); err != nil {
|
|
| 185 |
- return err |
|
| 186 |
- } |
|
| 187 |
- } |
|
| 188 |
- return nil |
|
| 189 |
-} |
|
| 190 |
- |
|
| 191 |
-// remountProc is used to detach and remount the proc filesystem |
|
| 192 |
-// commonly needed with running a new process inside an existing container |
|
| 193 |
-func remountProc() error {
|
|
| 194 |
- if err := system.Unmount("/proc", syscall.MNT_DETACH); err != nil {
|
|
| 195 |
- return err |
|
| 196 |
- } |
|
| 197 |
- if err := system.Mount("proc", "/proc", "proc", uintptr(defaultMountFlags), ""); err != nil {
|
|
| 198 |
- return err |
|
| 199 |
- } |
|
| 200 |
- return nil |
|
| 201 |
-} |
|
| 202 |
- |
|
| 203 |
-func remountSys() error {
|
|
| 204 |
- if err := system.Unmount("/sys", syscall.MNT_DETACH); err != nil {
|
|
| 205 |
- if err != syscall.EINVAL {
|
|
| 206 |
- return err |
|
| 207 |
- } |
|
| 208 |
- } else {
|
|
| 209 |
- if err := system.Mount("sysfs", "/sys", "sysfs", uintptr(defaultMountFlags), ""); err != nil {
|
|
| 210 |
- return err |
|
| 211 |
- } |
|
| 212 |
- } |
|
| 213 |
- return nil |
|
| 214 |
-} |
|
| 215 |
- |
|
| 216 |
-func setupBindmounts(rootfs string, bindMounts libcontainer.Mounts) error {
|
|
| 217 |
- for _, m := range bindMounts.OfType("bind") {
|
|
| 218 |
- var ( |
|
| 219 |
- flags = syscall.MS_BIND | syscall.MS_REC |
|
| 220 |
- dest = filepath.Join(rootfs, m.Destination) |
|
| 221 |
- ) |
|
| 222 |
- if !m.Writable {
|
|
| 223 |
- flags = flags | syscall.MS_RDONLY |
|
| 224 |
- } |
|
| 225 |
- if err := system.Mount(m.Source, dest, "bind", uintptr(flags), ""); err != nil {
|
|
| 226 |
- return fmt.Errorf("mounting %s into %s %s", m.Source, dest, err)
|
|
| 227 |
- } |
|
| 228 |
- if !m.Writable {
|
|
| 229 |
- if err := system.Mount(m.Source, dest, "bind", uintptr(flags|syscall.MS_REMOUNT), ""); err != nil {
|
|
| 230 |
- return fmt.Errorf("remounting %s into %s %s", m.Source, dest, err)
|
|
| 231 |
- } |
|
| 232 |
- } |
|
| 233 |
- if m.Private {
|
|
| 234 |
- if err := system.Mount("", dest, "none", uintptr(syscall.MS_PRIVATE), ""); err != nil {
|
|
| 235 |
- return fmt.Errorf("mounting %s private %s", dest, err)
|
|
| 236 |
- } |
|
| 237 |
- } |
|
| 238 |
- } |
|
| 239 |
- return nil |
|
| 240 |
-} |
|
| 241 |
- |
|
| 242 |
-func newSystemMounts(rootfs, mountLabel string, mounts libcontainer.Mounts) []mount {
|
|
| 243 |
- devMounts := []mount{
|
|
| 244 |
- {source: "shm", path: filepath.Join(rootfs, "dev", "shm"), device: "tmpfs", flags: defaultMountFlags, data: label.FormatMountLabel("mode=1777,size=65536k", mountLabel)},
|
|
| 245 |
- {source: "devpts", path: filepath.Join(rootfs, "dev", "pts"), device: "devpts", flags: syscall.MS_NOSUID | syscall.MS_NOEXEC, data: label.FormatMountLabel("newinstance,ptmxmode=0666,mode=620,gid=5", mountLabel)},
|
|
| 246 |
- } |
|
| 247 |
- |
|
| 248 |
- systemMounts := []mount{
|
|
| 249 |
- {source: "proc", path: filepath.Join(rootfs, "proc"), device: "proc", flags: defaultMountFlags},
|
|
| 250 |
- } |
|
| 251 |
- |
|
| 252 |
- if len(mounts.OfType("devtmpfs")) == 1 {
|
|
| 253 |
- systemMounts = append(systemMounts, mount{source: "tmpfs", path: filepath.Join(rootfs, "dev"), device: "tmpfs", flags: syscall.MS_NOSUID | syscall.MS_STRICTATIME, data: "mode=755"})
|
|
| 254 |
- } |
|
| 255 |
- systemMounts = append(systemMounts, devMounts...) |
|
| 256 |
- |
|
| 257 |
- if len(mounts.OfType("sysfs")) == 1 {
|
|
| 258 |
- systemMounts = append(systemMounts, mount{source: "sysfs", path: filepath.Join(rootfs, "sys"), device: "sysfs", flags: defaultMountFlags})
|
|
| 259 |
- } |
|
| 260 |
- return systemMounts |
|
| 261 |
-} |