2014-02-20 08:50:10 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-04-11 23:06:56 +08:00
|
|
|
package mount
|
2014-02-19 08:56:11 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-04-29 18:41:44 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
|
2014-03-19 04:49:16 +08:00
|
|
|
"github.com/dotcloud/docker/pkg/label"
|
2014-03-03 23:15:29 +08:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
2014-04-11 23:06:56 +08:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/mount/nodes"
|
2014-02-19 15:13:36 +08:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-02-19 08:56:11 +08:00
|
|
|
)
|
|
|
|
|
2014-02-20 14:43:40 +08:00
|
|
|
// default mount point flags
|
2014-02-20 08:40:36 +08:00
|
|
|
const defaultMountFlags = syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NODEV
|
2014-02-19 08:56:11 +08:00
|
|
|
|
2014-04-11 19:45:39 +08:00
|
|
|
type mount struct {
|
|
|
|
source string
|
|
|
|
path string
|
|
|
|
device string
|
|
|
|
flags int
|
|
|
|
data string
|
|
|
|
}
|
|
|
|
|
2014-04-11 23:06:56 +08:00
|
|
|
// InitializeMountNamespace setups up the devices, mount points, and filesystems for use inside a
|
|
|
|
// new mount namepsace
|
|
|
|
func InitializeMountNamespace(rootfs, console string, container *libcontainer.Container) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
flag = syscall.MS_PRIVATE
|
|
|
|
)
|
2014-04-11 07:03:52 +08:00
|
|
|
if container.NoPivotRoot {
|
2014-03-07 11:30:52 +08:00
|
|
|
flag = syscall.MS_SLAVE
|
|
|
|
}
|
|
|
|
if err := system.Mount("", "/", "", uintptr(flag|syscall.MS_REC), ""); err != nil {
|
2014-02-19 08:56:11 +08:00
|
|
|
return fmt.Errorf("mounting / as slave %s", err)
|
|
|
|
}
|
2014-02-19 15:13:36 +08:00
|
|
|
if err := system.Mount(rootfs, rootfs, "bind", syscall.MS_BIND|syscall.MS_REC, ""); err != nil {
|
2014-02-19 08:56:11 +08:00
|
|
|
return fmt.Errorf("mouting %s as bind %s", rootfs, err)
|
|
|
|
}
|
2014-04-11 19:45:39 +08:00
|
|
|
if err := mountSystem(rootfs, container); err != nil {
|
2014-02-19 08:56:11 +08:00
|
|
|
return fmt.Errorf("mount system %s", err)
|
|
|
|
}
|
2014-04-11 07:03:52 +08:00
|
|
|
if err := setupBindmounts(rootfs, container.Mounts); err != nil {
|
|
|
|
return fmt.Errorf("bind mounts %s", err)
|
|
|
|
}
|
2014-04-11 23:06:56 +08:00
|
|
|
if err := nodes.CopyN(rootfs, nodes.DefaultNodes); err != nil {
|
2014-04-11 22:30:09 +08:00
|
|
|
return fmt.Errorf("copy dev nodes %s", err)
|
|
|
|
}
|
2014-04-11 23:06:56 +08:00
|
|
|
if err := SetupPtmx(rootfs, console, container.Context["mount_label"]); err != nil {
|
2014-03-14 05:31:09 +08:00
|
|
|
return err
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
2014-02-19 15:13:36 +08:00
|
|
|
if err := system.Chdir(rootfs); err != nil {
|
2014-02-19 08:56:11 +08:00
|
|
|
return fmt.Errorf("chdir into %s %s", rootfs, err)
|
|
|
|
}
|
2014-03-07 09:19:59 +08:00
|
|
|
|
2014-04-11 07:03:52 +08:00
|
|
|
if container.NoPivotRoot {
|
2014-04-11 23:06:56 +08:00
|
|
|
err = MsMoveRoot(rootfs)
|
2014-03-07 11:30:52 +08:00
|
|
|
} else {
|
2014-04-11 23:06:56 +08:00
|
|
|
err = PivotRoot(rootfs)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-03-07 11:30:52 +08:00
|
|
|
}
|
|
|
|
|
2014-04-11 07:03:52 +08:00
|
|
|
if container.ReadonlyFs {
|
2014-04-11 23:06:56 +08:00
|
|
|
if err := SetReadonly(); err != nil {
|
|
|
|
return fmt.Errorf("set readonly %s", err)
|
2014-03-21 22:17:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 11:30:52 +08:00
|
|
|
system.Umask(0022)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-19 08:56:11 +08:00
|
|
|
// mountSystem sets up linux specific system mounts like sys, proc, shm, and devpts
|
|
|
|
// inside the mount namespace
|
2014-04-11 19:45:39 +08:00
|
|
|
func mountSystem(rootfs string, container *libcontainer.Container) error {
|
|
|
|
for _, m := range newSystemMounts(rootfs, container.Context["mount_label"], container.Mounts) {
|
2014-02-19 08:56:11 +08:00
|
|
|
if err := os.MkdirAll(m.path, 0755); err != nil && !os.IsExist(err) {
|
|
|
|
return fmt.Errorf("mkdirall %s %s", m.path, err)
|
|
|
|
}
|
2014-02-19 15:13:36 +08:00
|
|
|
if err := system.Mount(m.source, m.path, m.device, uintptr(m.flags), m.data); err != nil {
|
2014-02-19 08:56:11 +08:00
|
|
|
return fmt.Errorf("mounting %s into %s %s", m.source, m.path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-09 01:58:33 +08:00
|
|
|
func createIfNotExists(path string, isDir bool) error {
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if isDir {
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(path, os.O_CREATE, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-11 19:45:39 +08:00
|
|
|
func setupBindmounts(rootfs string, bindMounts libcontainer.Mounts) error {
|
|
|
|
for _, m := range bindMounts.OfType("bind") {
|
2014-04-11 07:03:52 +08:00
|
|
|
var (
|
|
|
|
flags = syscall.MS_BIND | syscall.MS_REC
|
|
|
|
dest = filepath.Join(rootfs, m.Destination)
|
|
|
|
)
|
|
|
|
if !m.Writable {
|
|
|
|
flags = flags | syscall.MS_RDONLY
|
|
|
|
}
|
2014-05-09 01:58:33 +08:00
|
|
|
|
|
|
|
stat, err := os.Stat(m.Source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := createIfNotExists(dest, stat.IsDir()); err != nil {
|
|
|
|
return fmt.Errorf("Creating new bind-mount target, %s\n", err)
|
|
|
|
}
|
|
|
|
|
2014-04-11 07:03:52 +08:00
|
|
|
if err := system.Mount(m.Source, dest, "bind", uintptr(flags), ""); err != nil {
|
|
|
|
return fmt.Errorf("mounting %s into %s %s", m.Source, dest, err)
|
|
|
|
}
|
|
|
|
if !m.Writable {
|
|
|
|
if err := system.Mount(m.Source, dest, "bind", uintptr(flags|syscall.MS_REMOUNT), ""); err != nil {
|
|
|
|
return fmt.Errorf("remounting %s into %s %s", m.Source, dest, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if m.Private {
|
|
|
|
if err := system.Mount("", dest, "none", uintptr(syscall.MS_PRIVATE), ""); err != nil {
|
|
|
|
return fmt.Errorf("mounting %s private %s", dest, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-11 19:45:39 +08:00
|
|
|
|
2014-04-11 23:26:23 +08:00
|
|
|
// TODO: this is crappy right now and should be cleaned up with a better way of handling system and
|
2014-05-01 09:00:42 +08:00
|
|
|
// standard bind mounts allowing them to be more dynamic
|
2014-04-11 19:45:39 +08:00
|
|
|
func newSystemMounts(rootfs, mountLabel string, mounts libcontainer.Mounts) []mount {
|
2014-04-11 22:30:09 +08:00
|
|
|
systemMounts := []mount{
|
|
|
|
{source: "proc", path: filepath.Join(rootfs, "proc"), device: "proc", flags: defaultMountFlags},
|
2014-05-01 09:00:42 +08:00
|
|
|
{source: "sysfs", path: filepath.Join(rootfs, "sys"), device: "sysfs", flags: defaultMountFlags},
|
2014-05-02 01:08:18 +08:00
|
|
|
{source: "shm", path: filepath.Join(rootfs, "dev", "shm"), device: "tmpfs", flags: defaultMountFlags, data: label.FormatMountLabel("mode=1777,size=65536k", mountLabel)},
|
|
|
|
{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)},
|
2014-04-11 22:30:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(mounts.OfType("devtmpfs")) == 1 {
|
2014-05-03 04:55:45 +08:00
|
|
|
systemMounts = append([]mount{{source: "tmpfs", path: filepath.Join(rootfs, "dev"), device: "tmpfs", flags: syscall.MS_NOSUID | syscall.MS_STRICTATIME, data: label.FormatMountLabel("mode=755", mountLabel)}}, systemMounts...)
|
2014-04-11 22:30:09 +08:00
|
|
|
}
|
2014-04-11 19:45:39 +08:00
|
|
|
return systemMounts
|
|
|
|
}
|