Skip to content

Commit 2e2cdd4

Browse files
coolljt0725runcom
authored andcommitted
Fallback to use naive diff driver if enable CONFIG_OVERLAY_FS_REDIRECT_DIR
When use overlay2 as the graphdriver and the kernel enable `CONFIG_OVERLAY_FS_REDIRECT_DIR=y`, rename a dir in lower layer will has a xattr to redirct its dir to source dir. This make the image layer unportable. This patch fallback to use naive diff driver when kernel enable CONFIG_OVERLAY_FS_REDIRECT_DIR Signed-off-by: Lei Jitang <leijitang@huawei.com>
1 parent a98877b commit 2e2cdd4

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

daemon/graphdriver/overlay2/check.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import (
1515
"github.com/pkg/errors"
1616
)
1717

18-
// hasOpaqueCopyUpBug checks whether the filesystem has a bug
18+
// doesSupportNativeDiff checks whether the filesystem has a bug
1919
// which copies up the opaque flag when copying up an opaque
20-
// directory. When this bug exists naive diff should be used.
21-
func hasOpaqueCopyUpBug(d string) error {
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 {
2223
td, err := ioutil.TempDir(d, "opaque-bug-check")
2324
if err != nil {
2425
return err
@@ -29,10 +30,13 @@ func hasOpaqueCopyUpBug(d string) error {
2930
}
3031
}()
3132

32-
// Make directories l1/d, l2/d, l3, work, merged
33+
// Make directories l1/d, l1/d1, l2/d, l3, work, merged
3334
if err := os.MkdirAll(filepath.Join(td, "l1", "d"), 0755); err != nil {
3435
return err
3536
}
37+
if err := os.MkdirAll(filepath.Join(td, "l1", "d1"), 0755); err != nil {
38+
return err
39+
}
3640
if err := os.MkdirAll(filepath.Join(td, "l2", "d"), 0755); err != nil {
3741
return err
3842
}
@@ -75,5 +79,23 @@ func hasOpaqueCopyUpBug(d string) error {
7579
return errors.New("opaque flag erroneously copied up, consider update to kernel 4.8 or later to fix")
7680
}
7781

82+
// rename "d1" to "d2"
83+
if err := os.Rename(filepath.Join(td, "merged", "d1"), filepath.Join(td, "merged", "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+
78100
return nil
79101
}

daemon/graphdriver/overlay2/overlay.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ func supportsOverlay() error {
234234

235235
func useNaiveDiff(home string) bool {
236236
useNaiveDiffLock.Do(func() {
237-
if err := hasOpaqueCopyUpBug(home); err != nil {
238-
logrus.Warnf("Not using native diff for overlay2: %v", err)
237+
if err := doesSupportNativeDiff(home); err != nil {
238+
logrus.Warnf("Not using native diff for overlay2, this may cause degraded performance for building images: %v", err)
239239
useNaiveDiffOnly = true
240240
}
241241
})

0 commit comments

Comments
 (0)