# 28.4.USB 虚拟存储设备

> **注意**
>
> [cfumass(4)](https://man.freebsd.org/cgi/man.cgi?query=cfumass\&sektion=4\&format=html) 驱动程序是一个 USB 设备模式驱动程序，首次在 FreeBSD 12.0 中提供。

大容量存储目标由模板 0 和 10 提供。必须加载 [usb\_template(4)](https://man.freebsd.org/cgi/man.cgi?query=usb_template\&sektion=4\&format=html) 和 [cfumass(4)](https://man.freebsd.org/cgi/man.cgi?query=cfumass\&sektion=4\&format=html) 内核模块。[cfumass(4)](https://man.freebsd.org/cgi/man.cgi?query=cfumass\&sektion=4\&format=html) 接口连接到 CTL 子系统，该子系统与用于 iSCSI 或光纤通道目标的子系统相同。在主机端，USB 大容量存储发起者只能访问单个 LUN，LUN 0。

## 28.4.1. 使用 cfumass 启动脚本配置 USB 存储设备目标

最简单的设置只读 USB 存储目标的方法是使用 **cfumass** rc 脚本。要通过这种方式配置，请将要提供给 USB 主机计算机的文件复制到 `/var/cfumass` 目录，并在 **/etc/rc.conf** 中添加以下行：

```ini
cfumass_enable="YES"
```

要在不重启的情况下配置目标，运行以下命令：

```sh
# service cfumass start
```

与串口和网络功能不同，模板不应设置为 0 或 10，因为在设置模板之前，LUN 必须先设置。**cfumass** 启动脚本会在启动时自动设置正确的模板编号。

## 28.4.2. 使用其他方式配置 USB 存储

本章其余部分详细描述了如何在不使用 cfumass rc 文件的情况下设置目标。如果例如想提供可写的 LUN，这是必要的。

USB 存储设备模式不需要运行 [ctld(8)](https://man.freebsd.org/cgi/man.cgi?query=ctld\&sektion=8\&format=html) 守护进程，尽管如果需要，可以使用它。这与 iSCSI 不同。因此，配置目标有两种方式：使用 [ctladm(8)](https://man.freebsd.org/cgi/man.cgi?query=ctladm\&sektion=8\&format=html) 或 [ctld(8)](https://man.freebsd.org/cgi/man.cgi?query=ctld\&sektion=8\&format=html)。两者都需要加载 **cfumass.ko** 内核模块。可以手动加载模块：

```sh
# kldload cfumass
```

如果 **cfumass.ko** 没有被编译到内核中，可以通过在 **/boot/loader.conf** 中设置来使其在启动时加载：

```sh
cfumass_load="YES"
```

可以通过以下命令创建一个 LUN 而不需要 [ctld(8)](https://man.freebsd.org/cgi/man.cgi?query=ctld\&sektion=8\&format=html) 守护进程：

```sh
# ctladm create -b block -o file=/data/target0
```

这将 **/data/target0** 的内容作为 LUN 提供给 USB 主机。该文件必须在执行命令之前存在。要在系统启动时配置 LUN，请将命令添加到 **/etc/rc.local**。

[ctld(8)](https://man.freebsd.org/cgi/man.cgi?query=ctld\&sektion=8\&format=html) 也可以用来管理 LUN。创建 **/etc/ctl.conf** 文件，向 **/etc/rc.conf** 中添加一行以确保 [ctld(8)](https://man.freebsd.org/cgi/man.cgi?query=ctld\&sektion=8\&format=html) 在启动时自动启动，然后启动该守护进程。

这是一个简单的 **/etc/ctl.conf** 配置文件的示例。有关选项的完整描述，请参考 [ctl.conf(5)](https://man.freebsd.org/cgi/man.cgi?query=ctl.conf\&sektion=5\&format=html)。

```ini
target naa.50015178f369f092 {
	lun 0 {
		path /data/target0
		size 4G
	}
}
```

此示例创建了一个带有单个 LUN 的目标。`naa.50015178f369f092` 是一个由 32 个随机十六进制数字组成的设备标识符。`path` 行定义了支持 LUN 的文件或 zvol 的完整路径。该文件必须在启动 [ctld(8)](https://man.freebsd.org/cgi/man.cgi?query=ctld\&sektion=8\&format=html) 之前存在。第二行是可选的，指定了 LUN 的大小。

要确保 [ctld(8)](https://man.freebsd.org/cgi/man.cgi?query=ctld\&sektion=8\&format=html) 守护进程在启动时启动，请在 **/etc/rc.conf** 中添加以下行：

```ini
ctld_enable="YES"
```

要立即启动 [ctld(8)](https://man.freebsd.org/cgi/man.cgi?query=ctld\&sektion=8\&format=html) 守护进程，运行以下命令：

```sh
# service ctld start
```

当 [ctld(8)](https://man.freebsd.org/cgi/man.cgi?query=ctld\&sektion=8\&format=html) 守护进程启动时，它会读取 **/etc/ctl.conf** 文件。如果此文件在守护进程启动后被编辑，请重新加载更改以立即生效：

```sh
# service ctld reload
```


---

# 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-28-zhang-usb-she-bei-mo-shi-usb-otg/28.4.-usb-xu-ni-cun-chu-she-bei.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.
