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

fix(team): isolate distributed shared workspace

yechenzhenca40a08e6ae4
diff --git a/docs/en/DistributedTeam.md b/docs/en/DistributedTeam.md
index 703ed5ce9b..87cf0ac1b7 100644
--- a/docs/en/DistributedTeam.md
+++ b/docs/en/DistributedTeam.md
@@ -184,12 +184,26 @@ Both sides must agree on:
- Teammate advertises its own bootstrap endpoint; leader does not need teammate addresses
- `team.storage.params.connection_string` pointing at the **same database** (for example PostgreSQL, or a sqlite file visible to all nodes)
-Note: `team.workspace.enabled=true` enables team workspace semantics; unless both sides explicitly configure a jointly visible workspace root, leader and teammate create local directories under their own HOME:
+Note: the distributed templates explicitly configure the team workspace root:
+
+```yaml
+team:
+ workspace:
+ enabled: true
+ root_path: ${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}
+ version_control: false
+```
+
+Use the same `JIUWEN_TEAM_WORKSPACE_ROOT` on every node, and only share that directory over NFS. Do not share `.agent_teams`: it stores team.db, member workspaces, symlinks, and other local runtime state; sharing it across nodes can break kickoff and workspace initialization.
+
+NFS scripts, checks, and teardown: see `scripts/nfs/README.md`.
+
+Unless both sides explicitly configure a jointly visible workspace root, leader and teammate create local directories under their own HOME:
- `<LEADER_HOME>/.jiuwenclaw/.agent_teams/<team_name>/team-workspace`
- `<TEAMMATE_HOME>/.jiuwenclaw/.agent_teams/<team_name>/team-workspace`
-These paths have the same shape but are not the same physical directory. If the leader must directly read files written by a teammate, use a shared mount path or return results through messages, DB state, or file transfer tooling.
+These paths have the same shape but are not the same physical directory. If the leader must directly read files written by a teammate, configure a shared `team.workspace.root_path` or return results through messages, DB state, or file transfer tooling.
Open firewall ports as needed; replace `127.0.0.1` with real IPs for multi-host setups.
diff --git "a/docs/zh/\345\210\206\345\270\203\345\274\217Team.md" "b/docs/zh/\345\210\206\345\270\203\345\274\217Team.md"
index 4ceb341928..d56fe4ddb1 100644
--- "a/docs/zh/\345\210\206\345\270\203\345\274\217Team.md"
+++ "b/docs/zh/\345\210\206\345\270\203\345\274\217Team.md"
@@ -184,12 +184,26 @@ cp "<REPO_ROOT>/jiuwenclaw/resources/config.team.distributed.teammate.yaml" \
- teammate 发布自己的 bootstrap endpoint,leader 不需要知道 teammate 地址
- `team.storage.params.connection_string` 指向 **同一数据库**(如 PostgreSQL,或各节点都能访问的 sqlite 文件)
-注意:`team.workspace.enabled=true` 只启用 team workspace 语义;未显式配置共同可见的 workspace root 时,leader 和 teammate 会分别在各自 HOME 下创建:
+注意:分布式模板已经显式配置了 team workspace root:
+
+```yaml
+team:
+ workspace:
+ enabled: true
+ root_path: ${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}
+ version_control: false
+```
+
+部署时应在所有节点使用同一个 `JIUWEN_TEAM_WORKSPACE_ROOT`,并只把该目录作为 NFS 共享目录。不要共享 `.agent_teams`,该目录保存 team.db、成员 workspace、symlink 等本地运行状态,多节点共享会破坏 kickoff 和 workspace 初始化。
+
+NFS server/client 脚本、连通性检查、取消挂载:`scripts/nfs/README.md`。
+
+未显式配置共同可见的 workspace root 时,leader 和 teammate 会分别在各自 HOME 下创建:
- `<LEADER_HOME>/.jiuwenclaw/.agent_teams/<team_name>/team-workspace`
- `<TEAMMATE_HOME>/.jiuwenclaw/.agent_teams/<team_name>/team-workspace`
-这两个路径名字相同但不是同一个物理目录。需要让成员写出的文件被 leader 直接读取时,应配置双方都可访问的共享挂载路径,或通过消息 / DB / 文件传输工具回传结果。
+这两个路径名字相同但不是同一个物理目录。需要让成员写出的文件被 leader 直接读取时,应配置双方都可访问的 `team.workspace.root_path`,或通过消息 / DB / 文件传输工具回传结果。
端口与防火墙需保证 leader/teammate 机器互通(多机时把示例中的 `127.0.0.1` 换成真实 IP)。
diff --git "a/docs/zh/\345\210\206\345\270\203\345\274\217team nfs\344\275\277\347\224\250\350\257\264\346\230\216.md" "b/docs/zh/\345\210\206\345\270\203\345\274\217team nfs\344\275\277\347\224\250\350\257\264\346\230\216.md"
deleted file mode 100644
index 24fc70c06b..0000000000
--- "a/docs/zh/\345\210\206\345\270\203\345\274\217team nfs\344\275\277\347\224\250\350\257\264\346\230\216.md"
+++ /dev/null
@@ -1,166 +0,0 @@
-# 分布式 Team NFS 使用说明
-
-本文说明如何用 `scripts/nfs/` 下的脚本,在分布式 Team 场景中共享 Team 工作空间。
-
-默认共享工作空间目录:
-
-```text
-/root/.jiuwenclaw/.agent_teams
-```
-
-
-
-## 前提
-
-- leader 和 teammate 都是 Linux 节点。
-- 节点之间内网互通,优先使用内网 IP。
-- NFS / RPC 相关端口已在安全组或防火墙中放行。
-- 挂载或切换 NFS 前,先停止 leader / teammate 上正在运行的 JiuwenClaw Team 进程。
-
-下文使用占位符:
-
-```text
-<leader-private-ip> leader 节点内网 IP
-<teammate-private-ip> teammate 节点内网 IP
-```
-
-## 1. Leader 启动 NFS Server
-
-在 leader 节点执行:
-
-```bash
-cd /path/to/jiuwenclaw
-sudo bash scripts/nfs/setup_nfs_server.sh --client-ip <teammate-private-ip>
-```
-
-脚本默认导出:
-
-```text
-/root/.jiuwenclaw/.agent_teams
-```
-
-如需显式指定:
-
-```bash
-sudo bash scripts/nfs/setup_nfs_server.sh \
- --client-ip <teammate-private-ip> \
- --export-dir /root/.jiuwenclaw/.agent_teams \
- --mount-point /root/.jiuwenclaw/.agent_teams \
- --fsid 1002
-```
-
-`--fsid` 用于避免部分系统执行 `exportfs` 时出现 `requires fsid=`。
-
-## 2. Teammate 挂载 NFS Client
-
-在 teammate 节点执行:
-
-```bash
-cd /path/to/jiuwenclaw
-sudo bash scripts/nfs/setup_nfs_client.sh --server-ip <leader-private-ip>
-```
-
-如需显式指定:
-
-```bash
-sudo bash scripts/nfs/setup_nfs_client.sh \
- --server-ip <leader-private-ip> \
- --export-dir /root/.jiuwenclaw/.agent_teams \
- --mount-point /root/.jiuwenclaw/.agent_teams
-```
-
-client 脚本会在挂载前备份已有本地目录,备份目录形如:
-
-```text
-/root/.jiuwenclaw/.agent_teams.pre_nfs_backup_YYYYmmdd_HHMMSS
-```
-
-## 3. 检查挂载
-
-在 leader 节点检查导出:
-
-```bash
-exportfs -v
-showmount -e localhost
-```
-
-在 teammate 节点检查连通和挂载:
-
-```bash
-rpcinfo -p <leader-private-ip>
-showmount -e <leader-private-ip>
-mount | grep agent_teams
-df -h | grep agent_teams
-```
-
-看到类似输出即表示挂载成功:
-
-```text
-<leader-private-ip>:/root/.jiuwenclaw/.agent_teams on /root/.jiuwenclaw/.agent_teams type nfs4
-```
-
-## 4. 双向读写验证
-
-leader 写入:
-
-```bash
-echo from-leader > /root/.jiuwenclaw/.agent_teams/nfs_team_test.txt
-```
-
-teammate 读取并追加:
-
-```bash
-cat /root/.jiuwenclaw/.agent_teams/nfs_team_test.txt
-echo from-teammate >> /root/.jiuwenclaw/.agent_teams/nfs_team_test.txt
-```
-
-leader 再读取:
-
-```bash
-cat /root/.jiuwenclaw/.agent_teams/nfs_team_test.txt
-```
-
-如果能看到两端写入的内容,说明共享生效。
-
-## 5. 推荐启动顺序
-
-1. 停止 leader / teammate 上的 JiuwenClaw Team 进程。
-2. leader 执行 `setup_nfs_server.sh`。
-3. teammate 执行 `setup_nfs_client.sh`。
-4. 确认 `mount | grep agent_teams` 正常。
-5. 启动注册中心。
-6. 启动 teammate 的 `app_agentserver`。
-7. 启动 leader 后端和前端。
-8. 新建会话重新执行分布式 Team 测试。
-
-不要在 Team 运行过程中切换 `.agent_teams` 挂载关系。
-
-## 6. 清理
-
-teammate 卸载:
-
-```bash
-sudo umount /root/.jiuwenclaw/.agent_teams
-```
-
-如果提示 busy,先停止相关进程后重试。必要时使用:
-
-```bash
-sudo umount -l /root/.jiuwenclaw/.agent_teams
-```
-
-如需取消开机自动挂载,删除 `/etc/fstab` 中包含 `.agent_teams` 的 NFS 行。
-
-leader 清理导出:
-
-```bash
-sudo rm -f /etc/exports.d/jiuwenclaw.exports
-sudo exportfs -rav
-```
-
-## 7. 常见问题
-
-- `exportfs` 提示 `requires fsid=`:使用 server 脚本默认的 `fsid=1002`,或显式传入 `--fsid 1002`。
-- `rpcinfo` / `showmount` 超时:检查内网 IP、安全组、防火墙和 NFS 服务状态。
-- Team 看不到共享文件:确认 NFS 已挂载后再启动 Team 进程,并使用新会话测试。
-- 多个 teammate:每个 teammate 都执行 client 脚本;leader 侧需要允许对应 teammate 的内网 IP。
diff --git a/jiuwenclaw/resources/config.team.distributed.leader.yaml b/jiuwenclaw/resources/config.team.distributed.leader.yaml
index d760ccb6d2..7794121886 100644
--- a/jiuwenclaw/resources/config.team.distributed.leader.yaml
+++ b/jiuwenclaw/resources/config.team.distributed.leader.yaml
@@ -661,6 +661,8 @@ team:
workspace:
enabled: true
+ root_path: ${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}
+ version_control: false
transport:
type: pyzmq
@@ -761,6 +763,8 @@ modes:
workspace:
enabled: true
+ root_path: ${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}
+ version_control: false
transport:
type: pyzmq
diff --git a/jiuwenclaw/resources/config.team.distributed.teammate.yaml b/jiuwenclaw/resources/config.team.distributed.teammate.yaml
index 7a53ecde7d..d447bf5f67 100644
--- a/jiuwenclaw/resources/config.team.distributed.teammate.yaml
+++ b/jiuwenclaw/resources/config.team.distributed.teammate.yaml
@@ -662,6 +662,8 @@ team:
workspace:
enabled: true
+ root_path: ${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}
+ version_control: false
transport:
type: pyzmq
@@ -761,6 +763,8 @@ modes:
workspace:
enabled: true
+ root_path: ${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}
+ version_control: false
transport:
type: pyzmq
diff --git a/scripts/nfs/README.md b/scripts/nfs/README.md
index 4b50d7b829..c2a4112e1a 100644
--- a/scripts/nfs/README.md
+++ b/scripts/nfs/README.md
@@ -1,31 +1,35 @@
# Linux 跨节点 NFS 使用说明
-这两个脚本用于在 Linux 节点之间共享 `jiuwenclaw` 工作空间。
+这两个脚本用于在 Linux 节点之间共享 `jiuwenclaw` 团队共享工作空间。
适用场景:
- 一个中心节点作为 NFS server
- 一个或多个节点作为 NFS client
-- 多个节点共享同一个工作空间目录
+- 多个节点共享一个或多个工作空间目录
+- 分布式 Team 场景下,只共享 `team.workspace.root_path` 指向的团队共享目录,不要共享 `.agent_teams`
默认共享目录:
```text
-/root/.jiuwenclaw/agent/jiuwenclaw_workspace
+${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}
```
建议:
- 优先使用内网 IP
- 所有节点都先完成一次 `jiuwenclaw` 初始化
-- 尽量使用同一个用户运行,这里默认是 `root`
+- 尽量使用同一个用户运行
+- 如果要使用自定义挂载点,在所有节点设置相同的 `JIUWEN_TEAM_WORKSPACE_ROOT`
## 1. 服务端执行
-在中心节点执行:
+在中心节点执行(可重复传入多个 `--client-ip`):
```bash
-sudo bash scripts/nfs/setup_nfs_server.sh --client-ip <客户端内网IP>
+sudo bash scripts/nfs/setup_nfs_server.sh \
+ --client-ip <客户端1内网IP> \
+ --client-ip <客户端2内网IP>
```
@@ -34,9 +38,24 @@ sudo bash scripts/nfs/setup_nfs_server.sh --client-ip <客户端内网IP>
```bash
sudo bash scripts/nfs/setup_nfs_server.sh \
- --client-ip <客户端内网IP> \
- --export-dir /root/.jiuwenclaw/agent/jiuwenclaw_workspace \
- --mount-point /root/.jiuwenclaw/agent/jiuwenclaw_workspace
+ --client-ip <客户端1内网IP> \
+ --client-ip <客户端2内网IP> \
+ --export-dir /mnt/jiuwenclaw/shared_workspace/jiuwen_team \
+ --mount-point /mnt/jiuwenclaw/shared_workspace/jiuwen_team
+```
+
+如果只有一个客户端,保留单个 `--client-ip` 也可以。
+
+如果要共享多个目录(每组 `--export-dir` 必须对应一组 `--mount-point`):
+
+```bash
+sudo bash scripts/nfs/setup_nfs_server.sh \
+ --client-ip <客户端1内网IP> \
+ --client-ip <客户端2内网IP> \
+ --export-dir /mnt/jiuwenclaw/shared_workspace/jiuwen_team \
+ --mount-point /mnt/jiuwenclaw/shared_workspace/jiuwen_team \
+ --export-dir /mnt/jiuwenclaw/shared_artifacts \
+ --mount-point /mnt/jiuwenclaw/shared_artifacts
```
## 2. 客户端执行
@@ -54,8 +73,19 @@ sudo bash scripts/nfs/setup_nfs_client.sh --server-ip <服务端内网IP>
```bash
sudo bash scripts/nfs/setup_nfs_client.sh \
--server-ip <服务端内网IP> \
- --export-dir /root/.jiuwenclaw/agent/jiuwenclaw_workspace \
- --mount-point /root/.jiuwenclaw/agent/jiuwenclaw_workspace
+ --export-dir /mnt/jiuwenclaw/shared_workspace/jiuwen_team \
+ --mount-point /mnt/jiuwenclaw/shared_workspace/jiuwen_team
+```
+
+如果要挂载多个目录(参数成对出现):
+
+```bash
+sudo bash scripts/nfs/setup_nfs_client.sh \
+ --server-ip <服务端内网IP> \
+ --export-dir /mnt/jiuwenclaw/shared_workspace/jiuwen_team \
+ --mount-point /mnt/jiuwenclaw/shared_workspace/jiuwen_team \
+ --export-dir /mnt/jiuwenclaw/shared_artifacts \
+ --mount-point /mnt/jiuwenclaw/shared_artifacts
```
## 3. 连通性检查
@@ -74,8 +104,8 @@ showmount -e <服务端内网IP>
在客户端执行:
```bash
-mount | grep jiuwenclaw_workspace
-df -h | grep jiuwenclaw_workspace
+mount | grep jiuwen_team
+df -h | grep jiuwen_team
```
## 5. 同步验证
@@ -83,25 +113,25 @@ df -h | grep jiuwenclaw_workspace
在服务端执行:
```bash
-echo hello > /root/.jiuwenclaw/agent/jiuwenclaw_workspace/hello.txt
+echo hello > "${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}/hello.txt"
```
在客户端执行:
```bash
-cat /root/.jiuwenclaw/agent/jiuwenclaw_workspace/hello.txt
+cat "${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}/hello.txt"
```
再在客户端追加:
```bash
-echo world >> /root/.jiuwenclaw/agent/jiuwenclaw_workspace/hello.txt
+echo world >> "${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}/hello.txt"
```
回到服务端查看:
```bash
-cat /root/.jiuwenclaw/agent/jiuwenclaw_workspace/hello.txt
+cat "${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}/hello.txt"
```
如果两边都能看到相同内容,就说明同步成功。
@@ -110,4 +140,36 @@ cat /root/.jiuwenclaw/agent/jiuwenclaw_workspace/hello.txt
- 客户端脚本会在挂载前备份已有本地目录
- 如果有多个客户端,每个客户端都执行一次客户端脚本即可
+- 支持多客户端和多目录;多目录时 `--export-dir` 与 `--mount-point` 数量必须一致
- 这套方案共享的是文件系统,不是多节点分布式运行时
+- `.agent_teams` 保存 team.db、成员 workspace、symlink 等本地运行状态,不应通过 NFS 在多个 teammate 之间共享
+
+## 7. 取消挂载与回滚
+
+服务端回滚(删除脚本导出并重载 export):
+
+```bash
+sudo bash scripts/nfs/teardown_nfs_server.sh
+```
+
+如果还要同时停止并禁用 NFS 服务:
+
+```bash
+sudo bash scripts/nfs/teardown_nfs_server.sh --stop-service --disable-service
+```
+
+客户端取消挂载(按挂载点):
+
+```bash
+sudo bash scripts/nfs/teardown_nfs_client.sh \
+ --server-ip <服务端内网IP> \
+ --mount-point "${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}"
+```
+
+如果要清理该服务端在 `/etc/fstab` 的全部 nfs4 记录:
+
+```bash
+sudo bash scripts/nfs/teardown_nfs_client.sh \
+ --server-ip <服务端内网IP> \
+ --clean-all-server-entries
+```
diff --git a/scripts/nfs/setup_nfs_client.sh b/scripts/nfs/setup_nfs_client.sh
index dc29ac4993..f8c9f4aab0 100644
--- a/scripts/nfs/setup_nfs_client.sh
+++ b/scripts/nfs/setup_nfs_client.sh
@@ -3,9 +3,11 @@
set -euo pipefail
SERVER_IP="${SERVER_IP:-}"
-EXPORT_DIR="${EXPORT_DIR:-/root/.jiuwenclaw/.agent_teams}"
-MOUNT_POINT="${MOUNT_POINT:-/root/.jiuwenclaw/.agent_teams}"
-FSTAB_LINE=""
+DEFAULT_TEAM_WORKSPACE="${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}"
+EXPORT_DIR="${EXPORT_DIR:-${DEFAULT_TEAM_WORKSPACE}}"
+MOUNT_POINT="${MOUNT_POINT:-${DEFAULT_TEAM_WORKSPACE}}"
+EXPORT_DIRS=()
+MOUNT_POINTS=()
while [[ $# -gt 0 ]]; do
case "$1" in
@@ -14,11 +16,11 @@ while [[ $# -gt 0 ]]; do
shift 2
;;
--export-dir)
- EXPORT_DIR="$2"
+ EXPORT_DIRS+=("$2")
shift 2
;;
--mount-point)
- MOUNT_POINT="$2"
+ MOUNT_POINTS+=("$2")
shift 2
;;
-h|--help)
@@ -28,8 +30,11 @@ Usage:
Options:
--server-ip <ip> NFS server IP. Required unless SERVER_IP is set
- --export-dir <path> Server export directory. Default: /root/.jiuwenclaw/.agent_teams
- --mount-point <path> Local mount path. Default: /root/.jiuwenclaw/.agent_teams
+ --export-dir <path> Server export directory. Repeatable when paired with --mount-point
+ --mount-point <path> Local mount path. Repeatable and must match --export-dir count
+
+Defaults:
+ export/mount path: ${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}
EOF
exit 0
;;
@@ -50,7 +55,22 @@ if [[ -z "${SERVER_IP}" ]]; then
exit 1
fi
-FSTAB_LINE="${SERVER_IP}:${EXPORT_DIR} ${MOUNT_POINT} nfs4 vers=4.1,_netdev,defaults 0 0"
+if [[ "${#EXPORT_DIRS[@]}" -eq 0 ]] && [[ -n "${EXPORT_DIR}" ]]; then
+ EXPORT_DIRS+=("${EXPORT_DIR}")
+fi
+if [[ "${#MOUNT_POINTS[@]}" -eq 0 ]] && [[ -n "${MOUNT_POINT}" ]]; then
+ MOUNT_POINTS+=("${MOUNT_POINT}")
+fi
+
+if [[ "${#EXPORT_DIRS[@]}" -ne "${#MOUNT_POINTS[@]}" ]]; then
+ echo "The number of --export-dir and --mount-point arguments must match." >&2
+ exit 1
+fi
+
+if [[ "${#EXPORT_DIRS[@]}" -eq 0 ]]; then
+ echo "At least one export mapping is required." >&2
+ exit 1
+fi
install_nfs_client() {
if command -v apt-get >/dev/null 2>&1; then
@@ -75,30 +95,42 @@ ensure_fstab_line() {
}
backup_existing_mount_point() {
- if mountpoint -q "${MOUNT_POINT}"; then
+ local mount_point="$1"
+ if mountpoint -q "${mount_point}"; then
return
fi
- if [[ -d "${MOUNT_POINT}" ]] && [[ -n "$(find "${MOUNT_POINT}" -mindepth 1 -maxdepth 1 2>/dev/null)" ]]; then
- local backup_dir="${MOUNT_POINT}.pre_nfs_backup_$(date +%Y%m%d_%H%M%S)"
+ if [[ -d "${mount_point}" ]] && [[ -n "$(find "${mount_point}" -mindepth 1 -maxdepth 1 2>/dev/null)" ]]; then
+ local backup_dir="${mount_point}.pre_nfs_backup_$(date +%Y%m%d_%H%M%S)"
echo "Backing up existing local workspace to ${backup_dir}"
- mv "${MOUNT_POINT}" "${backup_dir}"
- mkdir -p "${MOUNT_POINT}"
+ mv "${mount_point}" "${backup_dir}"
+ mkdir -p "${mount_point}"
fi
}
echo "[1/4] Installing NFS client packages"
install_nfs_client
-echo "[2/4] Creating mount point ${MOUNT_POINT}"
-mkdir -p "${MOUNT_POINT}"
-backup_existing_mount_point
+echo "[2/4] Creating mount points and preparing backups"
+for mount_point in "${MOUNT_POINTS[@]}"; do
+ mkdir -p "${mount_point}"
+ backup_existing_mount_point "${mount_point}"
+done
-echo "[3/4] Mounting ${SERVER_IP}:${EXPORT_DIR}"
-mountpoint -q "${MOUNT_POINT}" || mount -t nfs4 -o vers=4.1 "${SERVER_IP}:${EXPORT_DIR}" "${MOUNT_POINT}"
+echo "[3/4] Mounting exports"
+for idx in "${!EXPORT_DIRS[@]}"; do
+ export_dir="${EXPORT_DIRS[$idx]}"
+ mount_point="${MOUNT_POINTS[$idx]}"
+ mountpoint -q "${mount_point}" || mount -t nfs4 -o vers=4.1 "${SERVER_IP}:${export_dir}" "${mount_point}"
+done
-echo "[4/4] Persisting mount to /etc/fstab"
-ensure_fstab_line "${FSTAB_LINE}" /etc/fstab
+echo "[4/4] Persisting mounts to /etc/fstab"
+for idx in "${!EXPORT_DIRS[@]}"; do
+ export_dir="${EXPORT_DIRS[$idx]}"
+ mount_point="${MOUNT_POINTS[$idx]}"
+ fstab_line="${SERVER_IP}:${export_dir} ${mount_point} nfs4 vers=4.1,_netdev,defaults 0 0"
+ ensure_fstab_line "${fstab_line}" /etc/fstab
+done
cat <<EOF
@@ -106,10 +138,9 @@ NFS client is ready.
Client node:
server : ${SERVER_IP}
- export dir : ${EXPORT_DIR}
- mount path : ${MOUNT_POINT}
+$(for idx in "${!EXPORT_DIRS[@]}"; do printf " [%s] %s -> %s\n" "$((idx + 1))" "${EXPORT_DIRS[$idx]}" "${MOUNT_POINTS[$idx]}"; done)
Quick verification:
- touch ${MOUNT_POINT}/nfs_client_probe.txt
- ls -la ${MOUNT_POINT}
+ touch <mount-point>/nfs_client_probe.txt
+ ls -la <mount-point>
EOF
diff --git a/scripts/nfs/setup_nfs_server.sh b/scripts/nfs/setup_nfs_server.sh
index 60d432e1a1..9c21c6e7f7 100644
--- a/scripts/nfs/setup_nfs_server.sh
+++ b/scripts/nfs/setup_nfs_server.sh
@@ -3,23 +3,27 @@
set -euo pipefail
CLIENT_IP="${CLIENT_IP:-}"
-EXPORT_DIR="${EXPORT_DIR:-/root/.jiuwenclaw/.agent_teams}"
-MOUNT_POINT="${MOUNT_POINT:-/root/.jiuwenclaw/.agent_teams}"
+CLIENT_IPS=()
+DEFAULT_TEAM_WORKSPACE="${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}"
+EXPORT_DIR="${EXPORT_DIR:-${DEFAULT_TEAM_WORKSPACE}}"
+MOUNT_POINT="${MOUNT_POINT:-${DEFAULT_TEAM_WORKSPACE}}"
+EXPORT_DIRS=()
+MOUNT_POINTS=()
FSID="${FSID:-1002}"
EXPORTS_FILE="/etc/exports.d/jiuwenclaw.exports"
while [[ $# -gt 0 ]]; do
case "$1" in
--client-ip)
- CLIENT_IP="$2"
+ CLIENT_IPS+=("$2")
shift 2
;;
--export-dir)
- EXPORT_DIR="$2"
+ EXPORT_DIRS+=("$2")
shift 2
;;
--mount-point)
- MOUNT_POINT="$2"
+ MOUNT_POINTS+=("$2")
shift 2
;;
--fsid)
@@ -32,10 +36,13 @@ Usage:
sudo bash scripts/nfs/setup_nfs_server.sh [options]
Options:
- --client-ip <ip> Allowed NFS client IP. Required unless CLIENT_IP is set
- --export-dir <path> Server export directory. Default: /root/.jiuwenclaw/.agent_teams
- --mount-point <path> Local mount path. Default: /root/.jiuwenclaw/.agent_teams
- --fsid <id> Stable NFS filesystem id for this export. Default: 1002
+ --client-ip <ip> Allowed NFS client IP. Repeat this option for multiple clients
+ --export-dir <path> Server export directory. Repeatable when paired with --mount-point
+ --mount-point <path> Local mount path. Repeatable and must match --export-dir count
+ --fsid <id> Base NFS filesystem id. Each export increments from this base. Default: 1002
+
+Defaults:
+ export/mount path: ${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}
EOF
exit 0
;;
@@ -51,8 +58,29 @@ if [[ "${EUID}" -ne 0 ]]; then
exit 1
fi
-if [[ -z "${CLIENT_IP}" ]]; then
- echo "CLIENT_IP is required. Pass --client-ip <private-ip> or export CLIENT_IP first." >&2
+if [[ -n "${CLIENT_IP}" ]]; then
+ CLIENT_IPS+=("${CLIENT_IP}")
+fi
+
+if [[ "${#CLIENT_IPS[@]}" -eq 0 ]]; then
+ echo "At least one client IP is required. Pass --client-ip <private-ip> (repeatable) or export CLIENT_IP first." >&2
+ exit 1
+fi
+
+if [[ "${#EXPORT_DIRS[@]}" -eq 0 ]] && [[ -n "${EXPORT_DIR}" ]]; then
+ EXPORT_DIRS+=("${EXPORT_DIR}")
+fi
+if [[ "${#MOUNT_POINTS[@]}" -eq 0 ]] && [[ -n "${MOUNT_POINT}" ]]; then
+ MOUNT_POINTS+=("${MOUNT_POINT}")
+fi
+
+if [[ "${#EXPORT_DIRS[@]}" -ne "${#MOUNT_POINTS[@]}" ]]; then
+ echo "The number of --export-dir and --mount-point arguments must match." >&2
+ exit 1
+fi
+
+if [[ "${#EXPORT_DIRS[@]}" -eq 0 ]]; then
+ echo "At least one export mapping is required." >&2
exit 1
fi
@@ -90,14 +118,28 @@ echo "[1/6] Installing NFS server packages"
install_nfs_server
echo "[2/6] Preparing shared directories"
-mkdir -p "${EXPORT_DIR}"
-chmod 755 "${EXPORT_DIR}"
+for export_dir in "${EXPORT_DIRS[@]}"; do
+ mkdir -p "${export_dir}"
+ chmod 755 "${export_dir}"
+done
echo "[3/6] Writing export rule to ${EXPORTS_FILE}"
mkdir -p /etc/exports.d
-cat > "${EXPORTS_FILE}" <<EOF
-${EXPORT_DIR} ${CLIENT_IP}(rw,sync,no_subtree_check,no_root_squash,fsid=${FSID})
-EOF
+{
+ for idx in "${!EXPORT_DIRS[@]}"; do
+ export_dir="${EXPORT_DIRS[$idx]}"
+ export_fsid=$((FSID + idx))
+ first=1
+ for ip in "${CLIENT_IPS[@]}"; do
+ if [[ "${first}" -eq 1 ]]; then
+ printf "%s %s(rw,sync,no_subtree_check,no_root_squash,fsid=%s)\n" "${export_dir}" "${ip}" "${export_fsid}"
+ first=0
+ else
+ printf "%s %s(rw,sync,no_subtree_check,no_root_squash)\n" "${export_dir}" "${ip}"
+ fi
+ done
+ done
+} > "${EXPORTS_FILE}"
echo "[4/6] Reloading exports"
exportfs -rav
@@ -105,22 +147,27 @@ exportfs -rav
echo "[5/6] Enabling NFS service"
enable_nfs_service
-echo "[6/6] Creating local bind mount at ${MOUNT_POINT}"
-mkdir -p "${MOUNT_POINT}"
-if [[ "${EXPORT_DIR}" != "${MOUNT_POINT}" ]]; then
- mountpoint -q "${MOUNT_POINT}" || mount --bind "${EXPORT_DIR}" "${MOUNT_POINT}"
- ensure_fstab_line "${EXPORT_DIR} ${MOUNT_POINT} none bind 0 0" /etc/fstab
-fi
+echo "[6/6] Creating local bind mounts"
+for idx in "${!EXPORT_DIRS[@]}"; do
+ export_dir="${EXPORT_DIRS[$idx]}"
+ mount_point="${MOUNT_POINTS[$idx]}"
+ mkdir -p "${mount_point}"
+ if [[ "${export_dir}" != "${mount_point}" ]]; then
+ mountpoint -q "${mount_point}" || mount --bind "${export_dir}" "${mount_point}"
+ ensure_fstab_line "${export_dir} ${mount_point} none bind 0 0" /etc/fstab
+ fi
+done
cat <<EOF
NFS server is ready.
Server node:
- export dir : ${EXPORT_DIR}
- mount path : ${MOUNT_POINT}
- client ip : ${CLIENT_IP}
- fsid : ${FSID}
+ clients : ${CLIENT_IPS[*]}
+ fsid base : ${FSID}
+
+Export mappings:
+$(for idx in "${!EXPORT_DIRS[@]}"; do printf " [%s] %s -> %s (fsid=%s)\n" "$((idx + 1))" "${EXPORT_DIRS[$idx]}" "${MOUNT_POINTS[$idx]}" "$((FSID + idx))"; done)
Next step on the client node:
sudo bash scripts/nfs/setup_nfs_client.sh --server-ip <server-private-ip>
diff --git a/scripts/nfs/teardown_nfs_client.sh b/scripts/nfs/teardown_nfs_client.sh
new file mode 100644
index 0000000000..aa0f84844c
--- /dev/null
+++ b/scripts/nfs/teardown_nfs_client.sh
@@ -0,0 +1,153 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+
+SERVER_IP="${SERVER_IP:-}"
+DEFAULT_TEAM_WORKSPACE="${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}"
+EXPORT_DIR="${EXPORT_DIR:-${DEFAULT_TEAM_WORKSPACE}}"
+MOUNT_POINT="${MOUNT_POINT:-${DEFAULT_TEAM_WORKSPACE}}"
+MOUNT_POINTS=()
+EXPORT_DIRS=()
+CLEAN_ALL_SERVER_ENTRIES=0
+
+while [[ $# -gt 0 ]]; do
+ case "$1" in
+ --server-ip)
+ SERVER_IP="$2"
+ shift 2
+ ;;
+ --mount-point)
+ MOUNT_POINTS+=("$2")
+ shift 2
+ ;;
+ --export-dir)
+ EXPORT_DIRS+=("$2")
+ shift 2
+ ;;
+ --clean-all-server-entries)
+ CLEAN_ALL_SERVER_ENTRIES=1
+ shift
+ ;;
+ -h|--help)
+ cat <<'EOF'
+Usage:
+ sudo bash scripts/nfs/teardown_nfs_client.sh [options]
+
+Options:
+ --server-ip <ip> NFS server IP. Required unless --clean-all-server-entries is set.
+ --mount-point <path> Local mount path to unmount and remove from /etc/fstab. Repeatable.
+ --export-dir <path> Used with --mount-point to remove exact fstab entry. Repeatable.
+ --clean-all-server-entries Remove all /etc/fstab nfs4 lines that start with <server-ip>:
+
+Defaults:
+ mount/export path: ${JIUWEN_TEAM_WORKSPACE_ROOT:-/tmp/jiuwenclaw/shared_workspace/jiuwen_team}
+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 [[ "${#MOUNT_POINTS[@]}" -eq 0 ]] && [[ "${CLEAN_ALL_SERVER_ENTRIES}" -eq 0 ]]; then
+ MOUNT_POINTS+=("${MOUNT_POINT}")
+fi
+if [[ "${#EXPORT_DIRS[@]}" -eq 0 ]] && [[ "${CLEAN_ALL_SERVER_ENTRIES}" -eq 0 ]] && [[ -n "${EXPORT_DIR}" ]]; then
+ EXPORT_DIRS+=("${EXPORT_DIR}")
+fi
+
+if [[ "${#MOUNT_POINTS[@]}" -eq 0 ]] && [[ "${CLEAN_ALL_SERVER_ENTRIES}" -eq 0 ]]; then
+ echo "You must pass at least one --mount-point, or set --clean-all-server-entries." >&2
+ exit 1
+fi
+
+if [[ -z "${SERVER_IP}" ]] && [[ "${CLEAN_ALL_SERVER_ENTRIES}" -eq 1 ]]; then
+ echo "--server-ip is required when --clean-all-server-entries is set." >&2
+ exit 1
+fi
+
+if [[ "${#EXPORT_DIRS[@]}" -gt 0 ]] && [[ "${#EXPORT_DIRS[@]}" -ne "${#MOUNT_POINTS[@]}" ]]; then
+ echo "When --export-dir is provided, its count must match --mount-point count." >&2
+ exit 1
+fi
+
+cp /etc/fstab "/etc/fstab.bak.$(date +%Y%m%d%H%M%S)"
+
+for mount_point in "${MOUNT_POINTS[@]}"; do
+ umount "${mount_point}" 2>/dev/null || true
+done
+
+mount_points_env="$(printf '%s\n' "${MOUNT_POINTS[@]}")"
+export_dirs_env="$(printf '%s\n' "${EXPORT_DIRS[@]}")"
+export JIUWEN_NFS_SERVER_IP="${SERVER_IP}"
+export JIUWEN_NFS_CLEAN_ALL="${CLEAN_ALL_SERVER_ENTRIES}"
+export JIUWEN_NFS_MOUNT_POINTS="${mount_points_env}"
+export JIUWEN_NFS_EXPORT_DIRS="${export_dirs_env}"
+
+/usr/bin/python3 - <<PY
+import os
+from pathlib import Path
+
+fstab = Path("/etc/fstab")
+lines = fstab.read_text().splitlines()
+out = []
+
+server_ip = os.environ["JIUWEN_NFS_SERVER_IP"]
+clean_all = int(os.environ["JIUWEN_NFS_CLEAN_ALL"])
+mount_points = os.environ.get("JIUWEN_NFS_MOUNT_POINTS", "")
+export_dirs = os.environ.get("JIUWEN_NFS_EXPORT_DIRS", "")
+
+mp_list = [line for line in mount_points.splitlines() if line]
+ed_list = [line for line in export_dirs.splitlines() if line]
+exact_pairs = set(zip(ed_list, mp_list)) if ed_list else set()
+
+for line in lines:
+ stripped = line.strip()
+ if not stripped or stripped.startswith("#"):
+ out.append(line)
+ continue
+ parts = stripped.split()
+ if len(parts) < 4:
+ out.append(line)
+ continue
+ source, target, fstype = parts[0], parts[1], parts[2]
+ if fstype != "nfs4":
+ out.append(line)
+ continue
+ if clean_all and source.startswith(f"{server_ip}:"):
+ continue
+ removed = False
+ if target in mp_list:
+ if exact_pairs:
+ for export_dir, mount_point in exact_pairs:
+ if source == f"{server_ip}:{export_dir}" and target == mount_point:
+ removed = True
+ break
+ else:
+ removed = True
+ if removed:
+ continue
+ out.append(line)
+
+fstab.write_text("\n".join(out) + ("\n" if out else ""))
+print("Updated /etc/fstab")
+PY
+
+cat <<EOF
+
+NFS client teardown completed.
+
+Client side:
+ server ip : ${SERVER_IP:-<not provided>}
+ mount points : ${MOUNT_POINTS[*]:-<none>}
+ export dirs : ${EXPORT_DIRS[*]:-<none>}
+ clean all lines : ${CLEAN_ALL_SERVER_ENTRIES}
+
+EOF
diff --git a/scripts/nfs/teardown_nfs_server.sh b/scripts/nfs/teardown_nfs_server.sh
new file mode 100644
index 0000000000..054e533195
--- /dev/null
+++ b/scripts/nfs/teardown_nfs_server.sh
@@ -0,0 +1,95 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+
+EXPORTS_FILE="${EXPORTS_FILE:-/etc/exports.d/jiuwenclaw.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/jiuwenclaw.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