driver.go 10 KB

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