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