driver.go 9.1 KB

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