driver.go 4.1 KB

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