From 20daff5e2c03dfda9dad5c26a5807522141bc931 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Mon, 9 Feb 2015 13:16:43 -0800 Subject: [PATCH] Move mount package into libcontainer root Signed-off-by: Michael Crosby --- mount/init.go => linux_rootfs.go | 70 ++++++++++++++++++++++++++++++-- linux_standard_init.go | 3 +- linux_userns_sidecar_init.go | 3 +- mount/msmoveroot.go | 15 ------- mount/pivotroot.go | 37 ----------------- mount/ptmx.go | 34 ---------------- mount/readonly.go | 11 ----- 7 files changed, 69 insertions(+), 104 deletions(-) rename mount/init.go => linux_rootfs.go (76%) delete mode 100644 mount/msmoveroot.go delete mode 100644 mount/pivotroot.go delete mode 100644 mount/ptmx.go delete mode 100644 mount/readonly.go diff --git a/mount/init.go b/linux_rootfs.go similarity index 76% rename from mount/init.go rename to linux_rootfs.go index a017ea51..2542dfe5 100644 --- a/mount/init.go +++ b/linux_rootfs.go @@ -1,14 +1,16 @@ // +build linux -package mount +package libcontainer import ( "fmt" + "io/ioutil" "os" "path/filepath" "syscall" "github.com/docker/libcontainer/configs" + "github.com/docker/libcontainer/console" "github.com/docker/libcontainer/label" ) @@ -22,9 +24,9 @@ type mount struct { data string } -// InitializeMountNamespace sets up the devices, mount points, and filesystems for use inside a +// setupRootfs sets up the devices, mount points, and filesystems for use inside a // new mount namespace. -func InitializeMountNamespace(config *configs.Config) (err error) { +func setupRootfs(config *configs.Config) (err error) { if err := prepareRoot(config); err != nil { return err } @@ -213,3 +215,65 @@ func prepareRoot(config *configs.Config) error { } return syscall.Mount(config.Rootfs, config.Rootfs, "bind", syscall.MS_BIND|syscall.MS_REC, "") } + +func setReadonly() error { + return syscall.Mount("/", "/", "bind", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_REC, "") +} + +func setupPtmx(config *configs.Config) error { + 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) + } + if config.Console != "" { + uid, err := config.HostUID() + if err != nil { + return err + } + gid, err := config.HostGID() + if err != nil { + return err + } + return console.Setup(config.Rootfs, config.Console, config.MountLabel, uid, gid) + } + return nil +} + +func pivotRoot(rootfs, pivotBaseDir string) error { + 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) + } + 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)) + if err := syscall.Unmount(pivotDir, syscall.MNT_DETACH); err != nil { + return fmt.Errorf("unmount pivot_root dir %s", err) + } + return os.Remove(pivotDir) +} + +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("/") +} diff --git a/linux_standard_init.go b/linux_standard_init.go index 2fe558e7..9b0ac76b 100644 --- a/linux_standard_init.go +++ b/linux_standard_init.go @@ -9,7 +9,6 @@ import ( "github.com/docker/libcontainer/configs" consolepkg "github.com/docker/libcontainer/console" "github.com/docker/libcontainer/label" - "github.com/docker/libcontainer/mount" "github.com/docker/libcontainer/security/restrict" "github.com/docker/libcontainer/system" ) @@ -49,7 +48,7 @@ func (l *linuxStandardInit) Init() error { label.Init() // InitializeMountNamespace() can be executed only for a new mount namespace if l.config.Config.Namespaces.Contains(configs.NEWNS) { - if err := mount.InitializeMountNamespace(l.config.Config); err != nil { + if err := setupRootfs(l.config.Config); err != nil { return err } } diff --git a/linux_userns_sidecar_init.go b/linux_userns_sidecar_init.go index 514b9dd4..24dea9ef 100644 --- a/linux_userns_sidecar_init.go +++ b/linux_userns_sidecar_init.go @@ -5,7 +5,6 @@ package libcontainer import ( "github.com/docker/libcontainer/configs" "github.com/docker/libcontainer/label" - "github.com/docker/libcontainer/mount" ) // linuxUsernsSideCar is run to setup mounts and networking related operations @@ -28,7 +27,7 @@ func (l *linuxUsernsSideCar) Init() error { label.Init() // InitializeMountNamespace() can be executed only for a new mount namespace if l.config.Config.Namespaces.Contains(configs.NEWNET) { - if err := mount.InitializeMountNamespace(l.config.Config); err != nil { + if err := setupRootfs(l.config.Config); err != nil { return err } } diff --git a/mount/msmoveroot.go b/mount/msmoveroot.go deleted file mode 100644 index 17b73293..00000000 --- a/mount/msmoveroot.go +++ /dev/null @@ -1,15 +0,0 @@ -// +build linux - -package mount - -import "syscall" - -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("/") -} diff --git a/mount/pivotroot.go b/mount/pivotroot.go deleted file mode 100644 index 3d422774..00000000 --- a/mount/pivotroot.go +++ /dev/null @@ -1,37 +0,0 @@ -// +build linux - -package mount - -import ( - "fmt" - "io/ioutil" - "os" - "path/filepath" - "syscall" -) - -func pivotRoot(rootfs, pivotBaseDir string) error { - 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) - } - 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)) - if err := syscall.Unmount(pivotDir, syscall.MNT_DETACH); err != nil { - return fmt.Errorf("unmount pivot_root dir %s", err) - } - return os.Remove(pivotDir) -} diff --git a/mount/ptmx.go b/mount/ptmx.go deleted file mode 100644 index 278f7299..00000000 --- a/mount/ptmx.go +++ /dev/null @@ -1,34 +0,0 @@ -// +build linux - -package mount - -import ( - "fmt" - "os" - "path/filepath" - - "github.com/docker/libcontainer/configs" - "github.com/docker/libcontainer/console" -) - -func setupPtmx(config *configs.Config) error { - 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) - } - if config.Console != "" { - uid, err := config.HostUID() - if err != nil { - return err - } - gid, err := config.HostGID() - if err != nil { - return err - } - return console.Setup(config.Rootfs, config.Console, config.MountLabel, uid, gid) - } - return nil -} diff --git a/mount/readonly.go b/mount/readonly.go deleted file mode 100644 index 855c9f99..00000000 --- a/mount/readonly.go +++ /dev/null @@ -1,11 +0,0 @@ -// +build linux - -package mount - -import ( - "syscall" -) - -func setReadonly() error { - return syscall.Mount("/", "/", "bind", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_REC, "") -}