mountwrapper 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/bash
  2. # $1 mount basedir relative to home,e.g. "Music"
  3. # $2 ssh host to look up, also final mountdir
  4. # => mountdir=$HOME/$1/$2
  5. # $3 remote directory to mount
  6. # the rest will be exec'd after mounting is done
  7. me="${0##*/}"
  8. #exec > /tmp/$me.log 2>&1
  9. (( $# < 4 )) && { echo "Please provide mountbase, host, remote dir and command to execute"; exit 1; }
  10. for var in mountbase host remotedir; do
  11. declare "$var"="$1"
  12. echo "$var"="$1"
  13. shift
  14. done
  15. mountdir="$HOME/$mountbase/$host" # will be expanded with name of host
  16. . ~/.config/bash/notify
  17. date
  18. text="mounted on $mountdir with"
  19. text="$(grep "$text" /proc/self/mountstats)"
  20. if [[ "$text" != "" ]]
  21. then
  22. notify "" "" "" "" "" "~${mountdir#$HOME} already mounted"
  23. else
  24. # dependency checks
  25. for dep in sshfs ssh fusermount; do
  26. command -V $dep >/dev/null || exit 1
  27. done
  28. hostname=''
  29. port=''
  30. user=''
  31. identityfile=''
  32. serveraliveinterval=''
  33. identitiesonly=''
  34. compression=''
  35. while read option value; do
  36. option="${option,,}"
  37. case "$option" in
  38. hostname|port|user|serveraliveinterval|identitiesonly|identityfile|compression)
  39. declare "$option"="$value"
  40. echo "$option"="$value"
  41. ;;
  42. esac
  43. done <<<"$(ssh -G "$host")"
  44. sshfs_options="follow_symlinks,ServerAliveInterval=$serveraliveinterval,reconnect,port=$port,IdentitiesOnly=$identitiesonly,IdentityFile=$identityfile,Compression=$compression"
  45. mkdir -p "$mountdir" || exit 1
  46. location="${user}@$hostname:$remotedir"
  47. if sshfs -C -o "$sshfs_options" "$location" "$mountdir"
  48. then
  49. notify "" "" "" "" "" "Mounted $location on ~${mountdir#$HOME}"
  50. else
  51. notify "" "icon-lock-warning" "" "" "" "Failed: mounting $location"
  52. fusermount -u -z "$mountdir"
  53. fi
  54. fi
  55. "$@"
  56. grep "mounted on $mountdir with" /proc/self/mountstats && fusermount -u "$mountdir"
  57. sleep 10
  58. grep "mounted on $mountdir with" /proc/self/mountstats && fusermount -u -z "$mountdir"
  59. exit 0