> 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/di-23-zhang-cun-chu-guan-li/di-23.4-jie-xin-zeng-jiao-huan-fen-qu.md).

# 23.4 新增交换分区

某些情况下可能需要增加交换空间。在 FreeBSD 中，可通过传统分区、交换文件或 ZFS 卷（ZVOL）等多种方式实现交换空间。本节介绍系统安装后添加交换空间的方法。

三种交换空间实现方式比较：

![交换空间实现比较](/files/zBbaPomY6LAk5VBz0yV5)

因为 UFS 及 ZFS 文件系统均不支持分区收缩操作，如果在系统安装阶段未配置交换分区，则只能通过 dd 创建交换文件或 ZFS 卷。

## 目录结构

```sh
/
├── usr
│   └── swap0                       # 交换文件
├── etc
│   ├── rc.conf                      # 系统启动配置文件
│   └── fstab                        # 持久化挂载配置文件
└── dev
    ├── md0                          # 内存磁盘（用于交换文件）
    ├── nda0p3                       # 交换分区
    └── zvol
        └── zroot
            └── swap                 # ZFS 交换卷
```

## 基于 dd 命令的传统交换文件方案

创建一个大小为 8 GB 的交换文件 **/usr/swap0**（1 GB = 1024 MB，如需更大容量，可按此比例换算）：

```sh
# dd if=/dev/zero of=/usr/swap0 bs=1M count=8192 status=progress  # bs=1M 表示以 1 MiB 块大小写入；status=progress 显示进度
  8416919552 bytes (8417 MB, 8027 MiB) transferred 4.011s, 2098 MB/s # 以上为 dd 实时进度输出
8192+0 records in
8192+0 records out
8589934592 bytes transferred in 4.071005 secs (2110028088 bytes/sec)
```

设置交换文件的访问权限，仅允许属主读写：

```sh
# chmod 0600 /usr/swap0
```

如需立即启用，先用 `mdconfig` 将交换文件配置为内存磁盘设备，再使用 `swapon` 激活交换空间。`mdconfig` 将文件映射为内存磁盘，`swapon` 激活交换设备：

```sh
# mdconfig -a -t vnode -f /usr/swap0 -u 0 && swapon /dev/md0
```

如果要在系统重启后仍能生效，还需在 **/etc/rc.conf** 配置文件中添加以下内容：

```ini
swapfile="/usr/swap0"
```

## 使用 ZFS 卷作为交换空间

> **警告**
>
> 本节所述操作可能会影响系统崩溃转储功能。

> **警告**
>
> 根据 OpenZFS 社区文档，对内存压力极高的系统，无论交换空间剩余多少，使用 zvol 作为交换设备都可能导致系统锁死（参见 [Swap deadlock in 0.7.9](https://github.com/openzfs/zfs/issues/7734)）。而将交换空间放在其他分区又可能影响 ZFS 对交换空间的数据校验，因此上游文档建议在极端内存压力场景下谨慎使用交换空间。此外，交换空间对系统休眠功能至关重要，如果需要该功能，需至少保证交换空间的容量不小于系统内存容量。

在 ZFS 池 zroot 下创建大小为 8 GB 的 zvol（ZFS 块设备卷）用作交换空间：

```sh
# zfs create -V 8G -b $(getconf PAGESIZE) -o logbias=throughput -o sync=always -o primarycache=metadata -o com.sun:auto-snapshot=false zroot/swap
```

对上述命令的参数说明如下：

| 参数                               | 说明                                                                                                                  |
| -------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
| `$(getconf PAGESIZE)`            | 返回系统页面大小（FreeBSD/amd64 下固定为 `4096`），使交换卷与系统页面对齐，以提高性能                                                               |
| `-o`                             | 用于指定选项（option），语法为 `-o 属性名=属性值`                                                                                     |
| `-o logbias=throughput`          | ZFS 将优化同步操作，提高池的全局吞吐量并有效利用资源，可提升文件写入性能                                                                              |
| `-o sync=always`                 | 强制所有写入操作实时同步                                                                                                        |
| `-o primarycache=metadata`       | 控制 ARC 的缓存策略，仅缓存元数据，不缓存实际数据块，避免 ARC 将交换数据缓存到内存                                                                      |
| `-o com.sun:auto-snapshot=false` | ZFS 用户自定义属性，供第三方工具 zfs-auto-snapshot（需通过 Ports 安装 **filesystems/zfstools**）读取——设 `false` 将禁用该工具对此交换卷的自动快照，因交换数据无需备份 |
| `-V`                             | 用于创建 ZFS 卷（zvol）而非 ZFS 文件系统                                                                                         |

> **注意**
>
> 在 FreeBSD 中，ZFS 默认的池名称为 `zroot`，本次创建的卷名称为 `swap`。

启用 ZFS zvol 作为交换空间：

```sh
# swapon /dev/zvol/zroot/swap
```

在 **/etc/fstab** 文件中添加 ZFS zvol 交换分区的挂载项，以实现开机时自动启用：

```ini
/dev/zvol/zroot/swap none swap sw 0 0
```

写入配置后，可使用命令 `swapon -a` 检查（`-a` 表示激活 **/etc/fstab** 中所有未标记 `noauto` 的交换条目），确保无错误输出。如需停用某个交换空间（如移除设备前），可执行 `swapoff /dev/zvol/zroot/swap`，该命令会将已换出的页面迁移至其余可用交换空间或物理内存中。

### 参考文献

* OpenZFS Project. FAQ\[EB/OL]. \[2026-03-25]. <https://openzfs.github.io/openzfs-docs/Project%20and%20Community/FAQ.html>. OpenZFS 官方常见问题解答，提供 ZFS 卷配置的最佳实践。
* Oracle. Oracle Solaris ZFS 管理指南\[EB/OL]. <https://docs.oracle.com/cd/E19253-01/819-7065/givdo/index.html>. Oracle 官方 ZFS 技术文档（基于 Solaris 10，部分信息已过时，可参考 OpenZFS 官方文档 <https://openzfs.github.io/openzfs-docs/>）。
* FreeBSD Project. swapon(8) -- specify additional devices for paging and swapping\[EB/OL]. \[2026-04-17]. <https://man.freebsd.org/cgi/man.cgi?query=swapon&sektion=8>. 交换空间管理工具手册页。
* FreeBSD Project. swapinfo(8) -- display system swap space usage\[EB/OL]. \[2026-04-17]. <https://man.freebsd.org/cgi/man.cgi?query=swapinfo&sektion=8>. 交换空间使用信息查询工具手册页。

## 查看交换空间使用量

以更易读的单位显示系统交换空间信息：

```sh
# swapinfo -h
Device              Size     Used    Avail Capacity
/dev/nda0p3         2.0G       0B     2.0G     0%
```

从输出可见，**/dev/nda0p3** 为交换分区，其大小为 2 GB，当前已使用量为 0。


---

# 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/di-23-zhang-cun-chu-guan-li/di-23.4-jie-xin-zeng-jiao-huan-fen-qu.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.
