github.com/ojongerius/docker@v1.11.2/daemon/graphdriver/driver.go (about)

     1  package graphdriver
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/Sirupsen/logrus"
    11  	"github.com/vbatts/tar-split/tar/storage"
    12  
    13  	"github.com/docker/docker/pkg/archive"
    14  	"github.com/docker/docker/pkg/idtools"
    15  )
    16  
    17  // FsMagic unsigned id of the filesystem in use.
    18  type FsMagic uint32
    19  
    20  const (
    21  	// FsMagicUnsupported is a predefined constant value other than a valid filesystem id.
    22  	FsMagicUnsupported = FsMagic(0x00000000)
    23  )
    24  
    25  var (
    26  	// All registered drivers
    27  	drivers map[string]InitFunc
    28  
    29  	// ErrNotSupported returned when driver is not supported.
    30  	ErrNotSupported = errors.New("driver not supported")
    31  	// ErrPrerequisites retuned when driver does not meet prerequisites.
    32  	ErrPrerequisites = errors.New("prerequisites for driver not satisfied (wrong filesystem?)")
    33  	// ErrIncompatibleFS returned when file system is not supported.
    34  	ErrIncompatibleFS = fmt.Errorf("backing file system is unsupported for this graph driver")
    35  )
    36  
    37  // InitFunc initializes the storage driver.
    38  type InitFunc func(root string, options []string, uidMaps, gidMaps []idtools.IDMap) (Driver, error)
    39  
    40  // ProtoDriver defines the basic capabilities of a driver.
    41  // This interface exists solely to be a minimum set of methods
    42  // for client code which choose not to implement the entire Driver
    43  // interface and use the NaiveDiffDriver wrapper constructor.
    44  //
    45  // Use of ProtoDriver directly by client code is not recommended.
    46  type ProtoDriver interface {
    47  	// String returns a string representation of this driver.
    48  	String() string
    49  	// Create creates a new, empty, filesystem layer with the
    50  	// specified id and parent and mountLabel. Parent and mountLabel may be "".
    51  	Create(id, parent, mountLabel string) error
    52  	// Remove attempts to remove the filesystem layer with this id.
    53  	Remove(id string) error
    54  	// Get returns the mountpoint for the layered filesystem referred
    55  	// to by this id. You can optionally specify a mountLabel or "".
    56  	// Returns the absolute path to the mounted layered filesystem.
    57  	Get(id, mountLabel string) (dir string, err error)
    58  	// Put releases the system resources for the specified id,
    59  	// e.g, unmounting layered filesystem.
    60  	Put(id string) error
    61  	// Exists returns whether a filesystem layer with the specified
    62  	// ID exists on this driver.
    63  	Exists(id string) bool
    64  	// Status returns a set of key-value pairs which give low
    65  	// level diagnostic status about this driver.
    66  	Status() [][2]string
    67  	// Returns a set of key-value pairs which give low level information
    68  	// about the image/container driver is managing.
    69  	GetMetadata(id string) (map[string]string, error)
    70  	// Cleanup performs necessary tasks to release resources
    71  	// held by the driver, e.g., unmounting all layered filesystems
    72  	// known to this driver.
    73  	Cleanup() error
    74  }
    75  
    76  // Driver is the interface for layered/snapshot file system drivers.
    77  type Driver interface {
    78  	ProtoDriver
    79  	// Diff produces an archive of the changes between the specified
    80  	// layer and its parent layer which may be "".
    81  	Diff(id, parent string) (archive.Archive, error)
    82  	// Changes produces a list of changes between the specified layer
    83  	// and its parent layer. If parent is "", then all changes will be ADD changes.
    84  	Changes(id, parent string) ([]archive.Change, error)
    85  	// ApplyDiff extracts the changeset from the given diff into the
    86  	// layer with the specified id and parent, returning the size of the
    87  	// new layer in bytes.
    88  	// The archive.Reader must be an uncompressed stream.
    89  	ApplyDiff(id, parent string, diff archive.Reader) (size int64, err error)
    90  	// DiffSize calculates the changes between the specified id
    91  	// and its parent and returns the size in bytes of the changes
    92  	// relative to its base filesystem directory.
    93  	DiffSize(id, parent string) (size int64, err error)
    94  }
    95  
    96  // DiffGetterDriver is the interface for layered file system drivers that
    97  // provide a specialized function for getting file contents for tar-split.
    98  type DiffGetterDriver interface {
    99  	Driver
   100  	// DiffGetter returns an interface to efficiently retrieve the contents
   101  	// of files in a layer.
   102  	DiffGetter(id string) (FileGetCloser, error)
   103  }
   104  
   105  // FileGetCloser extends the storage.FileGetter interface with a Close method
   106  // for cleaning up.
   107  type FileGetCloser interface {
   108  	storage.FileGetter
   109  	// Close cleans up any resources associated with the FileGetCloser.
   110  	Close() error
   111  }
   112  
   113  func init() {
   114  	drivers = make(map[string]InitFunc)
   115  }
   116  
   117  // Register registers a InitFunc for the driver.
   118  func Register(name string, initFunc InitFunc) error {
   119  	if _, exists := drivers[name]; exists {
   120  		return fmt.Errorf("Name already registered %s", name)
   121  	}
   122  	drivers[name] = initFunc
   123  
   124  	return nil
   125  }
   126  
   127  // GetDriver initializes and returns the registered driver
   128  func GetDriver(name, home string, options []string, uidMaps, gidMaps []idtools.IDMap) (Driver, error) {
   129  	if initFunc, exists := drivers[name]; exists {
   130  		return initFunc(filepath.Join(home, name), options, uidMaps, gidMaps)
   131  	}
   132  	if pluginDriver, err := lookupPlugin(name, home, options); err == nil {
   133  		return pluginDriver, nil
   134  	}
   135  	logrus.Errorf("Failed to GetDriver graph %s %s", name, home)
   136  	return nil, ErrNotSupported
   137  }
   138  
   139  // getBuiltinDriver initializes and returns the registered driver, but does not try to load from plugins
   140  func getBuiltinDriver(name, home string, options []string, uidMaps, gidMaps []idtools.IDMap) (Driver, error) {
   141  	if initFunc, exists := drivers[name]; exists {
   142  		return initFunc(filepath.Join(home, name), options, uidMaps, gidMaps)
   143  	}
   144  	logrus.Errorf("Failed to built-in GetDriver graph %s %s", name, home)
   145  	return nil, ErrNotSupported
   146  }
   147  
   148  // New creates the driver and initializes it at the specified root.
   149  func New(root string, name string, options []string, uidMaps, gidMaps []idtools.IDMap) (Driver, error) {
   150  	if name != "" {
   151  		logrus.Debugf("[graphdriver] trying provided driver %q", name) // so the logs show specified driver
   152  		return GetDriver(name, root, options, uidMaps, gidMaps)
   153  	}
   154  
   155  	// Guess for prior driver
   156  	driversMap := scanPriorDrivers(root)
   157  	for _, name := range priority {
   158  		if name == "vfs" {
   159  			// don't use vfs even if there is state present.
   160  			continue
   161  		}
   162  		if _, prior := driversMap[name]; prior {
   163  			// of the state found from prior drivers, check in order of our priority
   164  			// which we would prefer
   165  			driver, err := getBuiltinDriver(name, root, options, uidMaps, gidMaps)
   166  			if err != nil {
   167  				// unlike below, we will return error here, because there is prior
   168  				// state, and now it is no longer supported/prereq/compatible, so
   169  				// something changed and needs attention. Otherwise the daemon's
   170  				// images would just "disappear".
   171  				logrus.Errorf("[graphdriver] prior storage driver %q failed: %s", name, err)
   172  				return nil, err
   173  			}
   174  
   175  			// abort starting when there are other prior configured drivers
   176  			// to ensure the user explicitly selects the driver to load
   177  			if len(driversMap)-1 > 0 {
   178  				var driversSlice []string
   179  				for name := range driversMap {
   180  					driversSlice = append(driversSlice, name)
   181  				}
   182  
   183  				return nil, fmt.Errorf("%q contains several valid graphdrivers: %s; Please cleanup or explicitly choose storage driver (-s <DRIVER>)", root, strings.Join(driversSlice, ", "))
   184  			}
   185  
   186  			logrus.Infof("[graphdriver] using prior storage driver %q", name)
   187  			return driver, nil
   188  		}
   189  	}
   190  
   191  	// Check for priority drivers first
   192  	for _, name := range priority {
   193  		driver, err := getBuiltinDriver(name, root, options, uidMaps, gidMaps)
   194  		if err != nil {
   195  			if isDriverNotSupported(err) {
   196  				continue
   197  			}
   198  			return nil, err
   199  		}
   200  		return driver, nil
   201  	}
   202  
   203  	// Check all registered drivers if no priority driver is found
   204  	for name, initFunc := range drivers {
   205  		driver, err := initFunc(filepath.Join(root, name), options, uidMaps, gidMaps)
   206  		if err != nil {
   207  			if isDriverNotSupported(err) {
   208  				continue
   209  			}
   210  			return nil, err
   211  		}
   212  		return driver, nil
   213  	}
   214  	return nil, fmt.Errorf("No supported storage backend found")
   215  }
   216  
   217  // isDriverNotSupported returns true if the error initializing
   218  // the graph driver is a non-supported error.
   219  func isDriverNotSupported(err error) bool {
   220  	return err == ErrNotSupported || err == ErrPrerequisites || err == ErrIncompatibleFS
   221  }
   222  
   223  // scanPriorDrivers returns an un-ordered scan of directories of prior storage drivers
   224  func scanPriorDrivers(root string) map[string]bool {
   225  	driversMap := make(map[string]bool)
   226  
   227  	for driver := range drivers {
   228  		p := filepath.Join(root, driver)
   229  		if _, err := os.Stat(p); err == nil && driver != "vfs" {
   230  			driversMap[driver] = true
   231  		}
   232  	}
   233  	return driversMap
   234  }