64 lines
1.8 KiB
Bash
Executable File
64 lines
1.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
set -e pipefail
|
||
|
||
MAP_FILE="/etc/jimlab/sshfs.map"
|
||
SSHFS_BIN="/usr/bin/sshfs"
|
||
|
||
# Default options & user; override via env if you like.
|
||
SSHFS_OPTS="${SSHFS_OPTS:-delay_connect,reconnect,follow_symlinks,transform_symlinks}"
|
||
|
||
is_sshfs_mount() {
|
||
# true iff $1 is a real mountpoint and fstype looks like sshfs
|
||
local d="$1"
|
||
if ! mountpoint -q -- "$d"; then
|
||
return 1
|
||
fi
|
||
local fstype
|
||
fstype="$(findmnt -rn -o FSTYPE -T "$d" || true)"
|
||
[[ "$fstype" == "fuse.sshfs" || "$fstype" == sshfs ]]
|
||
}
|
||
|
||
mount_one() {
|
||
local remote="$1" # e.g., shrek:/path or jim@shrek:/path
|
||
local local_dir="$2"
|
||
|
||
# Ensure local mountpoint exists
|
||
mkdir -p -- "$local_dir"
|
||
|
||
# Skip if something already mounted there
|
||
if is_sshfs_mount "$local_dir"; then
|
||
echo "Already mounted: $local_dir (skipping)"
|
||
return 0
|
||
fi
|
||
|
||
echo "Mounting $remote -> $local_dir"
|
||
# Don’t fail the whole script on a single mount error
|
||
if ! "$SSHFS_BIN" -o "$SSHFS_OPTS" "$remote" "$local_dir"; then
|
||
echo "WARN: Failed to mount $remote to $local_dir" >&2
|
||
return 0
|
||
fi
|
||
}
|
||
|
||
if [[ -f "$MAP_FILE" ]]; then
|
||
# Read map file: "<host[:port]>:/remote-path /local-path"
|
||
while IFS= read -r line; do
|
||
# Trim leading/trailing whitespace
|
||
line="${line#"${line%%[![:space:]]*}"}"
|
||
line="${line%"${line##*[![:space:]]}"}"
|
||
# Skip blanks & comments
|
||
[[ -z "$line" || "${line:0:1}" == "#" ]] && continue
|
||
|
||
# Expect two fields: remote local
|
||
# (Paths with spaces aren’t supported in this simple format)
|
||
read -r remote local_dir <<<"$line"
|
||
if [[ -z "${remote:-}" || -z "${local_dir:-}" ]]; then
|
||
echo "WARN: Bad line in $MAP_FILE: $line" >&2
|
||
continue
|
||
fi
|
||
|
||
mount_one "$remote" "$local_dir"
|
||
done < "$MAP_FILE"
|
||
else
|
||
echo "Map file $MAP_FILE does not exist."
|
||
fi
|