gopkg.in/docker/docker.v23@v23.0.11/daemon/graphdriver/overlay2/overlay.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package overlay2 // import "github.com/docker/docker/daemon/graphdriver/overlay2"
     5  
     6  import (
     7  	"context"
     8  	"errors"
     9  	"fmt"
    10  	"io"
    11  	"os"
    12  	"path"
    13  	"path/filepath"
    14  	"strconv"
    15  	"strings"
    16  	"sync"
    17  
    18  	"github.com/containerd/continuity/fs"
    19  	"github.com/docker/docker/daemon/graphdriver"
    20  	"github.com/docker/docker/daemon/graphdriver/overlayutils"
    21  	"github.com/docker/docker/pkg/archive"
    22  	"github.com/docker/docker/pkg/chrootarchive"
    23  	"github.com/docker/docker/pkg/containerfs"
    24  	"github.com/docker/docker/pkg/directory"
    25  	"github.com/docker/docker/pkg/idtools"
    26  	"github.com/docker/docker/pkg/ioutils"
    27  	"github.com/docker/docker/pkg/parsers"
    28  	"github.com/docker/docker/quota"
    29  	units "github.com/docker/go-units"
    30  	"github.com/moby/locker"
    31  	"github.com/moby/sys/mount"
    32  	"github.com/opencontainers/selinux/go-selinux/label"
    33  	"github.com/sirupsen/logrus"
    34  	"golang.org/x/sys/unix"
    35  )
    36  
    37  var (
    38  	// untar defines the untar method
    39  	untar = chrootarchive.UntarUncompressed
    40  )
    41  
    42  // This backend uses the overlay union filesystem for containers
    43  // with diff directories for each layer.
    44  
    45  // This version of the overlay driver requires at least kernel
    46  // 4.0.0 in order to support mounting multiple diff directories.
    47  
    48  // Each container/image has at least a "diff" directory and "link" file.
    49  // If there is also a "lower" file when there are diff layers
    50  // below as well as "merged" and "work" directories. The "diff" directory
    51  // has the upper layer of the overlay and is used to capture any
    52  // changes to the layer. The "lower" file contains all the lower layer
    53  // mounts separated by ":" and ordered from uppermost to lowermost
    54  // layers. The overlay itself is mounted in the "merged" directory,
    55  // and the "work" dir is needed for overlay to work.
    56  
    57  // The "link" file for each layer contains a unique string for the layer.
    58  // Under the "l" directory at the root there will be a symbolic link
    59  // with that unique string pointing the "diff" directory for the layer.
    60  // The symbolic links are used to reference lower layers in the "lower"
    61  // file and on mount. The links are used to shorten the total length
    62  // of a layer reference without requiring changes to the layer identifier
    63  // or root directory. Mounts are always done relative to root and
    64  // referencing the symbolic links in order to ensure the number of
    65  // lower directories can fit in a single page for making the mount
    66  // syscall. A hard upper limit of 128 lower layers is enforced to ensure
    67  // that mounts do not fail due to length.
    68  
    69  const (
    70  	driverName    = "overlay2"
    71  	linkDir       = "l"
    72  	diffDirName   = "diff"
    73  	workDirName   = "work"
    74  	mergedDirName = "merged"
    75  	lowerFile     = "lower"
    76  	maxDepth      = 128
    77  
    78  	// idLength represents the number of random characters
    79  	// which can be used to create the unique link identifier
    80  	// for every layer. If this value is too long then the
    81  	// page size limit for the mount command may be exceeded.
    82  	// The idLength should be selected such that following equation
    83  	// is true (512 is a buffer for label metadata).
    84  	// ((idLength + len(linkDir) + 1) * maxDepth) <= (pageSize - 512)
    85  	idLength = 26
    86  )
    87  
    88  type overlayOptions struct {
    89  	quota quota.Quota
    90  }
    91  
    92  // Driver contains information about the home directory and the list of active
    93  // mounts that are created using this driver.
    94  type Driver struct {
    95  	home          string
    96  	idMap         idtools.IdentityMapping
    97  	ctr           *graphdriver.RefCounter
    98  	quotaCtl      *quota.Control
    99  	options       overlayOptions
   100  	naiveDiff     graphdriver.DiffDriver
   101  	supportsDType bool
   102  	usingMetacopy bool
   103  	locker        *locker.Locker
   104  }
   105  
   106  var (
   107  	logger                = logrus.WithField("storage-driver", "overlay2")
   108  	backingFs             = "<unknown>"
   109  	projectQuotaSupported = false
   110  
   111  	useNaiveDiffLock sync.Once
   112  	useNaiveDiffOnly bool
   113  
   114  	indexOff  string
   115  	userxattr string
   116  )
   117  
   118  func init() {
   119  	graphdriver.Register(driverName, Init)
   120  }
   121  
   122  // Init returns the native diff driver for overlay filesystem.
   123  // If overlay filesystem is not supported on the host, the error
   124  // graphdriver.ErrNotSupported is returned.
   125  // If an overlay filesystem is not supported over an existing filesystem then
   126  // the error graphdriver.ErrIncompatibleFS is returned.
   127  func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdriver.Driver, error) {
   128  	opts, err := parseOptions(options)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  
   133  	// Perform feature detection on /var/lib/docker/overlay2 if it's an existing directory.
   134  	// This covers situations where /var/lib/docker/overlay2 is a mount, and on a different
   135  	// filesystem than /var/lib/docker.
   136  	// If the path does not exist, fall back to using /var/lib/docker for feature detection.
   137  	testdir := home
   138  	if _, err := os.Stat(testdir); os.IsNotExist(err) {
   139  		testdir = filepath.Dir(testdir)
   140  	}
   141  
   142  	if err := overlayutils.SupportsOverlay(testdir, true); err != nil {
   143  		logger.Error(err)
   144  		return nil, graphdriver.ErrNotSupported
   145  	}
   146  
   147  	fsMagic, err := graphdriver.GetFSMagic(testdir)
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  	if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
   152  		backingFs = fsName
   153  	}
   154  
   155  	supportsDType, err := fs.SupportsDType(testdir)
   156  	if err != nil {
   157  		return nil, err
   158  	}
   159  	if !supportsDType {
   160  		return nil, overlayutils.ErrDTypeNotSupported("overlay2", backingFs)
   161  	}
   162  
   163  	usingMetacopy, err := usingMetacopy(testdir)
   164  	if err != nil {
   165  		return nil, err
   166  	}
   167  
   168  	cur := idtools.CurrentIdentity()
   169  	dirID := idtools.Identity{
   170  		UID: cur.UID,
   171  		GID: idMap.RootPair().GID,
   172  	}
   173  	if err := idtools.MkdirAllAndChown(home, 0710, dirID); err != nil {
   174  		return nil, err
   175  	}
   176  	if err := idtools.MkdirAllAndChown(path.Join(home, linkDir), 0700, cur); err != nil {
   177  		return nil, err
   178  	}
   179  
   180  	d := &Driver{
   181  		home:          home,
   182  		idMap:         idMap,
   183  		ctr:           graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicOverlay)),
   184  		supportsDType: supportsDType,
   185  		usingMetacopy: usingMetacopy,
   186  		locker:        locker.New(),
   187  		options:       *opts,
   188  	}
   189  
   190  	d.naiveDiff = graphdriver.NewNaiveDiffDriver(d, idMap)
   191  
   192  	if backingFs == "xfs" {
   193  		// Try to enable project quota support over xfs.
   194  		if d.quotaCtl, err = quota.NewControl(home); err == nil {
   195  			projectQuotaSupported = true
   196  		} else if opts.quota.Size > 0 {
   197  			return nil, fmt.Errorf("Storage option overlay2.size not supported. Filesystem does not support Project Quota: %v", err)
   198  		}
   199  	} else if opts.quota.Size > 0 {
   200  		// if xfs is not the backing fs then error out if the storage-opt overlay2.size is used.
   201  		return nil, fmt.Errorf("Storage Option overlay2.size only supported for backingFS XFS. Found %v", backingFs)
   202  	}
   203  
   204  	// figure out whether "index=off" option is recognized by the kernel
   205  	_, err = os.Stat("/sys/module/overlay/parameters/index")
   206  	switch {
   207  	case err == nil:
   208  		indexOff = "index=off,"
   209  	case os.IsNotExist(err):
   210  		// old kernel, no index -- do nothing
   211  	default:
   212  		logger.Warnf("Unable to detect whether overlay kernel module supports index parameter: %s", err)
   213  	}
   214  
   215  	needsUserXattr, err := overlayutils.NeedsUserXAttr(home)
   216  	if err != nil {
   217  		logger.Warnf("Unable to detect whether overlay kernel module needs \"userxattr\" parameter: %s", err)
   218  	}
   219  	if needsUserXattr {
   220  		userxattr = "userxattr,"
   221  	}
   222  
   223  	logger.Debugf("backingFs=%s, projectQuotaSupported=%v, usingMetacopy=%v, indexOff=%q, userxattr=%q",
   224  		backingFs, projectQuotaSupported, usingMetacopy, indexOff, userxattr)
   225  
   226  	return d, nil
   227  }
   228  
   229  func parseOptions(options []string) (*overlayOptions, error) {
   230  	o := &overlayOptions{}
   231  	for _, option := range options {
   232  		key, val, err := parsers.ParseKeyValueOpt(option)
   233  		if err != nil {
   234  			return nil, err
   235  		}
   236  		key = strings.ToLower(key)
   237  		switch key {
   238  		case "overlay2.override_kernel_check":
   239  			// TODO(thaJeztah): change this to an error, see https://github.com/docker/cli/pull/3806
   240  			logger.Warn("DEPRECATED: the overlay2.override_kernel_check option is ignored and will be removed in the next release. You can safely remove this option from your configuration.")
   241  		case "overlay2.size":
   242  			size, err := units.RAMInBytes(val)
   243  			if err != nil {
   244  				return nil, err
   245  			}
   246  			o.quota.Size = uint64(size)
   247  		default:
   248  			return nil, fmt.Errorf("overlay2: unknown option %s", key)
   249  		}
   250  	}
   251  	return o, nil
   252  }
   253  
   254  func useNaiveDiff(home string) bool {
   255  	useNaiveDiffLock.Do(func() {
   256  		if err := doesSupportNativeDiff(home); err != nil {
   257  			logger.Warnf("Not using native diff for overlay2, this may cause degraded performance for building images: %v", err)
   258  			useNaiveDiffOnly = true
   259  		}
   260  	})
   261  	return useNaiveDiffOnly
   262  }
   263  
   264  func (d *Driver) String() string {
   265  	return driverName
   266  }
   267  
   268  // Status returns current driver information in a two dimensional string array.
   269  // Output contains "Backing Filesystem" used in this implementation.
   270  func (d *Driver) Status() [][2]string {
   271  	return [][2]string{
   272  		{"Backing Filesystem", backingFs},
   273  		{"Supports d_type", strconv.FormatBool(d.supportsDType)},
   274  		{"Using metacopy", strconv.FormatBool(d.usingMetacopy)},
   275  		{"Native Overlay Diff", strconv.FormatBool(!useNaiveDiff(d.home))},
   276  		{"userxattr", strconv.FormatBool(userxattr != "")},
   277  	}
   278  }
   279  
   280  // GetMetadata returns metadata about the overlay driver such as the LowerDir,
   281  // UpperDir, WorkDir, and MergeDir used to store data.
   282  func (d *Driver) GetMetadata(id string) (map[string]string, error) {
   283  	dir := d.dir(id)
   284  	if _, err := os.Stat(dir); err != nil {
   285  		return nil, err
   286  	}
   287  
   288  	metadata := map[string]string{
   289  		"WorkDir":   path.Join(dir, workDirName),
   290  		"MergedDir": path.Join(dir, mergedDirName),
   291  		"UpperDir":  path.Join(dir, diffDirName),
   292  	}
   293  
   294  	lowerDirs, err := d.getLowerDirs(id)
   295  	if err != nil {
   296  		return nil, err
   297  	}
   298  	if len(lowerDirs) > 0 {
   299  		metadata["LowerDir"] = strings.Join(lowerDirs, ":")
   300  	}
   301  
   302  	return metadata, nil
   303  }
   304  
   305  // Cleanup any state created by overlay which should be cleaned when daemon
   306  // is being shutdown. For now, we just have to unmount the bind mounted
   307  // we had created.
   308  func (d *Driver) Cleanup() error {
   309  	return mount.RecursiveUnmount(d.home)
   310  }
   311  
   312  // CreateReadWrite creates a layer that is writable for use as a container
   313  // file system.
   314  func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
   315  	if opts == nil {
   316  		opts = &graphdriver.CreateOpts{
   317  			StorageOpt: make(map[string]string),
   318  		}
   319  	} else if opts.StorageOpt == nil {
   320  		opts.StorageOpt = make(map[string]string)
   321  	}
   322  
   323  	// Merge daemon default config.
   324  	if _, ok := opts.StorageOpt["size"]; !ok && d.options.quota.Size != 0 {
   325  		opts.StorageOpt["size"] = strconv.FormatUint(d.options.quota.Size, 10)
   326  	}
   327  
   328  	if _, ok := opts.StorageOpt["size"]; ok && !projectQuotaSupported {
   329  		return fmt.Errorf("--storage-opt is supported only for overlay over xfs with 'pquota' mount option")
   330  	}
   331  
   332  	return d.create(id, parent, opts)
   333  }
   334  
   335  // Create is used to create the upper, lower, and merge directories required for overlay fs for a given id.
   336  // The parent filesystem is used to configure these directories for the overlay.
   337  func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) (retErr error) {
   338  	if opts != nil && len(opts.StorageOpt) != 0 {
   339  		if _, ok := opts.StorageOpt["size"]; ok {
   340  			return fmt.Errorf("--storage-opt size is only supported for ReadWrite Layers")
   341  		}
   342  	}
   343  	return d.create(id, parent, opts)
   344  }
   345  
   346  func (d *Driver) create(id, parent string, opts *graphdriver.CreateOpts) (retErr error) {
   347  	dir := d.dir(id)
   348  
   349  	root := d.idMap.RootPair()
   350  	dirID := idtools.Identity{
   351  		UID: idtools.CurrentIdentity().UID,
   352  		GID: root.GID,
   353  	}
   354  
   355  	if err := idtools.MkdirAllAndChown(path.Dir(dir), 0710, dirID); err != nil {
   356  		return err
   357  	}
   358  	if err := idtools.MkdirAndChown(dir, 0710, dirID); err != nil {
   359  		return err
   360  	}
   361  
   362  	defer func() {
   363  		// Clean up on failure
   364  		if retErr != nil {
   365  			os.RemoveAll(dir)
   366  		}
   367  	}()
   368  
   369  	if opts != nil && len(opts.StorageOpt) > 0 {
   370  		driver := &Driver{}
   371  		if err := d.parseStorageOpt(opts.StorageOpt, driver); err != nil {
   372  			return err
   373  		}
   374  
   375  		if driver.options.quota.Size > 0 {
   376  			// Set container disk quota limit
   377  			if err := d.quotaCtl.SetQuota(dir, driver.options.quota); err != nil {
   378  				return err
   379  			}
   380  		}
   381  	}
   382  
   383  	if err := idtools.MkdirAndChown(path.Join(dir, diffDirName), 0755, root); err != nil {
   384  		return err
   385  	}
   386  
   387  	lid := overlayutils.GenerateID(idLength, logger)
   388  	if err := os.Symlink(path.Join("..", id, diffDirName), path.Join(d.home, linkDir, lid)); err != nil {
   389  		return err
   390  	}
   391  
   392  	// Write link id to link file
   393  	if err := ioutils.AtomicWriteFile(path.Join(dir, "link"), []byte(lid), 0o644); err != nil {
   394  		return err
   395  	}
   396  
   397  	// if no parent directory, done
   398  	if parent == "" {
   399  		return nil
   400  	}
   401  
   402  	if err := idtools.MkdirAndChown(path.Join(dir, workDirName), 0700, root); err != nil {
   403  		return err
   404  	}
   405  
   406  	if err := ioutils.AtomicWriteFile(path.Join(d.dir(parent), "committed"), []byte{}, 0o600); err != nil {
   407  		return err
   408  	}
   409  
   410  	lower, err := d.getLower(parent)
   411  	if err != nil {
   412  		return err
   413  	}
   414  	if lower != "" {
   415  		if err := ioutils.AtomicWriteFile(path.Join(dir, lowerFile), []byte(lower), 0o644); err != nil {
   416  			return err
   417  		}
   418  	}
   419  
   420  	return nil
   421  }
   422  
   423  // Parse overlay storage options
   424  func (d *Driver) parseStorageOpt(storageOpt map[string]string, driver *Driver) error {
   425  	// Read size to set the disk project quota per container
   426  	for key, val := range storageOpt {
   427  		key := strings.ToLower(key)
   428  		switch key {
   429  		case "size":
   430  			size, err := units.RAMInBytes(val)
   431  			if err != nil {
   432  				return err
   433  			}
   434  			driver.options.quota.Size = uint64(size)
   435  		default:
   436  			return fmt.Errorf("Unknown option %s", key)
   437  		}
   438  	}
   439  
   440  	return nil
   441  }
   442  
   443  func (d *Driver) getLower(parent string) (string, error) {
   444  	parentDir := d.dir(parent)
   445  
   446  	// Ensure parent exists
   447  	if _, err := os.Lstat(parentDir); err != nil {
   448  		return "", err
   449  	}
   450  
   451  	// Read Parent link fileA
   452  	parentLink, err := os.ReadFile(path.Join(parentDir, "link"))
   453  	if err != nil {
   454  		return "", err
   455  	}
   456  	lowers := []string{path.Join(linkDir, string(parentLink))}
   457  
   458  	parentLower, err := os.ReadFile(path.Join(parentDir, lowerFile))
   459  	if err == nil {
   460  		parentLowers := strings.Split(string(parentLower), ":")
   461  		lowers = append(lowers, parentLowers...)
   462  	}
   463  	if len(lowers) > maxDepth {
   464  		return "", errors.New("max depth exceeded")
   465  	}
   466  	return strings.Join(lowers, ":"), nil
   467  }
   468  
   469  func (d *Driver) dir(id string) string {
   470  	return path.Join(d.home, id)
   471  }
   472  
   473  func (d *Driver) getLowerDirs(id string) ([]string, error) {
   474  	var lowersArray []string
   475  	lowers, err := os.ReadFile(path.Join(d.dir(id), lowerFile))
   476  	if err == nil {
   477  		for _, s := range strings.Split(string(lowers), ":") {
   478  			lp, err := os.Readlink(path.Join(d.home, s))
   479  			if err != nil {
   480  				return nil, err
   481  			}
   482  			lowersArray = append(lowersArray, path.Clean(path.Join(d.home, linkDir, lp)))
   483  		}
   484  	} else if !os.IsNotExist(err) {
   485  		return nil, err
   486  	}
   487  	return lowersArray, nil
   488  }
   489  
   490  // Remove cleans the directories that are created for this id.
   491  func (d *Driver) Remove(id string) error {
   492  	if id == "" {
   493  		return fmt.Errorf("refusing to remove the directories: id is empty")
   494  	}
   495  	d.locker.Lock(id)
   496  	defer d.locker.Unlock(id)
   497  	dir := d.dir(id)
   498  	lid, err := os.ReadFile(path.Join(dir, "link"))
   499  	if err == nil {
   500  		if len(lid) == 0 {
   501  			logger.Errorf("refusing to remove empty link for layer %v", id)
   502  		} else if err := os.RemoveAll(path.Join(d.home, linkDir, string(lid))); err != nil {
   503  			logger.Debugf("Failed to remove link: %v", err)
   504  		}
   505  	}
   506  
   507  	if err := containerfs.EnsureRemoveAll(dir); err != nil && !os.IsNotExist(err) {
   508  		return err
   509  	}
   510  	return nil
   511  }
   512  
   513  // Get creates and mounts the required file system for the given id and returns the mount path.
   514  func (d *Driver) Get(id, mountLabel string) (_ containerfs.ContainerFS, retErr error) {
   515  	d.locker.Lock(id)
   516  	defer d.locker.Unlock(id)
   517  	dir := d.dir(id)
   518  	if _, err := os.Stat(dir); err != nil {
   519  		return nil, err
   520  	}
   521  
   522  	diffDir := path.Join(dir, diffDirName)
   523  	lowers, err := os.ReadFile(path.Join(dir, lowerFile))
   524  	if err != nil {
   525  		// If no lower, just return diff directory
   526  		if os.IsNotExist(err) {
   527  			return containerfs.NewLocalContainerFS(diffDir), nil
   528  		}
   529  		return nil, err
   530  	}
   531  
   532  	mergedDir := path.Join(dir, mergedDirName)
   533  	if count := d.ctr.Increment(mergedDir); count > 1 {
   534  		return containerfs.NewLocalContainerFS(mergedDir), nil
   535  	}
   536  	defer func() {
   537  		if retErr != nil {
   538  			if c := d.ctr.Decrement(mergedDir); c <= 0 {
   539  				if mntErr := unix.Unmount(mergedDir, 0); mntErr != nil {
   540  					logger.Errorf("error unmounting %v: %v", mergedDir, mntErr)
   541  				}
   542  				// Cleanup the created merged directory; see the comment in Put's rmdir
   543  				if rmErr := unix.Rmdir(mergedDir); rmErr != nil && !os.IsNotExist(rmErr) {
   544  					logger.Debugf("Failed to remove %s: %v: %v", id, rmErr, err)
   545  				}
   546  			}
   547  		}
   548  	}()
   549  
   550  	workDir := path.Join(dir, workDirName)
   551  	splitLowers := strings.Split(string(lowers), ":")
   552  	absLowers := make([]string, len(splitLowers))
   553  	for i, s := range splitLowers {
   554  		absLowers[i] = path.Join(d.home, s)
   555  	}
   556  	var readonly bool
   557  	if _, err := os.Stat(path.Join(dir, "committed")); err == nil {
   558  		readonly = true
   559  	} else if !os.IsNotExist(err) {
   560  		return nil, err
   561  	}
   562  
   563  	var opts string
   564  	if readonly {
   565  		opts = indexOff + userxattr + "lowerdir=" + diffDir + ":" + strings.Join(absLowers, ":")
   566  	} else {
   567  		opts = indexOff + userxattr + "lowerdir=" + strings.Join(absLowers, ":") + ",upperdir=" + diffDir + ",workdir=" + workDir
   568  	}
   569  
   570  	mountData := label.FormatMountLabel(opts, mountLabel)
   571  	mount := unix.Mount
   572  	mountTarget := mergedDir
   573  
   574  	root := d.idMap.RootPair()
   575  	if err := idtools.MkdirAndChown(mergedDir, 0700, root); err != nil {
   576  		return nil, err
   577  	}
   578  
   579  	pageSize := unix.Getpagesize()
   580  
   581  	// Use relative paths and mountFrom when the mount data has exceeded
   582  	// the page size. The mount syscall fails if the mount data cannot
   583  	// fit within a page and relative links make the mount data much
   584  	// smaller at the expense of requiring a fork exec to chroot.
   585  	if len(mountData) > pageSize-1 {
   586  		if readonly {
   587  			opts = indexOff + userxattr + "lowerdir=" + path.Join(id, diffDirName) + ":" + string(lowers)
   588  		} else {
   589  			opts = indexOff + userxattr + "lowerdir=" + string(lowers) + ",upperdir=" + path.Join(id, diffDirName) + ",workdir=" + path.Join(id, workDirName)
   590  		}
   591  		mountData = label.FormatMountLabel(opts, mountLabel)
   592  		if len(mountData) > pageSize-1 {
   593  			return nil, fmt.Errorf("cannot mount layer, mount label too large %d", len(mountData))
   594  		}
   595  
   596  		mount = func(source string, target string, mType string, flags uintptr, label string) error {
   597  			return mountFrom(d.home, source, target, mType, flags, label)
   598  		}
   599  		mountTarget = path.Join(id, mergedDirName)
   600  	}
   601  
   602  	if err := mount("overlay", mountTarget, "overlay", 0, mountData); err != nil {
   603  		return nil, fmt.Errorf("error creating overlay mount to %s: %v", mergedDir, err)
   604  	}
   605  
   606  	if !readonly {
   607  		// chown "workdir/work" to the remapped root UID/GID. Overlay fs inside a
   608  		// user namespace requires this to move a directory from lower to upper.
   609  		if err := root.Chown(path.Join(workDir, workDirName)); err != nil {
   610  			return nil, err
   611  		}
   612  	}
   613  
   614  	return containerfs.NewLocalContainerFS(mergedDir), nil
   615  }
   616  
   617  // Put unmounts the mount path created for the give id.
   618  // It also removes the 'merged' directory to force the kernel to unmount the
   619  // overlay mount in other namespaces.
   620  func (d *Driver) Put(id string) error {
   621  	d.locker.Lock(id)
   622  	defer d.locker.Unlock(id)
   623  	dir := d.dir(id)
   624  	_, err := os.ReadFile(path.Join(dir, lowerFile))
   625  	if err != nil {
   626  		// If no lower, no mount happened and just return directly
   627  		if os.IsNotExist(err) {
   628  			return nil
   629  		}
   630  		return err
   631  	}
   632  
   633  	mountpoint := path.Join(dir, mergedDirName)
   634  	if count := d.ctr.Decrement(mountpoint); count > 0 {
   635  		return nil
   636  	}
   637  	if err := unix.Unmount(mountpoint, unix.MNT_DETACH); err != nil {
   638  		logger.Debugf("Failed to unmount %s overlay: %s - %v", id, mountpoint, err)
   639  	}
   640  	// Remove the mountpoint here. Removing the mountpoint (in newer kernels)
   641  	// will cause all other instances of this mount in other mount namespaces
   642  	// to be unmounted. This is necessary to avoid cases where an overlay mount
   643  	// that is present in another namespace will cause subsequent mounts
   644  	// operations to fail with ebusy.  We ignore any errors here because this may
   645  	// fail on older kernels which don't have
   646  	// torvalds/linux@8ed936b5671bfb33d89bc60bdcc7cf0470ba52fe applied.
   647  	if err := unix.Rmdir(mountpoint); err != nil && !os.IsNotExist(err) {
   648  		logger.Debugf("Failed to remove %s overlay: %v", id, err)
   649  	}
   650  	return nil
   651  }
   652  
   653  // Exists checks to see if the id is already mounted.
   654  func (d *Driver) Exists(id string) bool {
   655  	_, err := os.Stat(d.dir(id))
   656  	return err == nil
   657  }
   658  
   659  // isParent determines whether the given parent is the direct parent of the
   660  // given layer id
   661  func (d *Driver) isParent(id, parent string) bool {
   662  	lowers, err := d.getLowerDirs(id)
   663  	if err != nil {
   664  		return false
   665  	}
   666  	if parent == "" && len(lowers) > 0 {
   667  		return false
   668  	}
   669  
   670  	parentDir := d.dir(parent)
   671  	var ld string
   672  	if len(lowers) > 0 {
   673  		ld = filepath.Dir(lowers[0])
   674  	}
   675  	if ld == "" && parent == "" {
   676  		return true
   677  	}
   678  	return ld == parentDir
   679  }
   680  
   681  // ApplyDiff applies the new layer into a root
   682  func (d *Driver) ApplyDiff(id string, parent string, diff io.Reader) (size int64, err error) {
   683  	if useNaiveDiff(d.home) || !d.isParent(id, parent) {
   684  		return d.naiveDiff.ApplyDiff(id, parent, diff)
   685  	}
   686  
   687  	// never reach here if we are running in UserNS
   688  	applyDir := d.getDiffPath(id)
   689  
   690  	logger.Debugf("Applying tar in %s", applyDir)
   691  	// Overlay doesn't need the parent id to apply the diff
   692  	if err := untar(diff, applyDir, &archive.TarOptions{
   693  		IDMap:          d.idMap,
   694  		WhiteoutFormat: archive.OverlayWhiteoutFormat,
   695  	}); err != nil {
   696  		return 0, err
   697  	}
   698  
   699  	return directory.Size(context.TODO(), applyDir)
   700  }
   701  
   702  func (d *Driver) getDiffPath(id string) string {
   703  	dir := d.dir(id)
   704  
   705  	return path.Join(dir, diffDirName)
   706  }
   707  
   708  // DiffSize calculates the changes between the specified id
   709  // and its parent and returns the size in bytes of the changes
   710  // relative to its base filesystem directory.
   711  func (d *Driver) DiffSize(id, parent string) (size int64, err error) {
   712  	if useNaiveDiff(d.home) || !d.isParent(id, parent) {
   713  		return d.naiveDiff.DiffSize(id, parent)
   714  	}
   715  	return directory.Size(context.TODO(), d.getDiffPath(id))
   716  }
   717  
   718  // Diff produces an archive of the changes between the specified
   719  // layer and its parent layer which may be "".
   720  func (d *Driver) Diff(id, parent string) (io.ReadCloser, error) {
   721  	if useNaiveDiff(d.home) || !d.isParent(id, parent) {
   722  		return d.naiveDiff.Diff(id, parent)
   723  	}
   724  
   725  	// never reach here if we are running in UserNS
   726  	diffPath := d.getDiffPath(id)
   727  	logger.Debugf("Tar with options on %s", diffPath)
   728  	return archive.TarWithOptions(diffPath, &archive.TarOptions{
   729  		Compression:    archive.Uncompressed,
   730  		IDMap:          d.idMap,
   731  		WhiteoutFormat: archive.OverlayWhiteoutFormat,
   732  	})
   733  }
   734  
   735  // Changes produces a list of changes between the specified layer and its
   736  // parent layer. If parent is "", then all changes will be ADD changes.
   737  func (d *Driver) Changes(id, parent string) ([]archive.Change, error) {
   738  	return d.naiveDiff.Changes(id, parent)
   739  }