driver.go 5.6 KB

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