Mount SFTP folders in network storage, as CoreDesktop does

configure storage now finds machines that announce SFTP as well as SMB
servers, and takes sftp://[user@]host[:port][/path] addresses. Folders
are mounted with SSHFS on macFUSE, as you, with your SSH keys
(Bitwarden's SSH agent while it runs one), and never ask for a
password. storage-mount mounts them again at login and when the
network changes, as it does SMB shares; each line of the list now
records its mount point.

macFUSE and SSHFS are offered for download, checked against their
Team ID (3T5GSNBU6W, read from the packages' signatures). On macOS 26
and later macFUSE mounts through FSKit, whose extension you turn on
once in System Settings. Earlier macOS needs its kernel extension,
which this uses if it's running but doesn't set up.

smoketest: 85-sftp installs both and mounts a folder from the VM
itself over SSH, if macFUSE's extension can be turned on there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 08:55:55 -07:00
co-authored by Claude Opus 5.5
parent 08e6b2ee4f
commit 8e2a886215
7 changed files with 436 additions and 60 deletions
+139 -34
View File
@@ -6,14 +6,19 @@ TITLE="Network storage"
# shellcheck disable=SC2034
DESCRIPTION="Mount shared folders from your network"
# What's mounted, one smb://user@host/share per line. It exists once you've
# run this, even if you added nothing, so this isn't "to do" without a server.
# What's mounted, one "<url>|<mount point>" per line (see
# system/storage-mount). It exists once you've run this, even if you added
# nothing, so this isn't "to do" without a server.
STORAGE_CONF="$CONF_DIR/storage"
STORAGE_MOUNT="$CONNECTOR/system/storage-mount"
LABEL="computer.federated.connector.storage"
PLIST="$HOME/Library/LaunchAgents/$LABEL.plist"
LOGIN_ITEMS_PANE="x-apple.systempreferences:com.apple.LoginItems-Settings.extension"
source "$CONNECTOR/configure.d/lib/json.sh"
source "$CONNECTOR/configure.d/lib/apps.sh"
source "$CONNECTOR/configure.d/lib/mdns.sh"
source "$CONNECTOR/configure.d/lib/smb.sh"
source "$CONNECTOR/configure.d/lib/sftp.sh"
is_configured() {
[ -f "$STORAGE_CONF" ]
@@ -96,95 +101,195 @@ add_share() {
fi
}
# record <url> <mount point>: add it to the list, and the job that mounts
# it again at login
record() {
local url="$1" point="$2" line
while IFS= read -r line; do
[ "$(share_key "$line")" = "$(share_key "$url")" ] && { echo "$url was already added."; return 0; }
while IFS='|' read -r line _; do
case "$url" in
smb://*) [ "$(share_key "$line")" = "$(share_key "$url")" ] ;;
*) [ "$line" = "$url" ] ;;
esac && { echo "$url was already added."; return 0; }
done < "$STORAGE_CONF"
echo "$url" >> "$STORAGE_CONF"
echo "$url|$point" >> "$STORAGE_CONF"
[ -f "$PLIST" ] || install_job
echo "$url is at $point (in Finder's sidebar under Locations). It's mounted again at login."
}
# Unmount it and take it out of the list
remove_share() {
local url="$1" point tmp
if point="$(mounted_at "$url")"; then
diskutil unmount "$point" > /dev/null 2>&1 || umount "$point" 2> /dev/null \
|| echo "Couldn't unmount $point; eject it in Finder."
fi
tmp="$(mktemp)" || return 1
grep -vxF "$url" "$STORAGE_CONF" > "$tmp"
mv -f "$tmp" "$STORAGE_CONF"
[ -s "$STORAGE_CONF" ] || remove_job
echo "$url won't be mounted again. Its password stays in your keychain (Keychain Access)."
# A name usable in a path: "Pi-Star Files" -> pi-star-files
slug() {
local s
s="$(tr '[:upper:]' '[:lower:]' <<< "$1" | sed -E 's/[^a-z0-9._-]+/-/g; s/^-+//; s/-+$//')"
echo "${s:-folder}"
}
# The SMB servers on the network, other than this Mac: "host|name" per line
# <host>[-<folder>], with -2, -3... if that's taken
sftp_name() {
local base n=2 name
base="$(slug "${1%.local}")"
[ -n "$2" ] && base="$base-$(slug "$(basename "$2")")"
name="$base"
while [ -e "/Volumes/$name" ] || [ -e "$HOME/Volumes/$name" ] || grep -qsE "/$name\$" "$STORAGE_CONF"; do
name="$base-$n"
n=$((n + 1))
done
echo "$name"
}
# Mount sftp://user@host[:port][/path] and record it. Installs macFUSE and
# SSHFS first if they're missing, and has you turn on macFUSE's file system
# extension.
add_sftp() {
local url="$1" backend point
if ! backend="$(fuse_backend)"; then
echo "Folders over SSH need macOS 26 or later. On earlier versions of macOS, macFUSE"
echo "needs its kernel extension, which on Apple silicon means starting up in Recovery"
echo "and lowering security. This doesn't do that, but once macFUSE's kernel extension"
echo "is running, it uses it."
return 1
fi
app_ensure macfuse "It lets macOS mount folders from other machines over SSH." || return 1
app_ensure sshfs "It mounts folders over SSH (SFTP) with macFUSE." || return 1
if [ "$backend" = fskit ] && ! fskit_ready; then
echo "Turn on macFUSE's file system extension: System Settings opens at Login Items &"
echo "Extensions. Under Extensions, open File System Extensions and turn on macFUSE."
open "$LOGIN_ITEMS_PANE"
wait_until 300 fskit_ready || { echo "macFUSE's extension is still off."; return 1; }
fi
sftp_parse "$url"
echo "Checking that $SFTP_HOST accepts one of your SSH keys..."
if ! sftp_key_works "$url"; then
echo "$SFTP_HOST didn't accept any of your SSH keys, and the mount can't ask for a password."
echo "Add your key to $SFTP_HOST first (e.g. ssh-copy-id $SFTP_USER@$SFTP_HOST), then try again."
return 1
fi
point="$(sftp_mount_point "$(sftp_name "$SFTP_HOST" "$SFTP_PATH")")" || return 1
if ! sftp_mount "$url" "$point"; then
echo "SSHFS couldn't mount $url."
return 1
fi
record "$url" "$point"
}
# Ask who to sign in as and which folder, as CoreDesktop does
ask_sftp() {
local host="$1" user="${2:-$USER}" path="$3" port=""
[[ "$host" == *:* ]] && { port=":${host##*:}"; host="${host%%:*}"; }
ask user "User name on $host" "$user"
[ -n "$path" ] || read -rp "Folder on $host (Enter for your home folder): " path
path="${path#/}"
add_sftp "sftp://$user@$host$port${path:+/$path}"
}
# Unmount it and take it out of the list
remove_share() {
local url="$1" point="$2" tmp
case "$url" in
smb://*)
if point="$(mounted_at "$url")"; then
diskutil unmount "$point" > /dev/null 2>&1 || umount "$point" 2> /dev/null \
|| echo "Couldn't unmount $point; eject it in Finder."
fi ;;
sftp://*)
sftp_unmount "$point" || echo "Couldn't unmount $point; eject it in Finder." ;;
esac
tmp="$(mktemp)" || return 1
awk -F'|' -v url="$url" '$1 != url' "$STORAGE_CONF" > "$tmp"
mv -f "$tmp" "$STORAGE_CONF"
[ -s "$STORAGE_CONF" ] || remove_job
case "$url" in
smb://*) echo "$url won't be mounted again. Its password stays in your keychain (Keychain Access)." ;;
*) echo "$url won't be mounted again." ;;
esac
}
# The file servers on the network, other than this Mac: "kind|host|name" per
# line. Macs with Remote Login on announce SFTP; Linux machines often do too.
find_servers() {
local own name host
local own name type host
own="$(mdns_own_host)"
mdns_services _smb._tcp | while IFS='|' read -r name _ host _; do
mdns_services _smb._tcp _sftp-ssh._tcp | while IFS='|' read -r name type host _; do
[ "$(tr '[:upper:]' '[:lower:]' <<< "$host")" = "$(tr '[:upper:]' '[:lower:]' <<< "$own")" ] && continue
echo "$host|$name"
case "$type" in
_smb._tcp) echo "smb|$host|$name" ;;
_sftp-ssh._tcp) echo "sftp|$host|$name" ;;
esac
done | sort -u
}
find_and_add() {
local found=() line host name choice i
local found=() line kind host name choice i
echo "Looking for shared folders on your network..."
while IFS= read -r line; do
found+=("$line")
done < <(find_servers)
if [ ${#found[@]} -eq 0 ]; then
echo "Nothing found. Windows PCs, NAS boxes and Macs with File Sharing on share folders"
echo "over SMB, but some don't announce it. Enter the address instead."
echo "over SMB, and machines you can ssh to over SFTP, but some don't announce it."
echo "Enter the address instead."
return 0
fi
for i in "${!found[@]}"; do
IFS='|' read -r host name <<< "${found[$i]}"
printf ' %2d) %-26s %s\n' $((i + 1)) "$host" "$name"
IFS='|' read -r kind host name <<< "${found[$i]}"
printf ' %2d) %-26s %-22s %s\n' $((i + 1)) "$host" \
"$([ "$kind" = smb ] && echo "SMB shared folders" || echo "SFTP (over SSH)")" "$name"
done
read -rp "Which? " choice
if ! [[ "$choice" =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt ${#found[@]} ]; then
echo "Choose a number from the list."
return 1
fi
add_share "smb://${found[$((choice - 1))]%%|*}"
IFS='|' read -r kind host _ <<< "${found[$((choice - 1))]}"
case "$kind" in
smb) add_share "smb://$host" ;;
sftp) ask_sftp "$host" ;;
esac
}
# smb://host[/share] or sftp://[user@]host[:port][/path]
enter_and_add() {
local address
read -rp "Address, e.g. smb://nas.local/files (or smb://nas.local to choose a share): " address
local address rest user="" host path=""
read -rp "Address, e.g. smb://nas.local/files or sftp://me@server.local/srv/data: " address
case "$address" in
"") return 0 ;;
smb://?*) add_share "$address" ;;
*) echo "Start it with smb://. (Folders over SSH need macFUSE, which this doesn't set up.)"
sftp://?*)
rest="${address#sftp://}"
[[ "$rest" == *@* ]] && { user="${rest%%@*}"; rest="${rest#*@}"; }
host="${rest%%/*}"
[[ "$rest" == */?* ]] && path="/${rest#*/}"
ask_sftp "$host" "$user" "$path" ;;
*) echo "Start it with smb:// or sftp://."
return 1 ;;
esac
}
choose_and_remove() {
local entries=() line choice i
local entries=() line url point choice i
while IFS= read -r line; do
[ -n "$line" ] && entries+=("$line")
done < "$STORAGE_CONF"
for i in "${!entries[@]}"; do
printf ' %2d) %s\n' $((i + 1)) "${entries[$i]}"
IFS='|' read -r url point <<< "${entries[$i]}"
printf ' %2d) %s\n' $((i + 1)) "$url"
done
read -rp "Which? " choice
if ! [[ "$choice" =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt ${#entries[@]} ]; then
echo "Choose a number from the list."
return 1
fi
remove_share "${entries[$((choice - 1))]}"
IFS='|' read -r url point <<< "${entries[$((choice - 1))]}"
remove_share "$url" "$point"
}
run_configure() {
local choice url point
mkdir -p "$(dirname "$STORAGE_CONF")" && touch "$STORAGE_CONF" || return 1
echo "This mounts shared folders from other machines on your network (Windows PCs, NAS"
echo "boxes, Macs with File Sharing on) and mounts them again each time you log in."
echo "This mounts shared folders from other machines on your network and mounts them"
echo "again each time you log in: SMB shares (Windows PCs, NAS boxes, Macs with File"
echo "Sharing on) and, over SSH, any machine you can ssh to."
while true; do
echo