driver.go 6.3 KB

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