driver.go 5.6 KB

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