driver.go 5.9 KB

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