driver.go 5.6 KB

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