MCPcopy Index your code
hub / github.com/linuxkit/linuxkit / copyFS

Function copyFS

pkg/init/cmd/init/init.go:41–201  ·  view source on GitHub ↗
(newRoot string)

Source from the content-addressed store, hash-verified

39}
40
41func copyFS(newRoot string) error {
42 // find the device of the root filesystem so we can avoid changing filesystem
43 info, err := os.Stat("/")
44 if err != nil {
45 return err
46 }
47 stat := info.Sys().(*syscall.Stat_t)
48 rootDev := stat.Dev
49
50 if err = unix.Mount("rootfs", newRoot, "tmpfs", 0, ""); err != nil {
51 return err
52 }
53
54 // copy directory tree first
55 if err := filepath.Walk("/", func(path string, info os.FileInfo, err error) error {
56 if err != nil {
57 return err
58 }
59 // skip non directories
60 if !info.Mode().IsDir() {
61 return nil
62 }
63 dest := filepath.Join(newRoot, path)
64 // create the directory
65 if path == "/" {
66 // the mountpoint already exists but may have wrong mode, metadata
67 if err := os.Chmod(newRoot, info.Mode()); err != nil {
68 return err
69 }
70 } else {
71 if err := os.Mkdir(dest, info.Mode()); err != nil {
72 return err
73 }
74 }
75 if err := copyMetadata(info, dest); err != nil {
76 return err
77 }
78 // skip recurse into other filesystems
79 stat := info.Sys().(*syscall.Stat_t)
80 if rootDev != stat.Dev {
81 return filepath.SkipDir
82 }
83 return nil
84 }); err != nil {
85 return err
86 }
87
88 buf := make([]byte, 32768)
89
90 // now move files
91 if err := filepath.Walk("/", func(path string, info os.FileInfo, err error) error {
92 if err != nil {
93 return err
94 }
95 // skip other filesystems
96 stat := info.Sys().(*syscall.Stat_t)
97 if rootDev != stat.Dev && info.Mode().IsDir() {
98 return filepath.SkipDir

Callers 1

mainFunction · 0.85

Calls 5

copyMetadataFunction · 0.85
RemoveMethod · 0.80
OpenMethod · 0.65
SymlinkMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected