local_unix.go 2.2 KB

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