shell-network/modules/sshfs.mod

123 lines
2.9 KiB
Bash
Executable File

#!/bin/bash
# depends on sshfs fuse for ssh
# smount <sshfs options> host:dir mountpoint <ssh run opts> -- < more sshoptions >
# example: smount
module_load filesystem # mounted
module_load net-utils # host_reachable
module_load helpers
module_load ssh
function smount() {
local user=${DEFAULT_USER:-ubuntu}
local opts; local ropts; local sshopts
local host; local sshcmd; local cmd
local dir; local MNT; local SRC
local MNTUSER;
local OPTION; local OPTARG; local OPTIND
while getopts ':uno:' OPTION; do
# echo $OPTION $OPTARG
case "$OPTION" in
u)
MNTUSER=$OPTARG
;;
n)
MNTUSER=_NONE_
;;
o)
echo "adding sshfs option: $OPTARG"
opts+=" -o $OPTARG"
;;
*)
echo unknown sshfs option $OPTION
;;
esac
done
shift $((OPTIND - 1))
SRC=$1
MNT=$2
shift 2
if [[ ! $* =~ "--" ]]; then
ropts=$*
else
ropts=$(sed 's/\(.*\)--.*/\1/' <<< "$*")
sshopts=$(sed 's/.*--\(.*\)/\1/' <<< "$*")
fi
host=$(sed 's/\(.*\):.*/\1/' <<<"$SRC")
dir=$(sed 's/.*:\(.*\)/\1/' <<<"$SRC")
[[ ! $host ]] && { echo no remote host given, aborting; return 4; }
[[ ! $dir ]] && { echo no remote directory given, aborting; return 5; }
module_load array
declare -a ret
echo $ropts
scmd="ssh -r ${ropts} ${host}"
String::split ret "$($scmd)" ,
host=${ret[0]}; sshopts+=" ${ret[1]}"; local sshpass=${ret[2]}
# echo "$host;$sshopts;$sshpass"
# scmd="'$sshpass /usr/bin/ssh $sshopts'"
# echo ssh command: "$scmd"
# opts+=" $scmd"
# remove_end_spaces "$scmd"
# return
opts+=" -o ssh_command=$(remove_end_spaces "'$sshpass /usr/bin/ssh $sshopts'")"
[[ ! $(host_reachable $host $(parse_option $sshopts -p)) ]] \
&& echo host $host not reachable, aborting mount && return 1
if [[ $(mounted $MNT) ]]; then
echo "some remote already mounted at $MNT. Umount with: $ umount $MNT"
else
mkdir -p $MNT
if [[ ! $MNTUSER == "_NONE_" ]]; then
MNTUSER=${MNTUSER:-$USER}
id=$(id -u ${MNTUSER})
if [[ $id ]]; then
opts+=" -o uid=$id -o allow_other -o idmap=user"
else
echo no user ${MNTUSER} on this machine, aborting mount
return 1
fi
else
MNTUSER=""
fi
cmd="sshfs $host:$dir $MNT $opts"
echo $cmd
eval $cmd
if [[ $? -gt 0 ]]; then
echo "ERROR: unable to mount remote directory"
echo "CMD=> $cmd"
else
[[ ! $MNTUSER == "_NONE_" ]] && MNTUSER=" as user $MNTUSER " || MNTUSER=""
echo "mounted directory $dir from $(parse_host $host)${MNTUSER}at $MNT"
fi
fi
}
function usmount() {
if [[ $(mounted $1) ]]; then
echo "unmounting remote file system at $1"
fusermount -u $1
else
echo "nothing mounted at $1, aborting unmount"
fi
}
function mntBackup() {
smount root@$1:/backup /backup/remote
}
function umntBackup() {
usmount /backup/remote
}