github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/graphdriver/overlayutils/overlayutils.go (about)

     1  // +build linux
     2  
     3  package overlayutils // import "github.com/demonoid81/moby/daemon/graphdriver/overlayutils"
     4  
     5  import (
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  	"path"
    10  	"path/filepath"
    11  
    12  	"github.com/demonoid81/moby/daemon/graphdriver"
    13  	"github.com/pkg/errors"
    14  	"github.com/sirupsen/logrus"
    15  	"golang.org/x/sys/unix"
    16  )
    17  
    18  // ErrDTypeNotSupported denotes that the backing filesystem doesn't support d_type.
    19  func ErrDTypeNotSupported(driver, backingFs string) error {
    20  	msg := fmt.Sprintf("%s: the backing %s filesystem is formatted without d_type support, which leads to incorrect behavior.", driver, backingFs)
    21  	if backingFs == "xfs" {
    22  		msg += " Reformat the filesystem with ftype=1 to enable d_type support."
    23  	}
    24  
    25  	if backingFs == "extfs" {
    26  		msg += " Reformat the filesystem (or use tune2fs) with -O filetype flag to enable d_type support."
    27  	}
    28  
    29  	msg += " Backing filesystems without d_type support are not supported."
    30  
    31  	return graphdriver.NotSupportedError(msg)
    32  }
    33  
    34  // SupportsOverlay checks if the system supports overlay filesystem
    35  // by performing an actual overlay mount.
    36  //
    37  // checkMultipleLowers parameter enables check for multiple lowerdirs,
    38  // which is required for the overlay2 driver.
    39  func SupportsOverlay(d string, checkMultipleLowers bool) error {
    40  	td, err := ioutil.TempDir(d, "check-overlayfs-support")
    41  	if err != nil {
    42  		return err
    43  	}
    44  	defer func() {
    45  		if err := os.RemoveAll(td); err != nil {
    46  			logrus.Warnf("Failed to remove check directory %v: %v", td, err)
    47  		}
    48  	}()
    49  
    50  	for _, dir := range []string{"lower1", "lower2", "upper", "work", "merged"} {
    51  		if err := os.Mkdir(filepath.Join(td, dir), 0755); err != nil {
    52  			return err
    53  		}
    54  	}
    55  
    56  	mnt := filepath.Join(td, "merged")
    57  	lowerDir := path.Join(td, "lower2")
    58  	if checkMultipleLowers {
    59  		lowerDir += ":" + path.Join(td, "lower1")
    60  	}
    61  	opts := fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", lowerDir, path.Join(td, "upper"), path.Join(td, "work"))
    62  	if err := unix.Mount("overlay", mnt, "overlay", 0, opts); err != nil {
    63  		return errors.Wrap(err, "failed to mount overlay")
    64  	}
    65  	if err := unix.Unmount(mnt, 0); err != nil {
    66  		logrus.Warnf("Failed to unmount check directory %v: %v", mnt, err)
    67  	}
    68  	return nil
    69  }