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