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