github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/daemon/graphdriver/driver.go (about) 1 package graphdriver 2 3 import ( 4 "errors" 5 "fmt" 6 "io" 7 "os" 8 "path/filepath" 9 "strings" 10 11 "github.com/Sirupsen/logrus" 12 "github.com/vbatts/tar-split/tar/storage" 13 14 "github.com/docker/docker/pkg/archive" 15 "github.com/docker/docker/pkg/idtools" 16 "github.com/docker/docker/pkg/plugingetter" 17 ) 18 19 // FsMagic unsigned id of the filesystem in use. 20 type FsMagic uint32 21 22 const ( 23 // FsMagicUnsupported is a predefined constant value other than a valid filesystem id. 24 FsMagicUnsupported = FsMagic(0x00000000) 25 ) 26 27 var ( 28 // All registered drivers 29 drivers map[string]InitFunc 30 31 // ErrNotSupported returned when driver is not supported. 32 ErrNotSupported = errors.New("driver not supported") 33 // ErrPrerequisites retuned when driver does not meet prerequisites. 34 ErrPrerequisites = errors.New("prerequisites for driver not satisfied (wrong filesystem?)") 35 // ErrIncompatibleFS returned when file system is not supported. 36 ErrIncompatibleFS = fmt.Errorf("backing file system is unsupported for this graph driver") 37 ) 38 39 //CreateOpts contains optional arguments for Create() and CreateReadWrite() 40 // methods. 41 type CreateOpts struct { 42 MountLabel string 43 StorageOpt map[string]string 44 } 45 46 // InitFunc initializes the storage driver. 47 type InitFunc func(root string, options []string, uidMaps, gidMaps []idtools.IDMap) (Driver, error) 48 49 // ProtoDriver defines the basic capabilities of a driver. 50 // This interface exists solely to be a minimum set of methods 51 // for client code which choose not to implement the entire Driver 52 // interface and use the NaiveDiffDriver wrapper constructor. 53 // 54 // Use of ProtoDriver directly by client code is not recommended. 55 type ProtoDriver interface { 56 // String returns a string representation of this driver. 57 String() string 58 // CreateReadWrite creates a new, empty filesystem layer that is ready 59 // to be used as the storage for a container. Additional options can 60 // be passed in opts. parent may be "" and opts may be nil. 61 CreateReadWrite(id, parent string, opts *CreateOpts) error 62 // Create creates a new, empty, filesystem layer with the 63 // specified id and parent and options passed in opts. Parent 64 // may be "" and opts may be nil. 65 Create(id, parent string, opts *CreateOpts) error 66 // Remove attempts to remove the filesystem layer with this id. 67 Remove(id string) error 68 // Get returns the mountpoint for the layered filesystem referred 69 // to by this id. You can optionally specify a mountLabel or "". 70 // Returns the absolute path to the mounted layered filesystem. 71 Get(id, mountLabel string) (dir string, err error) 72 // Put releases the system resources for the specified id, 73 // e.g, unmounting layered filesystem. 74 Put(id string) error 75 // Exists returns whether a filesystem layer with the specified 76 // ID exists on this driver. 77 Exists(id string) bool 78 // Status returns a set of key-value pairs which give low 79 // level diagnostic status about this driver. 80 Status() [][2]string 81 // Returns a set of key-value pairs which give low level information 82 // about the image/container driver is managing. 83 GetMetadata(id string) (map[string]string, error) 84 // Cleanup performs necessary tasks to release resources 85 // held by the driver, e.g., unmounting all layered filesystems 86 // known to this driver. 87 Cleanup() error 88 } 89 90 // DiffDriver is the interface to use to implement graph diffs 91 type DiffDriver interface { 92 // Diff produces an archive of the changes between the specified 93 // layer and its parent layer which may be "". 94 Diff(id, parent string) (io.ReadCloser, error) 95 // Changes produces a list of changes between the specified layer 96 // and its parent layer. If parent is "", then all changes will be ADD changes. 97 Changes(id, parent string) ([]archive.Change, error) 98 // ApplyDiff extracts the changeset from the given diff into the 99 // layer with the specified id and parent, returning the size of the 100 // new layer in bytes. 101 // The archive.Reader must be an uncompressed stream. 102 ApplyDiff(id, parent string, diff io.Reader) (size int64, err error) 103 // DiffSize calculates the changes between the specified id 104 // and its parent and returns the size in bytes of the changes 105 // relative to its base filesystem directory. 106 DiffSize(id, parent string) (size int64, err error) 107 } 108 109 // Driver is the interface for layered/snapshot file system drivers. 110 type Driver interface { 111 ProtoDriver 112 DiffDriver 113 } 114 115 // DiffGetterDriver is the interface for layered file system drivers that 116 // provide a specialized function for getting file contents for tar-split. 117 type DiffGetterDriver interface { 118 Driver 119 // DiffGetter returns an interface to efficiently retrieve the contents 120 // of files in a layer. 121 DiffGetter(id string) (FileGetCloser, error) 122 } 123 124 // FileGetCloser extends the storage.FileGetter interface with a Close method 125 // for cleaning up. 126 type FileGetCloser interface { 127 storage.FileGetter 128 // Close cleans up any resources associated with the FileGetCloser. 129 Close() error 130 } 131 132 // Checker makes checks on specified filesystems. 133 type Checker interface { 134 // IsMounted returns true if the provided path is mounted for the specific checker 135 IsMounted(path string) bool 136 } 137 138 func init() { 139 drivers = make(map[string]InitFunc) 140 } 141 142 // Register registers an InitFunc for the driver. 143 func Register(name string, initFunc InitFunc) error { 144 if _, exists := drivers[name]; exists { 145 return fmt.Errorf("Name already registered %s", name) 146 } 147 drivers[name] = initFunc 148 149 return nil 150 } 151 152 // GetDriver initializes and returns the registered driver 153 func GetDriver(name, home string, options []string, uidMaps, gidMaps []idtools.IDMap, pg plugingetter.PluginGetter) (Driver, error) { 154 if initFunc, exists := drivers[name]; exists { 155 return initFunc(filepath.Join(home, name), options, uidMaps, gidMaps) 156 } 157 if pluginDriver, err := lookupPlugin(name, home, options, pg); err == nil { 158 return pluginDriver, nil 159 } 160 logrus.Errorf("Failed to GetDriver graph %s %s", name, home) 161 return nil, ErrNotSupported 162 } 163 164 // getBuiltinDriver initializes and returns the registered driver, but does not try to load from plugins 165 func getBuiltinDriver(name, home string, options []string, uidMaps, gidMaps []idtools.IDMap) (Driver, error) { 166 if initFunc, exists := drivers[name]; exists { 167 return initFunc(filepath.Join(home, name), options, uidMaps, gidMaps) 168 } 169 logrus.Errorf("Failed to built-in GetDriver graph %s %s", name, home) 170 return nil, ErrNotSupported 171 } 172 173 // New creates the driver and initializes it at the specified root. 174 func New(root, name string, options []string, uidMaps, gidMaps []idtools.IDMap, pg plugingetter.PluginGetter) (Driver, error) { 175 if name != "" { 176 logrus.Debugf("[graphdriver] trying provided driver: %s", name) // so the logs show specified driver 177 return GetDriver(name, root, options, uidMaps, gidMaps, pg) 178 } 179 180 // Guess for prior driver 181 driversMap := scanPriorDrivers(root) 182 for _, name := range priority { 183 if name == "vfs" { 184 // don't use vfs even if there is state present. 185 continue 186 } 187 if _, prior := driversMap[name]; prior { 188 // of the state found from prior drivers, check in order of our priority 189 // which we would prefer 190 driver, err := getBuiltinDriver(name, root, options, uidMaps, gidMaps) 191 if err != nil { 192 // unlike below, we will return error here, because there is prior 193 // state, and now it is no longer supported/prereq/compatible, so 194 // something changed and needs attention. Otherwise the daemon's 195 // images would just "disappear". 196 logrus.Errorf("[graphdriver] prior storage driver %s failed: %s", name, err) 197 return nil, err 198 } 199 200 // abort starting when there are other prior configured drivers 201 // to ensure the user explicitly selects the driver to load 202 if len(driversMap)-1 > 0 { 203 var driversSlice []string 204 for name := range driversMap { 205 driversSlice = append(driversSlice, name) 206 } 207 208 return nil, fmt.Errorf("%s contains several valid graphdrivers: %s; Please cleanup or explicitly choose storage driver (-s <DRIVER>)", root, strings.Join(driversSlice, ", ")) 209 } 210 211 logrus.Infof("[graphdriver] using prior storage driver: %s", name) 212 return driver, nil 213 } 214 } 215 216 // Check for priority drivers first 217 for _, name := range priority { 218 driver, err := getBuiltinDriver(name, root, options, uidMaps, gidMaps) 219 if err != nil { 220 if isDriverNotSupported(err) { 221 continue 222 } 223 return nil, err 224 } 225 return driver, nil 226 } 227 228 // Check all registered drivers if no priority driver is found 229 for name, initFunc := range drivers { 230 driver, err := initFunc(filepath.Join(root, name), options, uidMaps, gidMaps) 231 if err != nil { 232 if isDriverNotSupported(err) { 233 continue 234 } 235 return nil, err 236 } 237 return driver, nil 238 } 239 return nil, fmt.Errorf("No supported storage backend found") 240 } 241 242 // isDriverNotSupported returns true if the error initializing 243 // the graph driver is a non-supported error. 244 func isDriverNotSupported(err error) bool { 245 return err == ErrNotSupported || err == ErrPrerequisites || err == ErrIncompatibleFS 246 } 247 248 // scanPriorDrivers returns an un-ordered scan of directories of prior storage drivers 249 func scanPriorDrivers(root string) map[string]bool { 250 driversMap := make(map[string]bool) 251 252 for driver := range drivers { 253 p := filepath.Join(root, driver) 254 if _, err := os.Stat(p); err == nil && driver != "vfs" { 255 driversMap[driver] = true 256 } 257 } 258 return driversMap 259 }