Files
Linux_post_install/bin/pos-share-nfs-server
T
Your Name 3536f267c7 refactor: move usb/nfs tools into new 'share' category
- rename bin/pos-usb-server -> pos-share-usb-server, pos-system-nfs-{client,server} -> pos-share-nfs-{client,server}
- update # POS: headers, usage strings, INTERACTIVE_CMDS, usage() EXAMPLES, notify-scope comment
- docs: new DOC/howto/share.md (USB+NFS consolidated), drop usb.md + system.md NFS sections,
  POS.md ### share section, HOWTO/README/AGENT_Context/README/DEV/AGENTS updates
- make gen && make check green
2026-08-11 14:52:18 -04:00

135 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# POS: share nfs-server — Manage the NFS kernel server (status, share/unshare exports, enable/disable)
source "$(dirname "$0")/../lib/common.sh" 2>/dev/null || source "$(dirname "$0")/common.sh"
source "$(dirname "$0")/../lib/notify.sh" 2>/dev/null || source "$(dirname "$0")/notify.sh"
command -v exportfs &>/dev/null || err "exportfs not found (install nfs-kernel-server)"
usage() {
cat <<EOF
Usage: pos share nfs-server <command> [args]
Manage the NFS kernel server (nfs-kernel-server, /etc/exports).
Commands:
status Show server status + current exports
share <path> [client] Add or update an export and reload
(default client: *(rw,sync,no_subtree_check))
unshare <path> Remove an export and reload
list Show current exports (exportfs -v)
reload Re-apply /etc/exports after hand edits
enable Start nfs-server and enable it on boot
disable Stop nfs-server and disable it on boot
Clients are /etc/exports entries — restrict them to your trusted network:
pos share nfs-server share /mnt/hdd '100.64.0.0/10(rw,sync,no_subtree_check)' # Tailscale CGNAT
pos share nfs-server share /mnt/hdd '10.10.0.0/24(rw,sync,no_subtree_check)' # WireGuard
pos share nfs-server share /mnt/backups '192.168.1.0/24(ro,sync,no_subtree_check)'
Examples:
pos share nfs-server status
pos share nfs-server share /mnt/hdd
pos share nfs-server list
pos share nfs-server unshare /mnt/hdd
EOF
exit 0
}
cmd="${1:-}"
case "$cmd" in
-h|--help) usage ;;
status|share|unshare|list|reload|enable|disable) ;;
"") err "Missing command (status|share|unshare|list|reload|enable|disable)" ;;
*) err "Unknown command '$cmd' (see --help)" ;;
esac
EXPORTS_FILE=/etc/exports
require_root_dir() {
local path="$1"
case "$path" in
/*) ;;
*) err "Path must be absolute: $path" ;;
esac
[ -d "$path" ] || err "Path not found: $path"
}
case "$cmd" in
status)
if systemctl is-active --quiet nfs-server 2>/dev/null; then
ok "nfs-server: running"
else
warn "nfs-server: not running (enable with 'pos share nfs-server enable')"
fi
echo
section "Exports"
exportfs -v 2>/dev/null || echo " (none)"
;;
share)
path="${2:-}"
client="${3:-*(rw,sync,no_subtree_check)}"
[ -n "$path" ] || err "Usage: pos share nfs-server share <path> [client]"
require_root_dir "$path"
if [ "$client" = "*(rw,sync,no_subtree_check)" ]; then
warn "Generic export '$client' — ANY client can mount $path. Restrict it, e.g.:"
echo " pos share nfs-server share $path '100.64.0.0/10(rw,sync,no_subtree_check)'"
echo " pos share nfs-server share $path '10.10.0.0/24(rw,sync,no_subtree_check)'"
echo
fi
tmp="$(mktemp)"
awk -v p="$path" '$1 != p' "$EXPORTS_FILE" > "$tmp"
echo "$path $client" >> "$tmp"
sudo cp "$tmp" "$EXPORTS_FILE"
rm -f "$tmp"
sudo exportfs -ra
log "Exported: $path $client"
notify_send "NFS share added: $path $client"
;;
unshare)
path="${2:-}"
[ -n "$path" ] || err "Usage: pos share nfs-server unshare <path>"
require_root_dir "$path"
if ! awk -v p="$path" '$1 == p {found=1} END {exit !found}' "$EXPORTS_FILE"; then
warn "No export for $path in $EXPORTS_FILE"
exit 0
fi
tmp="$(mktemp)"
awk -v p="$path" '$1 != p' "$EXPORTS_FILE" > "$tmp"
sudo cp "$tmp" "$EXPORTS_FILE"
rm -f "$tmp"
sudo exportfs -ra
log "Removed export: $path"
notify_send "NFS share removed: $path"
;;
list)
exportfs -v 2>/dev/null || echo "No exports"
;;
reload)
sudo exportfs -ra
log "NFS exports reloaded"
;;
enable)
sudo systemctl enable --now nfs-server
log "nfs-server enabled (starts on boot)"
notify_send "NFS server enabled"
;;
disable)
sudo systemctl disable --now nfs-server
log "nfs-server disabled (will not start on boot)"
notify_send "NFS server disabled"
;;
esac