validate.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package container
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/docker/swarmkit/api"
  7. )
  8. func validateMounts(mounts []api.Mount) error {
  9. for _, mount := range mounts {
  10. // Target must always be absolute
  11. if !filepath.IsAbs(mount.Target) {
  12. return fmt.Errorf("invalid mount target, must be an absolute path: %s", mount.Target)
  13. }
  14. switch mount.Type {
  15. // The checks on abs paths are required due to the container API confusing
  16. // volume mounts as bind mounts when the source is absolute (and vice-versa)
  17. // See #25253
  18. // TODO: This is probably not necessary once #22373 is merged
  19. case api.MountTypeBind:
  20. if !filepath.IsAbs(mount.Source) {
  21. return fmt.Errorf("invalid bind mount source, must be an absolute path: %s", mount.Source)
  22. }
  23. if _, err := os.Stat(mount.Source); os.IsNotExist(err) {
  24. return fmt.Errorf("invalid bind mount source, source path not found: %s", mount.Source)
  25. }
  26. case api.MountTypeVolume:
  27. if filepath.IsAbs(mount.Source) {
  28. return fmt.Errorf("invalid volume mount source, must not be an absolute path: %s", mount.Source)
  29. }
  30. case api.MountTypeTmpfs:
  31. if mount.Source != "" {
  32. return fmt.Errorf("invalid tmpfs source, source must be empty")
  33. }
  34. default:
  35. return fmt.Errorf("invalid mount type: %s", mount.Type)
  36. }
  37. }
  38. return nil
  39. }