mount-copybind 649 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/bin/sh
  2. #
  3. # Perform a bind mount, copying existing files as we do so to ensure the
  4. # overlaid path has the necessary content.
  5. if [ $# -lt 2 ]; then
  6. echo >&2 "Usage: $0 spec mountpoint [OPTIONS]"
  7. exit 1
  8. fi
  9. spec=$1
  10. mountpoint=$2
  11. if [ $# -gt 2 ]; then
  12. options=$3
  13. else
  14. options=
  15. fi
  16. [ -n "$options" ] && options=",$options"
  17. mkdir -p "${spec%/*}"
  18. if [ -d "$mountpoint" ]; then
  19. if [ ! -d "$spec" ]; then
  20. mkdir "$spec"
  21. cp -pPR "$mountpoint"/. "$spec/"
  22. fi
  23. elif [ -f "$mountpoint" ]; then
  24. if [ ! -f "$spec" ]; then
  25. cp -pP "$mountpoint" "$spec"
  26. fi
  27. fi
  28. mount -o "bind$options" "$spec" "$mountpoint"