daemon/graphdriver/overlay/overlay.go
453552c8
 // +build linux
 
2352f00e
 package overlay
453552c8
 
 import (
 	"bufio"
 	"fmt"
aa2cc187
 	"io"
453552c8
 	"io/ioutil"
 	"os"
 	"os/exec"
 	"path"
2e20e63d
 	"strconv"
453552c8
 
 	"github.com/docker/docker/daemon/graphdriver"
2e20e63d
 	"github.com/docker/docker/daemon/graphdriver/overlayutils"
453552c8
 	"github.com/docker/docker/pkg/archive"
7a7357da
 	"github.com/docker/docker/pkg/containerfs"
2e20e63d
 	"github.com/docker/docker/pkg/fsutils"
442b4562
 	"github.com/docker/docker/pkg/idtools"
fc1cf191
 	"github.com/docker/docker/pkg/locker"
e076bccb
 	"github.com/docker/docker/pkg/mount"
54dcbab2
 	"github.com/docker/docker/pkg/system"
abbbf914
 	"github.com/opencontainers/selinux/go-selinux/label"
1009e6a4
 	"github.com/sirupsen/logrus"
069fdc8a
 	"golang.org/x/sys/unix"
453552c8
 )
 
 // This is a small wrapper over the NaiveDiffWriter that lets us have a custom
 // implementation of ApplyDiff()
 
 var (
de394421
 	// ErrApplyDiffFallback is returned to indicate that a normal ApplyDiff is applied as a fallback from Naive diff writer.
453552c8
 	ErrApplyDiffFallback = fmt.Errorf("Fall back to normal ApplyDiff")
1fc0acc9
 	backingFs            = "<unknown>"
453552c8
 )
 
927b334e
 // ApplyDiffProtoDriver wraps the ProtoDriver by extending the interface with ApplyDiff method.
453552c8
 type ApplyDiffProtoDriver interface {
 	graphdriver.ProtoDriver
de394421
 	// ApplyDiff writes the diff to the archive for the given id and parent id.
 	// It returns the size in bytes written if successful, an error ErrApplyDiffFallback is returned otherwise.
aa2cc187
 	ApplyDiff(id, parent string, diff io.Reader) (size int64, err error)
453552c8
 }
 
 type naiveDiffDriverWithApply struct {
 	graphdriver.Driver
 	applyDiff ApplyDiffProtoDriver
 }
 
de394421
 // NaiveDiffDriverWithApply returns a NaiveDiff driver with custom ApplyDiff.
442b4562
 func NaiveDiffDriverWithApply(driver ApplyDiffProtoDriver, uidMaps, gidMaps []idtools.IDMap) graphdriver.Driver {
453552c8
 	return &naiveDiffDriverWithApply{
442b4562
 		Driver:    graphdriver.NewNaiveDiffDriver(driver, uidMaps, gidMaps),
453552c8
 		applyDiff: driver,
 	}
 }
 
de394421
 // ApplyDiff creates a diff layer with either the NaiveDiffDriver or with a fallback.
aa2cc187
 func (d *naiveDiffDriverWithApply) ApplyDiff(id, parent string, diff io.Reader) (int64, error) {
453552c8
 	b, err := d.applyDiff.ApplyDiff(id, parent, diff)
 	if err == ErrApplyDiffFallback {
 		return d.Driver.ApplyDiff(id, parent, diff)
 	}
 	return b, err
 }
 
d680ca5c
 // This backend uses the overlay union filesystem for containers
453552c8
 // plus hard link file sharing for images.
 
 // Each container/image can have a "root" subdirectory which is a plain
d680ca5c
 // filesystem hierarchy, or they can use overlay.
453552c8
 
d680ca5c
 // If they use overlay there is a "upper" directory and a "lower-id"
453552c8
 // file, as well as "merged" and "work" directories. The "upper"
 // directory has the upper layer of the overlay, and "lower-id" contains
 // the id of the parent whose "root" directory shall be used as the lower
 // layer in the overlay. The overlay itself is mounted in the "merged"
d680ca5c
 // directory, and the "work" dir is needed for overlay to work.
453552c8
 
c1be45fa
 // When an overlay layer is created there are two cases, either the
 // parent has a "root" dir, then we start out with an empty "upper"
453552c8
 // directory overlaid on the parents root. This is typically the
 // case with the init layer of a container which is based on an image.
 // If there is no "root" in the parent, we inherit the lower-id from
5bac5302
 // the parent and start by making a copy in the parent's "upper" dir.
453552c8
 // This is typically the case for a container layer which copies
 // its parent -init upper layer.
 
 // Additionally we also have a custom implementation of ApplyLayer
 // which makes a recursive copy of the parent "root" layer using
 // hardlinks to share file data, and then applies the layer on top
 // of that. This means all child images share file (but not directory)
 // data with the parent.
 
de394421
 // Driver contains information about the home directory and the list of active mounts that are created using this driver.
453552c8
 type Driver struct {
2e20e63d
 	home          string
 	uidMaps       []idtools.IDMap
 	gidMaps       []idtools.IDMap
 	ctr           *graphdriver.RefCounter
 	supportsDType bool
fc1cf191
 	locker        *locker.Locker
453552c8
 }
 
 func init() {
d680ca5c
 	graphdriver.Register("overlay", Init)
453552c8
 }
 
de394421
 // Init returns the NaiveDiffDriver, a native diff driver for overlay filesystem.
 // If overlay filesystem is not supported on the host, graphdriver.ErrNotSupported is returned as error.
c1be45fa
 // If an overlay filesystem is not supported over an existing filesystem then error graphdriver.ErrIncompatibleFS is returned.
442b4562
 func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
48b1dd00
 
2352f00e
 	if err := supportsOverlay(); err != nil {
453552c8
 		return nil, graphdriver.ErrNotSupported
 	}
 
48b1dd00
 	fsMagic, err := graphdriver.GetFSMagic(home)
 	if err != nil {
32f1025b
 		return nil, err
 	}
48b1dd00
 	if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
 		backingFs = fsName
 	}
32f1025b
 
48b1dd00
 	switch fsMagic {
5e85ec82
 	case graphdriver.FsMagicAufs, graphdriver.FsMagicBtrfs, graphdriver.FsMagicOverlay, graphdriver.FsMagicZfs, graphdriver.FsMagicEcryptfs:
1fc0acc9
 		logrus.Errorf("'overlay' is not supported over %s", backingFs)
824c72f4
 		return nil, graphdriver.ErrIncompatibleFS
32f1025b
 	}
 
442b4562
 	rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
 	if err != nil {
 		return nil, err
 	}
453552c8
 	// Create the driver home dir
e8532023
 	if err := idtools.MkdirAllAs(home, 0700, rootUID, rootGID); err != nil && !os.IsExist(err) {
453552c8
 		return nil, err
 	}
 
e076bccb
 	if err := mount.MakePrivate(home); err != nil {
 		return nil, err
 	}
 
2e20e63d
 	supportsDType, err := fsutils.SupportsDType(home)
 	if err != nil {
 		return nil, err
 	}
 	if !supportsDType {
5a9cee7b
 		// not a fatal error until v17.12 (#27443)
2e20e63d
 		logrus.Warn(overlayutils.ErrDTypeNotSupported("overlay", backingFs))
 	}
 
453552c8
 	d := &Driver{
2e20e63d
 		home:          home,
 		uidMaps:       uidMaps,
 		gidMaps:       gidMaps,
 		ctr:           graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicOverlay)),
 		supportsDType: supportsDType,
fc1cf191
 		locker:        locker.New(),
453552c8
 	}
 
442b4562
 	return NaiveDiffDriverWithApply(d, uidMaps, gidMaps), nil
453552c8
 }
 
2352f00e
 func supportsOverlay() error {
d680ca5c
 	// We can try to modprobe overlay first before looking at
 	// proc/filesystems for when overlay is supported
c5731789
 	exec.Command("modprobe", "overlay").Run()
453552c8
 
 	f, err := os.Open("/proc/filesystems")
 	if err != nil {
 		return err
 	}
 	defer f.Close()
 
 	s := bufio.NewScanner(f)
 	for s.Scan() {
c5731789
 		if s.Text() == "nodev\toverlay" {
453552c8
 			return nil
 		}
 	}
6f4d8470
 	logrus.Error("'overlay' not found as a supported filesystem on this host. Please ensure kernel is new enough and has overlay support loaded.")
453552c8
 	return graphdriver.ErrNotSupported
 }
 
 func (d *Driver) String() string {
d680ca5c
 	return "overlay"
453552c8
 }
 
de394421
 // Status returns current driver information in a two dimensional string array.
 // Output contains "Backing Filesystem" used in this implementation.
453552c8
 func (d *Driver) Status() [][2]string {
48b1dd00
 	return [][2]string{
 		{"Backing Filesystem", backingFs},
2e20e63d
 		{"Supports d_type", strconv.FormatBool(d.supportsDType)},
48b1dd00
 	}
453552c8
 }
 
de394421
 // GetMetadata returns meta data about the overlay driver such as root, LowerDir, UpperDir, WorkDir and MergeDir used to store data.
407a626b
 func (d *Driver) GetMetadata(id string) (map[string]string, error) {
67473c6d
 	dir := d.dir(id)
 	if _, err := os.Stat(dir); err != nil {
 		return nil, err
 	}
 
 	metadata := make(map[string]string)
 
 	// If id has a root, it is an image
 	rootDir := path.Join(dir, "root")
 	if _, err := os.Stat(rootDir); err == nil {
 		metadata["RootDir"] = rootDir
 		return metadata, nil
 	}
 
de394421
 	lowerID, err := ioutil.ReadFile(path.Join(dir, "lower-id"))
67473c6d
 	if err != nil {
 		return nil, err
 	}
 
de394421
 	metadata["LowerDir"] = path.Join(d.dir(string(lowerID)), "root")
67473c6d
 	metadata["UpperDir"] = path.Join(dir, "upper")
 	metadata["WorkDir"] = path.Join(dir, "work")
 	metadata["MergedDir"] = path.Join(dir, "merged")
 
 	return metadata, nil
407a626b
 }
 
e076bccb
 // Cleanup any state created by overlay which should be cleaned when daemon
 // is being shutdown. For now, we just have to unmount the bind mounted
 // we had created.
453552c8
 func (d *Driver) Cleanup() error {
e076bccb
 	return mount.Unmount(d.home)
453552c8
 }
 
ef5bfad3
 // CreateReadWrite creates a layer that is writable for use as a container
 // file system.
b937aa8e
 func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
 	return d.Create(id, parent, opts)
ef5bfad3
 }
 
de394421
 // Create is used to create the upper, lower, and merge directories required for overlay fs for a given id.
 // The parent filesystem is used to configure these directories for the overlay.
b937aa8e
 func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) (retErr error) {
b16decfc
 
b937aa8e
 	if opts != nil && len(opts.StorageOpt) != 0 {
b16decfc
 		return fmt.Errorf("--storage-opt is not supported for overlay")
 	}
 
453552c8
 	dir := d.dir(id)
442b4562
 
 	rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
 	if err != nil {
 		return err
 	}
 	if err := idtools.MkdirAllAs(path.Dir(dir), 0700, rootUID, rootGID); err != nil {
453552c8
 		return err
 	}
442b4562
 	if err := idtools.MkdirAs(dir, 0700, rootUID, rootGID); err != nil {
453552c8
 		return err
 	}
 
 	defer func() {
 		// Clean up on failure
 		if retErr != nil {
 			os.RemoveAll(dir)
 		}
 	}()
 
 	// Toplevel images are just a "root" dir
 	if parent == "" {
f7f101d5
 		return idtools.MkdirAndChown(path.Join(dir, "root"), 0755, idtools.IDPair{rootUID, rootGID})
453552c8
 	}
 
 	parentDir := d.dir(parent)
 
 	// Ensure parent exists
 	if _, err := os.Lstat(parentDir); err != nil {
 		return err
 	}
 
c1be45fa
 	// If parent has a root, just do an overlay to it
453552c8
 	parentRoot := path.Join(parentDir, "root")
 
 	if s, err := os.Lstat(parentRoot); err == nil {
191cefba
 		if err := idtools.MkdirAs(path.Join(dir, "upper"), s.Mode(), rootUID, rootGID); err != nil {
453552c8
 			return err
 		}
191cefba
 		if err := idtools.MkdirAs(path.Join(dir, "work"), 0700, rootUID, rootGID); err != nil {
453552c8
 			return err
 		}
442b4562
 		if err := idtools.MkdirAs(path.Join(dir, "merged"), 0700, rootUID, rootGID); err != nil {
453552c8
 			return err
 		}
 		if err := ioutil.WriteFile(path.Join(dir, "lower-id"), []byte(parent), 0666); err != nil {
 			return err
 		}
 		return nil
 	}
 
 	// Otherwise, copy the upper and the lower-id from the parent
 
de394421
 	lowerID, err := ioutil.ReadFile(path.Join(parentDir, "lower-id"))
453552c8
 	if err != nil {
 		return err
 	}
 
de394421
 	if err := ioutil.WriteFile(path.Join(dir, "lower-id"), lowerID, 0666); err != nil {
453552c8
 		return err
 	}
 
 	parentUpperDir := path.Join(parentDir, "upper")
 	s, err := os.Lstat(parentUpperDir)
 	if err != nil {
 		return err
 	}
 
 	upperDir := path.Join(dir, "upper")
191cefba
 	if err := idtools.MkdirAs(upperDir, s.Mode(), rootUID, rootGID); err != nil {
453552c8
 		return err
 	}
191cefba
 	if err := idtools.MkdirAs(path.Join(dir, "work"), 0700, rootUID, rootGID); err != nil {
453552c8
 		return err
 	}
442b4562
 	if err := idtools.MkdirAs(path.Join(dir, "merged"), 0700, rootUID, rootGID); err != nil {
453552c8
 		return err
 	}
 
 	return copyDir(parentUpperDir, upperDir, 0)
 }
 
 func (d *Driver) dir(id string) string {
 	return path.Join(d.home, id)
 }
 
de394421
 // Remove cleans the directories that are created for this id.
453552c8
 func (d *Driver) Remove(id string) error {
fc1cf191
 	d.locker.Lock(id)
 	defer d.locker.Unlock(id)
54dcbab2
 	return system.EnsureRemoveAll(d.dir(id))
453552c8
 }
 
de394421
 // Get creates and mounts the required file system for the given id and returns the mount path.
7a7357da
 func (d *Driver) Get(id, mountLabel string) (_ containerfs.ContainerFS, err error) {
fc1cf191
 	d.locker.Lock(id)
 	defer d.locker.Unlock(id)
453552c8
 	dir := d.dir(id)
 	if _, err := os.Stat(dir); err != nil {
7a7357da
 		return nil, err
453552c8
 	}
36a82c20
 	// If id has a root, just return it
 	rootDir := path.Join(dir, "root")
 	if _, err := os.Stat(rootDir); err == nil {
7a7357da
 		return containerfs.NewLocalContainerFS(rootDir), nil
36a82c20
 	}
009ee16b
 	mergedDir := path.Join(dir, "merged")
 	if count := d.ctr.Increment(mergedDir); count > 1 {
7a7357da
 		return containerfs.NewLocalContainerFS(mergedDir), nil
009ee16b
 	}
290be017
 	defer func() {
 		if err != nil {
36a82c20
 			if c := d.ctr.Decrement(mergedDir); c <= 0 {
069fdc8a
 				unix.Unmount(mergedDir, 0)
36a82c20
 			}
290be017
 		}
 	}()
de394421
 	lowerID, err := ioutil.ReadFile(path.Join(dir, "lower-id"))
453552c8
 	if err != nil {
7a7357da
 		return nil, err
453552c8
 	}
290be017
 	var (
 		lowerDir = path.Join(d.dir(string(lowerID)), "root")
 		upperDir = path.Join(dir, "upper")
 		workDir  = path.Join(dir, "work")
 		opts     = fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", lowerDir, upperDir, workDir)
 	)
069fdc8a
 	if err := unix.Mount("overlay", mergedDir, "overlay", 0, label.FormatMountLabel(opts, mountLabel)); err != nil {
7a7357da
 		return nil, fmt.Errorf("error creating overlay mount to %s: %v", mergedDir, err)
453552c8
 	}
442b4562
 	// chown "workdir/work" to the remapped root UID/GID. Overlay fs inside a
 	// user namespace requires this to move a directory from lower to upper.
 	rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
0e025b4b
 	if err != nil {
7a7357da
 		return nil, err
0e025b4b
 	}
442b4562
 	if err := os.Chown(path.Join(workDir, "work"), rootUID, rootGID); err != nil {
7a7357da
 		return nil, err
442b4562
 	}
7a7357da
 	return containerfs.NewLocalContainerFS(mergedDir), nil
65d79e3e
 }
 
de394421
 // Put unmounts the mount path created for the give id.
00fd63e5
 func (d *Driver) Put(id string) error {
fc1cf191
 	d.locker.Lock(id)
 	defer d.locker.Unlock(id)
e4349ad9
 	// If id has a root, just return
 	if _, err := os.Stat(path.Join(d.dir(id), "root")); err == nil {
 		return nil
 	}
290be017
 	mountpoint := path.Join(d.dir(id), "merged")
009ee16b
 	if count := d.ctr.Decrement(mountpoint); count > 0 {
 		return nil
 	}
069fdc8a
 	if err := unix.Unmount(mountpoint, unix.MNT_DETACH); err != nil {
290be017
 		logrus.Debugf("Failed to unmount %s overlay: %v", id, err)
453552c8
 	}
00fd63e5
 	return nil
453552c8
 }
 
c1be45fa
 // ApplyDiff applies the new layer on top of the root, if parent does not exist with will return an ErrApplyDiffFallback error.
aa2cc187
 func (d *Driver) ApplyDiff(id string, parent string, diff io.Reader) (size int64, err error) {
453552c8
 	dir := d.dir(id)
 
 	if parent == "" {
 		return 0, ErrApplyDiffFallback
 	}
 
 	parentRootDir := path.Join(d.dir(parent), "root")
 	if _, err := os.Stat(parentRootDir); err != nil {
 		return 0, ErrApplyDiffFallback
 	}
 
 	// We now know there is a parent, and it has a "root" directory containing
 	// the full root filesystem. We can just hardlink it and apply the
 	// layer. This relies on two things:
 	// 1) ApplyDiff is only run once on a clean (no writes to upper layer) container
 	// 2) ApplyDiff doesn't do any in-place writes to files (would break hardlinks)
 	// These are all currently true and are not expected to break
 
 	tmpRootDir, err := ioutil.TempDir(dir, "tmproot")
 	if err != nil {
 		return 0, err
 	}
 	defer func() {
 		if err != nil {
 			os.RemoveAll(tmpRootDir)
 		} else {
 			os.RemoveAll(path.Join(dir, "upper"))
 			os.RemoveAll(path.Join(dir, "work"))
 			os.RemoveAll(path.Join(dir, "merged"))
 			os.RemoveAll(path.Join(dir, "lower-id"))
 		}
 	}()
 
de394421
 	if err = copyDir(parentRootDir, tmpRootDir, copyHardlink); err != nil {
453552c8
 		return 0, err
 	}
 
442b4562
 	options := &archive.TarOptions{UIDMaps: d.uidMaps, GIDMaps: d.gidMaps}
246e9930
 	if size, err = graphdriver.ApplyUncompressedLayer(tmpRootDir, diff, options); err != nil {
453552c8
 		return 0, err
 	}
 
 	rootDir := path.Join(dir, "root")
 	if err := os.Rename(tmpRootDir, rootDir); err != nil {
 		return 0, err
 	}
 
35a22c9e
 	return
453552c8
 }
 
de394421
 // Exists checks to see if the id is already mounted.
453552c8
 func (d *Driver) Exists(id string) bool {
 	_, err := os.Stat(d.dir(id))
 	return err == nil
 }