2015-09-23 04:56:29 +00:00
|
|
|
package allocdir
|
|
|
|
|
|
|
|
import (
|
2016-02-08 22:11:53 +00:00
|
|
|
"fmt"
|
2015-09-23 04:56:29 +00:00
|
|
|
"os"
|
2016-02-08 22:11:53 +00:00
|
|
|
"path/filepath"
|
2015-09-23 04:56:29 +00:00
|
|
|
"syscall"
|
2016-02-08 22:11:53 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
2015-09-23 04:56:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Bind mounts the shared directory into the task directory. Must be root to
|
|
|
|
// run.
|
|
|
|
func (d *AllocDir) mountSharedDir(taskDir string) error {
|
2015-11-10 22:12:30 +00:00
|
|
|
if err := os.MkdirAll(taskDir, 0777); err != nil {
|
2015-09-23 04:56:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
return syscall.Mount(d.SharedDir, taskDir, "", syscall.MS_BIND, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AllocDir) unmountSharedDir(dir string) error {
|
|
|
|
return syscall.Unmount(dir, 0)
|
2015-09-23 04:56:29 +00:00
|
|
|
}
|
2016-02-08 22:11:53 +00:00
|
|
|
|
|
|
|
// MountSpecialDirs mounts the dev and proc file system from the host to the
|
|
|
|
// chroot
|
|
|
|
func (d *AllocDir) MountSpecialDirs(taskDir string) error {
|
|
|
|
// Mount dev
|
|
|
|
dev := filepath.Join(taskDir, "dev")
|
|
|
|
if !d.pathExists(dev) {
|
2016-03-28 21:33:53 +00:00
|
|
|
if err := os.MkdirAll(dev, 0777); err != nil {
|
2016-02-08 22:11:53 +00:00
|
|
|
return fmt.Errorf("Mkdir(%v) failed: %v", dev, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := syscall.Mount("none", dev, "devtmpfs", syscall.MS_RDONLY, ""); err != nil {
|
|
|
|
return fmt.Errorf("Couldn't mount /dev to %v: %v", dev, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mount proc
|
|
|
|
proc := filepath.Join(taskDir, "proc")
|
|
|
|
if !d.pathExists(proc) {
|
2016-03-28 21:33:53 +00:00
|
|
|
if err := os.MkdirAll(proc, 0777); err != nil {
|
2016-02-08 22:11:53 +00:00
|
|
|
return fmt.Errorf("Mkdir(%v) failed: %v", proc, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := syscall.Mount("none", proc, "proc", syscall.MS_RDONLY, ""); err != nil {
|
|
|
|
return fmt.Errorf("Couldn't mount /proc to %v: %v", proc, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-09 02:51:11 +00:00
|
|
|
// unmountSpecialDirs unmounts the dev and proc file system from the chroot
|
|
|
|
func (d *AllocDir) unmountSpecialDirs(taskDir string) error {
|
2016-02-08 22:11:53 +00:00
|
|
|
errs := new(multierror.Error)
|
|
|
|
dev := filepath.Join(taskDir, "dev")
|
|
|
|
if d.pathExists(dev) {
|
|
|
|
if err := syscall.Unmount(dev, 0); err != nil {
|
|
|
|
errs = multierror.Append(errs, fmt.Errorf("Failed to unmount dev (%v): %v", dev, err))
|
2016-05-10 21:57:16 +00:00
|
|
|
} else if err := os.RemoveAll(dev); err != nil {
|
2016-02-08 22:11:53 +00:00
|
|
|
errs = multierror.Append(errs, fmt.Errorf("Failed to delete dev directory (%v): %v", dev, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmount proc.
|
|
|
|
proc := filepath.Join(taskDir, "proc")
|
|
|
|
if d.pathExists(proc) {
|
|
|
|
if err := syscall.Unmount(proc, 0); err != nil {
|
|
|
|
errs = multierror.Append(errs, fmt.Errorf("Failed to unmount proc (%v): %v", proc, err))
|
2016-05-10 21:57:16 +00:00
|
|
|
} else if err := os.RemoveAll(proc); err != nil {
|
2016-02-08 22:11:53 +00:00
|
|
|
errs = multierror.Append(errs, fmt.Errorf("Failed to delete proc directory (%v): %v", dev, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs.ErrorOrNil()
|
|
|
|
}
|