driver.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. 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. }
  24. var (
  25. // All registred drivers
  26. drivers map[string]InitFunc
  27. // Slice of drivers that should be used in an order
  28. priority = []string{
  29. "aufs",
  30. "devicemapper",
  31. "dummy",
  32. }
  33. )
  34. func init() {
  35. drivers = make(map[string]InitFunc)
  36. }
  37. func Register(name string, initFunc InitFunc) error {
  38. if _, exists := drivers[name]; exists {
  39. return fmt.Errorf("Name already registered %s", name)
  40. }
  41. drivers[name] = initFunc
  42. return nil
  43. }
  44. func GetDriver(name, home string) (Driver, error) {
  45. if initFunc, exists := drivers[name]; exists {
  46. return initFunc(path.Join(home, name))
  47. }
  48. return nil, fmt.Errorf("No such driver: %s", name)
  49. }
  50. func New(root string) (Driver, error) {
  51. var driver Driver
  52. var lastError error
  53. // Use environment variable DOCKER_DRIVER to force a choice of driver
  54. if name := os.Getenv("DOCKER_DRIVER"); name != "" {
  55. return GetDriver(name, root)
  56. }
  57. // Check for priority drivers first
  58. for _, name := range priority {
  59. driver, lastError = GetDriver(name, root)
  60. if lastError != nil {
  61. utils.Debugf("Error loading driver %s: %s", name, lastError)
  62. continue
  63. }
  64. return driver, nil
  65. }
  66. // Check all registered drivers if no priority driver is found
  67. for _, initFunc := range drivers {
  68. driver, lastError = initFunc(root)
  69. if lastError != nil {
  70. continue
  71. }
  72. return driver, nil
  73. }
  74. return nil, lastError
  75. }