github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/graphdriver/overlay2/check.go (about) 1 // +build linux 2 3 package overlay2 // import "github.com/demonoid81/moby/daemon/graphdriver/overlay2" 4 5 import ( 6 "fmt" 7 "io/ioutil" 8 "os" 9 "path" 10 "path/filepath" 11 "syscall" 12 13 "github.com/demonoid81/moby/pkg/system" 14 "github.com/pkg/errors" 15 "golang.org/x/sys/unix" 16 ) 17 18 // doesSupportNativeDiff checks whether the filesystem has a bug 19 // which copies up the opaque flag when copying up an opaque 20 // directory or the kernel enable CONFIG_OVERLAY_FS_REDIRECT_DIR. 21 // When these exist naive diff should be used. 22 func doesSupportNativeDiff(d string) error { 23 td, err := ioutil.TempDir(d, "opaque-bug-check") 24 if err != nil { 25 return err 26 } 27 defer func() { 28 if err := os.RemoveAll(td); err != nil { 29 logger.Warnf("Failed to remove check directory %v: %v", td, err) 30 } 31 }() 32 33 // Make directories l1/d, l1/d1, l2/d, l3, work, merged 34 if err := os.MkdirAll(filepath.Join(td, "l1", "d"), 0755); err != nil { 35 return err 36 } 37 if err := os.MkdirAll(filepath.Join(td, "l1", "d1"), 0755); err != nil { 38 return err 39 } 40 if err := os.MkdirAll(filepath.Join(td, "l2", "d"), 0755); err != nil { 41 return err 42 } 43 if err := os.Mkdir(filepath.Join(td, "l3"), 0755); err != nil { 44 return err 45 } 46 if err := os.Mkdir(filepath.Join(td, workDirName), 0755); err != nil { 47 return err 48 } 49 if err := os.Mkdir(filepath.Join(td, mergedDirName), 0755); err != nil { 50 return err 51 } 52 53 // Mark l2/d as opaque 54 if err := system.Lsetxattr(filepath.Join(td, "l2", "d"), "trusted.overlay.opaque", []byte("y"), 0); err != nil { 55 return errors.Wrap(err, "failed to set opaque flag on middle layer") 56 } 57 58 opts := fmt.Sprintf("lowerdir=%s:%s,upperdir=%s,workdir=%s", path.Join(td, "l2"), path.Join(td, "l1"), path.Join(td, "l3"), path.Join(td, workDirName)) 59 if err := unix.Mount("overlay", filepath.Join(td, mergedDirName), "overlay", 0, opts); err != nil { 60 return errors.Wrap(err, "failed to mount overlay") 61 } 62 defer func() { 63 if err := unix.Unmount(filepath.Join(td, mergedDirName), 0); err != nil { 64 logger.Warnf("Failed to unmount check directory %v: %v", filepath.Join(td, mergedDirName), err) 65 } 66 }() 67 68 // Touch file in d to force copy up of opaque directory "d" from "l2" to "l3" 69 if err := ioutil.WriteFile(filepath.Join(td, mergedDirName, "d", "f"), []byte{}, 0644); err != nil { 70 return errors.Wrap(err, "failed to write to merged directory") 71 } 72 73 // Check l3/d does not have opaque flag 74 xattrOpaque, err := system.Lgetxattr(filepath.Join(td, "l3", "d"), "trusted.overlay.opaque") 75 if err != nil { 76 return errors.Wrap(err, "failed to read opaque flag on upper layer") 77 } 78 if string(xattrOpaque) == "y" { 79 return errors.New("opaque flag erroneously copied up, consider update to kernel 4.8 or later to fix") 80 } 81 82 // rename "d1" to "d2" 83 if err := os.Rename(filepath.Join(td, mergedDirName, "d1"), filepath.Join(td, mergedDirName, "d2")); err != nil { 84 // if rename failed with syscall.EXDEV, the kernel doesn't have CONFIG_OVERLAY_FS_REDIRECT_DIR enabled 85 if err.(*os.LinkError).Err == syscall.EXDEV { 86 return nil 87 } 88 return errors.Wrap(err, "failed to rename dir in merged directory") 89 } 90 // get the xattr of "d2" 91 xattrRedirect, err := system.Lgetxattr(filepath.Join(td, "l3", "d2"), "trusted.overlay.redirect") 92 if err != nil { 93 return errors.Wrap(err, "failed to read redirect flag on upper layer") 94 } 95 96 if string(xattrRedirect) == "d1" { 97 return errors.New("kernel has CONFIG_OVERLAY_FS_REDIRECT_DIR enabled") 98 } 99 100 return nil 101 }