driver.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 returned 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. // Capabilities defines a list of capabilities a driver may implement.
  103. // These capabilities are not required; however, they do determine how a
  104. // graphdriver can be used.
  105. type Capabilities struct {
  106. // Flags that this driver is capable of reproducing exactly equivalent
  107. // diffs for read-only layers. If set, clients can rely on the driver
  108. // for consistent tar streams, and avoid extra processing to account
  109. // for potential differences (eg: the layer store's use of tar-split).
  110. ReproducesExactDiffs bool
  111. }
  112. // CapabilityDriver is the interface for layered file system drivers that
  113. // can report on their Capabilities.
  114. type CapabilityDriver interface {
  115. Capabilities() Capabilities
  116. }
  117. // DiffGetterDriver is the interface for layered file system drivers that
  118. // provide a specialized function for getting file contents for tar-split.
  119. type DiffGetterDriver interface {
  120. Driver
  121. // DiffGetter returns an interface to efficiently retrieve the contents
  122. // of files in a layer.
  123. DiffGetter(id string) (FileGetCloser, error)
  124. }
  125. // FileGetCloser extends the storage.FileGetter interface with a Close method
  126. // for cleaning up.
  127. type FileGetCloser interface {
  128. storage.FileGetter
  129. // Close cleans up any resources associated with the FileGetCloser.
  130. Close() error
  131. }
  132. // Checker makes checks on specified filesystems.
  133. type Checker interface {
  134. // IsMounted returns true if the provided path is mounted for the specific checker
  135. IsMounted(path string) bool
  136. }
  137. func init() {
  138. drivers = make(map[string]InitFunc)
  139. }
  140. // Register registers an InitFunc for the driver.
  141. func Register(name string, initFunc InitFunc) error {
  142. if _, exists := drivers[name]; exists {
  143. return fmt.Errorf("Name already registered %s", name)
  144. }
  145. drivers[name] = initFunc
  146. return nil
  147. }
  148. // GetDriver initializes and returns the registered driver
  149. func GetDriver(name string, pg plugingetter.PluginGetter, config Options) (Driver, error) {
  150. if initFunc, exists := drivers[name]; exists {
  151. return initFunc(filepath.Join(config.Root, name), config.DriverOptions, config.UIDMaps, config.GIDMaps)
  152. }
  153. pluginDriver, err := lookupPlugin(name, pg, config)
  154. if err == nil {
  155. return pluginDriver, nil
  156. }
  157. logrus.WithError(err).WithField("driver", name).WithField("home-dir", config.Root).Error("Failed to GetDriver graph")
  158. return nil, ErrNotSupported
  159. }
  160. // getBuiltinDriver initializes and returns the registered driver, but does not try to load from plugins
  161. func getBuiltinDriver(name, home string, options []string, uidMaps, gidMaps []idtools.IDMap) (Driver, error) {
  162. if initFunc, exists := drivers[name]; exists {
  163. return initFunc(filepath.Join(home, name), options, uidMaps, gidMaps)
  164. }
  165. logrus.Errorf("Failed to built-in GetDriver graph %s %s", name, home)
  166. return nil, ErrNotSupported
  167. }
  168. // Options is used to initialize a graphdriver
  169. type Options struct {
  170. Root string
  171. DriverOptions []string
  172. UIDMaps []idtools.IDMap
  173. GIDMaps []idtools.IDMap
  174. ExperimentalEnabled bool
  175. }
  176. // New creates the driver and initializes it at the specified root.
  177. func New(name string, pg plugingetter.PluginGetter, config Options) (Driver, error) {
  178. if name != "" {
  179. logrus.Debugf("[graphdriver] trying provided driver: %s", name) // so the logs show specified driver
  180. return GetDriver(name, pg, config)
  181. }
  182. // Guess for prior driver
  183. driversMap := scanPriorDrivers(config.Root)
  184. for _, name := range priority {
  185. if name == "vfs" {
  186. // don't use vfs even if there is state present.
  187. continue
  188. }
  189. if _, prior := driversMap[name]; prior {
  190. // of the state found from prior drivers, check in order of our priority
  191. // which we would prefer
  192. driver, err := getBuiltinDriver(name, config.Root, config.DriverOptions, config.UIDMaps, config.GIDMaps)
  193. if err != nil {
  194. // unlike below, we will return error here, because there is prior
  195. // state, and now it is no longer supported/prereq/compatible, so
  196. // something changed and needs attention. Otherwise the daemon's
  197. // images would just "disappear".
  198. logrus.Errorf("[graphdriver] prior storage driver %s failed: %s", name, err)
  199. return nil, err
  200. }
  201. // abort starting when there are other prior configured drivers
  202. // to ensure the user explicitly selects the driver to load
  203. if len(driversMap)-1 > 0 {
  204. var driversSlice []string
  205. for name := range driversMap {
  206. driversSlice = append(driversSlice, name)
  207. }
  208. return nil, fmt.Errorf("%s contains several valid graphdrivers: %s; Please cleanup or explicitly choose storage driver (-s <DRIVER>)", config.Root, strings.Join(driversSlice, ", "))
  209. }
  210. logrus.Infof("[graphdriver] using prior storage driver: %s", name)
  211. return driver, nil
  212. }
  213. }
  214. // Check for priority drivers first
  215. for _, name := range priority {
  216. driver, err := getBuiltinDriver(name, config.Root, config.DriverOptions, config.UIDMaps, config.GIDMaps)
  217. if err != nil {
  218. if isDriverNotSupported(err) {
  219. continue
  220. }
  221. return nil, err
  222. }
  223. return driver, nil
  224. }
  225. // Check all registered drivers if no priority driver is found
  226. for name, initFunc := range drivers {
  227. driver, err := initFunc(filepath.Join(config.Root, name), config.DriverOptions, config.UIDMaps, config.GIDMaps)
  228. if err != nil {
  229. if isDriverNotSupported(err) {
  230. continue
  231. }
  232. return nil, err
  233. }
  234. return driver, nil
  235. }
  236. return nil, fmt.Errorf("No supported storage backend found")
  237. }
  238. // isDriverNotSupported returns true if the error initializing
  239. // the graph driver is a non-supported error.
  240. func isDriverNotSupported(err error) bool {
  241. return err == ErrNotSupported || err == ErrPrerequisites || err == ErrIncompatibleFS
  242. }
  243. // scanPriorDrivers returns an un-ordered scan of directories of prior storage drivers
  244. func scanPriorDrivers(root string) map[string]bool {
  245. driversMap := make(map[string]bool)
  246. for driver := range drivers {
  247. p := filepath.Join(root, driver)
  248. if _, err := os.Stat(p); err == nil && driver != "vfs" {
  249. driversMap[driver] = true
  250. }
  251. }
  252. return driversMap
  253. }