driver.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.NewArchiver(nil).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. idMappings: idtools.NewIDMappingsFromMaps(uidMaps, gidMaps),
  25. }
  26. rootIDs, err := d.idMappings.RootPair()
  27. if err != nil {
  28. return nil, err
  29. }
  30. if err := idtools.MkdirAllAndChown(home, 0700, rootIDs); 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. idMappings *idtools.IDMappings
  42. }
  43. func (d *Driver) String() string {
  44. return "vfs"
  45. }
  46. // Status is used for implementing the graphdriver.ProtoDriver interface. VFS does not currently have any status information.
  47. func (d *Driver) Status() [][2]string {
  48. return nil
  49. }
  50. // GetMetadata is used for implementing the graphdriver.ProtoDriver interface. VFS does not currently have any meta data.
  51. func (d *Driver) GetMetadata(id string) (map[string]string, error) {
  52. return nil, nil
  53. }
  54. // Cleanup is used to implement graphdriver.ProtoDriver. There is no cleanup required for this driver.
  55. func (d *Driver) Cleanup() error {
  56. return nil
  57. }
  58. // CreateReadWrite creates a layer that is writable for use as a container
  59. // file system.
  60. func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
  61. return d.Create(id, parent, opts)
  62. }
  63. // Create prepares the filesystem for the VFS driver and copies the directory for the given id under the parent.
  64. func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
  65. if opts != nil && len(opts.StorageOpt) != 0 {
  66. return fmt.Errorf("--storage-opt is not supported for vfs")
  67. }
  68. dir := d.dir(id)
  69. rootIDs, err := d.idMappings.RootPair()
  70. if err != nil {
  71. return err
  72. }
  73. if err := idtools.MkdirAllAndChown(filepath.Dir(dir), 0700, rootIDs); err != nil {
  74. return err
  75. }
  76. if err := idtools.MkdirAndChown(dir, 0755, rootIDs); err != nil {
  77. return err
  78. }
  79. labelOpts := []string{"level:s0"}
  80. if _, mountLabel, err := label.InitLabels(labelOpts); err == nil {
  81. label.SetFileLabel(dir, mountLabel)
  82. }
  83. if parent == "" {
  84. return nil
  85. }
  86. parentDir, err := d.Get(parent, "")
  87. if err != nil {
  88. return fmt.Errorf("%s: %s", parent, err)
  89. }
  90. return CopyWithTar(parentDir, dir)
  91. }
  92. func (d *Driver) dir(id string) string {
  93. return filepath.Join(d.home, "dir", filepath.Base(id))
  94. }
  95. // Remove deletes the content from the directory for a given id.
  96. func (d *Driver) Remove(id string) error {
  97. if err := system.EnsureRemoveAll(d.dir(id)); err != nil {
  98. return err
  99. }
  100. return nil
  101. }
  102. // Get returns the directory for the given id.
  103. func (d *Driver) Get(id, mountLabel string) (string, error) {
  104. dir := d.dir(id)
  105. if st, err := os.Stat(dir); err != nil {
  106. return "", err
  107. } else if !st.IsDir() {
  108. return "", fmt.Errorf("%s: not a directory", dir)
  109. }
  110. return dir, nil
  111. }
  112. // Put is a noop for vfs that return nil for the error, since this driver has no runtime resources to clean up.
  113. func (d *Driver) Put(id string) error {
  114. // The vfs driver has no runtime resources (e.g. mounts)
  115. // to clean up, so we don't need anything here
  116. return nil
  117. }
  118. // Exists checks to see if the directory exists for the given id.
  119. func (d *Driver) Exists(id string) bool {
  120. _, err := os.Stat(d.dir(id))
  121. return err == nil
  122. }