Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python
2.3 KB
Raw Blame
#!/usr/bin/env bash

set -euo pipefail

EXPORTS_FILE="${EXPORTS_FILE:-/etc/exports.d/jiuwenswarm.exports}"
STOP_SERVICE=0
DISABLE_SERVICE=0
REMOVE_BIND_FSTAB=0

while [[ $# -gt 0 ]]; do
  case "$1" in
    --exports-file)
      EXPORTS_FILE="$2"
      shift 2
      ;;
    --stop-service)
      STOP_SERVICE=1
      shift
      ;;
    --disable-service)
      DISABLE_SERVICE=1
      shift
      ;;
    --remove-bind-fstab)
      REMOVE_BIND_FSTAB=1
      shift
      ;;
    -h|--help)
      cat <<'EOF'
Usage:
  sudo bash scripts/nfs/teardown_nfs_server.sh [options]

Options:
  --exports-file <path>   Export config file to remove. Default: /etc/exports.d/jiuwenswarm.exports
  --stop-service          Stop NFS service after removing exports
  --disable-service       Disable NFS service (best effort, timeout protected)
  --remove-bind-fstab     Remove bind mount entries generated by setup_nfs_server.sh from /etc/fstab
EOF
      exit 0
      ;;
    *)
      echo "Unknown argument: $1" >&2
      exit 1
      ;;
  esac
done

if [[ "${EUID}" -ne 0 ]]; then
  echo "Please run as root: sudo bash $0" >&2
  exit 1
fi

if [[ "${REMOVE_BIND_FSTAB}" -eq 1 ]]; then
  if [[ -f /etc/fstab ]]; then
    cp /etc/fstab "/etc/fstab.bak.$(date +%Y%m%d%H%M%S)"
    /usr/bin/python3 - <<'PY'
from pathlib import Path
import re

fstab = Path("/etc/fstab")
lines = fstab.read_text().splitlines()
pattern = re.compile(r"^\S+\s+\S+\s+none\s+bind\s+0\s+0$")
filtered = [ln for ln in lines if not pattern.match(ln.strip())]
fstab.write_text("\n".join(filtered) + ("\n" if filtered else ""))
print("Removed bind entries from /etc/fstab")
PY
  fi
fi

echo "[1/3] Removing export file: ${EXPORTS_FILE}"
rm -f "${EXPORTS_FILE}"

echo "[2/3] Reloading NFS exports"
exportfs -rav

echo "[3/3] Optional service operations"
if [[ "${STOP_SERVICE}" -eq 1 ]]; then
  systemctl stop nfs-kernel-server 2>/dev/null || systemctl stop nfs-server 2>/dev/null || true
fi

if [[ "${DISABLE_SERVICE}" -eq 1 ]]; then
  timeout 10s systemctl disable nfs-kernel-server 2>/dev/null || timeout 10s systemctl disable nfs-server 2>/dev/null || true
fi

cat <<EOF

NFS server teardown completed.

Server side:
  exports removed : ${EXPORTS_FILE}
  stop service    : ${STOP_SERVICE}
  disable service : ${DISABLE_SERVICE}
  clean fstab bind: ${REMOVE_BIND_FSTAB}

EOF