2014-04-11 23:06:56 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
func PivotRoot(rootfs string) error {
|
|
|
|
pivotDir, err := ioutil.TempDir(rootfs, ".pivot_root")
|
|
|
|
if err != nil {
|
2014-06-12 13:52:31 +08:00
|
|
|
return fmt.Errorf("can't create pivot_root dir %s, error %v", pivotDir, err)
|
2014-04-11 23:06:56 +08:00
|
|
|
}
|
2014-07-15 08:00:05 +08:00
|
|
|
|
|
|
|
if err := syscall.PivotRoot(rootfs, pivotDir); err != nil {
|
2014-04-11 23:06:56 +08:00
|
|
|
return fmt.Errorf("pivot_root %s", err)
|
|
|
|
}
|
2014-07-15 08:00:05 +08:00
|
|
|
|
|
|
|
if err := syscall.Chdir("/"); err != nil {
|
2014-04-11 23:06:56 +08:00
|
|
|
return fmt.Errorf("chdir / %s", err)
|
|
|
|
}
|
2014-07-15 08:00:05 +08:00
|
|
|
|
2014-04-11 23:06:56 +08:00
|
|
|
// path to pivot dir now changed, update
|
|
|
|
pivotDir = filepath.Join("/", filepath.Base(pivotDir))
|
2014-07-15 08:00:05 +08:00
|
|
|
if err := syscall.Unmount(pivotDir, syscall.MNT_DETACH); err != nil {
|
2014-04-11 23:06:56 +08:00
|
|
|
return fmt.Errorf("unmount pivot_root dir %s", err)
|
|
|
|
}
|
2014-07-15 08:00:05 +08:00
|
|
|
|
2014-04-11 23:06:56 +08:00
|
|
|
return os.Remove(pivotDir)
|
|
|
|
}
|