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