driver.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // +build linux windows
  2. package vfs
  3. import (
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "github.com/docker/docker/daemon/graphdriver"
  8. "github.com/docker/docker/pkg/chrootarchive"
  9. "github.com/docker/docker/pkg/system"
  10. "github.com/opencontainers/runc/libcontainer/label"
  11. )
  12. func init() {
  13. graphdriver.Register("vfs", Init)
  14. }
  15. // Init returns a new VFS driver.
  16. // This sets the home directory for the driver and returns NaiveDiffDriver.
  17. func Init(home string, options []string) (graphdriver.Driver, error) {
  18. d := &Driver{
  19. home: home,
  20. }
  21. return graphdriver.NaiveDiffDriver(d), nil
  22. }
  23. // Driver holds information about the driver, home directory of the driver.
  24. // Driver implements graphdriver.ProtoDriver. It uses only basic vfs operations.
  25. // In order to support layering, files are copied from the parent layer into the new layer. There is no copy-on-write support.
  26. // Driver must be wrapped in NaiveDiffDriver to be used as a graphdriver.Driver
  27. type Driver struct {
  28. home string
  29. }
  30. func (d *Driver) String() string {
  31. return "vfs"
  32. }
  33. // Status is used for implementing the graphdriver.ProtoDriver interface. VFS does not currently have any status information.
  34. func (d *Driver) Status() [][2]string {
  35. return nil
  36. }
  37. // GetMetadata is used for implementing the graphdriver.ProtoDriver interface. VFS does not currently have any meta data.
  38. func (d *Driver) GetMetadata(id string) (map[string]string, error) {
  39. return nil, nil
  40. }
  41. // Cleanup is used to implement graphdriver.ProtoDriver. There is no cleanup required for this driver.
  42. func (d *Driver) Cleanup() error {
  43. return nil
  44. }
  45. // Create prepares the filesystem for the VFS driver and copies the directory for the given id under the parent.
  46. func (d *Driver) Create(id, parent string) error {
  47. dir := d.dir(id)
  48. if err := system.MkdirAll(filepath.Dir(dir), 0700); err != nil {
  49. return err
  50. }
  51. if err := os.Mkdir(dir, 0755); err != nil {
  52. return err
  53. }
  54. opts := []string{"level:s0"}
  55. if _, mountLabel, err := label.InitLabels(opts); err == nil {
  56. label.SetFileLabel(dir, mountLabel)
  57. }
  58. if parent == "" {
  59. return nil
  60. }
  61. parentDir, err := d.Get(parent, "")
  62. if err != nil {
  63. return fmt.Errorf("%s: %s", parent, err)
  64. }
  65. if err := chrootarchive.CopyWithTar(parentDir, dir); err != nil {
  66. return err
  67. }
  68. return nil
  69. }
  70. func (d *Driver) dir(id string) string {
  71. return filepath.Join(d.home, "dir", filepath.Base(id))
  72. }
  73. // Remove deletes the content from the directory for a given id.
  74. func (d *Driver) Remove(id string) error {
  75. if _, err := os.Stat(d.dir(id)); err != nil {
  76. return err
  77. }
  78. return os.RemoveAll(d.dir(id))
  79. }
  80. // Get returns the directory for the given id.
  81. func (d *Driver) Get(id, mountLabel string) (string, error) {
  82. dir := d.dir(id)
  83. if st, err := os.Stat(dir); err != nil {
  84. return "", err
  85. } else if !st.IsDir() {
  86. return "", fmt.Errorf("%s: not a directory", dir)
  87. }
  88. return dir, nil
  89. }
  90. // Put is a noop for vfs that return nil for the error, since this driver has no runtime resources to clean up.
  91. func (d *Driver) Put(id string) error {
  92. // The vfs driver has no runtime resources (e.g. mounts)
  93. // to clean up, so we don't need anything here
  94. return nil
  95. }
  96. // Exists checks to see if the directory exists for the given id.
  97. func (d *Driver) Exists(id string) bool {
  98. _, err := os.Stat(d.dir(id))
  99. return err == nil
  100. }