> 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.6-jie-ufs-ci-pan-jia-mi.md).

# 25.6 UFS 磁盘加密

## GELI 加密 GEOM 类概述

`geli` 是一种加密 GEOM 类。该控制工具支持多种加密方案，提供以下特性：

* 利用 crypto(9) 框架，并在可用时自动使用加密硬件。
* 支持多种加密算法，例如 AES-XTS、AES-CBC 和 Camellia-CBC（不建议将 CBC 模式用于新部署）。
* 可以加密根分区。系统引导时会要求输入密码来访问加密根分区。
* 可以使用两个独立的密钥。
* 执行简单的扇区到扇区的加密，因此速度很快。
* 可以备份和恢复主密钥。如果销毁了密钥，仍可恢复备份的密钥来访问数据。
* 可以使用随机的一次性密钥附加磁盘，这对交换分区和临时文件系统非常有用。

以下示例演示密钥文件的生成方法。加密提供者将挂载在 **/mnt** 下，密钥文件用于保护加密提供者的主密钥。该密钥文件提供随机数据用于加密主密钥。主密钥同时受密码保护。提供者的扇区大小设为 4kB。本示例还包含 `geli` 提供者的附加、文件系统创建、挂载、操作和卸载。

## 加载内核模块

加载 `geli` 支持模块。`geli` 支持作为可加载的内核模块提供。要配置系统在引导时自动加载该模块，将以下行添加到 **/boot/loader.conf**：

```sh
geom_eli_load="YES"
```

要立即加载内核模块：

```sh
# kldload geom_eli
```

## 生成密钥文件

生成密钥文件用于主密钥（作为磁盘元数据存储）。以下命令生成密钥文件，所有数据都将以此为凭据加密。可通过 `geli setkey` 更改用户密钥。用户密钥由密钥文件 **/root/ada0.key** 的随机字节与口令短语组合而成。本例使用 **/dev/random** 作为密钥文件的数据源：

```sh
# dd if=/dev/random of=/root/ada0.key bs=64 count=1
```

## 加密磁盘

使用 **/root/ada0.key** 加密磁盘 **ada0**：

```sh
# geli init -K /root/ada0.key -s 4096 /dev/ada0
```

将显示如下输出：

```sh
Enter new passphrase: # 设置新密码：
Reenter new passphrase: # 再次输入新密码：

Metadata backup for provider /dev/ada0 can be found in /var/backups/ada0.eli
and can be restored with the following command:

	# geli restore /var/backups/ada0.eli /dev/ada0

```

不强制同时使用密码（允许直接回车为空）和密钥文件，可单独使用任意一种方法保护主密钥。

使用生成的密钥附加提供者。要附加提供者，请指定密钥文件、磁盘名称和密码：

```sh
# geli attach -k /root/ada0.key /dev/ada0
Enter passphrase: # 输入密码：
```

这将创建扩展名为 **.eli** 的新设备 **ada0.eli**：

```sh
# ls -al /dev/ada0*
crw-r-----  1 root operator 0x6f May 18 06:17 /dev/ada0
crw-r-----  1 root operator 0x75 May 18 06:17 /dev/ada0.eli
```

## 创建新文件系统

随后使用 UFS 文件系统格式化该设备，并将其挂载到现有挂载点：

```sh
# newfs -U /dev/ada0.eli
# mount /dev/ada0.eli /mnt
```

此时可使用该加密文件系统：

```sh
# df -H
Filesystem      Size    Used   Avail Capacity  Mounted on
/dev/nda0p2      19G    1.9G    16G    11%    /
devfs            1.0k     0B    1.0k     0%    /dev
/dev/nda0p1      268M   1.4M   267M     1%    /boot/efi
/dev/ada0.eli    5.2G   8.2k   4.8G     0%    /mnt
```

完成加密分区操作后，如果不再需要 **/mnt** 分区，可卸载并将 `geli` 加密分区从内核中分离，将该设备放入冷存储：

```sh
# umount /mnt
# geli detach ada0.eli
```

## 持久化挂载

为简化引导时挂载 `geli` 加密设备的流程，系统提供了 **rc.d** 脚本。本例中将以下行添加到 **/etc/rc.conf**：

```sh
geli_devices="ada0"
geli_ada0_flags="-k /root/ada0.key"
```

这将把 **/dev/ada0** 配置为 geli 设备，并指定 **/root/ada0.key** 作为用户密钥的密钥文件。此后，文件系统将挂载，通常通过 **/etc/fstab** 中的条目挂载（新增下行）：

```ini
/dev/ada0.eli   /mnt   ufs   rw   0   0
```

系统关闭前，系统将自动将提供者从内核中分离。在引导过程中，脚本将提示输入密码，随后附加提供者。引导过程中的其他内核消息可能会在密码提示之前或之后显示。

![输入密码以挂载提供者 ada0](/files/y11KAW5rudP6axA35D9D)

如果引导过程停滞，请仔细查看其他消息中的密码提示。输入正确的密码后，将附加提供者。


---

# 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.6-jie-ufs-ci-pan-jia-mi.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.
