driver.go 8.8 KB

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