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-04-29 10:41:44 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
|
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-05-13 17:54:08 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/symlink"
|
2014-02-19 07:13:36 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-02-19 00:56:11 +00:00
|
|
|
)
|
|
|
|
|
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-05-09 19:01:29 +00:00
|
|
|
return fmt.Errorf("mounting / with flags %X %s", (flag | syscall.MS_REC), err)
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
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-02-17 23:14:30 +00:00
|
|
|
if err := nodes.CreateDeviceNodes(rootfs, container.DeviceNodes); err != nil {
|
|
|
|
return fmt.Errorf("create device nodes %s", err)
|
2014-05-19 20:46:59 +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-05-12 12:41:07 +00:00
|
|
|
if err := setupDevSymlinks(rootfs); err != nil {
|
|
|
|
return fmt.Errorf("dev symlinks %s", err)
|
|
|
|
}
|
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-05-08 17:58:33 +00: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-05-12 12:41:07 +00:00
|
|
|
func setupDevSymlinks(rootfs string) error {
|
|
|
|
var links = [][2]string{
|
|
|
|
{"/proc/self/fd", "/dev/fd"},
|
|
|
|
{"/proc/self/fd/0", "/dev/stdin"},
|
|
|
|
{"/proc/self/fd/1", "/dev/stdout"},
|
|
|
|
{"/proc/self/fd/2", "/dev/stderr"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// kcore support can be toggled with CONFIG_PROC_KCORE; only create a symlink
|
|
|
|
// in /dev if it exists in /proc.
|
|
|
|
if _, err := os.Stat("/proc/kcore"); err == nil {
|
|
|
|
links = append(links, [2]string{"/proc/kcore", "/dev/kcore"})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, link := range links {
|
|
|
|
var (
|
|
|
|
src = link[0]
|
|
|
|
dst = filepath.Join(rootfs, link[1])
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := os.Symlink(src, dst); err != nil && !os.IsExist(err) {
|
|
|
|
return fmt.Errorf("symlink %s %s %s", src, dst, 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
|
|
|
|
}
|
2014-05-08 17:58:33 +00:00
|
|
|
|
|
|
|
stat, err := os.Stat(m.Source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-13 13:42:21 +00:00
|
|
|
|
2014-05-13 17:54:08 +00:00
|
|
|
dest, err = symlink.FollowSymlinkInScope(dest, rootfs)
|
2014-05-13 13:42:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-08 17:58:33 +00:00
|
|
|
if err := createIfNotExists(dest, stat.IsDir()); err != nil {
|
2014-05-12 19:24:30 +00:00
|
|
|
return fmt.Errorf("Creating new bind-mount target, %s", err)
|
2014-05-08 17:58:33 +00:00
|
|
|
}
|
|
|
|
|
2014-04-10 23:03:52 +00: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 11:45:39 +00:00
|
|
|
|
2014-04-11 15:26:23 +00:00
|
|
|
// TODO: this is crappy right now and should be cleaned up with a better way of handling system and
|
2014-05-01 01:00:42 +00:00
|
|
|
// standard bind mounts allowing them to be more dynamic
|
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
|
|
|
systemMounts := []mount{
|
|
|
|
{source: "proc", path: filepath.Join(rootfs, "proc"), device: "proc", flags: defaultMountFlags},
|
2014-05-01 01:00:42 +00:00
|
|
|
{source: "sysfs", path: filepath.Join(rootfs, "sys"), device: "sysfs", flags: defaultMountFlags},
|
2014-05-20 00:13:00 +00:00
|
|
|
{source: "tmpfs", path: filepath.Join(rootfs, "dev"), device: "tmpfs", flags: syscall.MS_NOSUID | syscall.MS_STRICTATIME, data: label.FormatMountLabel("mode=755", mountLabel)},
|
2014-05-01 17:08:18 +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
|
|
|
}
|
|
|
|
|
2014-04-11 11:45:39 +00:00
|
|
|
return systemMounts
|
|
|
|
}
|