github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/daemon/graphdriver/driver.go (about) 1 package graphdriver // import "github.com/Prakhar-Agarwal-byte/moby/daemon/graphdriver" 2 3 import ( 4 "context" 5 "fmt" 6 "io" 7 "os" 8 "path/filepath" 9 "strings" 10 11 "github.com/containerd/log" 12 "github.com/Prakhar-Agarwal-byte/moby/pkg/archive" 13 "github.com/Prakhar-Agarwal-byte/moby/pkg/idtools" 14 "github.com/Prakhar-Agarwal-byte/moby/pkg/plugingetter" 15 "github.com/pkg/errors" 16 "github.com/vbatts/tar-split/tar/storage" 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 // All registered drivers 28 var drivers map[string]InitFunc 29 30 // CreateOpts contains optional arguments for Create() and CreateReadWrite() 31 // methods. 32 type CreateOpts struct { 33 MountLabel string 34 StorageOpt map[string]string 35 } 36 37 // InitFunc initializes the storage driver. 38 type InitFunc func(root string, options []string, idMap idtools.IdentityMapping) (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. Additional options can 51 // be passed in opts. parent may be "" and opts may be nil. 52 CreateReadWrite(id, parent string, opts *CreateOpts) error 53 // Create creates a new, empty, filesystem layer with the 54 // specified id and parent and options passed in opts. Parent 55 // may be "" and opts may be nil. 56 Create(id, parent string, opts *CreateOpts) error 57 // Remove attempts to remove the filesystem layer with this id. 58 Remove(id string) error 59 // Get returns the mountpoint for the layered filesystem referred 60 // to by this id. You can optionally specify a mountLabel or "". 61 // Returns the absolute path to the mounted layered filesystem. 62 Get(id, mountLabel string) (fs string, err error) 63 // Put releases the system resources for the specified id, 64 // e.g, unmounting layered filesystem. 65 Put(id string) error 66 // Exists returns whether a filesystem layer with the specified 67 // ID exists on this driver. 68 Exists(id string) bool 69 // Status returns a set of key-value pairs which give low 70 // level diagnostic status about this driver. 71 Status() [][2]string 72 // Returns a set of key-value pairs which give low level information 73 // about the image/container driver is managing. 74 GetMetadata(id string) (map[string]string, error) 75 // Cleanup performs necessary tasks to release resources 76 // held by the driver, e.g., unmounting all layered filesystems 77 // known to this driver. 78 Cleanup() error 79 } 80 81 // DiffDriver is the interface to use to implement graph diffs 82 type DiffDriver interface { 83 // Diff produces an archive of the changes between the specified 84 // layer and its parent layer which may be "". 85 Diff(id, parent string) (io.ReadCloser, error) 86 // Changes produces a list of changes between the specified layer 87 // and its parent layer. If parent is "", then all changes will be ADD changes. 88 Changes(id, parent string) ([]archive.Change, error) 89 // ApplyDiff extracts the changeset from the given diff into the 90 // layer with the specified id and parent, returning the size of the 91 // new layer in bytes. 92 // The archive.Reader must be an uncompressed stream. 93 ApplyDiff(id, parent string, diff io.Reader) (size int64, err error) 94 // DiffSize calculates the changes between the specified id 95 // and its parent and returns the size in bytes of the changes 96 // relative to its base filesystem directory. 97 DiffSize(id, parent string) (size int64, err error) 98 } 99 100 // Driver is the interface for layered/snapshot file system drivers. 101 type Driver interface { 102 ProtoDriver 103 DiffDriver 104 } 105 106 // Capabilities defines a list of capabilities a driver may implement. 107 // These capabilities are not required; however, they do determine how a 108 // graphdriver can be used. 109 type Capabilities struct { 110 // Flags that this driver is capable of reproducing exactly equivalent 111 // diffs for read-only layers. If set, clients can rely on the driver 112 // for consistent tar streams, and avoid extra processing to account 113 // for potential differences (eg: the layer store's use of tar-split). 114 ReproducesExactDiffs bool 115 } 116 117 // CapabilityDriver is the interface for layered file system drivers that 118 // can report on their Capabilities. 119 type CapabilityDriver interface { 120 Capabilities() Capabilities 121 } 122 123 // DiffGetterDriver is the interface for layered file system drivers that 124 // provide a specialized function for getting file contents for tar-split. 125 type DiffGetterDriver interface { 126 Driver 127 // DiffGetter returns an interface to efficiently retrieve the contents 128 // of files in a layer. 129 DiffGetter(id string) (FileGetCloser, error) 130 } 131 132 // FileGetCloser extends the storage.FileGetter interface with a Close method 133 // for cleaning up. 134 type FileGetCloser interface { 135 storage.FileGetter 136 // Close cleans up any resources associated with the FileGetCloser. 137 Close() error 138 } 139 140 // Checker makes checks on specified filesystems. 141 type Checker interface { 142 // IsMounted returns true if the provided path is mounted for the specific checker 143 IsMounted(path string) bool 144 } 145 146 func init() { 147 drivers = make(map[string]InitFunc) 148 } 149 150 // Register registers an InitFunc for the driver. 151 func Register(name string, initFunc InitFunc) error { 152 if _, exists := drivers[name]; exists { 153 return errors.Errorf("name already registered %s", name) 154 } 155 drivers[name] = initFunc 156 157 return nil 158 } 159 160 // GetDriver initializes and returns the registered driver 161 func GetDriver(name string, pg plugingetter.PluginGetter, config Options) (Driver, error) { 162 if initFunc, exists := drivers[name]; exists { 163 return initFunc(filepath.Join(config.Root, name), config.DriverOptions, config.IDMap) 164 } 165 166 pluginDriver, err := lookupPlugin(name, pg, config) 167 if err == nil { 168 return pluginDriver, nil 169 } 170 log.G(context.TODO()).WithError(err).WithField("driver", name).WithField("home-dir", config.Root).Error("Failed to GetDriver graph") 171 return nil, ErrNotSupported 172 } 173 174 // getBuiltinDriver initializes and returns the registered driver, but does not try to load from plugins 175 func getBuiltinDriver(name, home string, options []string, idMap idtools.IdentityMapping) (Driver, error) { 176 if initFunc, exists := drivers[name]; exists { 177 return initFunc(filepath.Join(home, name), options, idMap) 178 } 179 log.G(context.TODO()).Errorf("Failed to built-in GetDriver graph %s %s", name, home) 180 return nil, ErrNotSupported 181 } 182 183 // Options is used to initialize a graphdriver 184 type Options struct { 185 Root string 186 DriverOptions []string 187 IDMap idtools.IdentityMapping 188 ExperimentalEnabled bool 189 } 190 191 // New creates the driver and initializes it at the specified root. 192 func New(name string, pg plugingetter.PluginGetter, config Options) (Driver, error) { 193 ctx := context.TODO() 194 if name != "" { 195 log.G(ctx).Infof("[graphdriver] trying configured driver: %s", name) 196 if err := checkRemoved(name); err != nil { 197 return nil, err 198 } 199 return GetDriver(name, pg, config) 200 } 201 202 // Guess for prior driver 203 driversMap := scanPriorDrivers(config.Root) 204 priorityList := strings.Split(priority, ",") 205 log.G(ctx).Debugf("[graphdriver] priority list: %v", priorityList) 206 for _, name := range priorityList { 207 if _, prior := driversMap[name]; prior { 208 // of the state found from prior drivers, check in order of our priority 209 // which we would prefer 210 driver, err := getBuiltinDriver(name, config.Root, config.DriverOptions, config.IDMap) 211 if err != nil { 212 // unlike below, we will return error here, because there is prior 213 // state, and now it is no longer supported/prereq/compatible, so 214 // something changed and needs attention. Otherwise the daemon's 215 // images would just "disappear". 216 log.G(ctx).Errorf("[graphdriver] prior storage driver %s failed: %s", name, err) 217 return nil, err 218 } 219 220 // abort starting when there are other prior configured drivers 221 // to ensure the user explicitly selects the driver to load 222 if len(driversMap) > 1 { 223 var driversSlice []string 224 for name := range driversMap { 225 driversSlice = append(driversSlice, name) 226 } 227 228 err = errors.Errorf("%s contains several valid graphdrivers: %s; cleanup or explicitly choose storage driver (-s <DRIVER>)", config.Root, strings.Join(driversSlice, ", ")) 229 log.G(ctx).Errorf("[graphdriver] %v", err) 230 return nil, err 231 } 232 233 log.G(ctx).Infof("[graphdriver] using prior storage driver: %s", name) 234 return driver, nil 235 } 236 } 237 238 // If no prior state was found, continue with automatic selection, and pick 239 // the first supported, non-deprecated, storage driver (in order of priorityList). 240 for _, name := range priorityList { 241 driver, err := getBuiltinDriver(name, config.Root, config.DriverOptions, config.IDMap) 242 if err != nil { 243 if IsDriverNotSupported(err) { 244 continue 245 } 246 return nil, err 247 } 248 return driver, nil 249 } 250 251 // Check all registered drivers if no priority driver is found 252 for name, initFunc := range drivers { 253 driver, err := initFunc(filepath.Join(config.Root, name), config.DriverOptions, config.IDMap) 254 if err != nil { 255 if IsDriverNotSupported(err) { 256 continue 257 } 258 return nil, err 259 } 260 return driver, nil 261 } 262 263 return nil, errors.Errorf("no supported storage driver found") 264 } 265 266 // scanPriorDrivers returns an un-ordered scan of directories of prior storage 267 // drivers. The 'vfs' storage driver is not taken into account, and ignored. 268 func scanPriorDrivers(root string) map[string]bool { 269 driversMap := make(map[string]bool) 270 271 for driver := range drivers { 272 p := filepath.Join(root, driver) 273 if _, err := os.Stat(p); err == nil && driver != "vfs" { 274 if !isEmptyDir(p) { 275 driversMap[driver] = true 276 } 277 } 278 } 279 return driversMap 280 } 281 282 // isEmptyDir checks if a directory is empty. It is used to check if prior 283 // storage-driver directories exist. If an error occurs, it also assumes the 284 // directory is not empty (which preserves the behavior _before_ this check 285 // was added) 286 func isEmptyDir(name string) bool { 287 f, err := os.Open(name) 288 if err != nil { 289 return false 290 } 291 defer f.Close() 292 293 if _, err = f.Readdirnames(1); err == io.EOF { 294 return true 295 } 296 return false 297 } 298 299 // checkRemoved checks if a storage-driver has been deprecated (and removed) 300 func checkRemoved(name string) error { 301 switch name { 302 case "aufs", "devicemapper", "overlay": 303 return NotSupportedError(fmt.Sprintf("[graphdriver] ERROR: the %s storage-driver has been deprecated and removed; visit https://docs.docker.com/go/storage-driver/ for more information", name)) 304 } 305 return nil 306 }