driver.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package vfs
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/docker/docker/daemon/graphdriver"
  7. "github.com/docker/docker/pkg/chrootarchive"
  8. "github.com/docker/docker/pkg/idtools"
  9. "github.com/opencontainers/runc/libcontainer/label"
  10. )
  11. var (
  12. // CopyWithTar defines the copy method to use.
  13. CopyWithTar = chrootarchive.CopyWithTar
  14. )
  15. func init() {
  16. graphdriver.Register("vfs", Init)
  17. }
  18. // Init returns a new VFS driver.
  19. // This sets the home directory for the driver and returns NaiveDiffDriver.
  20. func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
  21. d := &Driver{
  22. home: home,
  23. uidMaps: uidMaps,
  24. gidMaps: gidMaps,
  25. }
  26. rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
  27. if err != nil {
  28. return nil, err
  29. }
  30. if err := idtools.MkdirAllAs(home, 0700, rootUID, rootGID); err != nil {
  31. return nil, err
  32. }
  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. home string
  41. uidMaps []idtools.IDMap
  42. gidMaps []idtools.IDMap
  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, mountLabel string, storageOpt map[string]string) error {
  62. return d.Create(id, parent, mountLabel, storageOpt)
  63. }
  64. // Create prepares the filesystem for the VFS driver and copies the directory for the given id under the parent.
  65. func (d *Driver) Create(id, parent, mountLabel string, storageOpt map[string]string) error {
  66. if len(storageOpt) != 0 {
  67. return fmt.Errorf("--storage-opt is not supported for vfs")
  68. }
  69. dir := d.dir(id)
  70. rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
  71. if err != nil {
  72. return err
  73. }
  74. if err := idtools.MkdirAllAs(filepath.Dir(dir), 0700, rootUID, rootGID); err != nil {
  75. return err
  76. }
  77. if err := idtools.MkdirAs(dir, 0755, rootUID, rootGID); err != nil {
  78. return err
  79. }
  80. opts := []string{"level:s0"}
  81. if _, mountLabel, err := label.InitLabels(opts); err == nil {
  82. label.SetFileLabel(dir, mountLabel)
  83. }
  84. if parent == "" {
  85. return nil
  86. }
  87. parentDir, err := d.Get(parent, "")
  88. if err != nil {
  89. return fmt.Errorf("%s: %s", parent, err)
  90. }
  91. if err := CopyWithTar(parentDir, dir); err != nil {
  92. return err
  93. }
  94. return nil
  95. }
  96. func (d *Driver) dir(id string) string {
  97. return filepath.Join(d.home, "dir", filepath.Base(id))
  98. }
  99. // Remove deletes the content from the directory for a given id.
  100. func (d *Driver) Remove(id string) error {
  101. if err := os.RemoveAll(d.dir(id)); err != nil && !os.IsNotExist(err) {
  102. return err
  103. }
  104. return nil
  105. }
  106. // Get returns the directory for the given id.
  107. func (d *Driver) Get(id, mountLabel string) (string, error) {
  108. dir := d.dir(id)
  109. if st, err := os.Stat(dir); err != nil {
  110. return "", err
  111. } else if !st.IsDir() {
  112. return "", fmt.Errorf("%s: not a directory", dir)
  113. }
  114. return dir, nil
  115. }
  116. // Put is a noop for vfs that return nil for the error, since this driver has no runtime resources to clean up.
  117. func (d *Driver) Put(id string) error {
  118. // The vfs driver has no runtime resources (e.g. mounts)
  119. // to clean up, so we don't need anything here
  120. return nil
  121. }
  122. // Exists checks to see if the directory exists for the given id.
  123. func (d *Driver) Exists(id string) bool {
  124. _, err := os.Stat(d.dir(id))
  125. return err == nil
  126. }