github.com/jandre/docker@v1.7.0/daemon/graphdriver/driver.go (about) 1 package graphdriver 2 3 import ( 4 "errors" 5 "fmt" 6 "os" 7 "path/filepath" 8 "strings" 9 10 "github.com/Sirupsen/logrus" 11 "github.com/docker/docker/pkg/archive" 12 ) 13 14 type FsMagic uint32 15 16 const ( 17 FsMagicUnsupported = FsMagic(0x00000000) 18 ) 19 20 var ( 21 DefaultDriver string 22 // All registred drivers 23 drivers map[string]InitFunc 24 25 ErrNotSupported = errors.New("driver not supported") 26 ErrPrerequisites = errors.New("prerequisites for driver not satisfied (wrong filesystem?)") 27 ErrIncompatibleFS = fmt.Errorf("backing file system is unsupported for this graph driver") 28 ) 29 30 type InitFunc func(root string, options []string) (Driver, error) 31 32 // ProtoDriver defines the basic capabilities of a driver. 33 // This interface exists solely to be a minimum set of methods 34 // for client code which choose not to implement the entire Driver 35 // interface and use the NaiveDiffDriver wrapper constructor. 36 // 37 // Use of ProtoDriver directly by client code is not recommended. 38 type ProtoDriver interface { 39 // String returns a string representation of this driver. 40 String() string 41 // Create creates a new, empty, filesystem layer with the 42 // specified id and parent. Parent may be "". 43 Create(id, parent string) error 44 // Remove attempts to remove the filesystem layer with this id. 45 Remove(id string) error 46 // Get returns the mountpoint for the layered filesystem referred 47 // to by this id. You can optionally specify a mountLabel or "". 48 // Returns the absolute path to the mounted layered filesystem. 49 Get(id, mountLabel string) (dir string, err error) 50 // Put releases the system resources for the specified id, 51 // e.g, unmounting layered filesystem. 52 Put(id string) error 53 // Exists returns whether a filesystem layer with the specified 54 // ID exists on this driver. 55 Exists(id string) bool 56 // Status returns a set of key-value pairs which give low 57 // level diagnostic status about this driver. 58 Status() [][2]string 59 // Cleanup performs necessary tasks to release resources 60 // held by the driver, e.g., unmounting all layered filesystems 61 // known to this driver. 62 Cleanup() error 63 } 64 65 // Driver is the interface for layered/snapshot file system drivers. 66 type Driver interface { 67 ProtoDriver 68 // Diff produces an archive of the changes between the specified 69 // layer and its parent layer which may be "". 70 Diff(id, parent string) (archive.Archive, error) 71 // Changes produces a list of changes between the specified layer 72 // and its parent layer. If parent is "", then all changes will be ADD changes. 73 Changes(id, parent string) ([]archive.Change, error) 74 // ApplyDiff extracts the changeset from the given diff into the 75 // layer with the specified id and parent, returning the size of the 76 // new layer in bytes. 77 ApplyDiff(id, parent string, diff archive.ArchiveReader) (size int64, err error) 78 // DiffSize calculates the changes between the specified id 79 // and its parent and returns the size in bytes of the changes 80 // relative to its base filesystem directory. 81 DiffSize(id, parent string) (size int64, err error) 82 } 83 84 func init() { 85 drivers = make(map[string]InitFunc) 86 } 87 88 func Register(name string, initFunc InitFunc) error { 89 if _, exists := drivers[name]; exists { 90 return fmt.Errorf("Name already registered %s", name) 91 } 92 drivers[name] = initFunc 93 94 return nil 95 } 96 97 func GetDriver(name, home string, options []string) (Driver, error) { 98 if initFunc, exists := drivers[name]; exists { 99 return initFunc(filepath.Join(home, name), options) 100 } 101 return nil, ErrNotSupported 102 } 103 104 func New(root string, options []string) (driver Driver, err error) { 105 for _, name := range []string{os.Getenv("DOCKER_DRIVER"), DefaultDriver} { 106 if name != "" { 107 logrus.Debugf("[graphdriver] trying provided driver %q", name) // so the logs show specified driver 108 return GetDriver(name, root, options) 109 } 110 } 111 112 // Guess for prior driver 113 priorDrivers := scanPriorDrivers(root) 114 for _, name := range priority { 115 if name == "vfs" { 116 // don't use vfs even if there is state present. 117 continue 118 } 119 for _, prior := range priorDrivers { 120 // of the state found from prior drivers, check in order of our priority 121 // which we would prefer 122 if prior == name { 123 driver, err = GetDriver(name, root, options) 124 if err != nil { 125 // unlike below, we will return error here, because there is prior 126 // state, and now it is no longer supported/prereq/compatible, so 127 // something changed and needs attention. Otherwise the daemon's 128 // images would just "disappear". 129 logrus.Errorf("[graphdriver] prior storage driver %q failed: %s", name, err) 130 return nil, err 131 } 132 if err := checkPriorDriver(name, root); err != nil { 133 return nil, err 134 } 135 logrus.Infof("[graphdriver] using prior storage driver %q", name) 136 return driver, nil 137 } 138 } 139 } 140 141 // Check for priority drivers first 142 for _, name := range priority { 143 driver, err = GetDriver(name, root, options) 144 if err != nil { 145 if err == ErrNotSupported || err == ErrPrerequisites || err == ErrIncompatibleFS { 146 continue 147 } 148 return nil, err 149 } 150 return driver, nil 151 } 152 153 // Check all registered drivers if no priority driver is found 154 for _, initFunc := range drivers { 155 if driver, err = initFunc(root, options); err != nil { 156 if err == ErrNotSupported || err == ErrPrerequisites || err == ErrIncompatibleFS { 157 continue 158 } 159 return nil, err 160 } 161 return driver, nil 162 } 163 return nil, fmt.Errorf("No supported storage backend found") 164 } 165 166 // scanPriorDrivers returns an un-ordered scan of directories of prior storage drivers 167 func scanPriorDrivers(root string) []string { 168 priorDrivers := []string{} 169 for driver := range drivers { 170 p := filepath.Join(root, driver) 171 if _, err := os.Stat(p); err == nil { 172 priorDrivers = append(priorDrivers, driver) 173 } 174 } 175 return priorDrivers 176 } 177 178 func checkPriorDriver(name, root string) error { 179 priorDrivers := []string{} 180 for _, prior := range scanPriorDrivers(root) { 181 if prior != name && prior != "vfs" { 182 if _, err := os.Stat(filepath.Join(root, prior)); err == nil { 183 priorDrivers = append(priorDrivers, prior) 184 } 185 } 186 } 187 188 if len(priorDrivers) > 0 { 189 190 return errors.New(fmt.Sprintf("%q contains other graphdrivers: %s; Please cleanup or explicitly choose storage driver (-s <DRIVER>)", root, strings.Join(priorDrivers, ","))) 191 } 192 return nil 193 }