From 203d3e258e617a3f326b6348d83d7246ccb1c4a8 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Thu, 24 Sep 2015 09:43:12 -0700 Subject: [PATCH] Move mount methods out of configs pkg Do not have methods and actions that require syscalls in the configs package because it breaks cross compile. Signed-off-by: Michael Crosby --- libcontainer/configs/mount.go | 45 ----------------------------- libcontainer/rootfs_linux.go | 53 +++++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 53 deletions(-) diff --git a/libcontainer/configs/mount.go b/libcontainer/configs/mount.go index f982da14..cc770c91 100644 --- a/libcontainer/configs/mount.go +++ b/libcontainer/configs/mount.go @@ -1,13 +1,5 @@ package configs -import ( - "path/filepath" - "strings" - "syscall" - - "github.com/opencontainers/runc/libcontainer/label" -) - type Mount struct { // Source path for the mount. Source string `json:"source"` @@ -36,40 +28,3 @@ type Mount struct { // Optional Command to be run after Source is mounted. PostmountCmds []Command `json:"postmount_cmds"` } - -func (m *Mount) Remount(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 (m *Mount) MountPropagate(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 -} diff --git a/libcontainer/rootfs_linux.go b/libcontainer/rootfs_linux.go index c38b80ad..3e3a7d2e 100644 --- a/libcontainer/rootfs_linux.go +++ b/libcontainer/rootfs_linux.go @@ -106,12 +106,12 @@ func mountToRootfs(m *configs.Mount, rootfs, mountLabel string) error { if err := os.MkdirAll(dest, 0755); err != nil { return err } - return m.MountPropagate(rootfs, mountLabel) + return mountPropagate(m, rootfs, mountLabel) case "mqueue": if err := os.MkdirAll(dest, 0755); err != nil { return err } - if err := m.MountPropagate(rootfs, mountLabel); err != nil { + if err := mountPropagate(m, rootfs, mountLabel); err != nil { return err } return label.SetFileLabel(dest, mountLabel) @@ -122,7 +122,7 @@ func mountToRootfs(m *configs.Mount, rootfs, mountLabel string) error { return err } } - if err := m.MountPropagate(rootfs, mountLabel); err != nil { + if err := mountPropagate(m, rootfs, mountLabel); err != nil { return err } if stat != nil { @@ -135,12 +135,12 @@ func mountToRootfs(m *configs.Mount, rootfs, mountLabel string) error { if err := os.MkdirAll(dest, 0755); err != nil { return err } - return m.MountPropagate(rootfs, mountLabel) + return mountPropagate(m, rootfs, mountLabel) case "securityfs": if err := os.MkdirAll(dest, 0755); err != nil { return err } - return m.MountPropagate(rootfs, mountLabel) + return mountPropagate(m, rootfs, mountLabel) case "bind": stat, err := os.Stat(m.Source) if err != nil { @@ -163,11 +163,11 @@ func mountToRootfs(m *configs.Mount, rootfs, mountLabel string) error { if err := createIfNotExists(dest, stat.IsDir()); err != nil { return err } - if err := m.MountPropagate(rootfs, mountLabel); err != nil { + if err := mountPropagate(m, rootfs, mountLabel); err != nil { return err } // bind mount won't change mount options, we need remount to make mount options effective. - if err := m.Remount(rootfs); err != nil { + if err := remount(m, rootfs); err != nil { return err } if m.Relabel != "" { @@ -236,7 +236,7 @@ func mountToRootfs(m *configs.Mount, rootfs, mountLabel string) error { Destination: m.Destination, Flags: defaultMountFlags | syscall.MS_RDONLY, } - if err := mcgrouproot.Remount(rootfs); err != nil { + if err := remount(mcgrouproot, rootfs); err != nil { return err } } @@ -534,3 +534,40 @@ func writeSystemProperty(key, value string) error { keyPath := strings.Replace(key, ".", "/", -1) return ioutil.WriteFile(path.Join("/proc/sys", keyPath), []byte(value), 0644) } + +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 +}