2016-11-30 00:08:30 +00:00
|
|
|
package containerd
|
2016-09-27 01:15:19 +00:00
|
|
|
|
2016-09-27 04:35:34 +00:00
|
|
|
import (
|
|
|
|
"strings"
|
2016-12-09 19:27:30 +00:00
|
|
|
"syscall"
|
2017-02-01 02:04:13 +00:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/containerd/log"
|
2016-09-27 04:35:34 +00:00
|
|
|
)
|
|
|
|
|
2016-12-14 14:34:25 +00:00
|
|
|
// Mount is the lingua franca of containerd. A mount represents a
|
2016-09-27 01:15:19 +00:00
|
|
|
// serialized mount syscall. Components either emit or consume mounts.
|
|
|
|
type Mount struct {
|
|
|
|
// Type specifies the host-specific of the mount.
|
|
|
|
Type string
|
|
|
|
// Source specifies where to mount from. Depending on the host system, this
|
|
|
|
// can be a source path or device.
|
|
|
|
Source string
|
|
|
|
// Options contains zero or more fstab-style mount options. Typically,
|
|
|
|
// these are platform specific.
|
|
|
|
Options []string
|
|
|
|
}
|
2016-09-27 04:35:34 +00:00
|
|
|
|
2017-01-17 21:26:56 +00:00
|
|
|
func (m *Mount) Mount(target string) error {
|
|
|
|
flags, data := parseMountOptions(m.Options)
|
2017-02-01 02:04:13 +00:00
|
|
|
|
|
|
|
lfields := logrus.Fields{"target": target, "source": m.Source}
|
|
|
|
if data != "" {
|
|
|
|
lfields["data"] = data
|
|
|
|
}
|
|
|
|
log.L.WithFields(lfields).Debug("syscall.Mount")
|
|
|
|
|
2017-01-17 21:26:56 +00:00
|
|
|
return syscall.Mount(m.Source, target, m.Type, uintptr(flags), data)
|
2016-09-27 04:35:34 +00:00
|
|
|
}
|
2016-12-09 19:27:30 +00:00
|
|
|
|
2017-01-17 21:26:56 +00:00
|
|
|
// MountAll mounts all the provided mounts to the provided target
|
|
|
|
func MountAll(mounts []Mount, target string) error {
|
2016-12-09 19:27:30 +00:00
|
|
|
for _, m := range mounts {
|
2017-01-17 21:26:56 +00:00
|
|
|
if err := m.Mount(target); err != nil {
|
2016-12-09 19:27:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseMountOptions takes fstab style mount options and parses them for
|
|
|
|
// use with a standard mount() syscall
|
|
|
|
func parseMountOptions(options []string) (int, string) {
|
|
|
|
var (
|
|
|
|
flag int
|
|
|
|
data []string
|
|
|
|
)
|
|
|
|
flags := map[string]struct {
|
|
|
|
clear bool
|
|
|
|
flag int
|
|
|
|
}{
|
|
|
|
"async": {true, syscall.MS_SYNCHRONOUS},
|
|
|
|
"atime": {true, syscall.MS_NOATIME},
|
|
|
|
"bind": {false, syscall.MS_BIND},
|
|
|
|
"defaults": {false, 0},
|
|
|
|
"dev": {true, syscall.MS_NODEV},
|
|
|
|
"diratime": {true, syscall.MS_NODIRATIME},
|
|
|
|
"dirsync": {false, syscall.MS_DIRSYNC},
|
|
|
|
"exec": {true, syscall.MS_NOEXEC},
|
|
|
|
"mand": {false, syscall.MS_MANDLOCK},
|
|
|
|
"noatime": {false, syscall.MS_NOATIME},
|
|
|
|
"nodev": {false, syscall.MS_NODEV},
|
|
|
|
"nodiratime": {false, syscall.MS_NODIRATIME},
|
|
|
|
"noexec": {false, syscall.MS_NOEXEC},
|
|
|
|
"nomand": {true, syscall.MS_MANDLOCK},
|
|
|
|
"norelatime": {true, syscall.MS_RELATIME},
|
|
|
|
"nostrictatime": {true, syscall.MS_STRICTATIME},
|
|
|
|
"nosuid": {false, syscall.MS_NOSUID},
|
|
|
|
"rbind": {false, syscall.MS_BIND | syscall.MS_REC},
|
|
|
|
"relatime": {false, syscall.MS_RELATIME},
|
|
|
|
"remount": {false, syscall.MS_REMOUNT},
|
|
|
|
"ro": {false, syscall.MS_RDONLY},
|
|
|
|
"rw": {true, syscall.MS_RDONLY},
|
|
|
|
"strictatime": {false, syscall.MS_STRICTATIME},
|
|
|
|
"suid": {true, syscall.MS_NOSUID},
|
|
|
|
"sync": {false, syscall.MS_SYNCHRONOUS},
|
|
|
|
}
|
|
|
|
for _, o := range options {
|
|
|
|
// If the option does not exist in the flags table or the flag
|
|
|
|
// is not supported on the platform,
|
|
|
|
// then it is a data value for a specific fs type
|
|
|
|
if f, exists := flags[o]; exists && f.flag != 0 {
|
|
|
|
if f.clear {
|
|
|
|
flag &= ^f.flag
|
|
|
|
} else {
|
|
|
|
flag |= f.flag
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data = append(data, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flag, strings.Join(data, ",")
|
|
|
|
}
|