local_unix.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // +build linux freebsd solaris
  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
  6. import (
  7. "fmt"
  8. "net"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "syscall"
  13. "time"
  14. "github.com/pkg/errors"
  15. "github.com/docker/docker/pkg/mount"
  16. )
  17. var (
  18. oldVfsDir = filepath.Join("vfs", "dir")
  19. validOpts = map[string]bool{
  20. "type": true, // specify the filesystem type for mount, e.g. nfs
  21. "o": true, // generic mount options
  22. "device": true, // device to mount from
  23. }
  24. )
  25. type optsConfig struct {
  26. MountType string
  27. MountOpts string
  28. MountDevice string
  29. }
  30. func (o *optsConfig) String() string {
  31. return fmt.Sprintf("type='%s' device='%s' o='%s'", o.MountType, o.MountDevice, o.MountOpts)
  32. }
  33. // scopedPath verifies that the path where the volume is located
  34. // is under Docker's root and the valid local paths.
  35. func (r *Root) scopedPath(realPath string) bool {
  36. // Volumes path for Docker version >= 1.7
  37. if strings.HasPrefix(realPath, filepath.Join(r.scope, volumesPathName)) && realPath != filepath.Join(r.scope, volumesPathName) {
  38. return true
  39. }
  40. // Volumes path for Docker version < 1.7
  41. if strings.HasPrefix(realPath, filepath.Join(r.scope, oldVfsDir)) {
  42. return true
  43. }
  44. return false
  45. }
  46. func setOpts(v *localVolume, opts map[string]string) error {
  47. if len(opts) == 0 {
  48. return nil
  49. }
  50. if err := validateOpts(opts); err != nil {
  51. return err
  52. }
  53. v.opts = &optsConfig{
  54. MountType: opts["type"],
  55. MountOpts: opts["o"],
  56. MountDevice: opts["device"],
  57. }
  58. return nil
  59. }
  60. func (v *localVolume) mount() error {
  61. if v.opts.MountDevice == "" {
  62. return fmt.Errorf("missing device in volume options")
  63. }
  64. mountOpts := v.opts.MountOpts
  65. if v.opts.MountType == "nfs" {
  66. if addrValue := getAddress(v.opts.MountOpts); addrValue != "" && net.ParseIP(addrValue).To4() == nil {
  67. ipAddr, err := net.ResolveIPAddr("ip", addrValue)
  68. if err != nil {
  69. return errors.Wrapf(err, "error resolving passed in nfs address")
  70. }
  71. mountOpts = strings.Replace(mountOpts, "addr="+addrValue, "addr="+ipAddr.String(), 1)
  72. }
  73. }
  74. err := mount.Mount(v.opts.MountDevice, v.path, v.opts.MountType, mountOpts)
  75. return errors.Wrapf(err, "error while mounting volume with options: %s", v.opts)
  76. }
  77. func (v *localVolume) CreatedAt() (time.Time, error) {
  78. fileInfo, err := os.Stat(v.path)
  79. if err != nil {
  80. return time.Time{}, err
  81. }
  82. sec, nsec := fileInfo.Sys().(*syscall.Stat_t).Ctim.Unix()
  83. return time.Unix(sec, nsec), nil
  84. }