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