driver.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package graphdriver
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/dotcloud/docker/archive"
  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, mountLabel string) (dir string, err error)
  15. Put(id string)
  16. Exists(id string) bool
  17. Status() [][2]string
  18. Cleanup() error
  19. }
  20. type Differ interface {
  21. Diff(id string) (archive.Archive, error)
  22. Changes(id string) ([]archive.Change, error)
  23. ApplyDiff(id string, diff archive.ArchiveReader) error
  24. DiffSize(id string) (bytes int64, err error)
  25. }
  26. var (
  27. DefaultDriver string
  28. // All registred drivers
  29. drivers map[string]InitFunc
  30. // Slice of drivers that should be used in an order
  31. priority = []string{
  32. "aufs",
  33. "btrfs",
  34. "devicemapper",
  35. "vfs",
  36. }
  37. ErrNotSupported = errors.New("driver not supported")
  38. )
  39. func init() {
  40. drivers = make(map[string]InitFunc)
  41. }
  42. func Register(name string, initFunc InitFunc) error {
  43. if _, exists := drivers[name]; exists {
  44. return fmt.Errorf("Name already registered %s", name)
  45. }
  46. drivers[name] = initFunc
  47. return nil
  48. }
  49. func GetDriver(name, home string) (Driver, error) {
  50. if initFunc, exists := drivers[name]; exists {
  51. return initFunc(path.Join(home, name))
  52. }
  53. return nil, ErrNotSupported
  54. }
  55. func New(root string) (driver Driver, err error) {
  56. for _, name := range []string{os.Getenv("DOCKER_DRIVER"), DefaultDriver} {
  57. if name != "" {
  58. return GetDriver(name, root)
  59. }
  60. }
  61. // Check for priority drivers first
  62. for _, name := range priority {
  63. driver, err = GetDriver(name, root)
  64. if err != nil {
  65. if err == ErrNotSupported {
  66. continue
  67. }
  68. return nil, err
  69. }
  70. return driver, nil
  71. }
  72. // Check all registered drivers if no priority driver is found
  73. for _, initFunc := range drivers {
  74. if driver, err = initFunc(root); err != nil {
  75. if err == ErrNotSupported {
  76. continue
  77. }
  78. return nil, err
  79. }
  80. return driver, nil
  81. }
  82. return nil, fmt.Errorf("No supported storage backend found")
  83. }