shell-network/modules/sshfs.mod

156 lines
3.7 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
}
function enable_mounts() {
DIRS=($BASH_SHELL_NETWORK_PATHS "$BASH_SHELL_HOST" "$BASH_SHELL_USER_DIR" "$BASH_SHELL_DEV")
# echo DIRS "${DIRS[@]}"
CDIRS=()
j=0
cnt=${#DIRS[@]}
for ((i = 0; i < cnt; i++)); do
# echo $i of $cnt
# looks in ssh/config subdirectory of each DIRS if not passed
DIR="${DIRS[i]}/ssh/mounts"
# echo ----- $i, ${DIRS[i]} trying $DIR
[ -d $DIR ] && CDIRS[j]=$DIR
j+=1 || echo no directory $DIR
done
# CDIRS=("${CDIRS[@]}")
# echo ${CDIRS[@]}
module_load file
for CDIR in "${CDIRS[@]}"; do
# echo $CDIR
for f in $(_find -n '*.mnt' -p 'archive off' -d 0 $CDIR); do
# echo "adding mounts file $f";
[[ $f ]] && source "$f"
done
done
[[ -f "$HOME/.ssh/mounts" ]] && source "$HOME/.ssh/mounts"
}