driver.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package 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/pkg/containerfs"
  9. "github.com/docker/docker/pkg/idtools"
  10. "github.com/docker/docker/pkg/system"
  11. units "github.com/docker/go-units"
  12. "github.com/opencontainers/selinux/go-selinux/label"
  13. )
  14. var (
  15. // CopyDir defines the copy method to use.
  16. CopyDir = dirCopy
  17. )
  18. func init() {
  19. graphdriver.Register("vfs", Init)
  20. }
  21. // Init returns a new VFS driver.
  22. // This sets the home directory for the driver and returns NaiveDiffDriver.
  23. func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
  24. d := &Driver{
  25. home: home,
  26. idMappings: idtools.NewIDMappingsFromMaps(uidMaps, gidMaps),
  27. }
  28. rootIDs := d.idMappings.RootPair()
  29. if err := idtools.MkdirAllAndChown(home, 0700, rootIDs); err != nil {
  30. return nil, err
  31. }
  32. setupDriverQuota(d)
  33. return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil
  34. }
  35. // Driver holds information about the driver, home directory of the driver.
  36. // Driver implements graphdriver.ProtoDriver. It uses only basic vfs operations.
  37. // In order to support layering, files are copied from the parent layer into the new layer. There is no copy-on-write support.
  38. // Driver must be wrapped in NaiveDiffDriver to be used as a graphdriver.Driver
  39. type Driver struct {
  40. driverQuota
  41. home string
  42. idMappings *idtools.IDMappings
  43. }
  44. func (d *Driver) String() string {
  45. return "vfs"
  46. }
  47. // Status is used for implementing the graphdriver.ProtoDriver interface. VFS does not currently have any status information.
  48. func (d *Driver) Status() [][2]string {
  49. return nil
  50. }
  51. // GetMetadata is used for implementing the graphdriver.ProtoDriver interface. VFS does not currently have any meta data.
  52. func (d *Driver) GetMetadata(id string) (map[string]string, error) {
  53. return nil, nil
  54. }
  55. // Cleanup is used to implement graphdriver.ProtoDriver. There is no cleanup required for this driver.
  56. func (d *Driver) Cleanup() error {
  57. return nil
  58. }
  59. // CreateReadWrite creates a layer that is writable for use as a container
  60. // file system.
  61. func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
  62. var err error
  63. var size int64
  64. if opts != nil {
  65. for key, val := range opts.StorageOpt {
  66. switch key {
  67. case "size":
  68. if !d.quotaSupported() {
  69. return quota.ErrQuotaNotSupported
  70. }
  71. if size, err = units.RAMInBytes(val); err != nil {
  72. return err
  73. }
  74. default:
  75. return fmt.Errorf("Storage opt %s not supported", key)
  76. }
  77. }
  78. }
  79. return d.create(id, parent, uint64(size))
  80. }
  81. // Create prepares the filesystem for the VFS driver and copies the directory for the given id under the parent.
  82. func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
  83. if opts != nil && len(opts.StorageOpt) != 0 {
  84. return fmt.Errorf("--storage-opt is not supported for vfs on read-only layers")
  85. }
  86. return d.create(id, parent, 0)
  87. }
  88. func (d *Driver) create(id, parent string, size uint64) error {
  89. dir := d.dir(id)
  90. rootIDs := d.idMappings.RootPair()
  91. if err := idtools.MkdirAllAndChown(filepath.Dir(dir), 0700, rootIDs); err != nil {
  92. return err
  93. }
  94. if err := idtools.MkdirAndChown(dir, 0755, rootIDs); err != nil {
  95. return err
  96. }
  97. if size != 0 {
  98. if err := d.setupQuota(dir, size); err != nil {
  99. return err
  100. }
  101. }
  102. labelOpts := []string{"level:s0"}
  103. if _, mountLabel, err := label.InitLabels(labelOpts); err == nil {
  104. label.SetFileLabel(dir, mountLabel)
  105. }
  106. if parent == "" {
  107. return nil
  108. }
  109. parentDir, err := d.Get(parent, "")
  110. if err != nil {
  111. return fmt.Errorf("%s: %s", parent, err)
  112. }
  113. return CopyDir(parentDir.Path(), dir)
  114. }
  115. func (d *Driver) dir(id string) string {
  116. return filepath.Join(d.home, "dir", filepath.Base(id))
  117. }
  118. // Remove deletes the content from the directory for a given id.
  119. func (d *Driver) Remove(id string) error {
  120. return system.EnsureRemoveAll(d.dir(id))
  121. }
  122. // Get returns the directory for the given id.
  123. func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) {
  124. dir := d.dir(id)
  125. if st, err := os.Stat(dir); err != nil {
  126. return nil, err
  127. } else if !st.IsDir() {
  128. return nil, fmt.Errorf("%s: not a directory", dir)
  129. }
  130. return containerfs.NewLocalContainerFS(dir), nil
  131. }
  132. // Put is a noop for vfs that return nil for the error, since this driver has no runtime resources to clean up.
  133. func (d *Driver) Put(id string) error {
  134. // The vfs driver has no runtime resources (e.g. mounts)
  135. // to clean up, so we don't need anything here
  136. return nil
  137. }
  138. // Exists checks to see if the directory exists for the given id.
  139. func (d *Driver) Exists(id string) bool {
  140. _, err := os.Stat(d.dir(id))
  141. return err == nil
  142. }