-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmount.go
64 lines (52 loc) · 1.16 KB
/
mount.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"os"
"path/filepath"
"syscall"
)
func pivotRoot(newroot string) error {
putold := filepath.Join(newroot, "/oldrootfs")
// pivot_rootの条件を満たすために、新たなrootで自分自身をバインドマウント
if err := syscall.Mount(
newroot,
newroot,
"",
syscall.MS_BIND|syscall.MS_REC,
"",
); err != nil {
return err
}
if err := os.MkdirAll(putold, 0700); err != nil {
return err
}
if err := syscall.PivotRoot(newroot, putold); err != nil {
return err
}
if err := os.Chdir("/"); err != nil {
return err
}
putold = "/oldrootfs"
if err := syscall.Unmount(putold, syscall.MNT_DETACH); err != nil {
return err
}
if err := os.RemoveAll(putold); err != nil {
return err
}
return nil
}
func mountProc(newroot string) error {
target := filepath.Join(newroot, "/proc")
os.MkdirAll(target, 0755)
if err := syscall.Mount("proc", target, "proc", uintptr(0), ""); err != nil {
return err
}
return nil
}
func exitIfRootfsNotFound(rootfsPath string) {
if _, err := os.Stat(rootfsPath); os.IsNotExist(err) {
errorMsg := fmt.Sprint("rootfsPath not set")
fmt.Println(errorMsg)
os.Exit(1)
}
}