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