driver.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package graphdriver
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/archive"
  5. "github.com/dotcloud/docker/utils"
  6. "os"
  7. "path"
  8. )
  9. type InitFunc func(root string) (Driver, error)
  10. type Driver interface {
  11. String() string
  12. Create(id, parent string) error
  13. Remove(id string) error
  14. Get(id string) (dir string, err error)
  15. Size(id string) (bytes int64, err error)
  16. Cleanup() error
  17. }
  18. type Differ interface {
  19. Diff(id string) (archive.Archive, error)
  20. Changes(id string) ([]archive.Change, error)
  21. ApplyDiff(id string, diff archive.Archive) error
  22. }
  23. var (
  24. // All registred drivers
  25. drivers map[string]InitFunc
  26. // Slice of drivers that should be used in an order
  27. priority = []string{
  28. "aufs",
  29. "devicemapper",
  30. "dummy",
  31. }
  32. )
  33. func init() {
  34. drivers = make(map[string]InitFunc)
  35. }
  36. func Register(name string, initFunc InitFunc) error {
  37. if _, exists := drivers[name]; exists {
  38. return fmt.Errorf("Name already registered %s", name)
  39. }
  40. drivers[name] = initFunc
  41. return nil
  42. }
  43. func getDriver(name, home string) (Driver, error) {
  44. if initFunc, exists := drivers[name]; exists {
  45. return initFunc(path.Join(home, name))
  46. }
  47. return nil, fmt.Errorf("No such driver: %s", name)
  48. }
  49. func New(root string) (Driver, error) {
  50. var driver Driver
  51. var lastError error
  52. // Use environment variable DOCKER_DRIVER to force a choice of driver
  53. if name := os.Getenv("DOCKER_DRIVER"); name != "" {
  54. return getDriver(name, root)
  55. }
  56. // Check for priority drivers first
  57. for _, name := range priority {
  58. driver, lastError = getDriver(name, root)
  59. if lastError != nil {
  60. utils.Debugf("Error loading driver %s: %s", name, lastError)
  61. continue
  62. }
  63. return driver, nil
  64. }
  65. // Check all registered drivers if no priority driver is found
  66. for _, initFunc := range drivers {
  67. driver, lastError = initFunc(root)
  68. if lastError != nil {
  69. continue
  70. }
  71. return driver, nil
  72. }
  73. return nil, lastError
  74. }