driver.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package graphdriver
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path"
  7. "github.com/dotcloud/docker/archive"
  8. "github.com/dotcloud/docker/pkg/mount"
  9. )
  10. type FsMagic uint64
  11. const (
  12. FsMagicBtrfs = FsMagic(0x9123683E)
  13. FsMagicAufs = FsMagic(0x61756673)
  14. )
  15. type InitFunc func(root string, options []string) (Driver, error)
  16. type Driver interface {
  17. String() string
  18. Create(id, parent string) error
  19. Remove(id string) error
  20. Get(id, mountLabel string) (dir string, err error)
  21. Put(id string)
  22. Exists(id string) bool
  23. Status() [][2]string
  24. Cleanup() error
  25. }
  26. type Differ interface {
  27. Diff(id string) (archive.Archive, error)
  28. Changes(id string) ([]archive.Change, error)
  29. ApplyDiff(id string, diff archive.ArchiveReader) error
  30. DiffSize(id string) (bytes int64, err error)
  31. }
  32. var (
  33. DefaultDriver string
  34. // All registred drivers
  35. drivers map[string]InitFunc
  36. // Slice of drivers that should be used in an order
  37. priority = []string{
  38. "aufs",
  39. "btrfs",
  40. "devicemapper",
  41. "vfs",
  42. }
  43. ErrNotSupported = errors.New("driver not supported")
  44. ErrPrerequisites = errors.New("prerequisites for driver not satisfied (wrong filesystem?)")
  45. ErrIncompatibleFS = fmt.Errorf("backing file system is unsupported for this graph driver")
  46. )
  47. func init() {
  48. drivers = make(map[string]InitFunc)
  49. }
  50. func Register(name string, initFunc InitFunc) error {
  51. if _, exists := drivers[name]; exists {
  52. return fmt.Errorf("Name already registered %s", name)
  53. }
  54. drivers[name] = initFunc
  55. return nil
  56. }
  57. func GetDriver(name, home string, options []string) (Driver, error) {
  58. if initFunc, exists := drivers[name]; exists {
  59. return initFunc(path.Join(home, name), options)
  60. }
  61. return nil, ErrNotSupported
  62. }
  63. func New(root string, options []string) (driver Driver, err error) {
  64. for _, name := range []string{os.Getenv("DOCKER_DRIVER"), DefaultDriver} {
  65. if name != "" {
  66. return GetDriver(name, root, options)
  67. }
  68. }
  69. // Check for priority drivers first
  70. for _, name := range priority {
  71. driver, err = GetDriver(name, root, options)
  72. if err != nil {
  73. if err == ErrNotSupported || err == ErrPrerequisites || err == ErrIncompatibleFS {
  74. continue
  75. }
  76. return nil, err
  77. }
  78. return driver, nil
  79. }
  80. // Check all registered drivers if no priority driver is found
  81. for _, initFunc := range drivers {
  82. if driver, err = initFunc(root, options); err != nil {
  83. if err == ErrNotSupported || err == ErrPrerequisites || err == ErrIncompatibleFS {
  84. continue
  85. }
  86. return nil, err
  87. }
  88. return driver, nil
  89. }
  90. return nil, fmt.Errorf("No supported storage backend found")
  91. }
  92. func MakePrivate(mountPoint string) error {
  93. mounted, err := mount.Mounted(mountPoint)
  94. if err != nil {
  95. return err
  96. }
  97. if !mounted {
  98. if err := mount.Mount(mountPoint, mountPoint, "none", "bind,rw"); err != nil {
  99. return err
  100. }
  101. }
  102. return mount.ForceMount("", mountPoint, "none", "private")
  103. }