> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/handbook/di-23-zhang-z-wen-jian-xi-tong-zfs/23.7.-yin-dao-huan-jing.md).

# 23.7.引导环境

引导环境是包含操作系统的数据集树的可引导克隆。在采用 Root-on-ZFS 布局安装的系统上，这些数据集位于 **zroot/ROOT** 之下，正在运行的系统位于 **zroot/ROOT/default**。创建引导环境会对根数据集进行快照和克隆，这一过程在几秒内完成，并且在各环境开始分化之前几乎不占用空间。

使用 **bectl(8)** 管理引导环境。在进行重大更改（如操作系统升级或大型软件包升级）之前，创建一个新的环境：

```sh
# bectl create beforeupgrade
# bectl list
BE            Active Mountpoint Space Created
default       NR     /          2.43G 2026-07-11 11:26
beforeupgrade -      -          328K  2026-07-11 11:31
```

在 `Active` 列中，`N` 标记当前正在使用的环境，`R` 标记重启后变为活动的环境。

像往常一样使用 **freebsd-update(8)** 或 **pkg(8)** 执行升级。升级会修改活动环境，而 `beforeupgrade` 保留系统原状。如果升级后的系统无法启动或运行异常，重启并从 FreeBSD 加载器的 `Boot Environments` 菜单中选择 `beforeupgrade`。在加载器菜单中选择环境仅影响当前启动。要使回滚永久生效，激活该环境：

```sh
# bectl activate beforeupgrade
```

运行 `bectl activate -t _beName_` 则仅为下次启动激活一个环境，这在远程系统上测试更改时非常有用。

要在不启动的情况下检查或修复非活动环境的内容，将其挂载到临时位置：

```sh
# bectl mount beforeupgrade
/tmp/be_mount.c1Xk
# bectl umount beforeupgrade
```

销毁不再需要的环境以回收其占用的空间：

```sh
# bectl destroy beforeupgrade
```

> **提示**
>
> 在每次 **freebsd-update(8)** 升级或大型 **pkg(8)** 操作之前创建引导环境，提供了一种无需从备份恢复即可返回已知良好系统的即时方式。

## 23.7.1. 引导环境与应用

引导环境仅包含 **zroot/ROOT** 下的数据集。在默认的 Root-on-ZFS 布局中，这包含整个根文件系统（包括 **/usr/local** 和 **/var/db**），因此已安装的软件包及其元数据会与操作系统一起回滚。**zroot/ROOT** 之外的数据集（如 **zroot/home**、**zroot/var/log** 和 **zroot/var/mail**）是共享的：每个环境看到相同的文件，激活较旧的环境不会使它们恢复到较早的状态。

在依赖引导环境之前，围绕这种划分规划数据集布局。必须能在回滚后保留的数据（如从软件包安装的应用程序的数据库）应位于 **zroot/ROOT** 之外的独立数据集上：

```sh
# zfs create -o mountpoint=/var/db/postgres zroot/pgdata
```

采用此布局，回滚到较旧的环境会将数据库服务器二进制文件恢复到之前的版本，而数据库内容保持不变。反之亦然：在测试新环境时写入共享数据集的文件在切换回来后仍然存在，因此引导环境不能替代这些数据集的快照。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/handbook/di-23-zhang-z-wen-jian-xi-tong-zfs/23.7.-yin-dao-huan-jing.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
