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