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>
This commit is contained in:
Lei Jitang 2017-08-01 08:05:27 -04:00
parent 1df67a5c36
commit 49c3a7c4ba
2 changed files with 29 additions and 6 deletions

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"syscall"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/system" "github.com/docker/docker/pkg/system"
@ -15,10 +16,11 @@ import (
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
// hasOpaqueCopyUpBug checks whether the filesystem has a bug // doesSupportNativeDiff checks whether the filesystem has a bug
// which copies up the opaque flag when copying up an opaque // which copies up the opaque flag when copying up an opaque
// directory. When this bug exists naive diff should be used. // directory or the kernel enable CONFIG_OVERLAY_FS_REDIRECT_DIR.
func hasOpaqueCopyUpBug(d string) error { // When these exist naive diff should be used.
func doesSupportNativeDiff(d string) error {
td, err := ioutil.TempDir(d, "opaque-bug-check") td, err := ioutil.TempDir(d, "opaque-bug-check")
if err != nil { if err != nil {
return err return err
@ -29,10 +31,13 @@ func hasOpaqueCopyUpBug(d string) error {
} }
}() }()
// Make directories l1/d, l2/d, l3, work, merged // Make directories l1/d, l1/d1, l2/d, l3, work, merged
if err := os.MkdirAll(filepath.Join(td, "l1", "d"), 0755); err != nil { if err := os.MkdirAll(filepath.Join(td, "l1", "d"), 0755); err != nil {
return err return err
} }
if err := os.MkdirAll(filepath.Join(td, "l1", "d1"), 0755); err != nil {
return err
}
if err := os.MkdirAll(filepath.Join(td, "l2", "d"), 0755); err != nil { if err := os.MkdirAll(filepath.Join(td, "l2", "d"), 0755); err != nil {
return err return err
} }
@ -75,5 +80,23 @@ func hasOpaqueCopyUpBug(d string) error {
return errors.New("opaque flag erroneously copied up, consider update to kernel 4.8 or later to fix") return errors.New("opaque flag erroneously copied up, consider update to kernel 4.8 or later to fix")
} }
// rename "d1" to "d2"
if err := os.Rename(filepath.Join(td, "merged", "d1"), filepath.Join(td, "merged", "d2")); err != nil {
// if rename failed with syscall.EXDEV, the kernel doesn't have CONFIG_OVERLAY_FS_REDIRECT_DIR enabled
if err.(*os.LinkError).Err == syscall.EXDEV {
return nil
}
return errors.Wrap(err, "failed to rename dir in merged directory")
}
// get the xattr of "d2"
xattrRedirect, err := system.Lgetxattr(filepath.Join(td, "l3", "d2"), "trusted.overlay.redirect")
if err != nil {
return errors.Wrap(err, "failed to read redirect flag on upper layer")
}
if string(xattrRedirect) == "d1" {
return errors.New("kernel has CONFIG_OVERLAY_FS_REDIRECT_DIR enabled")
}
return nil return nil
} }

View File

@ -266,8 +266,8 @@ func supportsOverlay() error {
func useNaiveDiff(home string) bool { func useNaiveDiff(home string) bool {
useNaiveDiffLock.Do(func() { useNaiveDiffLock.Do(func() {
if err := hasOpaqueCopyUpBug(home); err != nil { if err := doesSupportNativeDiff(home); err != nil {
logrus.Warnf("Not using native diff for overlay2: %v", err) logrus.Warnf("Not using native diff for overlay2, this may cause degraded performance for building images: %v", err)
useNaiveDiffOnly = true useNaiveDiffOnly = true
} }
}) })