# 21.8.通过 GEOM 实现 UFS 日志

FreeBSD 支持 UFS 文件系统的日志功能。该实现通过 GEOM 子系统提供，并通过 `gjournal` 配置。与其他文件系统日志实现不同，`gjournal` 方法是基于块的，而不是作为文件系统的一部分实现的。它是 GEOM 的扩展。

日志记录文件系统事务，例如在元数据和文件写入被提交到磁盘之前，完成的磁盘写操作的变更。这个事务日志可以在之后被回放，以重新执行文件系统事务，从而防止文件系统不一致性。

该方法提供了另一种保护数据丢失和文件系统不一致性的方法。与 Soft Updates（跟踪并强制执行元数据更新）和快照（创建文件系统的镜像）不同，日志存储在专门为此任务分配的磁盘空间中。为了获得更好的性能，日志可以存储在另一个磁盘上。在这种配置中，日志提供者或存储设备应列在启用日志功能的设备之后。

**GENERIC** 内核提供对 `gjournal` 的支持。为了在启动时自动加载 **geom\_journal.ko** 内核模块，可以将以下行添加到 **/boot/loader.conf**：

```sh
geom_journal_load="YES"
```

如果使用自定义内核，确保在内核配置文件中包含以下行：

```ini
options GEOM_JOURNAL
```

模块加载后，可以按照以下步骤在新文件系统上创建日志。在此示例中，**da4** 是一个新的 SCSI 磁盘：

```sh
# gjournal load
# gjournal label /dev/da4
```

这将加载模块并在 **/dev/da4** 上创建 **/dev/da4.journal** 设备节点。

现在可以在日志设备上创建 UFS 文件系统，并将其挂载到现有的挂载点：

```sh
# newfs -O 2 -J /dev/da4.journal
# mount /dev/da4.journal /mnt
```

> **注意**
>
> 如果有多个分区，则为每个分区创建一个日志。例如，如果 **ad4s1** 和 **ad4s2** 是两个分区，则 `gjournal` 会分别创建 **ad4s1.journal** 和 **ad4s2.journal**。

还可以通过使用 `tunefs` 在当前文件系统上启用日志功能。然而，在尝试修改现有文件系统之前，*始终* 要进行备份。在大多数情况下，如果 `gjournal` 无法创建日志，它将失败，但这并不防止由于错误使用 `tunefs` 而导致的数据丢失。有关这些命令的更多信息，请参考 [gjournal(8)](https://man.freebsd.org/cgi/man.cgi?query=gjournal\&sektion=8\&format=html) 和 [tunefs(8)](https://man.freebsd.org/cgi/man.cgi?query=tunefs\&sektion=8\&format=html) 手册。

也可以对 FreeBSD 系统的引导磁盘启用日志功能。有关详细说明，请参考文章 [在桌面 PC 上实现 UFS 日志功能](https://docs.freebsd.org/en/articles/gjournal-desktop/)。


---

# Agent Instructions: 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:

```
GET https://handbook.bsdcn.org/di-21-zhang-geom-mo-kuai-hua-ci-pan-zhuan-huan-kuang-jia/21.8.-tong-guo-geom-shi-xian-ufs-ri-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
