github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/daemon/graphdriver/vfs/driver.go (about)

     1  package vfs // import "github.com/docker/docker/daemon/graphdriver/vfs"
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/docker/docker/daemon/graphdriver"
     9  	"github.com/docker/docker/daemon/graphdriver/quota"
    10  	"github.com/docker/docker/errdefs"
    11  	"github.com/docker/docker/pkg/containerfs"
    12  	"github.com/docker/docker/pkg/idtools"
    13  	"github.com/docker/docker/pkg/parsers"
    14  	"github.com/docker/docker/pkg/system"
    15  	units "github.com/docker/go-units"
    16  	"github.com/opencontainers/selinux/go-selinux/label"
    17  	"github.com/pkg/errors"
    18  )
    19  
    20  var (
    21  	// CopyDir defines the copy method to use.
    22  	CopyDir = dirCopy
    23  )
    24  
    25  func init() {
    26  	graphdriver.Register("vfs", Init)
    27  }
    28  
    29  // Init returns a new VFS driver.
    30  // This sets the home directory for the driver and returns NaiveDiffDriver.
    31  func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
    32  	d := &Driver{
    33  		home:      home,
    34  		idMapping: idtools.NewIDMappingsFromMaps(uidMaps, gidMaps),
    35  	}
    36  
    37  	if err := d.parseOptions(options); err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	rootIDs := d.idMapping.RootPair()
    42  	if err := idtools.MkdirAllAndChown(home, 0700, rootIDs); err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	setupDriverQuota(d)
    47  
    48  	if size := d.getQuotaOpt(); !d.quotaSupported() && size > 0 {
    49  		return nil, quota.ErrQuotaNotSupported
    50  	}
    51  
    52  	return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil
    53  }
    54  
    55  // Driver holds information about the driver, home directory of the driver.
    56  // Driver implements graphdriver.ProtoDriver. It uses only basic vfs operations.
    57  // In order to support layering, files are copied from the parent layer into the new layer. There is no copy-on-write support.
    58  // Driver must be wrapped in NaiveDiffDriver to be used as a graphdriver.Driver
    59  type Driver struct {
    60  	driverQuota
    61  	home      string
    62  	idMapping *idtools.IdentityMapping
    63  }
    64  
    65  func (d *Driver) String() string {
    66  	return "vfs"
    67  }
    68  
    69  // Status is used for implementing the graphdriver.ProtoDriver interface. VFS does not currently have any status information.
    70  func (d *Driver) Status() [][2]string {
    71  	return nil
    72  }
    73  
    74  // GetMetadata is used for implementing the graphdriver.ProtoDriver interface. VFS does not currently have any meta data.
    75  func (d *Driver) GetMetadata(id string) (map[string]string, error) {
    76  	return nil, nil
    77  }
    78  
    79  // Cleanup is used to implement graphdriver.ProtoDriver. There is no cleanup required for this driver.
    80  func (d *Driver) Cleanup() error {
    81  	return nil
    82  }
    83  
    84  func (d *Driver) parseOptions(options []string) error {
    85  	for _, option := range options {
    86  		key, val, err := parsers.ParseKeyValueOpt(option)
    87  		if err != nil {
    88  			return errdefs.InvalidParameter(err)
    89  		}
    90  		switch key {
    91  		case "size":
    92  			size, err := units.RAMInBytes(val)
    93  			if err != nil {
    94  				return errdefs.InvalidParameter(err)
    95  			}
    96  			if err = d.setQuotaOpt(uint64(size)); err != nil {
    97  				return errdefs.InvalidParameter(errors.Wrap(err, "failed to set option size for vfs"))
    98  			}
    99  		default:
   100  			return errdefs.InvalidParameter(errors.Errorf("unknown option %s for vfs", key))
   101  		}
   102  	}
   103  	return nil
   104  }
   105  
   106  // CreateReadWrite creates a layer that is writable for use as a container
   107  // file system.
   108  func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
   109  	quotaSize := d.getQuotaOpt()
   110  
   111  	if opts != nil {
   112  		for key, val := range opts.StorageOpt {
   113  			switch key {
   114  			case "size":
   115  				if !d.quotaSupported() {
   116  					return quota.ErrQuotaNotSupported
   117  				}
   118  				size, err := units.RAMInBytes(val)
   119  				if err != nil {
   120  					return errdefs.InvalidParameter(err)
   121  				}
   122  				quotaSize = uint64(size)
   123  			default:
   124  				return errdefs.InvalidParameter(errors.Errorf("Storage opt %s not supported", key))
   125  			}
   126  		}
   127  	}
   128  
   129  	return d.create(id, parent, quotaSize)
   130  }
   131  
   132  // Create prepares the filesystem for the VFS driver and copies the directory for the given id under the parent.
   133  func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
   134  	if opts != nil && len(opts.StorageOpt) != 0 {
   135  		return fmt.Errorf("--storage-opt is not supported for vfs on read-only layers")
   136  	}
   137  
   138  	return d.create(id, parent, 0)
   139  }
   140  
   141  func (d *Driver) create(id, parent string, size uint64) error {
   142  	dir := d.dir(id)
   143  	rootIDs := d.idMapping.RootPair()
   144  	if err := idtools.MkdirAllAndChown(filepath.Dir(dir), 0700, rootIDs); err != nil {
   145  		return err
   146  	}
   147  	if err := idtools.MkdirAndChown(dir, 0755, rootIDs); err != nil {
   148  		return err
   149  	}
   150  
   151  	if size != 0 {
   152  		if err := d.setupQuota(dir, size); err != nil {
   153  			return err
   154  		}
   155  	}
   156  
   157  	labelOpts := []string{"level:s0"}
   158  	if _, mountLabel, err := label.InitLabels(labelOpts); err == nil {
   159  		label.SetFileLabel(dir, mountLabel)
   160  	}
   161  	if parent == "" {
   162  		return nil
   163  	}
   164  	parentDir, err := d.Get(parent, "")
   165  	if err != nil {
   166  		return fmt.Errorf("%s: %s", parent, err)
   167  	}
   168  	return CopyDir(parentDir.Path(), dir)
   169  }
   170  
   171  func (d *Driver) dir(id string) string {
   172  	return filepath.Join(d.home, "dir", filepath.Base(id))
   173  }
   174  
   175  // Remove deletes the content from the directory for a given id.
   176  func (d *Driver) Remove(id string) error {
   177  	return system.EnsureRemoveAll(d.dir(id))
   178  }
   179  
   180  // Get returns the directory for the given id.
   181  func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) {
   182  	dir := d.dir(id)
   183  	if st, err := os.Stat(dir); err != nil {
   184  		return nil, err
   185  	} else if !st.IsDir() {
   186  		return nil, fmt.Errorf("%s: not a directory", dir)
   187  	}
   188  	return containerfs.NewLocalContainerFS(dir), nil
   189  }
   190  
   191  // Put is a noop for vfs that return nil for the error, since this driver has no runtime resources to clean up.
   192  func (d *Driver) Put(id string) error {
   193  	// The vfs driver has no runtime resources (e.g. mounts)
   194  	// to clean up, so we don't need anything here
   195  	return nil
   196  }
   197  
   198  // Exists checks to see if the directory exists for the given id.
   199  func (d *Driver) Exists(id string) bool {
   200  	_, err := os.Stat(d.dir(id))
   201  	return err == nil
   202  }