overlay: move supportsMultipleLowerDir to utils

This moves supportsMultipleLowerDir() to overlayutils
so it can be used from both overlay and overlay2.

The only changes made were:
 * replace logger with logrus
 * don't use workDirName mergedDirName constants
 * add mnt var to improve readability a bit

This is a preparation for the next commit.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit d5687079ad)
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
This commit is contained in:
Kir Kolyshkin 2019-11-07 14:20:40 -08:00 committed by Akihiro Suda
parent 5e4574526d
commit 5571ceb5ac
3 changed files with 41 additions and 33 deletions

View file

@ -99,35 +99,3 @@ func doesSupportNativeDiff(d string) error {
return nil
}
// 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 {
logger.Warnf("Failed to remove check directory %v: %v", td, err)
}
}()
for _, dir := range []string{"lower1", "lower2", "upper", workDirName, mergedDirName} {
if err := os.Mkdir(filepath.Join(td, dir), 0755); err != nil {
return err
}
}
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 {
return errors.Wrap(err, "failed to mount overlay")
}
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)
}
return nil
}

View file

@ -180,7 +180,7 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
if opts.overrideKernelCheck {
logger.Warn("Using pre-4.0.0 kernel for overlay2, mount failures may require kernel update")
} else {
if err := supportsMultipleLowerDir(testdir); err != nil {
if err := overlayutils.SupportsMultipleLowerDir(testdir); err != nil {
logger.Debugf("Multiple lower dirs not supported: %v", err)
return nil, graphdriver.ErrNotSupported
}

View file

@ -4,8 +4,15 @@ package overlayutils // import "github.com/docker/docker/daemon/graphdriver/over
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/docker/docker/daemon/graphdriver"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)
// ErrDTypeNotSupported denotes that the backing filesystem doesn't support d_type.
@ -23,3 +30,36 @@ func ErrDTypeNotSupported(driver, backingFs string) error {
return graphdriver.NotSupportedError(msg)
}
// 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 {
logrus.Warnf("Failed to remove check directory %v: %v", td, err)
}
}()
for _, dir := range []string{"lower1", "lower2", "upper", "work", "merged"} {
if err := os.Mkdir(filepath.Join(td, dir), 0755); err != nil {
return err
}
}
mnt := filepath.Join(td, "merged")
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, "work"))
if err := unix.Mount("overlay", mnt, "overlay", 0, opts); err != nil {
return errors.Wrap(err, "failed to mount overlay")
}
if err := unix.Unmount(mnt, 0); err != nil {
logrus.Warnf("Failed to unmount check directory %v: %v", mnt, err)
}
return nil
}