github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/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 "syscall" 19 20 "github.com/Sirupsen/logrus" 21 22 "github.com/docker/docker/daemon/graphdriver" 23 "github.com/docker/docker/daemon/graphdriver/overlayutils" 24 "github.com/docker/docker/daemon/graphdriver/quota" 25 "github.com/docker/docker/pkg/archive" 26 "github.com/docker/docker/pkg/chrootarchive" 27 "github.com/docker/docker/pkg/directory" 28 "github.com/docker/docker/pkg/fsutils" 29 "github.com/docker/docker/pkg/idtools" 30 "github.com/docker/docker/pkg/locker" 31 "github.com/docker/docker/pkg/mount" 32 "github.com/docker/docker/pkg/parsers" 33 "github.com/docker/docker/pkg/parsers/kernel" 34 units "github.com/docker/go-units" 35 36 "github.com/opencontainers/runc/libcontainer/label" 37 ) 38 39 var ( 40 // untar defines the untar method 41 untar = chrootarchive.UntarUncompressed 42 ) 43 44 // This backend uses the overlay union filesystem for containers 45 // with diff directories for each layer. 46 47 // This version of the overlay driver requires at least kernel 48 // 4.0.0 in order to support mounting multiple diff directories. 49 50 // Each container/image has at least a "diff" directory and "link" file. 51 // If there is also a "lower" file when there are diff layers 52 // below as well as "merged" and "work" directories. The "diff" directory 53 // has the upper layer of the overlay and is used to capture any 54 // changes to the layer. The "lower" file contains all the lower layer 55 // mounts separated by ":" and ordered from uppermost to lowermost 56 // layers. The overlay itself is mounted in the "merged" directory, 57 // and the "work" dir is needed for overlay to work. 58 59 // The "link" file for each layer contains a unique string for the layer. 60 // Under the "l" directory at the root there will be a symbolic link 61 // with that unique string pointing the "diff" directory for the layer. 62 // The symbolic links are used to reference lower layers in the "lower" 63 // file and on mount. The links are used to shorten the total length 64 // of a layer reference without requiring changes to the layer identifier 65 // or root directory. Mounts are always done relative to root and 66 // referencing the symbolic links in order to ensure the number of 67 // lower directories can fit in a single page for making the mount 68 // syscall. A hard upper limit of 128 lower layers is enforced to ensure 69 // that mounts do not fail due to length. 70 71 const ( 72 driverName = "overlay2" 73 linkDir = "l" 74 lowerFile = "lower" 75 maxDepth = 128 76 77 // idLength represents the number of random characters 78 // which can be used to create the unique link identifer 79 // for every layer. If this value is too long then the 80 // page size limit for the mount command may be exceeded. 81 // The idLength should be selected such that following equation 82 // is true (512 is a buffer for label metadata). 83 // ((idLength + len(linkDir) + 1) * maxDepth) <= (pageSize - 512) 84 idLength = 26 85 ) 86 87 type overlayOptions struct { 88 overrideKernelCheck bool 89 quota quota.Quota 90 } 91 92 // Driver contains information about the home directory and the list of active mounts that are created using this driver. 93 type Driver struct { 94 home string 95 uidMaps []idtools.IDMap 96 gidMaps []idtools.IDMap 97 ctr *graphdriver.RefCounter 98 quotaCtl *quota.Control 99 options overlayOptions 100 naiveDiff graphdriver.DiffDriver 101 supportsDType bool 102 locker *locker.Locker 103 } 104 105 var ( 106 backingFs = "<unknown>" 107 projectQuotaSupported = false 108 109 useNaiveDiffLock sync.Once 110 useNaiveDiffOnly bool 111 ) 112 113 func init() { 114 graphdriver.Register(driverName, Init) 115 } 116 117 // Init returns the a native diff driver for overlay filesystem. 118 // If overlay filesystem is not supported on the host, graphdriver.ErrNotSupported is returned as error. 119 // If an overlay filesystem is not supported over an existing filesystem then error graphdriver.ErrIncompatibleFS is returned. 120 func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) { 121 opts, err := parseOptions(options) 122 if err != nil { 123 return nil, err 124 } 125 126 if err := supportsOverlay(); err != nil { 127 return nil, graphdriver.ErrNotSupported 128 } 129 130 // require kernel 4.0.0 to ensure multiple lower dirs are supported 131 v, err := kernel.GetKernelVersion() 132 if err != nil { 133 return nil, err 134 } 135 if kernel.CompareKernelVersion(*v, kernel.VersionInfo{Kernel: 4, Major: 0, Minor: 0}) < 0 { 136 if !opts.overrideKernelCheck { 137 return nil, graphdriver.ErrNotSupported 138 } 139 logrus.Warn("Using pre-4.0.0 kernel for overlay2, mount failures may require kernel update") 140 } 141 142 fsMagic, err := graphdriver.GetFSMagic(home) 143 if err != nil { 144 return nil, err 145 } 146 if fsName, ok := graphdriver.FsNames[fsMagic]; ok { 147 backingFs = fsName 148 } 149 150 // check if they are running over btrfs, aufs, zfs, overlay, or ecryptfs 151 switch fsMagic { 152 case graphdriver.FsMagicBtrfs, graphdriver.FsMagicAufs, graphdriver.FsMagicZfs, graphdriver.FsMagicOverlay, graphdriver.FsMagicEcryptfs: 153 logrus.Errorf("'overlay2' is not supported over %s", backingFs) 154 return nil, graphdriver.ErrIncompatibleFS 155 } 156 157 rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps) 158 if err != nil { 159 return nil, err 160 } 161 // Create the driver home dir 162 if err := idtools.MkdirAllAs(path.Join(home, linkDir), 0700, rootUID, rootGID); err != nil && !os.IsExist(err) { 163 return nil, err 164 } 165 166 if err := mount.MakePrivate(home); err != nil { 167 return nil, err 168 } 169 170 supportsDType, err := fsutils.SupportsDType(home) 171 if err != nil { 172 return nil, err 173 } 174 if !supportsDType { 175 // not a fatal error until v1.16 (#27443) 176 logrus.Warn(overlayutils.ErrDTypeNotSupported("overlay2", backingFs)) 177 } 178 179 d := &Driver{ 180 home: home, 181 uidMaps: uidMaps, 182 gidMaps: gidMaps, 183 ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicOverlay)), 184 supportsDType: supportsDType, 185 locker: locker.New(), 186 } 187 188 d.naiveDiff = graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps) 189 190 if backingFs == "xfs" { 191 // Try to enable project quota support over xfs. 192 if d.quotaCtl, err = quota.NewControl(home); err == nil { 193 projectQuotaSupported = true 194 } 195 } 196 197 logrus.Debugf("backingFs=%s, projectQuotaSupported=%v", backingFs, projectQuotaSupported) 198 199 return d, nil 200 } 201 202 func parseOptions(options []string) (*overlayOptions, error) { 203 o := &overlayOptions{} 204 for _, option := range options { 205 key, val, err := parsers.ParseKeyValueOpt(option) 206 if err != nil { 207 return nil, err 208 } 209 key = strings.ToLower(key) 210 switch key { 211 case "overlay2.override_kernel_check": 212 o.overrideKernelCheck, err = strconv.ParseBool(val) 213 if err != nil { 214 return nil, err 215 } 216 217 default: 218 return nil, fmt.Errorf("overlay2: Unknown option %s\n", key) 219 } 220 } 221 return o, nil 222 } 223 224 func supportsOverlay() error { 225 // We can try to modprobe overlay first before looking at 226 // proc/filesystems for when overlay is supported 227 exec.Command("modprobe", "overlay").Run() 228 229 f, err := os.Open("/proc/filesystems") 230 if err != nil { 231 return err 232 } 233 defer f.Close() 234 235 s := bufio.NewScanner(f) 236 for s.Scan() { 237 if s.Text() == "nodev\toverlay" { 238 return nil 239 } 240 } 241 logrus.Error("'overlay' not found as a supported filesystem on this host. Please ensure kernel is new enough and has overlay support loaded.") 242 return graphdriver.ErrNotSupported 243 } 244 245 func useNaiveDiff(home string) bool { 246 useNaiveDiffLock.Do(func() { 247 if err := hasOpaqueCopyUpBug(home); err != nil { 248 logrus.Warnf("Not using native diff for overlay2: %v", err) 249 useNaiveDiffOnly = true 250 } 251 }) 252 return useNaiveDiffOnly 253 } 254 255 func (d *Driver) String() string { 256 return driverName 257 } 258 259 // Status returns current driver information in a two dimensional string array. 260 // Output contains "Backing Filesystem" used in this implementation. 261 func (d *Driver) Status() [][2]string { 262 return [][2]string{ 263 {"Backing Filesystem", backingFs}, 264 {"Supports d_type", strconv.FormatBool(d.supportsDType)}, 265 {"Native Overlay Diff", strconv.FormatBool(!useNaiveDiff(d.home))}, 266 } 267 } 268 269 // GetMetadata returns meta data about the overlay driver such as 270 // LowerDir, UpperDir, WorkDir and MergeDir used to store data. 271 func (d *Driver) GetMetadata(id string) (map[string]string, error) { 272 dir := d.dir(id) 273 if _, err := os.Stat(dir); err != nil { 274 return nil, err 275 } 276 277 metadata := map[string]string{ 278 "WorkDir": path.Join(dir, "work"), 279 "MergedDir": path.Join(dir, "merged"), 280 "UpperDir": path.Join(dir, "diff"), 281 } 282 283 lowerDirs, err := d.getLowerDirs(id) 284 if err != nil { 285 return nil, err 286 } 287 if len(lowerDirs) > 0 { 288 metadata["LowerDir"] = strings.Join(lowerDirs, ":") 289 } 290 291 return metadata, nil 292 } 293 294 // Cleanup any state created by overlay which should be cleaned when daemon 295 // is being shutdown. For now, we just have to unmount the bind mounted 296 // we had created. 297 func (d *Driver) Cleanup() error { 298 return mount.Unmount(d.home) 299 } 300 301 // CreateReadWrite creates a layer that is writable for use as a container 302 // file system. 303 func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error { 304 return d.Create(id, parent, opts) 305 } 306 307 // Create is used to create the upper, lower, and merge directories required for overlay fs for a given id. 308 // The parent filesystem is used to configure these directories for the overlay. 309 func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) (retErr error) { 310 311 if opts != nil && len(opts.StorageOpt) != 0 && !projectQuotaSupported { 312 return fmt.Errorf("--storage-opt is supported only for overlay over xfs with 'pquota' mount option") 313 } 314 315 dir := d.dir(id) 316 317 rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps) 318 if err != nil { 319 return err 320 } 321 if err := idtools.MkdirAllAs(path.Dir(dir), 0700, rootUID, rootGID); err != nil { 322 return err 323 } 324 if err := idtools.MkdirAs(dir, 0700, rootUID, rootGID); err != nil { 325 return err 326 } 327 328 defer func() { 329 // Clean up on failure 330 if retErr != nil { 331 os.RemoveAll(dir) 332 } 333 }() 334 335 if opts != nil && len(opts.StorageOpt) > 0 { 336 driver := &Driver{} 337 if err := d.parseStorageOpt(opts.StorageOpt, driver); err != nil { 338 return err 339 } 340 341 if driver.options.quota.Size > 0 { 342 // Set container disk quota limit 343 if err := d.quotaCtl.SetQuota(dir, driver.options.quota); err != nil { 344 return err 345 } 346 } 347 } 348 349 if err := idtools.MkdirAs(path.Join(dir, "diff"), 0755, rootUID, rootGID); err != nil { 350 return err 351 } 352 353 lid := generateID(idLength) 354 if err := os.Symlink(path.Join("..", id, "diff"), path.Join(d.home, linkDir, lid)); err != nil { 355 return err 356 } 357 358 // Write link id to link file 359 if err := ioutil.WriteFile(path.Join(dir, "link"), []byte(lid), 0644); err != nil { 360 return err 361 } 362 363 // if no parent directory, done 364 if parent == "" { 365 return nil 366 } 367 368 if err := idtools.MkdirAs(path.Join(dir, "work"), 0700, rootUID, rootGID); err != nil { 369 return err 370 } 371 if err := idtools.MkdirAs(path.Join(dir, "merged"), 0700, rootUID, rootGID); err != nil { 372 return err 373 } 374 375 lower, err := d.getLower(parent) 376 if err != nil { 377 return err 378 } 379 if lower != "" { 380 if err := ioutil.WriteFile(path.Join(dir, lowerFile), []byte(lower), 0666); err != nil { 381 return err 382 } 383 } 384 385 return nil 386 } 387 388 // Parse overlay storage options 389 func (d *Driver) parseStorageOpt(storageOpt map[string]string, driver *Driver) error { 390 // Read size to set the disk project quota per container 391 for key, val := range storageOpt { 392 key := strings.ToLower(key) 393 switch key { 394 case "size": 395 size, err := units.RAMInBytes(val) 396 if err != nil { 397 return err 398 } 399 driver.options.quota.Size = uint64(size) 400 default: 401 return fmt.Errorf("Unknown option %s", key) 402 } 403 } 404 405 return nil 406 } 407 408 func (d *Driver) getLower(parent string) (string, error) { 409 parentDir := d.dir(parent) 410 411 // Ensure parent exists 412 if _, err := os.Lstat(parentDir); err != nil { 413 return "", err 414 } 415 416 // Read Parent link fileA 417 parentLink, err := ioutil.ReadFile(path.Join(parentDir, "link")) 418 if err != nil { 419 return "", err 420 } 421 lowers := []string{path.Join(linkDir, string(parentLink))} 422 423 parentLower, err := ioutil.ReadFile(path.Join(parentDir, lowerFile)) 424 if err == nil { 425 parentLowers := strings.Split(string(parentLower), ":") 426 lowers = append(lowers, parentLowers...) 427 } 428 if len(lowers) > maxDepth { 429 return "", errors.New("max depth exceeded") 430 } 431 return strings.Join(lowers, ":"), nil 432 } 433 434 func (d *Driver) dir(id string) string { 435 return path.Join(d.home, id) 436 } 437 438 func (d *Driver) getLowerDirs(id string) ([]string, error) { 439 var lowersArray []string 440 lowers, err := ioutil.ReadFile(path.Join(d.dir(id), lowerFile)) 441 if err == nil { 442 for _, s := range strings.Split(string(lowers), ":") { 443 lp, err := os.Readlink(path.Join(d.home, s)) 444 if err != nil { 445 return nil, err 446 } 447 lowersArray = append(lowersArray, path.Clean(path.Join(d.home, linkDir, lp))) 448 } 449 } else if !os.IsNotExist(err) { 450 return nil, err 451 } 452 return lowersArray, nil 453 } 454 455 // Remove cleans the directories that are created for this id. 456 func (d *Driver) Remove(id string) error { 457 d.locker.Lock(id) 458 defer d.locker.Unlock(id) 459 dir := d.dir(id) 460 lid, err := ioutil.ReadFile(path.Join(dir, "link")) 461 if err == nil { 462 if err := os.RemoveAll(path.Join(d.home, linkDir, string(lid))); err != nil { 463 logrus.Debugf("Failed to remove link: %v", err) 464 } 465 } 466 467 if err := os.RemoveAll(dir); err != nil && !os.IsNotExist(err) { 468 return err 469 } 470 return nil 471 } 472 473 // Get creates and mounts the required file system for the given id and returns the mount path. 474 func (d *Driver) Get(id string, mountLabel string) (s string, err error) { 475 d.locker.Lock(id) 476 defer d.locker.Unlock(id) 477 dir := d.dir(id) 478 if _, err := os.Stat(dir); err != nil { 479 return "", err 480 } 481 482 diffDir := path.Join(dir, "diff") 483 lowers, err := ioutil.ReadFile(path.Join(dir, lowerFile)) 484 if err != nil { 485 // If no lower, just return diff directory 486 if os.IsNotExist(err) { 487 return diffDir, nil 488 } 489 return "", err 490 } 491 492 mergedDir := path.Join(dir, "merged") 493 if count := d.ctr.Increment(mergedDir); count > 1 { 494 return mergedDir, nil 495 } 496 defer func() { 497 if err != nil { 498 if c := d.ctr.Decrement(mergedDir); c <= 0 { 499 syscall.Unmount(mergedDir, 0) 500 } 501 } 502 }() 503 504 workDir := path.Join(dir, "work") 505 splitLowers := strings.Split(string(lowers), ":") 506 absLowers := make([]string, len(splitLowers)) 507 for i, s := range splitLowers { 508 absLowers[i] = path.Join(d.home, s) 509 } 510 opts := fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", strings.Join(absLowers, ":"), path.Join(dir, "diff"), path.Join(dir, "work")) 511 mountData := label.FormatMountLabel(opts, mountLabel) 512 mount := syscall.Mount 513 mountTarget := mergedDir 514 515 pageSize := syscall.Getpagesize() 516 517 // Go can return a larger page size than supported by the system 518 // as of go 1.7. This will be fixed in 1.8 and this block can be 519 // removed when building with 1.8. 520 // See https://github.com/golang/go/commit/1b9499b06989d2831e5b156161d6c07642926ee1 521 // See https://github.com/docker/docker/issues/27384 522 if pageSize > 4096 { 523 pageSize = 4096 524 } 525 526 // Use relative paths and mountFrom when the mount data has exceeded 527 // the page size. The mount syscall fails if the mount data cannot 528 // fit within a page and relative links make the mount data much 529 // smaller at the expense of requiring a fork exec to chroot. 530 if len(mountData) > pageSize { 531 opts = fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", string(lowers), path.Join(id, "diff"), path.Join(id, "work")) 532 mountData = label.FormatMountLabel(opts, mountLabel) 533 if len(mountData) > pageSize { 534 return "", fmt.Errorf("cannot mount layer, mount label too large %d", len(mountData)) 535 } 536 537 mount = func(source string, target string, mType string, flags uintptr, label string) error { 538 return mountFrom(d.home, source, target, mType, flags, label) 539 } 540 mountTarget = path.Join(id, "merged") 541 } 542 543 if err := mount("overlay", mountTarget, "overlay", 0, mountData); err != nil { 544 return "", fmt.Errorf("error creating overlay mount to %s: %v", mergedDir, err) 545 } 546 547 // chown "workdir/work" to the remapped root UID/GID. Overlay fs inside a 548 // user namespace requires this to move a directory from lower to upper. 549 rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps) 550 if err != nil { 551 return "", err 552 } 553 554 if err := os.Chown(path.Join(workDir, "work"), rootUID, rootGID); err != nil { 555 return "", err 556 } 557 558 return mergedDir, nil 559 } 560 561 // Put unmounts the mount path created for the give id. 562 func (d *Driver) Put(id string) error { 563 d.locker.Lock(id) 564 defer d.locker.Unlock(id) 565 dir := d.dir(id) 566 _, err := ioutil.ReadFile(path.Join(dir, lowerFile)) 567 if err != nil { 568 // If no lower, no mount happened and just return directly 569 if os.IsNotExist(err) { 570 return nil 571 } 572 return err 573 } 574 575 mountpoint := path.Join(dir, "merged") 576 if count := d.ctr.Decrement(mountpoint); count > 0 { 577 return nil 578 } 579 if err := syscall.Unmount(mountpoint, 0); err != nil { 580 logrus.Debugf("Failed to unmount %s overlay: %s - %v", id, mountpoint, err) 581 } 582 return nil 583 } 584 585 // Exists checks to see if the id is already mounted. 586 func (d *Driver) Exists(id string) bool { 587 _, err := os.Stat(d.dir(id)) 588 return err == nil 589 } 590 591 // isParent returns if the passed in parent is the direct parent of the passed in layer 592 func (d *Driver) isParent(id, parent string) bool { 593 lowers, err := d.getLowerDirs(id) 594 if err != nil { 595 return false 596 } 597 if parent == "" && len(lowers) > 0 { 598 return false 599 } 600 601 parentDir := d.dir(parent) 602 var ld string 603 if len(lowers) > 0 { 604 ld = filepath.Dir(lowers[0]) 605 } 606 if ld == "" && parent == "" { 607 return true 608 } 609 return ld == parentDir 610 } 611 612 // ApplyDiff applies the new layer into a root 613 func (d *Driver) ApplyDiff(id string, parent string, diff io.Reader) (size int64, err error) { 614 if !d.isParent(id, parent) { 615 return d.naiveDiff.ApplyDiff(id, parent, diff) 616 } 617 618 applyDir := d.getDiffPath(id) 619 620 logrus.Debugf("Applying tar in %s", applyDir) 621 // Overlay doesn't need the parent id to apply the diff 622 if err := untar(diff, applyDir, &archive.TarOptions{ 623 UIDMaps: d.uidMaps, 624 GIDMaps: d.gidMaps, 625 WhiteoutFormat: archive.OverlayWhiteoutFormat, 626 }); err != nil { 627 return 0, err 628 } 629 630 return directory.Size(applyDir) 631 } 632 633 func (d *Driver) getDiffPath(id string) string { 634 dir := d.dir(id) 635 636 return path.Join(dir, "diff") 637 } 638 639 // DiffSize calculates the changes between the specified id 640 // and its parent and returns the size in bytes of the changes 641 // relative to its base filesystem directory. 642 func (d *Driver) DiffSize(id, parent string) (size int64, err error) { 643 if useNaiveDiff(d.home) || !d.isParent(id, parent) { 644 return d.naiveDiff.DiffSize(id, parent) 645 } 646 return directory.Size(d.getDiffPath(id)) 647 } 648 649 // Diff produces an archive of the changes between the specified 650 // layer and its parent layer which may be "". 651 func (d *Driver) Diff(id, parent string) (io.ReadCloser, error) { 652 if useNaiveDiff(d.home) || !d.isParent(id, parent) { 653 return d.naiveDiff.Diff(id, parent) 654 } 655 656 diffPath := d.getDiffPath(id) 657 logrus.Debugf("Tar with options on %s", diffPath) 658 return archive.TarWithOptions(diffPath, &archive.TarOptions{ 659 Compression: archive.Uncompressed, 660 UIDMaps: d.uidMaps, 661 GIDMaps: d.gidMaps, 662 WhiteoutFormat: archive.OverlayWhiteoutFormat, 663 }) 664 } 665 666 // Changes produces a list of changes between the specified layer 667 // and its parent layer. If parent is "", then all changes will be ADD changes. 668 func (d *Driver) Changes(id, parent string) ([]archive.Change, error) { 669 if useNaiveDiff(d.home) || !d.isParent(id, parent) { 670 return d.naiveDiff.Changes(id, parent) 671 } 672 // Overlay doesn't have snapshots, so we need to get changes from all parent 673 // layers. 674 diffPath := d.getDiffPath(id) 675 layers, err := d.getLowerDirs(id) 676 if err != nil { 677 return nil, err 678 } 679 680 return archive.OverlayChanges(layers, diffPath) 681 }