github.com/thy00/storage@v1.12.8/drivers/driver.go (about)

     1  package graphdriver
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/pkg/errors"
    11  	"github.com/sirupsen/logrus"
    12  	"github.com/vbatts/tar-split/tar/storage"
    13  
    14  	"github.com/containers/storage/pkg/archive"
    15  	"github.com/containers/storage/pkg/idtools"
    16  )
    17  
    18  // FsMagic unsigned id of the filesystem in use.
    19  type FsMagic uint32
    20  
    21  const (
    22  	// FsMagicUnsupported is a predefined constant value other than a valid filesystem id.
    23  	FsMagicUnsupported = FsMagic(0x00000000)
    24  )
    25  
    26  var (
    27  	// All registered drivers
    28  	drivers map[string]InitFunc
    29  
    30  	// ErrNotSupported returned when driver is not supported.
    31  	ErrNotSupported = errors.New("driver not supported")
    32  	// ErrPrerequisites returned when driver does not meet prerequisites.
    33  	ErrPrerequisites = errors.New("prerequisites for driver not satisfied (wrong filesystem?)")
    34  	// ErrIncompatibleFS returned when file system is not supported.
    35  	ErrIncompatibleFS = fmt.Errorf("backing file system is unsupported for this graph driver")
    36  )
    37  
    38  //CreateOpts contains optional arguments for Create() and CreateReadWrite()
    39  // methods.
    40  type CreateOpts struct {
    41  	MountLabel string
    42  	StorageOpt map[string]string
    43  	*idtools.IDMappings
    44  }
    45  
    46  // MountOpts contains optional arguments for LayerStope.Mount() methods.
    47  type MountOpts struct {
    48  	// Mount label is the MAC Labels to assign to mount point (SELINUX)
    49  	MountLabel string
    50  	// UidMaps & GidMaps are the User Namespace mappings to be assigned to content in the mount point
    51  	UidMaps []idtools.IDMap
    52  	GidMaps []idtools.IDMap
    53  	Options []string
    54  }
    55  
    56  // InitFunc initializes the storage driver.
    57  type InitFunc func(root string, options []string, uidMaps, gidMaps []idtools.IDMap) (Driver, error)
    58  
    59  // ProtoDriver defines the basic capabilities of a driver.
    60  // This interface exists solely to be a minimum set of methods
    61  // for client code which choose not to implement the entire Driver
    62  // interface and use the NaiveDiffDriver wrapper constructor.
    63  //
    64  // Use of ProtoDriver directly by client code is not recommended.
    65  type ProtoDriver interface {
    66  	// String returns a string representation of this driver.
    67  	String() string
    68  	// CreateReadWrite creates a new, empty filesystem layer that is ready
    69  	// to be used as the storage for a container. Additional options can
    70  	// be passed in opts. parent may be "" and opts may be nil.
    71  	CreateReadWrite(id, parent string, opts *CreateOpts) error
    72  	// Create creates a new, empty, filesystem layer with the
    73  	// specified id and parent and options passed in opts. Parent
    74  	// may be "" and opts may be nil.
    75  	Create(id, parent string, opts *CreateOpts) error
    76  	// CreateFromTemplate creates a new filesystem layer with the specified id
    77  	// and parent, with contents identical to the specified template layer.
    78  	CreateFromTemplate(id, template string, templateIDMappings *idtools.IDMappings, parent string, parentIDMappings *idtools.IDMappings, opts *CreateOpts, readWrite bool) error
    79  	// Remove attempts to remove the filesystem layer with this id.
    80  	Remove(id string) error
    81  	// Get returns the mountpoint for the layered filesystem referred
    82  	// to by this id. You can optionally specify a mountLabel or "".
    83  	// Optionally it gets the mappings used to create the layer.
    84  	// Returns the absolute path to the mounted layered filesystem.
    85  	Get(id string, options MountOpts) (dir string, err error)
    86  	// Put releases the system resources for the specified id,
    87  	// e.g, unmounting layered filesystem.
    88  	Put(id string) error
    89  	// Exists returns whether a filesystem layer with the specified
    90  	// ID exists on this driver.
    91  	Exists(id string) bool
    92  	// Status returns a set of key-value pairs which give low
    93  	// level diagnostic status about this driver.
    94  	Status() [][2]string
    95  	// Returns a set of key-value pairs which give low level information
    96  	// about the image/container driver is managing.
    97  	Metadata(id string) (map[string]string, error)
    98  	// Cleanup performs necessary tasks to release resources
    99  	// held by the driver, e.g., unmounting all layered filesystems
   100  	// known to this driver.
   101  	Cleanup() error
   102  	// AdditionalImageStores returns additional image stores supported by the driver
   103  	AdditionalImageStores() []string
   104  }
   105  
   106  // DiffDriver is the interface to use to implement graph diffs
   107  type DiffDriver interface {
   108  	// Diff produces an archive of the changes between the specified
   109  	// layer and its parent layer which may be "".
   110  	Diff(id string, idMappings *idtools.IDMappings, parent string, parentIDMappings *idtools.IDMappings, mountLabel string) (io.ReadCloser, error)
   111  	// Changes produces a list of changes between the specified layer
   112  	// and its parent layer. If parent is "", then all changes will be ADD changes.
   113  	Changes(id string, idMappings *idtools.IDMappings, parent string, parentIDMappings *idtools.IDMappings, mountLabel string) ([]archive.Change, error)
   114  	// ApplyDiff extracts the changeset from the given diff into the
   115  	// layer with the specified id and parent, returning the size of the
   116  	// new layer in bytes.
   117  	// The io.Reader must be an uncompressed stream.
   118  	ApplyDiff(id string, idMappings *idtools.IDMappings, parent string, mountLabel string, diff io.Reader) (size int64, err error)
   119  	// DiffSize calculates the changes between the specified id
   120  	// and its parent and returns the size in bytes of the changes
   121  	// relative to its base filesystem directory.
   122  	DiffSize(id string, idMappings *idtools.IDMappings, parent string, parentIDMappings *idtools.IDMappings, mountLabel string) (size int64, err error)
   123  }
   124  
   125  // LayerIDMapUpdater is the interface that implements ID map changes for layers.
   126  type LayerIDMapUpdater interface {
   127  	// UpdateLayerIDMap walks the layer's filesystem tree, changing the ownership
   128  	// information using the toContainer and toHost mappings, using them to replace
   129  	// on-disk owner UIDs and GIDs which are "host" values in the first map with
   130  	// UIDs and GIDs for "host" values from the second map which correspond to the
   131  	// same "container" IDs.  This method should only be called after a layer is
   132  	// first created and populated, and before it is mounted, as other changes made
   133  	// relative to a parent layer, but before this method is called, may be discarded
   134  	// by Diff().
   135  	UpdateLayerIDMap(id string, toContainer, toHost *idtools.IDMappings, mountLabel string) error
   136  
   137  	// SupportsShifting tells whether the driver support shifting of the UIDs/GIDs in a
   138  	// image and it is not required to Chown the files when running in an user namespace.
   139  	SupportsShifting() bool
   140  }
   141  
   142  // Driver is the interface for layered/snapshot file system drivers.
   143  type Driver interface {
   144  	ProtoDriver
   145  	DiffDriver
   146  	LayerIDMapUpdater
   147  }
   148  
   149  // Capabilities defines a list of capabilities a driver may implement.
   150  // These capabilities are not required; however, they do determine how a
   151  // graphdriver can be used.
   152  type Capabilities struct {
   153  	// Flags that this driver is capable of reproducing exactly equivalent
   154  	// diffs for read-only layers. If set, clients can rely on the driver
   155  	// for consistent tar streams, and avoid extra processing to account
   156  	// for potential differences (eg: the layer store's use of tar-split).
   157  	ReproducesExactDiffs bool
   158  }
   159  
   160  // CapabilityDriver is the interface for layered file system drivers that
   161  // can report on their Capabilities.
   162  type CapabilityDriver interface {
   163  	Capabilities() Capabilities
   164  }
   165  
   166  // DiffGetterDriver is the interface for layered file system drivers that
   167  // provide a specialized function for getting file contents for tar-split.
   168  type DiffGetterDriver interface {
   169  	Driver
   170  	// DiffGetter returns an interface to efficiently retrieve the contents
   171  	// of files in a layer.
   172  	DiffGetter(id string) (FileGetCloser, error)
   173  }
   174  
   175  // FileGetCloser extends the storage.FileGetter interface with a Close method
   176  // for cleaning up.
   177  type FileGetCloser interface {
   178  	storage.FileGetter
   179  	// Close cleans up any resources associated with the FileGetCloser.
   180  	Close() error
   181  }
   182  
   183  // Checker makes checks on specified filesystems.
   184  type Checker interface {
   185  	// IsMounted returns true if the provided path is mounted for the specific checker
   186  	IsMounted(path string) bool
   187  }
   188  
   189  func init() {
   190  	drivers = make(map[string]InitFunc)
   191  }
   192  
   193  // Register registers an InitFunc for the driver.
   194  func Register(name string, initFunc InitFunc) error {
   195  	if _, exists := drivers[name]; exists {
   196  		return fmt.Errorf("Name already registered %s", name)
   197  	}
   198  	drivers[name] = initFunc
   199  
   200  	return nil
   201  }
   202  
   203  // GetDriver initializes and returns the registered driver
   204  func GetDriver(name string, config Options) (Driver, error) {
   205  	if initFunc, exists := drivers[name]; exists {
   206  		return initFunc(filepath.Join(config.Root, name), config.DriverOptions, config.UIDMaps, config.GIDMaps)
   207  	}
   208  
   209  	logrus.Errorf("Failed to GetDriver graph %s %s", name, config.Root)
   210  	return nil, errors.Wrapf(ErrNotSupported, "failed to GetDriver graph %s %s", name, config.Root)
   211  }
   212  
   213  // getBuiltinDriver initializes and returns the registered driver, but does not try to load from plugins
   214  func getBuiltinDriver(name, home string, options []string, uidMaps, gidMaps []idtools.IDMap) (Driver, error) {
   215  	if initFunc, exists := drivers[name]; exists {
   216  		return initFunc(filepath.Join(home, name), options, uidMaps, gidMaps)
   217  	}
   218  	logrus.Errorf("Failed to built-in GetDriver graph %s %s", name, home)
   219  	return nil, errors.Wrapf(ErrNotSupported, "failed to built-in GetDriver graph %s %s", name, home)
   220  }
   221  
   222  // Options is used to initialize a graphdriver
   223  type Options struct {
   224  	Root                string
   225  	DriverOptions       []string
   226  	UIDMaps             []idtools.IDMap
   227  	GIDMaps             []idtools.IDMap
   228  	ExperimentalEnabled bool
   229  }
   230  
   231  // New creates the driver and initializes it at the specified root.
   232  func New(name string, config Options) (Driver, error) {
   233  	if name != "" {
   234  		logrus.Debugf("[graphdriver] trying provided driver %q", name) // so the logs show specified driver
   235  		return GetDriver(name, config)
   236  	}
   237  
   238  	// Guess for prior driver
   239  	driversMap := scanPriorDrivers(config.Root)
   240  	for _, name := range priority {
   241  		if name == "vfs" {
   242  			// don't use vfs even if there is state present.
   243  			continue
   244  		}
   245  		if _, prior := driversMap[name]; prior {
   246  			// of the state found from prior drivers, check in order of our priority
   247  			// which we would prefer
   248  			driver, err := getBuiltinDriver(name, config.Root, config.DriverOptions, config.UIDMaps, config.GIDMaps)
   249  			if err != nil {
   250  				// unlike below, we will return error here, because there is prior
   251  				// state, and now it is no longer supported/prereq/compatible, so
   252  				// something changed and needs attention. Otherwise the daemon's
   253  				// images would just "disappear".
   254  				logrus.Errorf("[graphdriver] prior storage driver %s failed: %s", name, err)
   255  				return nil, err
   256  			}
   257  
   258  			// abort starting when there are other prior configured drivers
   259  			// to ensure the user explicitly selects the driver to load
   260  			if len(driversMap)-1 > 0 {
   261  				var driversSlice []string
   262  				for name := range driversMap {
   263  					driversSlice = append(driversSlice, name)
   264  				}
   265  
   266  				return nil, fmt.Errorf("%s contains several valid graphdrivers: %s; Please cleanup or explicitly choose storage driver (-s <DRIVER>)", config.Root, strings.Join(driversSlice, ", "))
   267  			}
   268  
   269  			logrus.Infof("[graphdriver] using prior storage driver: %s", name)
   270  			return driver, nil
   271  		}
   272  	}
   273  
   274  	// Check for priority drivers first
   275  	for _, name := range priority {
   276  		driver, err := getBuiltinDriver(name, config.Root, config.DriverOptions, config.UIDMaps, config.GIDMaps)
   277  		if err != nil {
   278  			if isDriverNotSupported(err) {
   279  				continue
   280  			}
   281  			return nil, err
   282  		}
   283  		return driver, nil
   284  	}
   285  
   286  	// Check all registered drivers if no priority driver is found
   287  	for name, initFunc := range drivers {
   288  		driver, err := initFunc(filepath.Join(config.Root, name), config.DriverOptions, config.UIDMaps, config.GIDMaps)
   289  		if err != nil {
   290  			if isDriverNotSupported(err) {
   291  				continue
   292  			}
   293  			return nil, err
   294  		}
   295  		return driver, nil
   296  	}
   297  	return nil, fmt.Errorf("No supported storage backend found")
   298  }
   299  
   300  // isDriverNotSupported returns true if the error initializing
   301  // the graph driver is a non-supported error.
   302  func isDriverNotSupported(err error) bool {
   303  	cause := errors.Cause(err)
   304  	return cause == ErrNotSupported || cause == ErrPrerequisites || cause == ErrIncompatibleFS
   305  }
   306  
   307  // scanPriorDrivers returns an un-ordered scan of directories of prior storage drivers
   308  func scanPriorDrivers(root string) map[string]bool {
   309  	driversMap := make(map[string]bool)
   310  
   311  	for driver := range drivers {
   312  		p := filepath.Join(root, driver)
   313  		if _, err := os.Stat(p); err == nil && driver != "vfs" {
   314  			driversMap[driver] = true
   315  		}
   316  	}
   317  	return driversMap
   318  }