driver.go 5.8 KB

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