> 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-25-zhang-ufs-wen-jian-xi-tong/di-25.4-jie-ufs-ci-pan-kuai-zhao.md).

# 25.4 UFS 磁盘快照

FreeBSD 提供了与软更新（Soft Updates）结合使用的文件系统快照功能。UFS 快照允许用户创建指定文件系统的镜像，可作为文件处理。

快照文件必须创建在所快照的文件系统内，每个 UFS 文件系统最多可创建 20 个快照。活动快照会记录在超级块中，并在系统重启后仍然存在。不再需要某个快照时，可使用 rm(1) 删除。尽管可以按任意顺序删除快照，但由于其他快照可能占用部分已释放的块，可能无法彻底回收所有已使用的空间。创建快照时，内核会设置 `SF_SNAPSHOT` 文件标志以防止意外修改。

## 创建快照

可使用 mksnap\_ffs(8) 创建 **/** 的快照，并存储到 **/.snap/snap1**：

```sh
# mksnap_ffs /.snap/snap1
```

> **技巧**
>
> mksnap\_ffs 会自动对指定目录所在的文件系统创建快照，不支持直接指定文件系统。以上示例基于 UFS 标准安装，因此将对整个根分区创建快照。

也可以通过 mount(8) 创建快照。如果要将 **/** 的快照保存到文件 **/home/ykla/snapshot/snap**，可使用以下命令：

```sh
# mkdir -p /home/ykla/snapshot/
# mount -u -o snapshot /home/ykla/snapshot/snap /
```

UFS 快照文件的大小等于其源文件系统的大小，但实际占用空间与 ZFS 类似：

```sh
# ls -loh /home/ykla/snapshot/snap	# 逻辑占用存储
-r--------  1 root ykla snapshot   19G May  1 01:55 /home/ykla/snapshot/snap
# du -hl /home/ykla/snapshot/snap	# 真实占用存储
6.1M	/home/ykla/snapshot/snap
```

## 文件标志

`SF_SNAPSHOT` 文件标志在快照文件创建时设置（无论通过 mksnap\_ffs(8) 还是 mount(8) 的 snapshot 操作），该标志可防止对快照文件的意外修改。不过，rm(1) 对快照文件做了例外处理，即使设有 `SF_SNAPSHOT` 标志也可以删除，无需事先清除该标志。

可使用 find(1) 查找文件系统中的快照文件，例如 **/**：

```sh
# find / -flags snapshot
/.snap/snap1
/home/ykla/snapshot/snap
```

## 挂载快照

快照可作为文件系统的冻结镜像挂载。如果要以只读方式挂载快照 **/home/ykla/snapshot/snap**，可执行以下命令：

```sh
# mdconfig -a -t vnode -f /home/ykla/snapshot/snap -u 4
# mount -r /dev/md4 /mnt
# ls /mnt/
.snap		dev		libexec		rescue		var
.sujournal	entropy		media		root
COPYRIGHT	etc		mnt		sbin
bin		home		net		tmp
boot		lib		proc		usr
```

选项说明：

| 参数         | 含义           |
| ---------- | ------------ |
| `-a`       | 创建 md 设备     |
| `-t vnode` | 用文件作为磁盘      |
| `-f file`  | 指定快照文件       |
| `-u 4`     | 指定至 md4，防止冲突 |

挂载后，冻结的 **/** 即可通过 **/mnt** 访问，所有内容均保持快照创建时的状态。唯一例外是，任何更早的快照将显示为空文件。卸载快照可执行以下命令：

```sh
# umount /mnt
# mdconfig -d -u 4
```

## 参考文献

* McKusick M. McKusick.com\[EB/OL]. \[2026-04-30]. <https://www.mckusick.com/>. 有关软更新和文件系统快照的更多信息（包括技术论文），可访问 Kirk McKusick 的网站。


---

# 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-25-zhang-ufs-wen-jian-xi-tong/di-25.4-jie-ufs-ci-pan-kuai-zhao.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.
