2014-02-20 08:50:10 +08:00
|
|
|
// +build linux
|
|
|
|
|
2015-02-10 05:16:43 +08:00
|
|
|
package libcontainer
|
2014-02-19 08:56:11 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-02-17 18:20:06 +08:00
|
|
|
"io"
|
2015-02-10 05:16:43 +08:00
|
|
|
"io/ioutil"
|
2014-04-29 18:41:44 +08:00
|
|
|
"os"
|
2015-04-02 20:31:47 +08:00
|
|
|
"os/exec"
|
2015-04-23 10:17:30 +08:00
|
|
|
"path"
|
2014-04-29 18:41:44 +08:00
|
|
|
"path/filepath"
|
2015-02-27 05:08:15 +08:00
|
|
|
"strings"
|
2014-04-29 18:41:44 +08:00
|
|
|
"syscall"
|
2015-02-10 06:42:21 +08:00
|
|
|
"time"
|
2014-04-29 18:41:44 +08:00
|
|
|
|
2015-10-02 05:03:02 +08:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2015-04-22 04:46:27 +08:00
|
|
|
"github.com/docker/docker/pkg/symlink"
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2016-01-09 02:19:00 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/system"
|
2016-01-22 07:27:20 +08:00
|
|
|
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
|
2014-02-19 08:56:11 +08:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2015-02-10 05:16:43 +08:00
|
|
|
// setupRootfs sets up the devices, mount points, and filesystems for use inside a
|
2014-06-24 20:30:27 +08:00
|
|
|
// new mount namespace.
|
2016-02-17 18:20:06 +08:00
|
|
|
func setupRootfs(config *configs.Config, console *linuxConsole, pipe io.ReadWriter) (err error) {
|
2015-02-01 11:56:27 +08:00
|
|
|
if err := prepareRoot(config); err != nil {
|
2015-02-18 13:37:02 +08:00
|
|
|
return newSystemError(err)
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
2015-07-08 05:46:44 +08:00
|
|
|
|
2015-10-21 16:06:26 +08:00
|
|
|
setupDev := len(config.Devices) != 0
|
2015-03-18 02:03:28 +08:00
|
|
|
for _, m := range config.Mounts {
|
2015-04-02 20:31:47 +08:00
|
|
|
for _, precmd := range m.PremountCmds {
|
|
|
|
if err := mountCmd(precmd); err != nil {
|
|
|
|
return newSystemError(err)
|
|
|
|
}
|
|
|
|
}
|
2015-03-20 01:17:32 +08:00
|
|
|
if err := mountToRootfs(m, config.Rootfs, config.MountLabel); err != nil {
|
2015-02-18 13:37:02 +08:00
|
|
|
return newSystemError(err)
|
2014-08-27 16:51:52 +08:00
|
|
|
}
|
2015-04-02 20:31:47 +08:00
|
|
|
|
|
|
|
for _, postcmd := range m.PostmountCmds {
|
|
|
|
if err := mountCmd(postcmd); err != nil {
|
|
|
|
return newSystemError(err)
|
|
|
|
}
|
|
|
|
}
|
2014-04-11 07:03:52 +08:00
|
|
|
}
|
2015-10-21 16:06:26 +08:00
|
|
|
if setupDev {
|
2015-07-08 05:46:44 +08:00
|
|
|
if err := createDevices(config); err != nil {
|
|
|
|
return newSystemError(err)
|
|
|
|
}
|
|
|
|
if err := setupPtmx(config, console); err != nil {
|
|
|
|
return newSystemError(err)
|
|
|
|
}
|
|
|
|
if err := setupDevSymlinks(config.Rootfs); err != nil {
|
|
|
|
return newSystemError(err)
|
|
|
|
}
|
2014-05-12 20:41:07 +08:00
|
|
|
}
|
2016-02-17 18:20:06 +08:00
|
|
|
// Signal the parent to run the pre-start hooks.
|
|
|
|
// The hooks are run after the mounts are setup, but before we switch to the new
|
|
|
|
// root, so that the old root is still available in the hooks for any mount
|
|
|
|
// manipulations.
|
|
|
|
if err := syncParentHooks(pipe); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-04 09:44:58 +08:00
|
|
|
if err := syscall.Chdir(config.Rootfs); err != nil {
|
2015-02-18 13:37:02 +08:00
|
|
|
return newSystemError(err)
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
2015-02-01 11:56:27 +08:00
|
|
|
if config.NoPivotRoot {
|
2015-02-04 09:44:58 +08:00
|
|
|
err = msMoveRoot(config.Rootfs)
|
2014-03-07 11:30:52 +08:00
|
|
|
} else {
|
2015-02-04 09:44:58 +08:00
|
|
|
err = pivotRoot(config.Rootfs, config.PivotDir)
|
2014-04-11 23:06:56 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2015-02-18 13:37:02 +08:00
|
|
|
return newSystemError(err)
|
2014-03-07 11:30:52 +08:00
|
|
|
}
|
2015-10-21 16:06:26 +08:00
|
|
|
if setupDev {
|
2015-09-21 17:50:29 +08:00
|
|
|
if err := reOpenDevNull(); err != nil {
|
2015-07-08 05:46:44 +08:00
|
|
|
return newSystemError(err)
|
|
|
|
}
|
2015-05-01 02:03:07 +08:00
|
|
|
}
|
2015-02-04 09:44:58 +08:00
|
|
|
if config.Readonlyfs {
|
2015-02-01 11:56:27 +08:00
|
|
|
if err := setReadonly(); err != nil {
|
2015-02-18 13:37:02 +08:00
|
|
|
return newSystemError(err)
|
2014-03-21 22:17:17 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-15 07:55:49 +08:00
|
|
|
syscall.Umask(0022)
|
2014-03-07 11:30:52 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-02 20:31:47 +08:00
|
|
|
func mountCmd(cmd configs.Command) error {
|
|
|
|
|
|
|
|
command := exec.Command(cmd.Path, cmd.Args[:]...)
|
|
|
|
command.Env = cmd.Env
|
|
|
|
command.Dir = cmd.Dir
|
|
|
|
if out, err := command.CombinedOutput(); err != nil {
|
2015-04-17 04:55:12 +08:00
|
|
|
return fmt.Errorf("%#v failed: %s: %v", cmd, string(out), err)
|
2015-04-02 20:31:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-20 01:17:32 +08:00
|
|
|
func mountToRootfs(m *configs.Mount, rootfs, mountLabel string) error {
|
2015-02-13 08:23:05 +08:00
|
|
|
var (
|
2015-02-27 05:08:15 +08:00
|
|
|
dest = m.Destination
|
2015-02-13 08:23:05 +08:00
|
|
|
)
|
2015-02-27 05:08:15 +08:00
|
|
|
if !strings.HasPrefix(dest, rootfs) {
|
|
|
|
dest = filepath.Join(rootfs, dest)
|
|
|
|
}
|
|
|
|
|
2015-02-13 08:23:05 +08:00
|
|
|
switch m.Device {
|
2015-04-07 11:12:59 +08:00
|
|
|
case "proc", "sysfs":
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is
returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-30 09:01:41 +08:00
|
|
|
if err := os.MkdirAll(dest, 0755); err != nil {
|
2015-02-13 08:23:05 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-09-27 18:52:24 +08:00
|
|
|
// Selinux kernels do not support labeling of /proc or /sys
|
|
|
|
return mountPropagate(m, rootfs, "")
|
2015-04-07 11:12:59 +08:00
|
|
|
case "mqueue":
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is
returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-30 09:01:41 +08:00
|
|
|
if err := os.MkdirAll(dest, 0755); err != nil {
|
2015-04-07 11:12:59 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-09-25 00:43:12 +08:00
|
|
|
if err := mountPropagate(m, rootfs, mountLabel); err != nil {
|
2015-09-28 23:02:37 +08:00
|
|
|
// older kernels do not support labeling of /dev/mqueue
|
|
|
|
if err := mountPropagate(m, rootfs, ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-07 11:12:59 +08:00
|
|
|
}
|
|
|
|
return label.SetFileLabel(dest, mountLabel)
|
2015-03-11 23:12:55 +08:00
|
|
|
case "tmpfs":
|
|
|
|
stat, err := os.Stat(dest)
|
|
|
|
if err != nil {
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is
returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-30 09:01:41 +08:00
|
|
|
if err := os.MkdirAll(dest, 0755); err != nil {
|
2015-03-11 23:12:55 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 00:43:12 +08:00
|
|
|
if err := mountPropagate(m, rootfs, mountLabel); err != nil {
|
2015-03-11 23:12:55 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if stat != nil {
|
|
|
|
if err = os.Chmod(dest, stat.Mode()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2015-03-17 02:05:20 +08:00
|
|
|
case "devpts":
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is
returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-30 09:01:41 +08:00
|
|
|
if err := os.MkdirAll(dest, 0755); err != nil {
|
2015-02-13 08:23:05 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-09-25 00:43:12 +08:00
|
|
|
return mountPropagate(m, rootfs, mountLabel)
|
2015-08-05 19:20:08 +08:00
|
|
|
case "securityfs":
|
|
|
|
if err := os.MkdirAll(dest, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-25 00:43:12 +08:00
|
|
|
return mountPropagate(m, rootfs, mountLabel)
|
2015-02-13 08:23:05 +08:00
|
|
|
case "bind":
|
|
|
|
stat, err := os.Stat(m.Source)
|
|
|
|
if err != nil {
|
|
|
|
// error out if the source of a bind mount does not exist as we will be
|
|
|
|
// unable to bind anything to it.
|
|
|
|
return err
|
|
|
|
}
|
2015-04-22 04:46:27 +08:00
|
|
|
// ensure that the destination of the bind mount is resolved of symlinks at mount time because
|
|
|
|
// any previous mounts can invalidate the next mount's destination.
|
|
|
|
// this can happen when a user specifies mounts within other mounts to cause breakouts or other
|
|
|
|
// evil stuff to try to escape the container's rootfs.
|
|
|
|
if dest, err = symlink.FollowSymlinkInScope(filepath.Join(rootfs, m.Destination), rootfs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := checkMountDestination(rootfs, dest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-24 01:07:18 +08:00
|
|
|
// update the mount with the correct dest after symlinks are resolved.
|
|
|
|
m.Destination = dest
|
2015-02-13 08:23:05 +08:00
|
|
|
if err := createIfNotExists(dest, stat.IsDir()); err != nil {
|
2015-02-10 06:22:52 +08:00
|
|
|
return err
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
2015-09-25 00:43:12 +08:00
|
|
|
if err := mountPropagate(m, rootfs, mountLabel); err != nil {
|
2015-02-10 06:22:52 +08:00
|
|
|
return err
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
2015-08-31 11:10:34 +08:00
|
|
|
// bind mount won't change mount options, we need remount to make mount options effective.
|
2015-09-29 22:53:21 +08:00
|
|
|
// first check that we have non-default options required before attempting a remount
|
|
|
|
if m.Flags&^(syscall.MS_REC|syscall.MS_REMOUNT|syscall.MS_BIND) != 0 {
|
|
|
|
// only remount if unique mount options are set
|
|
|
|
if err := remount(m, rootfs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-13 08:23:05 +08:00
|
|
|
}
|
2015-09-29 22:53:21 +08:00
|
|
|
|
2015-02-13 08:23:05 +08:00
|
|
|
if m.Relabel != "" {
|
2015-07-31 04:36:35 +08:00
|
|
|
if err := label.Validate(m.Relabel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
shared := label.IsShared(m.Relabel)
|
|
|
|
if err := label.Relabel(m.Source, mountLabel, shared); err != nil {
|
2015-02-13 08:23:05 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-04-18 08:41:11 +08:00
|
|
|
case "cgroup":
|
2015-07-21 02:25:22 +08:00
|
|
|
binds, err := getCgroupMounts(m)
|
2015-04-18 08:41:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-21 05:00:18 +08:00
|
|
|
var merged []string
|
|
|
|
for _, b := range binds {
|
|
|
|
ss := filepath.Base(b.Destination)
|
|
|
|
if strings.Contains(ss, ",") {
|
|
|
|
merged = append(merged, ss)
|
|
|
|
}
|
|
|
|
}
|
2015-04-18 08:41:11 +08:00
|
|
|
tmpfs := &configs.Mount{
|
libcontainer: Allow passing mount propagation flags
Right now if one passes a mount propagation flag in spec file, it
does not take effect. For example, try following in spec json file.
{
"type": "bind",
"source": "/root/mnt-source",
"destination": "/root/mnt-dest",
"options": "rbind,shared"
}
One would expect that /root/mnt-dest will be shared inside the container
but that's not the case.
#findmnt -o TARGET,PROPAGATION
`-/root/mnt-dest private
Reason being that propagation flags can't be passed in along with other
regular flags. They need to be passed in a separate call to mount syscall.
That too, one propagation flag at a time. (from mount man page).
Hence, store propagation flags separately in a slice and apply these
in that order after the mount call wherever appropriate. This allows
user to control the propagation property of mount point inside
the container.
Storing them separately also solves another problem where recursive flag
(syscall.MS_REC) can get mixed up. For example, options "rbind,private"
and "bind,rprivate" will be same and there will be no way to differentiate
between these if all the flags are stored in a single integer.
This patch would allow one to pass propagation flags "[r]shared,[r]slave,
[r]private,[r]unbindable" in spec file as per mount property.
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
2015-09-17 03:53:23 +08:00
|
|
|
Source: "tmpfs",
|
|
|
|
Device: "tmpfs",
|
|
|
|
Destination: m.Destination,
|
|
|
|
Flags: defaultMountFlags,
|
|
|
|
Data: "mode=755",
|
|
|
|
PropagationFlags: m.PropagationFlags,
|
2015-04-18 08:41:11 +08:00
|
|
|
}
|
|
|
|
if err := mountToRootfs(tmpfs, rootfs, mountLabel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, b := range binds {
|
|
|
|
if err := mountToRootfs(b, rootfs, mountLabel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-07-21 05:00:18 +08:00
|
|
|
// create symlinks for merged cgroups
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Chdir(filepath.Join(rootfs, m.Destination)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, mc := range merged {
|
|
|
|
for _, ss := range strings.Split(mc, ",") {
|
|
|
|
if err := os.Symlink(mc, ss); err != nil {
|
|
|
|
// if cgroup already exists, then okay(it could have been created before)
|
|
|
|
if os.IsExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
os.Chdir(cwd)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := os.Chdir(cwd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-21 23:56:37 +08:00
|
|
|
if m.Flags&syscall.MS_RDONLY != 0 {
|
|
|
|
// remount cgroup root as readonly
|
libcontainer: Allow passing mount propagation flags
Right now if one passes a mount propagation flag in spec file, it
does not take effect. For example, try following in spec json file.
{
"type": "bind",
"source": "/root/mnt-source",
"destination": "/root/mnt-dest",
"options": "rbind,shared"
}
One would expect that /root/mnt-dest will be shared inside the container
but that's not the case.
#findmnt -o TARGET,PROPAGATION
`-/root/mnt-dest private
Reason being that propagation flags can't be passed in along with other
regular flags. They need to be passed in a separate call to mount syscall.
That too, one propagation flag at a time. (from mount man page).
Hence, store propagation flags separately in a slice and apply these
in that order after the mount call wherever appropriate. This allows
user to control the propagation property of mount point inside
the container.
Storing them separately also solves another problem where recursive flag
(syscall.MS_REC) can get mixed up. For example, options "rbind,private"
and "bind,rprivate" will be same and there will be no way to differentiate
between these if all the flags are stored in a single integer.
This patch would allow one to pass propagation flags "[r]shared,[r]slave,
[r]private,[r]unbindable" in spec file as per mount property.
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
2015-09-17 03:53:23 +08:00
|
|
|
mcgrouproot := &configs.Mount{
|
|
|
|
Destination: m.Destination,
|
|
|
|
Flags: defaultMountFlags | syscall.MS_RDONLY,
|
|
|
|
}
|
2015-09-25 00:43:12 +08:00
|
|
|
if err := remount(mcgrouproot, rootfs); err != nil {
|
2015-07-21 23:56:37 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-02-13 08:23:05 +08:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown mount device %q to %q", m.Device, m.Destination)
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-21 02:25:22 +08:00
|
|
|
func getCgroupMounts(m *configs.Mount) ([]*configs.Mount, error) {
|
|
|
|
mounts, err := cgroups.GetCgroupMounts()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-10 16:58:08 +08:00
|
|
|
cgroupPaths, err := cgroups.ParseCgroupFile("/proc/self/cgroup")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-21 02:25:22 +08:00
|
|
|
var binds []*configs.Mount
|
|
|
|
|
|
|
|
for _, mm := range mounts {
|
2015-09-10 16:58:08 +08:00
|
|
|
dir, err := mm.GetThisCgroupDir(cgroupPaths)
|
2015-07-21 02:25:22 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
relDir, err := filepath.Rel(mm.Root, dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
binds = append(binds, &configs.Mount{
|
libcontainer: Allow passing mount propagation flags
Right now if one passes a mount propagation flag in spec file, it
does not take effect. For example, try following in spec json file.
{
"type": "bind",
"source": "/root/mnt-source",
"destination": "/root/mnt-dest",
"options": "rbind,shared"
}
One would expect that /root/mnt-dest will be shared inside the container
but that's not the case.
#findmnt -o TARGET,PROPAGATION
`-/root/mnt-dest private
Reason being that propagation flags can't be passed in along with other
regular flags. They need to be passed in a separate call to mount syscall.
That too, one propagation flag at a time. (from mount man page).
Hence, store propagation flags separately in a slice and apply these
in that order after the mount call wherever appropriate. This allows
user to control the propagation property of mount point inside
the container.
Storing them separately also solves another problem where recursive flag
(syscall.MS_REC) can get mixed up. For example, options "rbind,private"
and "bind,rprivate" will be same and there will be no way to differentiate
between these if all the flags are stored in a single integer.
This patch would allow one to pass propagation flags "[r]shared,[r]slave,
[r]private,[r]unbindable" in spec file as per mount property.
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
2015-09-17 03:53:23 +08:00
|
|
|
Device: "bind",
|
|
|
|
Source: filepath.Join(mm.Mountpoint, relDir),
|
|
|
|
Destination: filepath.Join(m.Destination, strings.Join(mm.Subsystems, ",")),
|
|
|
|
Flags: syscall.MS_BIND | syscall.MS_REC | m.Flags,
|
|
|
|
PropagationFlags: m.PropagationFlags,
|
2015-07-21 02:25:22 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return binds, nil
|
|
|
|
}
|
|
|
|
|
2015-11-16 19:16:27 +08:00
|
|
|
// checkMountDestination checks to ensure that the mount destination is not over the top of /proc.
|
2015-04-22 04:46:27 +08:00
|
|
|
// dest is required to be an abs path and have any symlinks resolved before calling this function.
|
|
|
|
func checkMountDestination(rootfs, dest string) error {
|
2016-01-22 07:27:20 +08:00
|
|
|
if libcontainerUtils.CleanPath(rootfs) == libcontainerUtils.CleanPath(dest) {
|
2015-05-01 01:23:42 +08:00
|
|
|
return fmt.Errorf("mounting into / is prohibited")
|
|
|
|
}
|
2015-04-22 04:46:27 +08:00
|
|
|
invalidDestinations := []string{
|
|
|
|
"/proc",
|
|
|
|
}
|
2016-01-06 14:48:40 +08:00
|
|
|
// White list, it should be sub directories of invalid destinations
|
|
|
|
validDestinations := []string{
|
|
|
|
// These entries can be bind mounted by files emulated by fuse,
|
|
|
|
// so commands like top, free displays stats in container.
|
|
|
|
"/proc/cpuinfo",
|
|
|
|
"/proc/diskstats",
|
|
|
|
"/proc/meminfo",
|
2016-02-16 15:59:27 +08:00
|
|
|
"/proc/stat",
|
|
|
|
"/proc/net/dev",
|
2016-01-06 14:48:40 +08:00
|
|
|
}
|
|
|
|
for _, valid := range validDestinations {
|
|
|
|
path, err := filepath.Rel(filepath.Join(rootfs, valid), dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if path == "." {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2015-04-22 04:46:27 +08:00
|
|
|
for _, invalid := range invalidDestinations {
|
2015-05-01 03:39:29 +08:00
|
|
|
path, err := filepath.Rel(filepath.Join(rootfs, invalid), dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if path == "." || !strings.HasPrefix(path, "..") {
|
2015-04-22 04:46:27 +08:00
|
|
|
return fmt.Errorf("%q cannot be mounted because it is located inside %q", dest, invalid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-12 20:41:07 +08: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 {
|
2016-02-15 20:22:19 +08:00
|
|
|
links = append(links, [2]string{"/proc/kcore", "/dev/core"})
|
2014-05-12 20:41:07 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:03:07 +08:00
|
|
|
// If stdin, stdout, and/or stderr are pointing to `/dev/null` in the parent's rootfs
|
|
|
|
// this method will make them point to `/dev/null` in this container's rootfs. This
|
|
|
|
// needs to be called after we chroot/pivot into the container's rootfs so that any
|
|
|
|
// symlinks are resolved locally.
|
2015-09-21 17:50:29 +08:00
|
|
|
func reOpenDevNull() error {
|
2014-07-17 02:33:43 +08:00
|
|
|
var stat, devNullStat syscall.Stat_t
|
2015-09-30 16:05:49 +08:00
|
|
|
file, err := os.OpenFile("/dev/null", os.O_RDWR, 0)
|
2014-07-17 02:33:43 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to open /dev/null - %s", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2015-02-13 08:23:05 +08:00
|
|
|
if err := syscall.Fstat(int(file.Fd()), &devNullStat); err != nil {
|
|
|
|
return err
|
2014-07-17 02:33:43 +08:00
|
|
|
}
|
|
|
|
for fd := 0; fd < 3; fd++ {
|
2015-02-13 08:23:05 +08:00
|
|
|
if err := syscall.Fstat(fd, &stat); err != nil {
|
|
|
|
return err
|
2014-07-17 02:33:43 +08:00
|
|
|
}
|
|
|
|
if stat.Rdev == devNullStat.Rdev {
|
|
|
|
// Close and re-open the fd.
|
2015-06-10 06:19:47 +08:00
|
|
|
if err := syscall.Dup3(int(file.Fd()), fd, 0); err != nil {
|
2015-02-13 08:23:05 +08:00
|
|
|
return err
|
2014-07-17 02:33:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-02-01 11:56:27 +08:00
|
|
|
|
|
|
|
// Create the device nodes in the container.
|
2015-02-04 09:44:58 +08:00
|
|
|
func createDevices(config *configs.Config) error {
|
2016-01-09 02:19:00 +08:00
|
|
|
useBindMount := system.RunningInUserNS() || config.Namespaces.Contains(configs.NEWUSER)
|
2015-02-01 11:56:27 +08:00
|
|
|
oldMask := syscall.Umask(0000)
|
2015-02-04 09:44:58 +08:00
|
|
|
for _, node := range config.Devices {
|
2015-03-25 08:19:12 +08:00
|
|
|
// containers running in a user namespace are not allowed to mknod
|
|
|
|
// devices so we can just bind mount it from the host.
|
2016-01-09 02:19:00 +08:00
|
|
|
if err := createDeviceNode(config.Rootfs, node, useBindMount); err != nil {
|
2015-02-01 11:56:27 +08:00
|
|
|
syscall.Umask(oldMask)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
syscall.Umask(oldMask)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:30:41 +08:00
|
|
|
func bindMountDeviceNode(dest string, node *configs.Device) error {
|
|
|
|
f, err := os.Create(dest)
|
|
|
|
if err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if f != nil {
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
return syscall.Mount(node.Path, dest, "bind", syscall.MS_BIND, "")
|
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
// Creates the device node in the rootfs of the container.
|
2015-03-25 08:19:12 +08:00
|
|
|
func createDeviceNode(rootfs string, node *configs.Device, bind bool) error {
|
2015-02-18 13:50:43 +08:00
|
|
|
dest := filepath.Join(rootfs, node.Path)
|
|
|
|
if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil {
|
2015-02-01 11:56:27 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-03-25 08:19:12 +08:00
|
|
|
|
|
|
|
if bind {
|
2015-10-12 06:30:41 +08:00
|
|
|
return bindMountDeviceNode(dest, node)
|
2015-02-18 13:37:02 +08:00
|
|
|
}
|
2015-03-25 08:19:12 +08:00
|
|
|
if err := mknodDevice(dest, node); err != nil {
|
|
|
|
if os.IsExist(err) {
|
|
|
|
return nil
|
2015-10-12 06:30:41 +08:00
|
|
|
} else if os.IsPermission(err) {
|
|
|
|
return bindMountDeviceNode(dest, node)
|
2015-03-25 08:19:12 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2015-02-18 13:37:02 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mknodDevice(dest string, node *configs.Device) error {
|
2015-02-01 11:56:27 +08:00
|
|
|
fileMode := node.FileMode
|
|
|
|
switch node.Type {
|
|
|
|
case 'c':
|
|
|
|
fileMode |= syscall.S_IFCHR
|
|
|
|
case 'b':
|
|
|
|
fileMode |= syscall.S_IFBLK
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("%c is not a valid device type for device %s", node.Type, node.Path)
|
|
|
|
}
|
2015-02-18 13:37:02 +08:00
|
|
|
if err := syscall.Mknod(dest, uint32(fileMode), node.Mkdev()); err != nil {
|
|
|
|
return err
|
2015-02-01 11:56:27 +08:00
|
|
|
}
|
2015-02-18 13:37:02 +08:00
|
|
|
return syscall.Chown(dest, int(node.Uid), int(node.Gid))
|
2015-02-01 11:56:27 +08:00
|
|
|
}
|
|
|
|
|
2015-10-06 16:42:19 +08:00
|
|
|
func getMountInfo(mountinfo []*mount.Info, dir string) *mount.Info {
|
2015-10-02 05:03:02 +08:00
|
|
|
for _, m := range mountinfo {
|
|
|
|
if m.Mountpoint == dir {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the parent mount point of directory passed in as argument. Also return
|
|
|
|
// optional fields.
|
|
|
|
func getParentMount(rootfs string) (string, string, error) {
|
|
|
|
var path string
|
|
|
|
|
|
|
|
mountinfos, err := mount.GetMounts()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
mountinfo := getMountInfo(mountinfos, rootfs)
|
|
|
|
if mountinfo != nil {
|
|
|
|
return rootfs, mountinfo.Optional, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
path = rootfs
|
|
|
|
for {
|
|
|
|
path = filepath.Dir(path)
|
|
|
|
|
|
|
|
mountinfo = getMountInfo(mountinfos, path)
|
|
|
|
if mountinfo != nil {
|
|
|
|
return path, mountinfo.Optional, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if path == "/" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are here, we did not find parent mount. Something is wrong.
|
|
|
|
return "", "", fmt.Errorf("Could not find parent mount of %s", rootfs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make parent mount private if it was shared
|
|
|
|
func rootfsParentMountPrivate(config *configs.Config) error {
|
|
|
|
sharedMount := false
|
|
|
|
|
|
|
|
parentMount, optionalOpts, err := getParentMount(config.Rootfs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
optsSplit := strings.Split(optionalOpts, " ")
|
|
|
|
for _, opt := range optsSplit {
|
|
|
|
if strings.HasPrefix(opt, "shared:") {
|
|
|
|
sharedMount = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make parent mount PRIVATE if it was shared. It is needed for two
|
|
|
|
// reasons. First of all pivot_root() will fail if parent mount is
|
|
|
|
// shared. Secondly when we bind mount rootfs it will propagate to
|
|
|
|
// parent namespace and we don't want that to happen.
|
|
|
|
if sharedMount {
|
|
|
|
return syscall.Mount("", parentMount, "", syscall.MS_PRIVATE, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
func prepareRoot(config *configs.Config) error {
|
2015-04-10 22:45:04 +08:00
|
|
|
flag := syscall.MS_SLAVE | syscall.MS_REC
|
2015-10-02 05:03:02 +08:00
|
|
|
if config.RootPropagation != 0 {
|
|
|
|
flag = config.RootPropagation
|
2015-02-01 11:56:27 +08:00
|
|
|
}
|
|
|
|
if err := syscall.Mount("", "/", "", uintptr(flag), ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-02 05:03:02 +08:00
|
|
|
|
|
|
|
if err := rootfsParentMountPrivate(config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-04 09:44:58 +08:00
|
|
|
return syscall.Mount(config.Rootfs, config.Rootfs, "bind", syscall.MS_BIND|syscall.MS_REC, "")
|
2015-02-01 11:56:27 +08:00
|
|
|
}
|
2015-02-10 05:16:43 +08:00
|
|
|
|
|
|
|
func setReadonly() error {
|
|
|
|
return syscall.Mount("/", "/", "bind", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_REC, "")
|
|
|
|
}
|
|
|
|
|
2015-02-26 06:31:39 +08:00
|
|
|
func setupPtmx(config *configs.Config, console *linuxConsole) error {
|
2015-02-10 05:16:43 +08:00
|
|
|
ptmx := filepath.Join(config.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)
|
|
|
|
}
|
2015-02-26 06:31:39 +08:00
|
|
|
if console != nil {
|
2015-09-21 17:50:29 +08:00
|
|
|
return console.mount(config.Rootfs, config.MountLabel)
|
2015-02-10 05:16:43 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-24 19:31:07 +08:00
|
|
|
func pivotRoot(rootfs, pivotBaseDir string) (err error) {
|
2015-02-10 05:16:43 +08:00
|
|
|
if pivotBaseDir == "" {
|
|
|
|
pivotBaseDir = "/"
|
|
|
|
}
|
|
|
|
tmpDir := filepath.Join(rootfs, pivotBaseDir)
|
|
|
|
if err := os.MkdirAll(tmpDir, 0755); err != nil {
|
|
|
|
return fmt.Errorf("can't create tmp dir %s, error %v", tmpDir, err)
|
|
|
|
}
|
|
|
|
pivotDir, err := ioutil.TempDir(tmpDir, ".pivot_root")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't create pivot_root dir %s, error %v", pivotDir, err)
|
|
|
|
}
|
2016-02-24 19:31:07 +08:00
|
|
|
defer func() {
|
|
|
|
errVal := os.Remove(pivotDir)
|
|
|
|
if err == nil {
|
|
|
|
err = errVal
|
|
|
|
}
|
|
|
|
}()
|
2015-02-10 05:16:43 +08:00
|
|
|
if err := syscall.PivotRoot(rootfs, pivotDir); err != nil {
|
|
|
|
return fmt.Errorf("pivot_root %s", err)
|
|
|
|
}
|
|
|
|
if err := syscall.Chdir("/"); err != nil {
|
|
|
|
return fmt.Errorf("chdir / %s", err)
|
|
|
|
}
|
|
|
|
// path to pivot dir now changed, update
|
|
|
|
pivotDir = filepath.Join(pivotBaseDir, filepath.Base(pivotDir))
|
2015-10-02 05:03:02 +08:00
|
|
|
|
|
|
|
// Make pivotDir rprivate to make sure any of the unmounts don't
|
|
|
|
// propagate to parent.
|
|
|
|
if err := syscall.Mount("", pivotDir, "", syscall.MS_PRIVATE|syscall.MS_REC, ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-10 05:16:43 +08:00
|
|
|
if err := syscall.Unmount(pivotDir, syscall.MNT_DETACH); err != nil {
|
|
|
|
return fmt.Errorf("unmount pivot_root dir %s", err)
|
|
|
|
}
|
2016-02-24 19:31:07 +08:00
|
|
|
return nil
|
2015-02-10 05:16:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func msMoveRoot(rootfs string) error {
|
|
|
|
if err := syscall.Mount(rootfs, "/", "", syscall.MS_MOVE, ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := syscall.Chroot("."); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return syscall.Chdir("/")
|
|
|
|
}
|
2015-02-10 06:22:52 +08:00
|
|
|
|
2015-02-10 06:42:21 +08:00
|
|
|
// createIfNotExists creates a file or a directory only if it does not already exist.
|
2015-02-10 06:22:52 +08:00
|
|
|
func createIfNotExists(path string, isDir bool) error {
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if isDir {
|
2015-02-10 06:42:21 +08:00
|
|
|
return os.MkdirAll(path, 0755)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// remountReadonly will bind over the top of an existing path and ensure that it is read-only.
|
|
|
|
func remountReadonly(path string) error {
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
if err := syscall.Mount("", path, "", syscall.MS_REMOUNT|syscall.MS_RDONLY, ""); err != nil && !os.IsNotExist(err) {
|
|
|
|
switch err {
|
|
|
|
case syscall.EINVAL:
|
|
|
|
// Probably not a mountpoint, use bind-mount
|
|
|
|
if err := syscall.Mount(path, path, "", syscall.MS_BIND, ""); err != nil {
|
2015-02-10 06:22:52 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-02-10 06:42:21 +08:00
|
|
|
return syscall.Mount(path, path, "", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_REC|defaultMountFlags, "")
|
|
|
|
case syscall.EBUSY:
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
return err
|
2015-02-10 06:22:52 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-10 06:42:21 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("unable to mount %s as readonly max retries reached", path)
|
|
|
|
}
|
|
|
|
|
2015-02-13 08:23:05 +08:00
|
|
|
// maskFile bind mounts /dev/null over the top of the specified path inside a container
|
|
|
|
// to avoid security issues from processes reading information from non-namespace aware mounts ( proc/kcore ).
|
|
|
|
func maskFile(path string) error {
|
|
|
|
if err := syscall.Mount("/dev/null", path, "", syscall.MS_BIND, ""); err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
2015-02-10 06:22:52 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-23 10:17:30 +08:00
|
|
|
|
|
|
|
// writeSystemProperty writes the value to a path under /proc/sys as determined from the key.
|
|
|
|
// For e.g. net.ipv4.ip_forward translated to /proc/sys/net/ipv4/ip_forward.
|
|
|
|
func writeSystemProperty(key, value string) error {
|
|
|
|
keyPath := strings.Replace(key, ".", "/", -1)
|
|
|
|
return ioutil.WriteFile(path.Join("/proc/sys", keyPath), []byte(value), 0644)
|
|
|
|
}
|
2015-09-25 00:43:12 +08:00
|
|
|
|
|
|
|
func remount(m *configs.Mount, rootfs string) error {
|
|
|
|
var (
|
|
|
|
dest = m.Destination
|
|
|
|
)
|
|
|
|
if !strings.HasPrefix(dest, rootfs) {
|
|
|
|
dest = filepath.Join(rootfs, dest)
|
|
|
|
}
|
|
|
|
if err := syscall.Mount(m.Source, dest, m.Device, uintptr(m.Flags|syscall.MS_REMOUNT), ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the mount operation followed by additional mounts required to take care
|
|
|
|
// of propagation flags.
|
|
|
|
func mountPropagate(m *configs.Mount, rootfs string, mountLabel string) error {
|
|
|
|
var (
|
|
|
|
dest = m.Destination
|
|
|
|
data = label.FormatMountLabel(m.Data, mountLabel)
|
|
|
|
)
|
|
|
|
if !strings.HasPrefix(dest, rootfs) {
|
|
|
|
dest = filepath.Join(rootfs, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := syscall.Mount(m.Source, dest, m.Device, uintptr(m.Flags), data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pflag := range m.PropagationFlags {
|
|
|
|
if err := syscall.Mount("", dest, "", uintptr(pflag), ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|