2022-03-28 10:05:26 -07:00
|
|
|
#!/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'")"
|
|
|
|
|
2022-05-02 10:40:07 -07:00
|
|
|
# [[ ! $(host_reachable $host $(parse_option $sshopts -p)) ]] \
|
|
|
|
# && echo host $host not reachable, aborting mount && return 1
|
2022-03-28 10:05:26 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-06-21 07:46:13 -07:00
|
|
|
opts+=" -o follow_symlinks"
|
|
|
|
|
2022-03-28 10:05:26 -07:00
|
|
|
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=""
|
2024-02-21 11:33:37 -08:00
|
|
|
echo "mounted directory $dir from $(get_hostname_host $host)${MNTUSER}at $MNT"
|
2022-03-28 10:05:26 -07:00
|
|
|
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
|
|
|
|
}
|
2022-12-23 08:30:48 -08:00
|
|
|
|
|
|
|
function enable_mounts() {
|
|
|
|
|
2023-01-01 10:43:15 -08:00
|
|
|
DIRS=($BASH_SHELL_NETWORKS_LOADED "$BASH_SHELL_HOST" "$BASH_SHELL_USER_DIR" "$BASH_SHELL_DEV")
|
2022-12-23 08:30:48 -08:00
|
|
|
# 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
|
2022-12-30 09:14:39 -08:00
|
|
|
# echo "adding mounts file $f";
|
2022-12-23 08:30:48 -08:00
|
|
|
[[ $f ]] && source "$f"
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
[[ -f "$HOME/.ssh/mounts" ]] && source "$HOME/.ssh/mounts"
|
|
|
|
|
|
|
|
}
|
|
|
|
|