|
| 1 | +--- |
| 2 | +sidebar_position: 4 |
| 3 | +title: Sandbox 沙箱 |
| 4 | +--- |
| 5 | + |
| 6 | +# Sandbox 沙箱模式 |
| 7 | + |
| 8 | +Sandbox 是 SubOS 的文件系统隔离进入方式。与普通 subos 的 PATH/env 隔离不同,sandbox 通过 bwrap 或 proot 提供隔离的 `$HOME`、dotfile、`/tmp` 和最小 `/etc` 视图。 |
| 9 | + |
| 10 | +:::info 平台限制 |
| 11 | +Sandbox 模式仅支持 Linux。macOS 和 Windows 上不可用。 |
| 12 | +::: |
| 13 | + |
| 14 | +## 创建并进入 Sandbox |
| 15 | + |
| 16 | +Sandbox 不是创建时的独立类型,而是进入 subos 时的模式: |
| 17 | + |
| 18 | +```bash |
| 19 | +xlings subos new mybox |
| 20 | +xlings subos use mybox --sandbox |
| 21 | +``` |
| 22 | + |
| 23 | +xlings 会复用当前 shell,并为该 subos 懒初始化 sandbox 私有目录和最小系统文件。 |
| 24 | + |
| 25 | +如果需要指定后端,可以显式传入: |
| 26 | + |
| 27 | +```bash |
| 28 | +xlings subos use mybox --sandbox bwrap |
| 29 | +xlings subos use mybox --sandbox proot |
| 30 | +``` |
| 31 | + |
| 32 | +进入后你会发现: |
| 33 | +- `$HOME` 指向 sandbox 私有的 `/home/<user>` 目录 |
| 34 | +- 所有 dotfile(`.gitconfig`、`.npm/`、`.cache/` 等)完全独立 |
| 35 | +- 优先使用 bwrap,受限环境下回退到 proot |
| 36 | +- xlings 是 sandbox 内唯一的包管理器 |
| 37 | + |
| 38 | +## 在 Sandbox 中安装工具 |
| 39 | + |
| 40 | +```bash |
| 41 | +# 进入 sandbox |
| 42 | +xlings subos use mybox --sandbox |
| 43 | + |
| 44 | +# 安装 gcc |
| 45 | +xlings install gcc@15 -y |
| 46 | +gcc --version |
| 47 | + |
| 48 | +# 安装 node |
| 49 | +xlings install node@24 -y |
| 50 | +node --version |
| 51 | +``` |
| 52 | + |
| 53 | +安装的工具仅在此 sandbox 内可见,不影响宿主环境或其他 subos。 |
| 54 | + |
| 55 | +## 三种 SubOS 模式对比 |
| 56 | + |
| 57 | +| | global | shell-level | sandbox | |
| 58 | +|---|---|---|---| |
| 59 | +| **创建** | `subos new foo` | `subos new foo` | `subos new foo` | |
| 60 | +| **进入** | `subos use foo --global` | `subos use foo` | `subos use foo --sandbox` | |
| 61 | +| **隔离级别** | PATH 优先级 | 子 shell + env | 文件系统视图 | |
| 62 | +| **$HOME** | 宿主主目录 | 宿主主目录 | sandbox 私有 | |
| 63 | +| **dotfile** | 共享宿主 | 共享宿主 | 完全独立 | |
| 64 | +| **后端** | env | env spawn shell | bwrap / proot | |
| 65 | +| **平台** | 全平台 | 全平台 | 仅 Linux | |
| 66 | + |
| 67 | +## 使用场景 |
| 68 | + |
| 69 | +- **隔离构建环境**:编译项目时不污染宿主 dotfile 和缓存 |
| 70 | +- **跨发行版测试**:在不同 sandbox 中测试不同的工具链组合 |
| 71 | +- **清理实验**:尝试新工具后直接删除 sandbox,宿主一尘不染 |
| 72 | +- **AI Agent 操场**:给 LLM agent 一个干净的沙箱环境 |
| 73 | + |
| 74 | +## 后端 |
| 75 | + |
| 76 | +xlings sandbox 支持双后端,自动检测可用后端: |
| 77 | + |
| 78 | +| 后端 | 说明 | |
| 79 | +|------|------| |
| 80 | +| **proot** | 无需特权,用户态文件系统重定向,零 sudo | |
| 81 | +| **bwrap** (bubblewrap) | 基于 Linux namespace,更强的隔离性 | |
| 82 | + |
| 83 | +xlings 会优先使用 bwrap(如果可用),否则自动回退到 proot。 |
| 84 | + |
| 85 | +## 删除 Sandbox |
| 86 | + |
| 87 | +```bash |
| 88 | +xlings subos remove mybox |
| 89 | +``` |
| 90 | + |
| 91 | +删除 sandbox 会清除其私有的根目录,包括所有 dotfile。共享的包 payload 不受影响。 |
0 commit comments