2013-11-03 20:54:51 -05:00
|
|
|
package graphdriver
|
2013-10-31 21:07:54 -04:00
|
|
|
|
2013-11-04 18:22:34 -05:00
|
|
|
import (
|
2014-03-27 12:41:06 -04:00
|
|
|
"errors"
|
2013-11-04 18:22:34 -05:00
|
|
|
"fmt"
|
2013-11-04 23:51:12 -05:00
|
|
|
"github.com/dotcloud/docker/archive"
|
2013-11-19 06:13:22 -05:00
|
|
|
"os"
|
2013-11-08 14:36:58 -05:00
|
|
|
"path"
|
2013-11-04 18:22:34 -05:00
|
|
|
)
|
|
|
|
|
2014-06-02 21:26:41 -04:00
|
|
|
type FsMagic uint64
|
|
|
|
|
|
|
|
const (
|
|
|
|
FsMagicBtrfs = FsMagic(0x9123683E)
|
|
|
|
FsMagicAufs = FsMagic(0x61756673)
|
|
|
|
)
|
|
|
|
|
2013-11-07 19:35:26 -05:00
|
|
|
type InitFunc func(root string) (Driver, error)
|
2013-11-04 23:51:12 -05:00
|
|
|
|
|
|
|
type Driver interface {
|
2013-11-15 04:24:48 -05:00
|
|
|
String() string
|
|
|
|
|
2014-04-17 19:47:27 -04:00
|
|
|
Create(id, parent string) error
|
2013-11-07 15:34:01 -05:00
|
|
|
Remove(id string) error
|
2013-11-04 23:51:12 -05:00
|
|
|
|
2014-04-17 19:47:27 -04:00
|
|
|
Get(id, mountLabel string) (dir string, err error)
|
2013-12-05 16:18:02 -05:00
|
|
|
Put(id string)
|
2013-11-19 05:32:08 -05:00
|
|
|
Exists(id string) bool
|
2013-11-04 23:51:12 -05:00
|
|
|
|
2013-11-15 05:04:02 -05:00
|
|
|
Status() [][2]string
|
|
|
|
|
2013-11-11 20:17:38 -05:00
|
|
|
Cleanup() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type Differ interface {
|
2013-11-11 15:09:26 -05:00
|
|
|
Diff(id string) (archive.Archive, error)
|
2013-11-08 17:54:20 -05:00
|
|
|
Changes(id string) ([]archive.Change, error)
|
2014-02-14 06:41:46 -05:00
|
|
|
ApplyDiff(id string, diff archive.ArchiveReader) error
|
2013-11-19 04:36:54 -05:00
|
|
|
DiffSize(id string) (bytes int64, err error)
|
2013-11-11 10:47:36 -05:00
|
|
|
}
|
|
|
|
|
2013-11-04 18:22:34 -05:00
|
|
|
var (
|
2013-11-20 14:39:15 -05:00
|
|
|
DefaultDriver string
|
2013-11-04 18:22:34 -05:00
|
|
|
// All registred drivers
|
|
|
|
drivers map[string]InitFunc
|
|
|
|
// Slice of drivers that should be used in an order
|
|
|
|
priority = []string{
|
|
|
|
"aufs",
|
2014-03-24 10:15:04 -04:00
|
|
|
"btrfs",
|
2013-11-04 18:22:34 -05:00
|
|
|
"devicemapper",
|
2013-11-25 13:28:17 -05:00
|
|
|
"vfs",
|
2013-11-04 18:22:34 -05:00
|
|
|
}
|
2014-03-27 12:41:06 -04:00
|
|
|
|
2014-05-29 15:55:59 -04:00
|
|
|
ErrNotSupported = errors.New("driver not supported")
|
|
|
|
ErrPrerequisites = errors.New("prerequisites for driver not satisfied (wrong filesystem?)")
|
|
|
|
ErrIncompatibleFS = fmt.Errorf("backing file system is unsupported for this graph driver")
|
2013-11-04 18:22:34 -05:00
|
|
|
)
|
|
|
|
|
2013-11-07 15:31:50 -05:00
|
|
|
func init() {
|
|
|
|
drivers = make(map[string]InitFunc)
|
|
|
|
}
|
|
|
|
|
2013-11-04 18:22:34 -05:00
|
|
|
func Register(name string, initFunc InitFunc) error {
|
|
|
|
if _, exists := drivers[name]; exists {
|
|
|
|
return fmt.Errorf("Name already registered %s", name)
|
|
|
|
}
|
|
|
|
drivers[name] = initFunc
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-15 05:30:28 -05:00
|
|
|
func GetDriver(name, home string) (Driver, error) {
|
2013-11-07 21:49:32 -05:00
|
|
|
if initFunc, exists := drivers[name]; exists {
|
2013-11-08 14:36:58 -05:00
|
|
|
return initFunc(path.Join(home, name))
|
2013-11-07 21:49:32 -05:00
|
|
|
}
|
2014-03-27 12:41:06 -04:00
|
|
|
return nil, ErrNotSupported
|
2013-11-07 21:49:32 -05:00
|
|
|
}
|
|
|
|
|
2013-11-20 14:39:15 -05:00
|
|
|
func New(root string) (driver Driver, err error) {
|
|
|
|
for _, name := range []string{os.Getenv("DOCKER_DRIVER"), DefaultDriver} {
|
2013-11-19 06:13:22 -05:00
|
|
|
if name != "" {
|
|
|
|
return GetDriver(name, root)
|
|
|
|
}
|
2013-11-07 21:49:32 -05:00
|
|
|
}
|
2013-11-19 06:13:22 -05:00
|
|
|
|
2013-11-04 18:22:34 -05:00
|
|
|
// Check for priority drivers first
|
|
|
|
for _, name := range priority {
|
2014-03-27 12:41:06 -04:00
|
|
|
driver, err = GetDriver(name, root)
|
|
|
|
if err != nil {
|
2014-05-29 15:55:59 -04:00
|
|
|
if err == ErrNotSupported || err == ErrPrerequisites || err == ErrIncompatibleFS {
|
2014-03-27 12:41:06 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, err
|
2013-11-04 18:22:34 -05:00
|
|
|
}
|
2013-11-07 21:49:32 -05:00
|
|
|
return driver, nil
|
2013-11-04 18:22:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check all registered drivers if no priority driver is found
|
|
|
|
for _, initFunc := range drivers {
|
2013-11-20 14:39:15 -05:00
|
|
|
if driver, err = initFunc(root); err != nil {
|
2014-05-29 15:55:59 -04:00
|
|
|
if err == ErrNotSupported || err == ErrPrerequisites || err == ErrIncompatibleFS {
|
2014-03-27 12:41:06 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, err
|
2013-11-04 18:22:34 -05:00
|
|
|
}
|
|
|
|
return driver, nil
|
|
|
|
}
|
2014-03-27 12:41:06 -04:00
|
|
|
return nil, fmt.Errorf("No supported storage backend found")
|
2013-11-04 18:22:34 -05:00
|
|
|
}
|