driver.go 4.0 KB

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