2014-02-20 00:50:10 +00:00
|
|
|
// +build linux
|
|
|
|
|
2014-04-11 15:06:56 +00:00
|
|
|
package mount
|
2014-02-19 00:56:11 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-03-18 20:49:16 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/label"
|
2014-03-03 15:15:29 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
2014-04-11 15:06:56 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/mount/nodes"
|
2014-04-10 23:03:52 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/security/restrict"
|
2014-02-19 07:13:36 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-02-19 00:56:11 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// default mount point flags
|
2014-02-20 00:40:36 +00:00
|
|
|
const defaultMountFlags = syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NODEV
|
2014-02-19 00:56:11 +00:00
|
|
|
|
2014-04-11 11:45:39 +00:00
|
|
|
type mount struct {
|
|
|
|
source string
|
|
|
|
path string
|
|
|
|
device string
|
|
|
|
flags int
|
|
|
|
data string
|
|
|
|
}
|
|
|
|
|
2014-04-11 15:06:56 +00: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-10 23:03:52 +00:00
|
|
|
if container.NoPivotRoot {
|
2014-03-07 03:30:52 +00:00
|
|
|
flag = syscall.MS_SLAVE
|
|
|
|
}
|
|
|
|
if err := system.Mount("", "/", "", uintptr(flag|syscall.MS_REC), ""); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return fmt.Errorf("mounting / as slave %s", err)
|
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Mount(rootfs, rootfs, "bind", syscall.MS_BIND|syscall.MS_REC, ""); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return fmt.Errorf("mouting %s as bind %s", rootfs, err)
|
|
|
|
}
|
2014-04-11 11:45:39 +00:00
|
|
|
if err := mountSystem(rootfs, container); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return fmt.Errorf("mount system %s", err)
|
|
|
|
}
|
2014-04-10 23:03:52 +00:00
|
|
|
if err := setupBindmounts(rootfs, container.Mounts); err != nil {
|
|
|
|
return fmt.Errorf("bind mounts %s", err)
|
|
|
|
}
|
2014-04-11 15:06:56 +00:00
|
|
|
if err := nodes.CopyN(rootfs, nodes.DefaultNodes); err != nil {
|
2014-04-11 14:30:09 +00:00
|
|
|
return fmt.Errorf("copy dev nodes %s", err)
|
|
|
|
}
|
2014-04-10 23:03:52 +00:00
|
|
|
if restrictionPath := container.Context["restriction_path"]; restrictionPath != "" {
|
|
|
|
if err := restrict.Restrict(rootfs, restrictionPath); err != nil {
|
|
|
|
return fmt.Errorf("restrict %s", err)
|
2014-03-03 15:15:29 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-11 15:06:56 +00:00
|
|
|
if err := SetupPtmx(rootfs, console, container.Context["mount_label"]); err != nil {
|
2014-03-13 21:31:09 +00:00
|
|
|
return err
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Chdir(rootfs); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return fmt.Errorf("chdir into %s %s", rootfs, err)
|
|
|
|
}
|
2014-03-07 01:19:59 +00:00
|
|
|
|
2014-04-10 23:03:52 +00:00
|
|
|
if container.NoPivotRoot {
|
2014-04-11 15:06:56 +00:00
|
|
|
err = MsMoveRoot(rootfs)
|
2014-03-07 03:30:52 +00:00
|
|
|
} else {
|
2014-04-11 15:06:56 +00:00
|
|
|
err = PivotRoot(rootfs)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-03-07 03:30:52 +00:00
|
|
|
}
|
|
|
|
|
2014-04-10 23:03:52 +00:00
|
|
|
if container.ReadonlyFs {
|
2014-04-11 15:06:56 +00:00
|
|
|
if err := SetReadonly(); err != nil {
|
|
|
|
return fmt.Errorf("set readonly %s", err)
|
2014-03-21 14:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 03:30:52 +00:00
|
|
|
system.Umask(0022)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-19 00:56:11 +00:00
|
|
|
// mountSystem sets up linux specific system mounts like sys, proc, shm, and devpts
|
|
|
|
// inside the mount namespace
|
2014-04-11 11:45:39 +00:00
|
|
|
func mountSystem(rootfs string, container *libcontainer.Container) error {
|
|
|
|
for _, m := range newSystemMounts(rootfs, container.Context["mount_label"], container.Mounts) {
|
2014-02-19 00:56:11 +00: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 07:13:36 +00:00
|
|
|
if err := system.Mount(m.source, m.path, m.device, uintptr(m.flags), m.data); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return fmt.Errorf("mounting %s into %s %s", m.source, m.path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-11 11:45:39 +00:00
|
|
|
func setupBindmounts(rootfs string, bindMounts libcontainer.Mounts) error {
|
|
|
|
for _, m := range bindMounts.OfType("bind") {
|
2014-04-10 23:03:52 +00:00
|
|
|
var (
|
|
|
|
flags = syscall.MS_BIND | syscall.MS_REC
|
|
|
|
dest = filepath.Join(rootfs, m.Destination)
|
|
|
|
)
|
|
|
|
if !m.Writable {
|
|
|
|
flags = flags | syscall.MS_RDONLY
|
|
|
|
}
|
|
|
|
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 11:45:39 +00:00
|
|
|
|
|
|
|
func newSystemMounts(rootfs, mountLabel string, mounts libcontainer.Mounts) []mount {
|
2014-04-11 14:30:09 +00:00
|
|
|
devMounts := []mount{
|
2014-04-11 11:45:39 +00: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 14:30:09 +00:00
|
|
|
systemMounts := []mount{
|
|
|
|
{source: "proc", path: filepath.Join(rootfs, "proc"), device: "proc", flags: defaultMountFlags},
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mounts.OfType("devtmpfs")) == 1 {
|
|
|
|
systemMounts = append(systemMounts, mount{source: "tmpfs", path: filepath.Join(rootfs, "dev"), device: "tmpfs", flags: syscall.MS_NOSUID | syscall.MS_STRICTATIME, data: "mode=755"})
|
|
|
|
}
|
|
|
|
systemMounts = append(systemMounts, devMounts...)
|
|
|
|
|
2014-04-11 11:45:39 +00:00
|
|
|
if len(mounts.OfType("sysfs")) == 1 {
|
|
|
|
systemMounts = append(systemMounts, mount{source: "sysfs", path: filepath.Join(rootfs, "sys"), device: "sysfs", flags: defaultMountFlags})
|
|
|
|
}
|
|
|
|
return systemMounts
|
|
|
|
}
|