-
Notifications
You must be signed in to change notification settings - Fork 0
/
rootfs.go
50 lines (40 loc) · 1.42 KB
/
rootfs.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
package lib
import (
"fmt"
"os"
"path/filepath"
"syscall"
)
func rootFSSetup(newRoot string) error {
putOld := "/old_root"
putOldAbsPath := filepath.Join(newRoot, putOld)
// Mount root file system as a mountpoint, then it can be used to pivot_root
if err := syscall.Mount(newRoot, newRoot, "", syscall.MS_BIND|syscall.MS_REC, ""); err != nil {
return fmt.Errorf("failed to mount new root filesystem %s: %w", newRoot, err)
}
// Create putOld directory if it doesn't exist already
if err := os.MkdirAll(putOldAbsPath, 0700); err != nil {
return fmt.Errorf("failed to mkdir %s: %w", putOldAbsPath, err)
}
// pivot_root to the newRoot
if err := syscall.PivotRoot(newRoot, putOldAbsPath); err != nil {
return fmt.Errorf("failed to pivot_root(%s, %s): %w", newRoot, putOldAbsPath, err)
}
// change root directory
if err := syscall.Chdir("/"); err != nil {
return fmt.Errorf("failed to chdir to /: %w", err)
}
// mount /proc
if err := syscall.Mount("proc", "/proc", "proc", 0, ""); err != nil {
return fmt.Errorf("failed to mount /proc: %w", err)
}
// unmount the old root filesystem
if err := syscall.Unmount(putOld, syscall.MNT_DETACH); err != nil {
return fmt.Errorf("failed to unmount the old root filesystem %s: %w", putOld, err)
}
// remove old root filesystem
if err := os.RemoveAll(putOld); err != nil {
return fmt.Errorf("failed to remove old root filesystem %s: %w", putOld, err)
}
return nil
}