driver.go 4.2 KB

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