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