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