2014-02-20 00:50:10 +00:00
|
|
|
// +build linux
|
|
|
|
|
2014-02-21 02:27:42 +00:00
|
|
|
package nsinit
|
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-02-19 07:13:36 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-03-07 01:19:59 +00:00
|
|
|
"io/ioutil"
|
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-02-20 06:43:40 +00:00
|
|
|
// setupNewMountNamespace is used to initialize a new mount namespace for an new
|
|
|
|
// container in the rootfs that is specified.
|
|
|
|
//
|
|
|
|
// There is no need to unmount the new mounts because as soon as the mount namespace
|
|
|
|
// is no longer in use, the mounts will be removed automatically
|
2014-03-18 20:49:16 +00:00
|
|
|
func setupNewMountNamespace(rootfs string, bindMounts []libcontainer.Mount, console string, readonly, noPivotRoot bool, mountLabel string) error {
|
2014-03-07 03:30:52 +00:00
|
|
|
flag := syscall.MS_PRIVATE
|
|
|
|
if noPivotRoot {
|
|
|
|
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-03-18 20:49:16 +00:00
|
|
|
if err := mountSystem(rootfs, mountLabel); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return fmt.Errorf("mount system %s", err)
|
|
|
|
}
|
2014-03-03 15:15:29 +00:00
|
|
|
|
|
|
|
for _, m := range bindMounts {
|
2014-04-08 01:23:22 +00:00
|
|
|
var (
|
|
|
|
flags = syscall.MS_BIND | syscall.MS_REC
|
|
|
|
dest = filepath.Join(rootfs, m.Destination)
|
|
|
|
)
|
2014-03-03 15:15:29 +00:00
|
|
|
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)
|
|
|
|
}
|
2014-04-08 01:23:22 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2014-03-03 15:15:29 +00:00
|
|
|
if m.Private {
|
|
|
|
if err := system.Mount("", dest, "none", uintptr(syscall.MS_PRIVATE), ""); err != nil {
|
|
|
|
return fmt.Errorf("mounting %s private %s", dest, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 00:56:11 +00:00
|
|
|
if err := copyDevNodes(rootfs); err != nil {
|
|
|
|
return fmt.Errorf("copy dev nodes %s", err)
|
|
|
|
}
|
2014-03-18 20:49:16 +00:00
|
|
|
if err := setupPtmx(rootfs, console, mountLabel); 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-03-07 03:30:52 +00:00
|
|
|
if noPivotRoot {
|
|
|
|
if err := rootMsMove(rootfs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := rootPivot(rootfs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-21 14:17:17 +00:00
|
|
|
if readonly {
|
|
|
|
if err := system.Mount("/", "/", "bind", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_REC, ""); err != nil {
|
|
|
|
return fmt.Errorf("mounting %s as readonly %s", rootfs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 03:30:52 +00:00
|
|
|
system.Umask(0022)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// use a pivot root to setup the rootfs
|
|
|
|
func rootPivot(rootfs string) error {
|
2014-03-07 01:19:59 +00:00
|
|
|
pivotDir, err := ioutil.TempDir(rootfs, ".pivot_root")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't create pivot_root dir %s", pivotDir, err)
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
2014-03-07 01:19:59 +00:00
|
|
|
if err := system.Pivotroot(rootfs, pivotDir); err != nil {
|
|
|
|
return fmt.Errorf("pivot_root %s", err)
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Chdir("/"); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return fmt.Errorf("chdir / %s", err)
|
|
|
|
}
|
2014-03-07 01:19:59 +00:00
|
|
|
// path to pivot dir now changed, update
|
|
|
|
pivotDir = filepath.Join("/", filepath.Base(pivotDir))
|
|
|
|
if err := system.Unmount(pivotDir, syscall.MNT_DETACH); err != nil {
|
|
|
|
return fmt.Errorf("unmount pivot_root dir %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Remove(pivotDir); err != nil {
|
|
|
|
return fmt.Errorf("remove pivot_root dir %s", err)
|
|
|
|
}
|
2014-03-07 03:30:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
2014-03-07 01:19:59 +00:00
|
|
|
|
2014-03-07 03:30:52 +00:00
|
|
|
// use MS_MOVE and chroot to setup the rootfs
|
|
|
|
func rootMsMove(rootfs string) error {
|
|
|
|
if err := system.Mount(rootfs, "/", "", syscall.MS_MOVE, ""); err != nil {
|
|
|
|
return fmt.Errorf("mount move %s into / %s", rootfs, err)
|
|
|
|
}
|
|
|
|
if err := system.Chroot("."); err != nil {
|
|
|
|
return fmt.Errorf("chroot . %s", err)
|
|
|
|
}
|
|
|
|
if err := system.Chdir("/"); err != nil {
|
|
|
|
return fmt.Errorf("chdir / %s", err)
|
|
|
|
}
|
2014-02-19 00:56:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// copyDevNodes mknods the hosts devices so the new container has access to them
|
2014-02-19 00:56:11 +00:00
|
|
|
func copyDevNodes(rootfs string) error {
|
2014-02-19 07:13:36 +00:00
|
|
|
oldMask := system.Umask(0000)
|
|
|
|
defer system.Umask(oldMask)
|
2014-02-19 00:56:11 +00:00
|
|
|
|
|
|
|
for _, node := range []string{
|
|
|
|
"null",
|
|
|
|
"zero",
|
|
|
|
"full",
|
|
|
|
"random",
|
|
|
|
"urandom",
|
|
|
|
"tty",
|
|
|
|
} {
|
2014-02-27 01:21:09 +00:00
|
|
|
if err := copyDevNode(rootfs, node); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-02-27 01:21:09 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyDevNode(rootfs, node string) error {
|
|
|
|
stat, err := os.Stat(filepath.Join("/dev", node))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
dest = filepath.Join(rootfs, "dev", node)
|
|
|
|
st = stat.Sys().(*syscall.Stat_t)
|
|
|
|
)
|
|
|
|
if err := system.Mknod(dest, st.Mode, int(st.Rdev)); err != nil && !os.IsExist(err) {
|
|
|
|
return fmt.Errorf("copy %s %s", node, err)
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// setupConsole ensures that the container has a proper /dev/console setup
|
2014-03-18 20:49:16 +00:00
|
|
|
func setupConsole(rootfs, console string, mountLabel string) error {
|
2014-02-19 07:13:36 +00:00
|
|
|
oldMask := system.Umask(0000)
|
|
|
|
defer system.Umask(oldMask)
|
2014-02-19 00:56:11 +00:00
|
|
|
|
|
|
|
stat, err := os.Stat(console)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("stat console %s %s", console, err)
|
|
|
|
}
|
2014-02-20 00:40:36 +00:00
|
|
|
var (
|
|
|
|
st = stat.Sys().(*syscall.Stat_t)
|
|
|
|
dest = filepath.Join(rootfs, "dev/console")
|
|
|
|
)
|
2014-02-19 00:56:11 +00:00
|
|
|
if err := os.Remove(dest); err != nil && !os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("remove %s %s", dest, err)
|
|
|
|
}
|
|
|
|
if err := os.Chmod(console, 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Chown(console, 0, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Mknod(dest, (st.Mode&^07777)|0600, int(st.Rdev)); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return fmt.Errorf("mknod %s %s", dest, err)
|
|
|
|
}
|
2014-03-18 20:49:16 +00:00
|
|
|
if err := label.SetFileLabel(console, mountLabel); err != nil {
|
|
|
|
return fmt.Errorf("SetFileLabel Failed %s %s", dest, err)
|
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Mount(console, dest, "bind", syscall.MS_BIND, ""); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return fmt.Errorf("bind %s to %s %s", console, dest, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// mountSystem sets up linux specific system mounts like sys, proc, shm, and devpts
|
|
|
|
// inside the mount namespace
|
2014-03-18 20:49:16 +00:00
|
|
|
func mountSystem(rootfs string, mountLabel string) error {
|
2014-02-19 07:13:36 +00:00
|
|
|
for _, m := range []struct {
|
2014-02-19 00:56:11 +00:00
|
|
|
source string
|
|
|
|
path string
|
|
|
|
device string
|
|
|
|
flags int
|
|
|
|
data string
|
|
|
|
}{
|
2014-02-20 00:40:36 +00:00
|
|
|
{source: "proc", path: filepath.Join(rootfs, "proc"), device: "proc", flags: defaultMountFlags},
|
|
|
|
{source: "sysfs", path: filepath.Join(rootfs, "sys"), device: "sysfs", flags: defaultMountFlags},
|
2014-04-09 10:53:32 +00:00
|
|
|
{source: "shm", path: filepath.Join(rootfs, "dev", "shm"), device: "tmpfs", flags: defaultMountFlags, data: label.FormatMountLabel("mode=1777,size=65536k", mountLabel)},
|
2014-03-18 20:49:16 +00:00
|
|
|
{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-02-19 07:13:36 +00:00
|
|
|
} {
|
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-02-20 06:43:40 +00:00
|
|
|
// setupPtmx adds a symlink to pts/ptmx for /dev/ptmx and
|
|
|
|
// finishes setting up /dev/console
|
2014-03-18 20:49:16 +00:00
|
|
|
func setupPtmx(rootfs, console string, mountLabel string) error {
|
2014-02-20 06:43:40 +00:00
|
|
|
ptmx := filepath.Join(rootfs, "dev/ptmx")
|
|
|
|
if err := os.Remove(ptmx); err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Symlink("pts/ptmx", ptmx); err != nil {
|
|
|
|
return fmt.Errorf("symlink dev ptmx %s", err)
|
|
|
|
}
|
2014-03-13 21:31:09 +00:00
|
|
|
if console != "" {
|
2014-03-18 20:49:16 +00:00
|
|
|
if err := setupConsole(rootfs, console, mountLabel); err != nil {
|
2014-03-13 21:31:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-02-20 06:43:40 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// remountProc is used to detach and remount the proc filesystem
|
|
|
|
// commonly needed with running a new process inside an existing container
|
2014-02-19 00:56:11 +00:00
|
|
|
func remountProc() error {
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Unmount("/proc", syscall.MNT_DETACH); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-02-20 00:40:36 +00:00
|
|
|
if err := system.Mount("proc", "/proc", "proc", uintptr(defaultMountFlags), ""); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func remountSys() error {
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Unmount("/sys", syscall.MNT_DETACH); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
if err != syscall.EINVAL {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2014-02-20 00:40:36 +00:00
|
|
|
if err := system.Mount("sysfs", "/sys", "sysfs", uintptr(defaultMountFlags), ""); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|