github.com/chenchun/docker@v1.3.2-0.20150629222414-20467faf132b/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/docker/docker/pkg/archive"
    12  )
    13  
    14  type FsMagic uint32
    15  
    16  const (
    17  	FsMagicUnsupported = FsMagic(0x00000000)
    18  )
    19  
    20  var (
    21  	DefaultDriver string
    22  	// All registred drivers
    23  	drivers map[string]InitFunc
    24  
    25  	ErrNotSupported   = errors.New("driver not supported")
    26  	ErrPrerequisites  = errors.New("prerequisites for driver not satisfied (wrong filesystem?)")
    27  	ErrIncompatibleFS = fmt.Errorf("backing file system is unsupported for this graph driver")
    28  )
    29  
    30  type InitFunc func(root string, options []string) (Driver, error)
    31  
    32  // ProtoDriver defines the basic capabilities of a driver.
    33  // This interface exists solely to be a minimum set of methods
    34  // for client code which choose not to implement the entire Driver
    35  // interface and use the NaiveDiffDriver wrapper constructor.
    36  //
    37  // Use of ProtoDriver directly by client code is not recommended.
    38  type ProtoDriver interface {
    39  	// String returns a string representation of this driver.
    40  	String() string
    41  	// Create creates a new, empty, filesystem layer with the
    42  	// specified id and parent. Parent may be "".
    43  	Create(id, parent string) error
    44  	// Remove attempts to remove the filesystem layer with this id.
    45  	Remove(id string) error
    46  	// Get returns the mountpoint for the layered filesystem referred
    47  	// to by this id. You can optionally specify a mountLabel or "".
    48  	// Returns the absolute path to the mounted layered filesystem.
    49  	Get(id, mountLabel string) (dir string, err error)
    50  	// Put releases the system resources for the specified id,
    51  	// e.g, unmounting layered filesystem.
    52  	Put(id string) error
    53  	// Exists returns whether a filesystem layer with the specified
    54  	// ID exists on this driver.
    55  	Exists(id string) bool
    56  	// Status returns a set of key-value pairs which give low
    57  	// level diagnostic status about this driver.
    58  	Status() [][2]string
    59  	// Returns a set of key-value pairs which give low level information
    60  	// about the image/container driver is managing.
    61  	GetMetadata(id string) (map[string]string, error)
    62  	// Cleanup performs necessary tasks to release resources
    63  	// held by the driver, e.g., unmounting all layered filesystems
    64  	// known to this driver.
    65  	Cleanup() error
    66  }
    67  
    68  // Driver is the interface for layered/snapshot file system drivers.
    69  type Driver interface {
    70  	ProtoDriver
    71  	// Diff produces an archive of the changes between the specified
    72  	// layer and its parent layer which may be "".
    73  	Diff(id, parent string) (archive.Archive, error)
    74  	// Changes produces a list of changes between the specified layer
    75  	// and its parent layer. If parent is "", then all changes will be ADD changes.
    76  	Changes(id, parent string) ([]archive.Change, error)
    77  	// ApplyDiff extracts the changeset from the given diff into the
    78  	// layer with the specified id and parent, returning the size of the
    79  	// new layer in bytes.
    80  	ApplyDiff(id, parent string, diff archive.ArchiveReader) (size int64, err error)
    81  	// DiffSize calculates the changes between the specified id
    82  	// and its parent and returns the size in bytes of the changes
    83  	// relative to its base filesystem directory.
    84  	DiffSize(id, parent string) (size int64, err error)
    85  }
    86  
    87  func init() {
    88  	drivers = make(map[string]InitFunc)
    89  }
    90  
    91  func Register(name string, initFunc InitFunc) error {
    92  	if _, exists := drivers[name]; exists {
    93  		return fmt.Errorf("Name already registered %s", name)
    94  	}
    95  	drivers[name] = initFunc
    96  
    97  	return nil
    98  }
    99  
   100  func GetDriver(name, home string, options []string) (Driver, error) {
   101  	if initFunc, exists := drivers[name]; exists {
   102  		return initFunc(filepath.Join(home, name), options)
   103  	}
   104  	return nil, ErrNotSupported
   105  }
   106  
   107  func New(root string, options []string) (driver Driver, err error) {
   108  	for _, name := range []string{os.Getenv("DOCKER_DRIVER"), DefaultDriver} {
   109  		if name != "" {
   110  			logrus.Debugf("[graphdriver] trying provided driver %q", name) // so the logs show specified driver
   111  			return GetDriver(name, root, options)
   112  		}
   113  	}
   114  
   115  	// Guess for prior driver
   116  	priorDrivers := scanPriorDrivers(root)
   117  	for _, name := range priority {
   118  		if name == "vfs" {
   119  			// don't use vfs even if there is state present.
   120  			continue
   121  		}
   122  		for _, prior := range priorDrivers {
   123  			// of the state found from prior drivers, check in order of our priority
   124  			// which we would prefer
   125  			if prior == name {
   126  				driver, err = GetDriver(name, root, options)
   127  				if err != nil {
   128  					// unlike below, we will return error here, because there is prior
   129  					// state, and now it is no longer supported/prereq/compatible, so
   130  					// something changed and needs attention. Otherwise the daemon's
   131  					// images would just "disappear".
   132  					logrus.Errorf("[graphdriver] prior storage driver %q failed: %s", name, err)
   133  					return nil, err
   134  				}
   135  				if err := checkPriorDriver(name, root); err != nil {
   136  					return nil, err
   137  				}
   138  				logrus.Infof("[graphdriver] using prior storage driver %q", name)
   139  				return driver, nil
   140  			}
   141  		}
   142  	}
   143  
   144  	// Check for priority drivers first
   145  	for _, name := range priority {
   146  		driver, err = GetDriver(name, root, options)
   147  		if err != nil {
   148  			if err == ErrNotSupported || err == ErrPrerequisites || err == ErrIncompatibleFS {
   149  				continue
   150  			}
   151  			return nil, err
   152  		}
   153  		return driver, nil
   154  	}
   155  
   156  	// Check all registered drivers if no priority driver is found
   157  	for _, initFunc := range drivers {
   158  		if driver, err = initFunc(root, options); err != nil {
   159  			if err == ErrNotSupported || err == ErrPrerequisites || err == ErrIncompatibleFS {
   160  				continue
   161  			}
   162  			return nil, err
   163  		}
   164  		return driver, nil
   165  	}
   166  	return nil, fmt.Errorf("No supported storage backend found")
   167  }
   168  
   169  // scanPriorDrivers returns an un-ordered scan of directories of prior storage drivers
   170  func scanPriorDrivers(root string) []string {
   171  	priorDrivers := []string{}
   172  	for driver := range drivers {
   173  		p := filepath.Join(root, driver)
   174  		if _, err := os.Stat(p); err == nil {
   175  			priorDrivers = append(priorDrivers, driver)
   176  		}
   177  	}
   178  	return priorDrivers
   179  }
   180  
   181  func checkPriorDriver(name, root string) error {
   182  	priorDrivers := []string{}
   183  	for _, prior := range scanPriorDrivers(root) {
   184  		if prior != name && prior != "vfs" {
   185  			if _, err := os.Stat(filepath.Join(root, prior)); err == nil {
   186  				priorDrivers = append(priorDrivers, prior)
   187  			}
   188  		}
   189  	}
   190  
   191  	if len(priorDrivers) > 0 {
   192  
   193  		return errors.New(fmt.Sprintf("%q contains other graphdrivers: %s; Please cleanup or explicitly choose storage driver (-s <DRIVER>)", root, strings.Join(priorDrivers, ",")))
   194  	}
   195  	return nil
   196  }