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

     1  // +build linux
     2  
     3  package overlay // import "github.com/demonoid81/moby/daemon/graphdriver/overlay"
     4  
     5  import (
     6  	"fmt"
     7  	"io"
     8  	"io/ioutil"
     9  	"os"
    10  	"path"
    11  	"path/filepath"
    12  	"strconv"
    13  	"strings"
    14  
    15  	"github.com/demonoid81/moby/daemon/graphdriver"
    16  	"github.com/demonoid81/moby/daemon/graphdriver/copy"
    17  	"github.com/demonoid81/moby/daemon/graphdriver/overlayutils"
    18  	"github.com/demonoid81/moby/pkg/archive"
    19  	"github.com/demonoid81/moby/pkg/containerfs"
    20  	"github.com/demonoid81/moby/pkg/fsutils"
    21  	"github.com/demonoid81/moby/pkg/idtools"
    22  	"github.com/demonoid81/moby/pkg/locker"
    23  	"github.com/demonoid81/moby/pkg/parsers"
    24  	"github.com/demonoid81/moby/pkg/system"
    25  	"github.com/moby/sys/mount"
    26  	"github.com/opencontainers/selinux/go-selinux/label"
    27  	"github.com/sirupsen/logrus"
    28  	"golang.org/x/sys/unix"
    29  )
    30  
    31  // This is a small wrapper over the NaiveDiffWriter that lets us have a custom
    32  // implementation of ApplyDiff()
    33  
    34  var (
    35  	// ErrApplyDiffFallback is returned to indicate that a normal ApplyDiff is applied as a fallback from Naive diff writer.
    36  	ErrApplyDiffFallback = fmt.Errorf("Fall back to normal ApplyDiff")
    37  	backingFs            = "<unknown>"
    38  )
    39  
    40  // ApplyDiffProtoDriver wraps the ProtoDriver by extending the interface with ApplyDiff method.
    41  type ApplyDiffProtoDriver interface {
    42  	graphdriver.ProtoDriver
    43  	// ApplyDiff writes the diff to the archive for the given id and parent id.
    44  	// It returns the size in bytes written if successful, an error ErrApplyDiffFallback is returned otherwise.
    45  	ApplyDiff(id, parent string, diff io.Reader) (size int64, err error)
    46  }
    47  
    48  type naiveDiffDriverWithApply struct {
    49  	graphdriver.Driver
    50  	applyDiff ApplyDiffProtoDriver
    51  }
    52  
    53  // NaiveDiffDriverWithApply returns a NaiveDiff driver with custom ApplyDiff.
    54  func NaiveDiffDriverWithApply(driver ApplyDiffProtoDriver, uidMaps, gidMaps []idtools.IDMap) graphdriver.Driver {
    55  	return &naiveDiffDriverWithApply{
    56  		Driver:    graphdriver.NewNaiveDiffDriver(driver, uidMaps, gidMaps),
    57  		applyDiff: driver,
    58  	}
    59  }
    60  
    61  // ApplyDiff creates a diff layer with either the NaiveDiffDriver or with a fallback.
    62  func (d *naiveDiffDriverWithApply) ApplyDiff(id, parent string, diff io.Reader) (int64, error) {
    63  	b, err := d.applyDiff.ApplyDiff(id, parent, diff)
    64  	if err == ErrApplyDiffFallback {
    65  		return d.Driver.ApplyDiff(id, parent, diff)
    66  	}
    67  	return b, err
    68  }
    69  
    70  // This backend uses the overlay union filesystem for containers
    71  // plus hard link file sharing for images.
    72  
    73  // Each container/image can have a "root" subdirectory which is a plain
    74  // filesystem hierarchy, or they can use overlay.
    75  
    76  // If they use overlay there is a "upper" directory and a "lower-id"
    77  // file, as well as "merged" and "work" directories. The "upper"
    78  // directory has the upper layer of the overlay, and "lower-id" contains
    79  // the id of the parent whose "root" directory shall be used as the lower
    80  // layer in the overlay. The overlay itself is mounted in the "merged"
    81  // directory, and the "work" dir is needed for overlay to work.
    82  
    83  // When an overlay layer is created there are two cases, either the
    84  // parent has a "root" dir, then we start out with an empty "upper"
    85  // directory overlaid on the parents root. This is typically the
    86  // case with the init layer of a container which is based on an image.
    87  // If there is no "root" in the parent, we inherit the lower-id from
    88  // the parent and start by making a copy in the parent's "upper" dir.
    89  // This is typically the case for a container layer which copies
    90  // its parent -init upper layer.
    91  
    92  // Additionally we also have a custom implementation of ApplyLayer
    93  // which makes a recursive copy of the parent "root" layer using
    94  // hardlinks to share file data, and then applies the layer on top
    95  // of that. This means all child images share file (but not directory)
    96  // data with the parent.
    97  
    98  type overlayOptions struct{}
    99  
   100  // Driver contains information about the home directory and the list of active mounts that are created using this driver.
   101  type Driver struct {
   102  	home          string
   103  	uidMaps       []idtools.IDMap
   104  	gidMaps       []idtools.IDMap
   105  	ctr           *graphdriver.RefCounter
   106  	supportsDType bool
   107  	locker        *locker.Locker
   108  }
   109  
   110  func init() {
   111  	graphdriver.Register("overlay", Init)
   112  }
   113  
   114  // Init returns the NaiveDiffDriver, a native diff driver for overlay filesystem.
   115  // If overlay filesystem is not supported on the host, the error
   116  // graphdriver.ErrNotSupported is returned.
   117  // If an overlay filesystem is not supported over an existing filesystem then
   118  // error graphdriver.ErrIncompatibleFS is returned.
   119  func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
   120  	_, err := parseOptions(options)
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  
   125  	// Perform feature detection on /var/lib/docker/overlay if it's an existing directory.
   126  	// This covers situations where /var/lib/docker/overlay is a mount, and on a different
   127  	// filesystem than /var/lib/docker.
   128  	// If the path does not exist, fall back to using /var/lib/docker for feature detection.
   129  	testdir := home
   130  	if _, err := os.Stat(testdir); os.IsNotExist(err) {
   131  		testdir = filepath.Dir(testdir)
   132  	}
   133  
   134  	if err := overlayutils.SupportsOverlay(testdir, false); err != nil {
   135  		logrus.WithField("storage-driver", "overlay").Error(err)
   136  		return nil, graphdriver.ErrNotSupported
   137  	}
   138  
   139  	fsMagic, err := graphdriver.GetFSMagic(testdir)
   140  	if err != nil {
   141  		return nil, err
   142  	}
   143  	if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
   144  		backingFs = fsName
   145  	}
   146  
   147  	supportsDType, err := fsutils.SupportsDType(testdir)
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  	if !supportsDType {
   152  		if !graphdriver.IsInitialized(home) {
   153  			return nil, overlayutils.ErrDTypeNotSupported("overlay", backingFs)
   154  		}
   155  		// allow running without d_type only for existing setups (#27443)
   156  		logrus.WithField("storage-driver", "overlay").Warn(overlayutils.ErrDTypeNotSupported("overlay", backingFs))
   157  	}
   158  
   159  	rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
   160  	if err != nil {
   161  		return nil, err
   162  	}
   163  	// Create the driver home dir
   164  	if err := idtools.MkdirAllAndChown(home, 0700, idtools.Identity{UID: rootUID, GID: rootGID}); err != nil {
   165  		return nil, err
   166  	}
   167  
   168  	d := &Driver{
   169  		home:          home,
   170  		uidMaps:       uidMaps,
   171  		gidMaps:       gidMaps,
   172  		ctr:           graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicOverlay)),
   173  		supportsDType: supportsDType,
   174  		locker:        locker.New(),
   175  	}
   176  
   177  	return NaiveDiffDriverWithApply(d, uidMaps, gidMaps), nil
   178  }
   179  
   180  func parseOptions(options []string) (*overlayOptions, error) {
   181  	o := &overlayOptions{}
   182  	for _, option := range options {
   183  		key, _, err := parsers.ParseKeyValueOpt(option)
   184  		if err != nil {
   185  			return nil, err
   186  		}
   187  		key = strings.ToLower(key)
   188  		switch key {
   189  		default:
   190  			return nil, fmt.Errorf("overlay: unknown option %s", key)
   191  		}
   192  	}
   193  	return o, nil
   194  }
   195  
   196  func (d *Driver) String() string {
   197  	return "overlay"
   198  }
   199  
   200  // Status returns current driver information in a two dimensional string array.
   201  // Output contains "Backing Filesystem" used in this implementation.
   202  func (d *Driver) Status() [][2]string {
   203  	return [][2]string{
   204  		{"Backing Filesystem", backingFs},
   205  		{"Supports d_type", strconv.FormatBool(d.supportsDType)},
   206  	}
   207  }
   208  
   209  // GetMetadata returns metadata about the overlay driver such as root,
   210  // LowerDir, UpperDir, WorkDir and MergeDir used to store data.
   211  func (d *Driver) GetMetadata(id string) (map[string]string, error) {
   212  	dir := d.dir(id)
   213  	if _, err := os.Stat(dir); err != nil {
   214  		return nil, err
   215  	}
   216  
   217  	metadata := make(map[string]string)
   218  
   219  	// If id has a root, it is an image
   220  	rootDir := path.Join(dir, "root")
   221  	if _, err := os.Stat(rootDir); err == nil {
   222  		metadata["RootDir"] = rootDir
   223  		return metadata, nil
   224  	}
   225  
   226  	lowerID, err := ioutil.ReadFile(path.Join(dir, "lower-id"))
   227  	if err != nil {
   228  		return nil, err
   229  	}
   230  
   231  	metadata["LowerDir"] = path.Join(d.dir(string(lowerID)), "root")
   232  	metadata["UpperDir"] = path.Join(dir, "upper")
   233  	metadata["WorkDir"] = path.Join(dir, "work")
   234  	metadata["MergedDir"] = path.Join(dir, "merged")
   235  
   236  	return metadata, nil
   237  }
   238  
   239  // Cleanup any state created by overlay which should be cleaned when daemon
   240  // is being shutdown. For now, we just have to unmount the bind mounted
   241  // we had created.
   242  func (d *Driver) Cleanup() error {
   243  	return mount.RecursiveUnmount(d.home)
   244  }
   245  
   246  // CreateReadWrite creates a layer that is writable for use as a container
   247  // file system.
   248  func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
   249  	return d.Create(id, parent, opts)
   250  }
   251  
   252  // Create is used to create the upper, lower, and merge directories required for overlay fs for a given id.
   253  // The parent filesystem is used to configure these directories for the overlay.
   254  func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) (retErr error) {
   255  
   256  	if opts != nil && len(opts.StorageOpt) != 0 {
   257  		return fmt.Errorf("--storage-opt is not supported for overlay")
   258  	}
   259  
   260  	dir := d.dir(id)
   261  
   262  	rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
   263  	if err != nil {
   264  		return err
   265  	}
   266  	root := idtools.Identity{UID: rootUID, GID: rootGID}
   267  
   268  	if err := idtools.MkdirAllAndChown(path.Dir(dir), 0700, root); err != nil {
   269  		return err
   270  	}
   271  	if err := idtools.MkdirAndChown(dir, 0700, root); err != nil {
   272  		return err
   273  	}
   274  
   275  	defer func() {
   276  		// Clean up on failure
   277  		if retErr != nil {
   278  			os.RemoveAll(dir)
   279  		}
   280  	}()
   281  
   282  	// Toplevel images are just a "root" dir
   283  	if parent == "" {
   284  		return idtools.MkdirAndChown(path.Join(dir, "root"), 0755, root)
   285  	}
   286  
   287  	parentDir := d.dir(parent)
   288  
   289  	// Ensure parent exists
   290  	if _, err := os.Lstat(parentDir); err != nil {
   291  		return err
   292  	}
   293  
   294  	// If parent has a root, just do an overlay to it
   295  	parentRoot := path.Join(parentDir, "root")
   296  
   297  	if s, err := os.Lstat(parentRoot); err == nil {
   298  		if err := idtools.MkdirAndChown(path.Join(dir, "upper"), s.Mode(), root); err != nil {
   299  			return err
   300  		}
   301  		if err := idtools.MkdirAndChown(path.Join(dir, "work"), 0700, root); err != nil {
   302  			return err
   303  		}
   304  		return ioutil.WriteFile(path.Join(dir, "lower-id"), []byte(parent), 0666)
   305  	}
   306  
   307  	// Otherwise, copy the upper and the lower-id from the parent
   308  
   309  	lowerID, err := ioutil.ReadFile(path.Join(parentDir, "lower-id"))
   310  	if err != nil {
   311  		return err
   312  	}
   313  
   314  	if err := ioutil.WriteFile(path.Join(dir, "lower-id"), lowerID, 0666); err != nil {
   315  		return err
   316  	}
   317  
   318  	parentUpperDir := path.Join(parentDir, "upper")
   319  	s, err := os.Lstat(parentUpperDir)
   320  	if err != nil {
   321  		return err
   322  	}
   323  
   324  	upperDir := path.Join(dir, "upper")
   325  	if err := idtools.MkdirAndChown(upperDir, s.Mode(), root); err != nil {
   326  		return err
   327  	}
   328  	if err := idtools.MkdirAndChown(path.Join(dir, "work"), 0700, root); err != nil {
   329  		return err
   330  	}
   331  
   332  	return copy.DirCopy(parentUpperDir, upperDir, copy.Content, true)
   333  }
   334  
   335  func (d *Driver) dir(id string) string {
   336  	return path.Join(d.home, id)
   337  }
   338  
   339  // Remove cleans the directories that are created for this id.
   340  func (d *Driver) Remove(id string) error {
   341  	if id == "" {
   342  		return fmt.Errorf("refusing to remove the directories: id is empty")
   343  	}
   344  	d.locker.Lock(id)
   345  	defer d.locker.Unlock(id)
   346  	return system.EnsureRemoveAll(d.dir(id))
   347  }
   348  
   349  // Get creates and mounts the required file system for the given id and returns the mount path.
   350  func (d *Driver) Get(id, mountLabel string) (_ containerfs.ContainerFS, err error) {
   351  	d.locker.Lock(id)
   352  	defer d.locker.Unlock(id)
   353  	dir := d.dir(id)
   354  	if _, err := os.Stat(dir); err != nil {
   355  		return nil, err
   356  	}
   357  	// If id has a root, just return it
   358  	rootDir := path.Join(dir, "root")
   359  	if _, err := os.Stat(rootDir); err == nil {
   360  		return containerfs.NewLocalContainerFS(rootDir), nil
   361  	}
   362  
   363  	mergedDir := path.Join(dir, "merged")
   364  	if count := d.ctr.Increment(mergedDir); count > 1 {
   365  		return containerfs.NewLocalContainerFS(mergedDir), nil
   366  	}
   367  	defer func() {
   368  		if err != nil {
   369  			if c := d.ctr.Decrement(mergedDir); c <= 0 {
   370  				if mntErr := unix.Unmount(mergedDir, 0); mntErr != nil {
   371  					logrus.WithField("storage-driver", "overlay").Debugf("Failed to unmount %s: %v: %v", id, mntErr, err)
   372  				}
   373  				// Cleanup the created merged directory; see the comment in Put's rmdir
   374  				if rmErr := unix.Rmdir(mergedDir); rmErr != nil && !os.IsNotExist(rmErr) {
   375  					logrus.WithField("storage-driver", "overlay").Warnf("Failed to remove %s: %v: %v", id, rmErr, err)
   376  				}
   377  			}
   378  		}
   379  	}()
   380  	lowerID, err := ioutil.ReadFile(path.Join(dir, "lower-id"))
   381  	if err != nil {
   382  		return nil, err
   383  	}
   384  	rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
   385  	if err != nil {
   386  		return nil, err
   387  	}
   388  	if err := idtools.MkdirAndChown(mergedDir, 0700, idtools.Identity{UID: rootUID, GID: rootGID}); err != nil {
   389  		return nil, err
   390  	}
   391  	var (
   392  		lowerDir = path.Join(d.dir(string(lowerID)), "root")
   393  		upperDir = path.Join(dir, "upper")
   394  		workDir  = path.Join(dir, "work")
   395  		opts     = fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", lowerDir, upperDir, workDir)
   396  	)
   397  	if err := unix.Mount("overlay", mergedDir, "overlay", 0, label.FormatMountLabel(opts, mountLabel)); err != nil {
   398  		return nil, fmt.Errorf("error creating overlay mount to %s: %v", mergedDir, err)
   399  	}
   400  	// chown "workdir/work" to the remapped root UID/GID. Overlay fs inside a
   401  	// user namespace requires this to move a directory from lower to upper.
   402  	if err := os.Chown(path.Join(workDir, "work"), rootUID, rootGID); err != nil {
   403  		return nil, err
   404  	}
   405  	return containerfs.NewLocalContainerFS(mergedDir), nil
   406  }
   407  
   408  // Put unmounts the mount path created for the give id.
   409  // It also removes the 'merged' directory to force the kernel to unmount the
   410  // overlay mount in other namespaces.
   411  func (d *Driver) Put(id string) error {
   412  	d.locker.Lock(id)
   413  	defer d.locker.Unlock(id)
   414  	// If id has a root, just return
   415  	if _, err := os.Stat(path.Join(d.dir(id), "root")); err == nil {
   416  		return nil
   417  	}
   418  	mountpoint := path.Join(d.dir(id), "merged")
   419  	logger := logrus.WithField("storage-driver", "overlay")
   420  	if count := d.ctr.Decrement(mountpoint); count > 0 {
   421  		return nil
   422  	}
   423  	if err := unix.Unmount(mountpoint, unix.MNT_DETACH); err != nil {
   424  		logger.Debugf("Failed to unmount %s overlay: %v", id, err)
   425  	}
   426  
   427  	// Remove the mountpoint here. Removing the mountpoint (in newer kernels)
   428  	// will cause all other instances of this mount in other mount namespaces
   429  	// to be unmounted. This is necessary to avoid cases where an overlay mount
   430  	// that is present in another namespace will cause subsequent mounts
   431  	// operations to fail with ebusy.  We ignore any errors here because this may
   432  	// fail on older kernels which don't have
   433  	// torvalds/linux@8ed936b5671bfb33d89bc60bdcc7cf0470ba52fe applied.
   434  	if err := unix.Rmdir(mountpoint); err != nil {
   435  		logger.Debugf("Failed to remove %s overlay: %v", id, err)
   436  	}
   437  	return nil
   438  }
   439  
   440  // ApplyDiff applies the new layer on top of the root, if parent does not exist with will return an ErrApplyDiffFallback error.
   441  func (d *Driver) ApplyDiff(id string, parent string, diff io.Reader) (size int64, err error) {
   442  	dir := d.dir(id)
   443  
   444  	if parent == "" {
   445  		return 0, ErrApplyDiffFallback
   446  	}
   447  
   448  	parentRootDir := path.Join(d.dir(parent), "root")
   449  	if _, err := os.Stat(parentRootDir); err != nil {
   450  		return 0, ErrApplyDiffFallback
   451  	}
   452  
   453  	// We now know there is a parent, and it has a "root" directory containing
   454  	// the full root filesystem. We can just hardlink it and apply the
   455  	// layer. This relies on two things:
   456  	// 1) ApplyDiff is only run once on a clean (no writes to upper layer) container
   457  	// 2) ApplyDiff doesn't do any in-place writes to files (would break hardlinks)
   458  	// These are all currently true and are not expected to break
   459  
   460  	tmpRootDir, err := ioutil.TempDir(dir, "tmproot")
   461  	if err != nil {
   462  		return 0, err
   463  	}
   464  	defer func() {
   465  		if err != nil {
   466  			os.RemoveAll(tmpRootDir)
   467  		} else {
   468  			os.RemoveAll(path.Join(dir, "upper"))
   469  			os.RemoveAll(path.Join(dir, "work"))
   470  			os.RemoveAll(path.Join(dir, "merged"))
   471  			os.RemoveAll(path.Join(dir, "lower-id"))
   472  		}
   473  	}()
   474  
   475  	if err = copy.DirCopy(parentRootDir, tmpRootDir, copy.Hardlink, true); err != nil {
   476  		return 0, err
   477  	}
   478  
   479  	options := &archive.TarOptions{UIDMaps: d.uidMaps, GIDMaps: d.gidMaps}
   480  	if size, err = graphdriver.ApplyUncompressedLayer(tmpRootDir, diff, options); err != nil {
   481  		return 0, err
   482  	}
   483  
   484  	rootDir := path.Join(dir, "root")
   485  	if err := os.Rename(tmpRootDir, rootDir); err != nil {
   486  		return 0, err
   487  	}
   488  
   489  	return
   490  }
   491  
   492  // Exists checks to see if the id is already mounted.
   493  func (d *Driver) Exists(id string) bool {
   494  	_, err := os.Stat(d.dir(id))
   495  	return err == nil
   496  }