2016-11-09 16:07:56 -05:00
|
|
|
// +build linux
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package overlay2 // import "github.com/docker/docker/daemon/graphdriver/overlay2"
|
2016-11-09 16:07:56 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2017-08-01 08:05:27 -04:00
|
|
|
"syscall"
|
2016-11-09 16:07:56 -05:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/system"
|
|
|
|
"github.com/pkg/errors"
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2016-11-09 16:07:56 -05:00
|
|
|
)
|
|
|
|
|
2017-08-01 08:05:27 -04:00
|
|
|
// doesSupportNativeDiff checks whether the filesystem has a bug
|
2016-11-09 16:07:56 -05:00
|
|
|
// which copies up the opaque flag when copying up an opaque
|
2017-08-01 08:05:27 -04:00
|
|
|
// directory or the kernel enable CONFIG_OVERLAY_FS_REDIRECT_DIR.
|
|
|
|
// When these exist naive diff should be used.
|
|
|
|
func doesSupportNativeDiff(d string) error {
|
2016-11-09 16:07:56 -05:00
|
|
|
td, err := ioutil.TempDir(d, "opaque-bug-check")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(td); err != nil {
|
2018-10-11 15:50:45 -04:00
|
|
|
logger.Warnf("Failed to remove check directory %v: %v", td, err)
|
2016-11-09 16:07:56 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-08-01 08:05:27 -04:00
|
|
|
// Make directories l1/d, l1/d1, l2/d, l3, work, merged
|
2016-11-09 16:07:56 -05:00
|
|
|
if err := os.MkdirAll(filepath.Join(td, "l1", "d"), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-01 08:05:27 -04:00
|
|
|
if err := os.MkdirAll(filepath.Join(td, "l1", "d1"), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-09 16:07:56 -05:00
|
|
|
if err := os.MkdirAll(filepath.Join(td, "l2", "d"), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Mkdir(filepath.Join(td, "l3"), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-28 23:48:04 -04:00
|
|
|
if err := os.Mkdir(filepath.Join(td, workDirName), 0755); err != nil {
|
2016-11-09 16:07:56 -05:00
|
|
|
return err
|
|
|
|
}
|
2019-03-28 23:48:04 -04:00
|
|
|
if err := os.Mkdir(filepath.Join(td, mergedDirName), 0755); err != nil {
|
2016-11-09 16:07:56 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark l2/d as opaque
|
|
|
|
if err := system.Lsetxattr(filepath.Join(td, "l2", "d"), "trusted.overlay.opaque", []byte("y"), 0); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to set opaque flag on middle layer")
|
|
|
|
}
|
|
|
|
|
2019-03-28 23:48:04 -04:00
|
|
|
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))
|
|
|
|
if err := unix.Mount("overlay", filepath.Join(td, mergedDirName), "overlay", 0, opts); err != nil {
|
2016-11-09 16:07:56 -05:00
|
|
|
return errors.Wrap(err, "failed to mount overlay")
|
|
|
|
}
|
|
|
|
defer func() {
|
2019-03-28 23:48:04 -04:00
|
|
|
if err := unix.Unmount(filepath.Join(td, mergedDirName), 0); err != nil {
|
|
|
|
logger.Warnf("Failed to unmount check directory %v: %v", filepath.Join(td, mergedDirName), err)
|
2016-11-09 16:07:56 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Touch file in d to force copy up of opaque directory "d" from "l2" to "l3"
|
2019-03-28 23:48:04 -04:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(td, mergedDirName, "d", "f"), []byte{}, 0644); err != nil {
|
2016-11-09 16:07:56 -05:00
|
|
|
return errors.Wrap(err, "failed to write to merged directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check l3/d does not have opaque flag
|
|
|
|
xattrOpaque, err := system.Lgetxattr(filepath.Join(td, "l3", "d"), "trusted.overlay.opaque")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to read opaque flag on upper layer")
|
|
|
|
}
|
|
|
|
if string(xattrOpaque) == "y" {
|
|
|
|
return errors.New("opaque flag erroneously copied up, consider update to kernel 4.8 or later to fix")
|
|
|
|
}
|
|
|
|
|
2017-08-01 08:05:27 -04:00
|
|
|
// rename "d1" to "d2"
|
2019-03-28 23:48:04 -04:00
|
|
|
if err := os.Rename(filepath.Join(td, mergedDirName, "d1"), filepath.Join(td, mergedDirName, "d2")); err != nil {
|
2017-08-01 08:05:27 -04:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:07:56 -05:00
|
|
|
return nil
|
|
|
|
}
|
2017-11-16 11:34:33 -05:00
|
|
|
|
|
|
|
// supportsMultipleLowerDir checks if the system supports multiple lowerdirs,
|
|
|
|
// which is required for the overlay2 driver. On 4.x kernels, multiple lowerdirs
|
|
|
|
// are always available (so this check isn't needed), and backported to RHEL and
|
|
|
|
// CentOS 3.x kernels (3.10.0-693.el7.x86_64 and up). This function is to detect
|
|
|
|
// support on those kernels, without doing a kernel version compare.
|
|
|
|
func supportsMultipleLowerDir(d string) error {
|
|
|
|
td, err := ioutil.TempDir(d, "multiple-lowerdir-check")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(td); err != nil {
|
2018-10-11 15:50:45 -04:00
|
|
|
logger.Warnf("Failed to remove check directory %v: %v", td, err)
|
2017-11-16 11:34:33 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-03-28 23:48:04 -04:00
|
|
|
for _, dir := range []string{"lower1", "lower2", "upper", workDirName, mergedDirName} {
|
2017-11-16 11:34:33 -05:00
|
|
|
if err := os.Mkdir(filepath.Join(td, dir), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 23:48:04 -04:00
|
|
|
opts := fmt.Sprintf("lowerdir=%s:%s,upperdir=%s,workdir=%s", path.Join(td, "lower2"), path.Join(td, "lower1"), path.Join(td, "upper"), path.Join(td, workDirName))
|
|
|
|
if err := unix.Mount("overlay", filepath.Join(td, mergedDirName), "overlay", 0, opts); err != nil {
|
2017-11-16 11:34:33 -05:00
|
|
|
return errors.Wrap(err, "failed to mount overlay")
|
|
|
|
}
|
2019-03-28 23:48:04 -04:00
|
|
|
if err := unix.Unmount(filepath.Join(td, mergedDirName), 0); err != nil {
|
|
|
|
logger.Warnf("Failed to unmount check directory %v: %v", filepath.Join(td, mergedDirName), err)
|
2017-11-16 11:34:33 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|