driver.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. var DefaultDriver string
  10. type InitFunc func(root string) (Driver, error)
  11. type Driver interface {
  12. String() string
  13. Create(id, parent string) error
  14. Remove(id string) error
  15. Get(id string) (dir string, err error)
  16. Status() [][2]string
  17. Cleanup() error
  18. }
  19. type Differ interface {
  20. Diff(id string) (archive.Archive, error)
  21. Changes(id string) ([]archive.Change, error)
  22. ApplyDiff(id string, diff archive.Archive) error
  23. DiffSize(id string) (bytes int64, err error)
  24. }
  25. var (
  26. // All registred drivers
  27. drivers map[string]InitFunc
  28. // Slice of drivers that should be used in an order
  29. priority = []string{
  30. "aufs",
  31. "devicemapper",
  32. "dummy",
  33. }
  34. )
  35. func init() {
  36. drivers = make(map[string]InitFunc)
  37. }
  38. func Register(name string, initFunc InitFunc) error {
  39. if _, exists := drivers[name]; exists {
  40. return fmt.Errorf("Name already registered %s", name)
  41. }
  42. drivers[name] = initFunc
  43. return nil
  44. }
  45. func GetDriver(name, home string) (Driver, error) {
  46. if initFunc, exists := drivers[name]; exists {
  47. return initFunc(path.Join(home, name))
  48. }
  49. return nil, fmt.Errorf("No such driver: %s", name)
  50. }
  51. func New(root string) (Driver, error) {
  52. var driver Driver
  53. var lastError error
  54. for _, name := range []string{
  55. os.Getenv("DOCKER_DRIVER"),
  56. DefaultDriver,
  57. } {
  58. if name != "" {
  59. return GetDriver(name, root)
  60. }
  61. }
  62. // Check for priority drivers first
  63. for _, name := range priority {
  64. driver, lastError = GetDriver(name, root)
  65. if lastError != nil {
  66. utils.Debugf("Error loading driver %s: %s", name, lastError)
  67. continue
  68. }
  69. return driver, nil
  70. }
  71. // Check all registered drivers if no priority driver is found
  72. for _, initFunc := range drivers {
  73. driver, lastError = initFunc(root)
  74. if lastError != nil {
  75. continue
  76. }
  77. return driver, nil
  78. }
  79. return nil, lastError
  80. }