local_unix.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. "path/filepath"
  9. "strings"
  10. "github.com/docker/docker/pkg/mount"
  11. )
  12. var (
  13. oldVfsDir = filepath.Join("vfs", "dir")
  14. validOpts = map[string]bool{
  15. "type": true, // specify the filesystem type for mount, e.g. nfs
  16. "o": true, // generic mount options
  17. "device": true, // device to mount from
  18. }
  19. )
  20. type optsConfig struct {
  21. MountType string
  22. MountOpts string
  23. MountDevice string
  24. }
  25. // scopedPath verifies that the path where the volume is located
  26. // is under Docker's root and the valid local paths.
  27. func (r *Root) scopedPath(realPath string) bool {
  28. // Volumes path for Docker version >= 1.7
  29. if strings.HasPrefix(realPath, filepath.Join(r.scope, volumesPathName)) && realPath != filepath.Join(r.scope, volumesPathName) {
  30. return true
  31. }
  32. // Volumes path for Docker version < 1.7
  33. if strings.HasPrefix(realPath, filepath.Join(r.scope, oldVfsDir)) {
  34. return true
  35. }
  36. return false
  37. }
  38. func setOpts(v *localVolume, opts map[string]string) error {
  39. if len(opts) == 0 {
  40. return nil
  41. }
  42. if err := validateOpts(opts); err != nil {
  43. return err
  44. }
  45. v.opts = &optsConfig{
  46. MountType: opts["type"],
  47. MountOpts: opts["o"],
  48. MountDevice: opts["device"],
  49. }
  50. return nil
  51. }
  52. func (v *localVolume) mount() error {
  53. if v.opts.MountDevice == "" {
  54. return fmt.Errorf("missing device in volume options")
  55. }
  56. return mount.Mount(v.opts.MountDevice, v.path, v.opts.MountType, v.opts.MountOpts)
  57. }