driver.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package graphdriver
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path"
  7. "strings"
  8. log "github.com/Sirupsen/logrus"
  9. "github.com/docker/docker/pkg/archive"
  10. )
  11. type FsMagic uint32
  12. const (
  13. FsMagicBtrfs = FsMagic(0x9123683E)
  14. FsMagicAufs = FsMagic(0x61756673)
  15. )
  16. type InitFunc func(root string, options []string) (Driver, error)
  17. // ProtoDriver defines the basic capabilities of a driver.
  18. // This interface exists solely to be a minimum set of methods
  19. // for client code which choose not to implement the entire Driver
  20. // interface and use the NaiveDiffDriver wrapper constructor.
  21. //
  22. // Use of ProtoDriver directly by client code is not recommended.
  23. type ProtoDriver interface {
  24. // String returns a string representation of this driver.
  25. String() string
  26. // Create creates a new, empty, filesystem layer with the
  27. // specified id and parent. Parent may be "".
  28. Create(id, parent string) error
  29. // Remove attempts to remove the filesystem layer with this id.
  30. Remove(id string) error
  31. // Get returns the mountpoint for the layered filesystem referred
  32. // to by this id. You can optionally specify a mountLabel or "".
  33. // Returns the absolute path to the mounted layered filesystem.
  34. Get(id, mountLabel string) (dir string, err error)
  35. // Put releases the system resources for the specified id,
  36. // e.g, unmounting layered filesystem.
  37. Put(id string) error
  38. // Exists returns whether a filesystem layer with the specified
  39. // ID exists on this driver.
  40. Exists(id string) bool
  41. // Status returns a set of key-value pairs which give low
  42. // level diagnostic status about this driver.
  43. Status() [][2]string
  44. // Cleanup performs necessary tasks to release resources
  45. // held by the driver, e.g., unmounting all layered filesystems
  46. // known to this driver.
  47. Cleanup() error
  48. }
  49. // Driver is the interface for layered/snapshot file system drivers.
  50. type Driver interface {
  51. ProtoDriver
  52. // Diff produces an archive of the changes between the specified
  53. // layer and its parent layer which may be "".
  54. Diff(id, parent string) (archive.Archive, error)
  55. // Changes produces a list of changes between the specified layer
  56. // and its parent layer. If parent is "", then all changes will be ADD changes.
  57. Changes(id, parent string) ([]archive.Change, error)
  58. // ApplyDiff extracts the changeset from the given diff into the
  59. // layer with the specified id and parent, returning the size of the
  60. // new layer in bytes.
  61. ApplyDiff(id, parent string, diff archive.ArchiveReader) (size int64, err error)
  62. // DiffSize calculates the changes between the specified id
  63. // and its parent and returns the size in bytes of the changes
  64. // relative to its base filesystem directory.
  65. DiffSize(id, parent string) (size int64, err error)
  66. }
  67. var (
  68. DefaultDriver string
  69. // All registred drivers
  70. drivers map[string]InitFunc
  71. // Slice of drivers that should be used in an order
  72. priority = []string{
  73. "aufs",
  74. "btrfs",
  75. "devicemapper",
  76. "vfs",
  77. // experimental, has to be enabled manually for now
  78. "overlay",
  79. }
  80. ErrNotSupported = errors.New("driver not supported")
  81. ErrPrerequisites = errors.New("prerequisites for driver not satisfied (wrong filesystem?)")
  82. ErrIncompatibleFS = fmt.Errorf("backing file system is unsupported for this graph driver")
  83. )
  84. func init() {
  85. drivers = make(map[string]InitFunc)
  86. }
  87. func Register(name string, initFunc InitFunc) error {
  88. if _, exists := drivers[name]; exists {
  89. return fmt.Errorf("Name already registered %s", name)
  90. }
  91. drivers[name] = initFunc
  92. return nil
  93. }
  94. func GetDriver(name, home string, options []string) (Driver, error) {
  95. if initFunc, exists := drivers[name]; exists {
  96. return initFunc(path.Join(home, name), options)
  97. }
  98. return nil, ErrNotSupported
  99. }
  100. func New(root string, options []string) (driver Driver, err error) {
  101. for _, name := range []string{os.Getenv("DOCKER_DRIVER"), DefaultDriver} {
  102. if name != "" {
  103. return GetDriver(name, root, options)
  104. }
  105. }
  106. // Check for priority drivers first
  107. for _, name := range priority {
  108. driver, err = GetDriver(name, root, options)
  109. if err != nil {
  110. if err == ErrNotSupported || err == ErrPrerequisites || err == ErrIncompatibleFS {
  111. continue
  112. }
  113. return nil, err
  114. }
  115. checkPriorDriver(name, root)
  116. return driver, nil
  117. }
  118. // Check all registered drivers if no priority driver is found
  119. for name, initFunc := range drivers {
  120. if driver, err = initFunc(root, options); err != nil {
  121. if err == ErrNotSupported || err == ErrPrerequisites || err == ErrIncompatibleFS {
  122. continue
  123. }
  124. return nil, err
  125. }
  126. checkPriorDriver(name, root)
  127. return driver, nil
  128. }
  129. return nil, fmt.Errorf("No supported storage backend found")
  130. }
  131. func checkPriorDriver(name, root string) {
  132. priorDrivers := []string{}
  133. for prior := range drivers {
  134. if prior != name {
  135. if _, err := os.Stat(path.Join(root, prior)); err == nil {
  136. priorDrivers = append(priorDrivers, prior)
  137. }
  138. }
  139. }
  140. if len(priorDrivers) > 0 {
  141. log.Warnf("graphdriver %s selected. Warning: your graphdriver directory %s already contains data managed by other graphdrivers: %s", name, root, strings.Join(priorDrivers, ","))
  142. }
  143. }