local_unix.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // +build linux freebsd
  2. // Package local provides the default implementation for volumes. It
  3. // is used to mount data volume containers and directories local to
  4. // the host server.
  5. package local // import "github.com/docker/docker/volume/local"
  6. import (
  7. "fmt"
  8. "net"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "syscall"
  13. "time"
  14. "github.com/docker/docker/errdefs"
  15. "github.com/docker/docker/pkg/mount"
  16. "github.com/pkg/errors"
  17. )
  18. var (
  19. oldVfsDir = filepath.Join("vfs", "dir")
  20. validOpts = map[string]struct{}{
  21. "type": {}, // specify the filesystem type for mount, e.g. nfs
  22. "o": {}, // generic mount options
  23. "device": {}, // device to mount from
  24. }
  25. mandatoryOpts = map[string]struct{}{
  26. "device": {},
  27. "type": {},
  28. }
  29. )
  30. type optsConfig struct {
  31. MountType string
  32. MountOpts string
  33. MountDevice string
  34. }
  35. func (o *optsConfig) String() string {
  36. return fmt.Sprintf("type='%s' device='%s' o='%s'", o.MountType, o.MountDevice, o.MountOpts)
  37. }
  38. // scopedPath verifies that the path where the volume is located
  39. // is under Docker's root and the valid local paths.
  40. func (r *Root) scopedPath(realPath string) bool {
  41. // Volumes path for Docker version >= 1.7
  42. if strings.HasPrefix(realPath, filepath.Join(r.scope, volumesPathName)) && realPath != filepath.Join(r.scope, volumesPathName) {
  43. return true
  44. }
  45. // Volumes path for Docker version < 1.7
  46. if strings.HasPrefix(realPath, filepath.Join(r.scope, oldVfsDir)) {
  47. return true
  48. }
  49. return false
  50. }
  51. func setOpts(v *localVolume, opts map[string]string) error {
  52. if len(opts) == 0 {
  53. return nil
  54. }
  55. if err := validateOpts(opts); err != nil {
  56. return err
  57. }
  58. v.opts = &optsConfig{
  59. MountType: opts["type"],
  60. MountOpts: opts["o"],
  61. MountDevice: opts["device"],
  62. }
  63. return nil
  64. }
  65. func validateOpts(opts map[string]string) error {
  66. if len(opts) == 0 {
  67. return nil
  68. }
  69. for opt := range opts {
  70. if _, ok := validOpts[opt]; !ok {
  71. return errdefs.InvalidParameter(errors.Errorf("invalid option: %q", opt))
  72. }
  73. }
  74. for opt := range mandatoryOpts {
  75. if _, ok := opts[opt]; !ok {
  76. return errdefs.InvalidParameter(errors.Errorf("missing required option: %q", opt))
  77. }
  78. }
  79. return nil
  80. }
  81. func (v *localVolume) mount() error {
  82. if v.opts.MountDevice == "" {
  83. return fmt.Errorf("missing device in volume options")
  84. }
  85. mountOpts := v.opts.MountOpts
  86. switch v.opts.MountType {
  87. case "nfs", "cifs":
  88. if addrValue := getAddress(v.opts.MountOpts); addrValue != "" && net.ParseIP(addrValue).To4() == nil {
  89. ipAddr, err := net.ResolveIPAddr("ip", addrValue)
  90. if err != nil {
  91. return errors.Wrapf(err, "error resolving passed in network volume address")
  92. }
  93. mountOpts = strings.Replace(mountOpts, "addr="+addrValue, "addr="+ipAddr.String(), 1)
  94. }
  95. }
  96. err := mount.Mount(v.opts.MountDevice, v.path, v.opts.MountType, mountOpts)
  97. return errors.Wrap(err, "failed to mount local volume")
  98. }
  99. func (v *localVolume) CreatedAt() (time.Time, error) {
  100. fileInfo, err := os.Stat(v.path)
  101. if err != nil {
  102. return time.Time{}, err
  103. }
  104. sec, nsec := fileInfo.Sys().(*syscall.Stat_t).Ctim.Unix()
  105. return time.Unix(sec, nsec), nil
  106. }