Files
connector-macos/configure.d/60-storage.sh
T
davidandClaude Opus 5.5 f92dbcb3bd Add an installer and update command
curl -fsSL https://federated.sh/mac | bash

install.sh checks the Mac (macOS 13+, not root, internet; warns if the
account isn't an administrator), then downloads the latest commit as an
archive from Gitea, as macOS has no git without the Command Line Tools.
It installs into ~/.local/share/federated-connector/connector, links
configure and update into ~/.local/bin and puts that on PATH at the top
of ~/.zprofile and ~/.bash_profile, then offers to run configure. Piped
into bash, it runs only once read in full, and asks from /dev/tty.

The folder never moves, so the backup and storage jobs and the Federated
Backup app (with its Full Disk Access) keep pointing at it. update shows
the commits since the installed one and swaps the new files in with two
renames.

If configure ran from another folder before, the installer says what
still runs from there; backup and storage now show as to do until
they're set up again from the installed copy.

smoketest: 05-install pipes install.sh into bash with the working copy,
runs update, then installs the published version from Gitea.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 09:21:17 -07:00

325 lines
11 KiB
Bash

#!/bin/bash
# configure reads TITLE and DESCRIPTION
# shellcheck disable=SC2034
TITLE="Network storage"
# shellcheck disable=SC2034
DESCRIPTION="Mount shared folders from your network"
# 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"
# The job runs this copy's storage-mount, not one in a folder configure ran
# from before it was installed
job_current() {
[ ! -f "$PLIST" ] || grep -qF "<string>$(xml_escape "$STORAGE_MOUNT")</string>" "$PLIST"
}
is_configured() {
[ -f "$STORAGE_CONF" ] && job_current
}
# launchd runs system/storage-mount at login and whenever the network changes
# (macOS rewrites SystemConfiguration then), so shares come back on their own
install_job() {
mkdir -p "$(dirname "$PLIST")"
cat > "$PLIST" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key><string>$LABEL</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>$(xml_escape "$STORAGE_MOUNT")</string>
</array>
<key>RunAtLoad</key><true/>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/SystemConfiguration</string>
</array>
<key>ThrottleInterval</key><integer>30</integer>
<key>ProcessType</key><string>Background</string>
</dict>
</plist>
EOF
launchctl bootout "gui/$(id -u)/$LABEL" 2> /dev/null
launchctl bootstrap "gui/$(id -u)" "$PLIST"
}
remove_job() {
launchctl bootout "gui/$(id -u)/$LABEL" 2> /dev/null
rm -f "$PLIST"
}
xml_escape() {
local s="$1"
s="${s//&/\&amp;}"
s="${s//</\&lt;}"
s="${s//>/\&gt;}"
printf '%s' "$s"
}
# Mount smb://[user@]host[/share] and record what was mounted. Without a
# share, macOS lists the server's shares to choose from. Its sign-in window
# can keep the password in your keychain, which mounting again later needs.
add_share() {
local url="$1" host before after line new=0 user share entry
host="$(host_of "$url")"
echo "macOS asks you to sign in to $host. Tick \"Remember this password in my"
echo "keychain\", so the share can be mounted again at login."
before="$(smb_mounts)"
if ! mount_share "$url"; then
if mounted_at "$url" > /dev/null; then
echo "$url is already mounted."
else
echo "$host's share wasn't mounted (wrong password, or cancelled)."
return 1
fi
fi
after="$(smb_mounts)"
while IFS= read -r line; do
[ -n "$line" ] || continue
grep -qxF "$line" <<< "$before" && continue
# //user@host/share -> smb://user@<the host you gave>/share
entry="${line%%|*}"
entry="${entry#//}"
user=""
[[ "$entry" == *@* ]] && user="${entry%%@*}@"
share="${entry#*/}"
record "smb://$user$host/$share" "${line#*|}"
new=$((new + 1))
done <<< "$after"
if [ $new -eq 0 ] && [[ "${url#smb://}" == */?* ]] && mounted_at "$url" > /dev/null; then
record "$url" "$(mounted_at "$url")"
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
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|$point" >> "$STORAGE_CONF"
[ -f "$PLIST" ] || install_job
echo "$url is at $point (in Finder's sidebar under Locations). It's mounted again at login."
}
# 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}"
}
# <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 type host
own="$(mdns_own_host)"
mdns_services _smb._tcp _sftp-ssh._tcp | while IFS='|' read -r name type host _; do
[ "$(tr '[:upper:]' '[:lower:]' <<< "$host")" = "$(tr '[:upper:]' '[:lower:]' <<< "$own")" ] && continue
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 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, 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 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
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 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" ;;
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 url point choice i
while IFS= read -r line; do
[ -n "$line" ] && entries+=("$line")
done < "$STORAGE_CONF"
for i in "${!entries[@]}"; do
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
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
if ! job_current; then
install_job && echo "Shared folders are now mounted again at login by this copy ($CONNECTOR)."
fi
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
if [ -s "$STORAGE_CONF" ]; then
while IFS='|' read -r url point; do
echo " $url ${point:-(not mounted now)}"
done < <(bash "$STORAGE_MOUNT" --list)
echo
fi
echo " 1) Find shared folders on your network"
echo " 2) Enter an address"
[ -s "$STORAGE_CONF" ] && echo " 3) Remove one"
echo " q) Done"
echo
read -rp "Choose: " choice || return 0
case "$choice" in
1) find_and_add ;;
2) enter_and_add ;;
3) [ -s "$STORAGE_CONF" ] && choose_and_remove ;;
*) return 0 ;;
esac
done
}