# 22.2.快速入门指南

FreeBSD 可以在系统初始化期间挂载 ZFS 存储池和数据集。要启用此功能，请将以下行添加到 **/etc/rc.conf** 文件中：

```sh
zfs_enable="YES"
```

然后启动服务：

```sh
# service zfs start
```

本节中的示例假设有三块 SCSI 磁盘，设备名称分别为 **da0**、**da1** 和 **da2**。使用 SATA 硬件的用户应使用 **ada** 设备名称。

## 22.2.1. 单磁盘池

要使用单个磁盘设备创建一个简单的非冗余池：

```sh
# zpool create example /dev/da0
```

要查看新创建的池，可以查看 `df` 的输出：

```sh
# df
Filesystem  1K-blocks    Used    Avail Capacity  Mounted on
/dev/ad0s1a   2026030  235230  1628718    13%    /
devfs               1       1        0   100%    /dev
/dev/ad0s1d  54098308 1032846 48737598     2%    /usr
example      17547136       0 17547136     0%    /example
```

此输出显示了创建并挂载了 `example` 池，现在它作为一个文件系统可访问。为用户创建文件以供浏览：

```sh
# cd /example
# ls
# touch testfile
# ls -al
total 4
drwxr-xr-x   2 root  wheel    3 Aug 29 23:15 .
drwxr-xr-x  21 root  wheel  512 Aug 29 23:12 ..
-rw-r--r--   1 root  wheel    0 Aug 29 23:15 testfile
```

这个池还没有使用任何高级 ZFS 功能和属性。要在此池上创建一个启用压缩的数据集：

```sh
# zfs create example/compressed
# zfs set compression=gzip example/compressed
```

`example/compressed` 数据集现在是一个 ZFS 压缩文件系统。尝试将一些大文件复制到 **/example/compressed**。

要禁用压缩，请使用：

```sh
# zfs set compression=off example/compressed
```

要卸载文件系统，使用 `zfs umount` 并通过 `df` 验证：

```sh
# zfs umount example/compressed
# df
Filesystem  1K-blocks    Used    Avail Capacity  Mounted on
/dev/ad0s1a   2026030  235232  1628716    13%    /
devfs               1       1        0   100%    /dev
/dev/ad0s1d  54098308 1032864 48737580     2%    /usr
example      17547008       0 17547008     0%    /example
```

要重新挂载文件系统以使其再次可访问，使用 `zfs mount` 并通过 `df` 验证：

```sh
# zfs mount example/compressed
# df
Filesystem         1K-blocks    Used    Avail Capacity  Mounted on
/dev/ad0s1a          2026030  235234  1628714    13%    /
devfs                      1       1        0   100%    /dev
/dev/ad0s1d         54098308 1032864 48737580     2%    /usr
example             17547008       0 17547008     0%    /example
example/compressed  17547008       0 17547008     0%    /example/compressed
```

运行 `mount` 显示池和文件系统：

```sh
# mount
/dev/ad0s1a on / (ufs, local)
devfs on /dev (devfs, local)
/dev/ad0s1d on /usr (ufs, local, soft-updates)
example on /example (zfs, local)
example/compressed on /example/compressed (zfs, local)
```

创建后，像使用任何文件系统一样使用 ZFS 数据集。根据需要在每个数据集上设置其他可用的特性。下面的示例创建了一个名为 `data` 的新文件系统。假设该文件系统包含重要文件，并将其配置为存储每个数据块的两个副本。

```sh
# zfs create example/data
# zfs set copies=2 example/data
```

使用 `df` 查看数据和空间使用情况：

```sh
# df
Filesystem         1K-blocks    Used    Avail Capacity  Mounted on
/dev/ad0s1a          2026030  235234  1628714    13%    /
devfs                      1       1        0   100%    /dev
/dev/ad0s1d         54098308 1032864 48737580     2%    /usr
example             17547008       0 17547008     0%    /example
example/compressed  17547008       0 17547008     0%    /example/compressed
example/data        17547008       0 17547008     0%    /example/data
```

注意，池中的所有文件系统都具有相同的可用空间。使用 `df` 命令显示的这些示例表明，文件系统按需使用空间，并且都来自同一个池。ZFS 摒弃了卷和分区的概念，允许多个文件系统共享同一个池。

要销毁不再需要的文件系统和池：

```sh
# zfs destroy example/compressed
# zfs destroy example/data
# zpool destroy example
```

## 22.2.2. RAID-Z

磁盘会发生故障。避免磁盘故障导致数据丢失的一种方法是使用 RAID。ZFS 在其池设计中支持此功能。RAID-Z 池需要三块或更多的磁盘，但提供比镜像池更多的可用空间。

此示例创建一个 RAID-Z 池，指定要添加到池中的磁盘：

```sh
# zpool create storage raidz da0 da1 da2
```

> **注意**
>
> Sun™ 建议在 RAID-Z 配置中使用的设备数量应在三块到九块之间。对于需要单个池包含十块或更多磁盘的环境，考虑将其拆分为较小的 RAID-Z 组。如果只有两块磁盘，ZFS 镜像提供了所需的冗余。有关更多详细信息，请参阅 [zpool(8)](https://man.freebsd.org/cgi/man.cgi?query=zpool\&sektion=8\&format=html)。

上面的示例创建了名为 `storage` 的 zpool。这个示例在该池中创建一个名为 `home` 的新文件系统：

```sh
# zfs create storage/home
```

启用压缩并存储目录和文件的额外副本：

```sh
# zfs set copies=2 storage/home
# zfs set compression=gzip storage/home
```

要将其作为用户的新主目录，请将用户数据复制到此目录并创建适当的符号链接：

```sh
# cp -rp /home/* /storage/home
# rm -rf /home /usr/home
# ln -s /storage/home /home
# ln -s /storage/home /usr/home
```

用户数据现在存储在新创建的 **/storage/home** 中。通过添加一个新用户并以该用户身份登录来进行测试。

创建一个文件系统快照，以便稍后回滚：

```sh
# zfs snapshot storage/home@08-30-08
```

ZFS 创建的是数据集的快照，而不是单个目录或文件。

`@` 字符是文件系统名称或卷名称之间的分隔符。在删除重要目录之前，先备份文件系统，然后回滚到该目录仍存在的早期快照：

```sh
# zfs rollback storage/home@08-30-08
```

要列出所有可用的快照，可以在文件系统的 **.zfs/snapshot** 目录中运行 `ls`。例如，要查看刚才创建的快照：

```sh
# ls /storage/home/.zfs/snapshot
```

编写脚本定期创建用户数据的快照。随着时间的推移，快照可能会占用大量磁盘空间。使用以下命令删除先前的快照：

```sh
# zfs destroy storage/home@08-30-08
```

测试完成后，使用以下命令将 **/storage/home** 设置为真正的 **/home**：

```sh
# zfs set mountpoint=/home storage/home
```

运行 `df` 和 `mount` 确认系统现在将该文件系统视为真实的 **/home**：

```sh
# mount
/dev/ad0s1a on / (ufs, local)
devfs on /dev (devfs, local)
/dev/ad0s1d on /usr (ufs, local, soft-updates)
storage on /storage (zfs, local)
storage/home on /home (zfs, local)
# df
Filesystem   1K-blocks    Used    Avail Capacity  Mounted on
/dev/ad0s1a    2026030  235240  1628708    13%    /
devfs                1       1        0   100%    /dev
/dev/ad0s1d   54098308 1032826 48737618     2%    /usr
storage       26320512       0 26320512     0%    /storage
storage/home  26320512       0 26320512     0%    /home
```

至此，RAID-Z 配置完成。通过在 **/etc/periodic.conf** 文件中添加以下行，向每晚的 [periodic(8)](https://man.freebsd.org/cgi/man.cgi?query=periodic\&sektion=8\&format=html) 运行添加文件系统的每日状态更新：

```sh
daily_status_zfs_enable="YES"
```

## 22.2.3. 恢复 RAID-Z

每个软件 RAID 都有一种方法来监控其 `状态`。使用以下命令查看 RAID-Z 设备的状态：

```sh
# zpool status -x
```

如果所有池都是 [在线](https://docs.freebsd.org/en/books/handbook/zfs/#zfs-term-online)，且一切正常，消息会显示：

```sh
all pools are healthy
```

如果出现问题，可能是磁盘处于 [离线](https://docs.freebsd.org/en/books/handbook/zfs/#zfs-term-offline) 状态，池的状态会显示如下：

```sh
pool: storage
 state: DEGRADED
status: One or more devices has been taken offline by the administrator.
	Sufficient replicas exist for the pool to continue functioning in a
	degraded state.
action: Online the device using 'zpool online' or replace the device with
	'zpool replace'.
 scrub: none requested
config:

	NAME        STATE     READ WRITE CKSUM
	storage     DEGRADED     0     0     0
	  raidz1    DEGRADED     0     0     0
	    da0     ONLINE       0     0     0
	    da1     OFFLINE      0     0     0
	    da2     ONLINE       0     0     0

errors: No known data errors
```

"OFFLINE" 显示管理员通过以下命令将 **da1** 设为离线：

```sh
# zpool offline storage da1
```

现在关机并替换 **da1**。开机后将 **da1** 返回池中：

```sh
# zpool replace storage da1
```

接下来，再次检查状态，这次不带 `-x` 选项以显示所有池：

```sh
# zpool status storage
 pool: storage
 state: ONLINE
 scrub: resilver completed with 0 errors on Sat Aug 30 19:44:11 2008
config:

	NAME        STATE     READ WRITE CKSUM
	storage     ONLINE       0     0     0
	  raidz1    ONLINE       0     0     0
	    da0     ONLINE       0     0     0
	    da1     ONLINE       0     0     0
	    da2     ONLINE       0     0     0

errors: No known data errors
```

在此示例中，一切正常。

## 22.2.4. 数据验证

ZFS 使用校验和来验证存储数据的完整性。创建文件系统时会自动启用校验和。

> **警告**
>
> 可以禁用校验和，但 *不* 推荐！校验和占用的存储空间很少，但能提供数据完整性。大多数 ZFS 特性在禁用校验和的情况下无法正常工作。禁用这些校验和不会明显提高性能。

验证数据校验和（称为​*扫描*​）可以确保 `storage` 池的完整性，命令如下：

```sh
# zpool scrub storage
```

扫描的持续时间取决于存储的数据量。数据量越大，验证所需的时间就越长。由于扫描是 I/O 密集型操作，ZFS 允许每次只能运行一个扫描。扫描完成后，使用 `zpool status` 查看状态：

```sh
# zpool status storage
 pool: storage
 state: ONLINE
 scrub: scrub completed with 0 errors on Sat Jan 26 19:57:37 2013
config:

	NAME        STATE     READ WRITE CKSUM
	storage     ONLINE       0     0     0
	  raidz1    ONLINE       0     0     0
	    da0     ONLINE       0     0     0
	    da1     ONLINE       0     0     0
	    da2     ONLINE       0     0     0

errors: No known data errors
```

显示上次扫描完成日期有助于决定何时开始另一次扫描。定期扫描有助于保护数据免受静默损坏，并确保池的完整性。

有关其他 ZFS 选项，请参阅 [zfs(8)](https://man.freebsd.org/cgi/man.cgi?query=zfs\&sektion=8\&format=html) 和 [zpool(8)](https://man.freebsd.org/cgi/man.cgi?query=zpool\&sektion=8\&format=html)。


---

# 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-22-zhang-z-wen-jian-xi-tong-zfs/22.2.-kuai-su-ru-men-zhi-nan.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.
