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