github.com/slene/docker@v1.8.0-rc1/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 // Returns a set of key-value pairs which give low level information 60 // about the image/container driver is managing. 61 GetMetadata(id string) (map[string]string, error) 62 // Cleanup performs necessary tasks to release resources 63 // held by the driver, e.g., unmounting all layered filesystems 64 // known to this driver. 65 Cleanup() error 66 } 67 68 // Driver is the interface for layered/snapshot file system drivers. 69 type Driver interface { 70 ProtoDriver 71 // Diff produces an archive of the changes between the specified 72 // layer and its parent layer which may be "". 73 Diff(id, parent string) (archive.Archive, error) 74 // Changes produces a list of changes between the specified layer 75 // and its parent layer. If parent is "", then all changes will be ADD changes. 76 Changes(id, parent string) ([]archive.Change, error) 77 // ApplyDiff extracts the changeset from the given diff into the 78 // layer with the specified id and parent, returning the size of the 79 // new layer in bytes. 80 ApplyDiff(id, parent string, diff archive.ArchiveReader) (size int64, err error) 81 // DiffSize calculates the changes between the specified id 82 // and its parent and returns the size in bytes of the changes 83 // relative to its base filesystem directory. 84 DiffSize(id, parent string) (size int64, err error) 85 } 86 87 func init() { 88 drivers = make(map[string]InitFunc) 89 } 90 91 func Register(name string, initFunc InitFunc) error { 92 if _, exists := drivers[name]; exists { 93 return fmt.Errorf("Name already registered %s", name) 94 } 95 drivers[name] = initFunc 96 97 return nil 98 } 99 100 func GetDriver(name, home string, options []string) (Driver, error) { 101 if initFunc, exists := drivers[name]; exists { 102 return initFunc(filepath.Join(home, name), options) 103 } 104 logrus.Errorf("Failed to GetDriver graph %s %s", name, home) 105 return nil, ErrNotSupported 106 } 107 108 func New(root string, options []string) (driver Driver, err error) { 109 for _, name := range []string{os.Getenv("DOCKER_DRIVER"), DefaultDriver} { 110 if name != "" { 111 logrus.Debugf("[graphdriver] trying provided driver %q", name) // so the logs show specified driver 112 return GetDriver(name, root, options) 113 } 114 } 115 116 // Guess for prior driver 117 priorDrivers := scanPriorDrivers(root) 118 for _, name := range priority { 119 if name == "vfs" { 120 // don't use vfs even if there is state present. 121 continue 122 } 123 for _, prior := range priorDrivers { 124 // of the state found from prior drivers, check in order of our priority 125 // which we would prefer 126 if prior == name { 127 driver, err = GetDriver(name, root, options) 128 if err != nil { 129 // unlike below, we will return error here, because there is prior 130 // state, and now it is no longer supported/prereq/compatible, so 131 // something changed and needs attention. Otherwise the daemon's 132 // images would just "disappear". 133 logrus.Errorf("[graphdriver] prior storage driver %q failed: %s", name, err) 134 return nil, err 135 } 136 if err := checkPriorDriver(name, root); err != nil { 137 return nil, err 138 } 139 logrus.Infof("[graphdriver] using prior storage driver %q", name) 140 return driver, nil 141 } 142 } 143 } 144 145 // Check for priority drivers first 146 for _, name := range priority { 147 driver, err = GetDriver(name, root, options) 148 if err != nil { 149 if err == ErrNotSupported || err == ErrPrerequisites || err == ErrIncompatibleFS { 150 continue 151 } 152 return nil, err 153 } 154 return driver, nil 155 } 156 157 // Check all registered drivers if no priority driver is found 158 for _, initFunc := range drivers { 159 if driver, err = initFunc(root, options); err != nil { 160 if err == ErrNotSupported || err == ErrPrerequisites || err == ErrIncompatibleFS { 161 continue 162 } 163 return nil, err 164 } 165 return driver, nil 166 } 167 return nil, fmt.Errorf("No supported storage backend found") 168 } 169 170 // scanPriorDrivers returns an un-ordered scan of directories of prior storage drivers 171 func scanPriorDrivers(root string) []string { 172 priorDrivers := []string{} 173 for driver := range drivers { 174 p := filepath.Join(root, driver) 175 if _, err := os.Stat(p); err == nil && driver != "vfs" { 176 priorDrivers = append(priorDrivers, driver) 177 } 178 } 179 return priorDrivers 180 } 181 182 func checkPriorDriver(name, root string) error { 183 priorDrivers := []string{} 184 for _, prior := range scanPriorDrivers(root) { 185 if prior != name && prior != "vfs" { 186 if _, err := os.Stat(filepath.Join(root, prior)); err == nil { 187 priorDrivers = append(priorDrivers, prior) 188 } 189 } 190 } 191 192 if len(priorDrivers) > 0 { 193 194 return errors.New(fmt.Sprintf("%q contains other graphdrivers: %s; Please cleanup or explicitly choose storage driver (-s <DRIVER>)", root, strings.Join(priorDrivers, ","))) 195 } 196 return nil 197 }