> 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-23-zhang-cun-chu-guan-li/di-23.1-jie-usb-cun-chu-she-bei.md).

# 23.1 USB 存储设备

许多外部存储解决方案，如硬盘、USB 闪存设备、CD 和 DVD 刻录机，都使用通用串行总线（USB）。FreeBSD 支持 USB 1.x、2.0 和 3.0 设备。

FreeBSD 使用 umass(4) 驱动程序，通过 SCSI 子系统访问 USB 存储设备。系统会将 USB 设备视为 SCSI 设备。

USB 存储设备在 FreeBSD 中的识别路径：

```sh
USB 存储设备在 FreeBSD 中的识别路径：

  USB 设备物理插入（Kingston DataTraveler 等）
     │
     ▼
  USB 总线（usbus，USB 1.x / 2.0 / 3.0）
     │
     ▼
  umass(4) 驱动层
     USB 大容量存储驱动
     │
     ▼
  CAM/SCSI 子系统
     将 USB 设备映射为 SCSI 设备
     scbus → target → lun
     │
     ▼
  da(4) 设备节点
     /dev/da0（块设备）
     /dev/da0p1（第一个分区）
     │
     ▼
  挂载点
     /mnt（mount -t msdosfs ...）
```

本节介绍如何验证 USB 存储设备是否被 FreeBSD 识别，并配置该设备以便使用。

## 设备配置

要测试 USB 配置，插入 USB 设备。使用 `dmesg` 命令确认驱动器出现在系统消息缓冲区中，输出类似如下：

```sh
ugen2.2: <Kingston DataTraveler 3.0> at usbus2
umass0 on uhub3
umass0: <Kingston DataTraveler 3.0, class 0/0, rev 3.20/1.10, addr 1> on usbus2
umass0:  SCSI over Bulk-Only; quirks = 0xc000<NO_SYNCHRONIZE_CACHE,NO_PREVENT_ALLOW>
umass0:3:0: Attached to scbus3
(probe0:umass-sim0:0:0:0): REPORT LUNS. CDB: a0 00 00 00 00 00 00 00 00 10 00 00
(probe0:umass-sim0:0:0:0): CAM status: SCSI Status Error
(probe0:umass-sim0:0:0:0): SCSI status: Check Condition
(probe0:umass-sim0:0:0:0): SCSI sense: ILLEGAL REQUEST asc:20,0 (Invalid command operation code)
(probe0:umass-sim0:0:0:0): Error 22, Unretryable error
da0 at umass-sim0 bus 0 scbus3 target 0 lun 0
da0: <Kingston DataTraveler 3.0 0000> Removable Direct Access SPC-4 SCSI device
da0: Serial Number E0D55EA5359C1750989B07F6
da0: 400.000MB/s transfers
da0: 59120MB (121077761 512 byte sectors)
da0: quirks=0x2<NO_6_BYTE>
```

品牌（**Kingston DataTraveler 3.0**）、设备节点（**da0**）、速度（**400.000MB/s**）和容量（**59120MB**）因设备而异。

由于系统将 USB 设备视为 SCSI 设备，可以使用 `camcontrol` 命令列出连接到系统的 USB 存储设备：

```sh
# camcontrol devlist
<Kingston DataTraveler 3.0 0000>   at scbus3 target 0 lun 0 (da0,pass3)

……省略其他无关输出……
```

也可以使用 `usbconfig` 列出设备。

```sh
# usbconfig
ugen2.2: <DataTraveler 100 G3/G4/SE9 G2/50 Kyson Kingston Technology> at usbus2, cfg=0 md=HOST spd=SUPER (5.0Gbps) pwr=ON (36mA)

……省略其他无关输出……
```

## 挂载 U 盘

上述输出表明，USB 设备已被识别为 **/dev/da0**。如果该设备包含分区表，其第一个分区通常为 **/dev/da0p1**。

如果该设备已格式化为 FAT32 文件系统，用户可以使用以下命令将其挂载至 **/mnt**：

```sh
# mount -t msdosfs /dev/da0p1 /mnt
```

拔出设备前，必须先卸载：

```sh
# umount /mnt
```

物理拔出设备后，系统消息缓冲区将显示类似如下内容的消息：

```sh
ugen2.2: <Kingston DataTraveler 3.0> at usbus2 (disconnected)
umass0: at uhub3, port 1, addr 1 (disconnected)
da0 at umass-sim0 bus 0 scbus3 target 0 lun 0
da0: <Kingston DataTraveler 3.0 0000>  s/n E0D55EA5359C1750989B07F6 detached
(da0:umass-sim0:0:0:0): Periph destroyed
umass0: detached
```

## 作为普通用户挂载 USB 设备

为使普通用户能够挂载设备，可将所有使用该设备的用户添加到 `operator` 组中。接下来，确保 `operator` 组能够读写设备，可以在 **/etc/devfs.rules** 中添加以下行：

```ini
[localrules=5]
add path 'da*' mode 0660 group operator
```

> **注意**
>
> 如果系统中还安装了其他内置 SCSI 磁盘，请将其修改如下：
>
> ```ini
> [localrules=5]
> add path 'da[2-9]*' mode 0660 group operator
> ```
>
> 这将把前 2 块 SCSI 磁盘（**da0** 到 **da1**）排除出 `operator` 组。请将 **2** 替换为内置 SCSI 磁盘的实际数量。

接下来，在 **/etc/rc.conf** 中启用规则集：

```ini
devfs_system_ruleset="localrules"
```

此后，通过在 **/etc/sysctl.conf** 中添加以下行，要求系统允许普通用户挂载文件系统：

```ini
vfs.usermount=1
```

> **警告**
>
> 虽然启用 `vfs.usermount` 会让不受信任的用户挂载任意介质，从安全角度而言并不可取。大多数文件系统并非为防范恶意设备而设计。

此设置仅在下次重启后生效。

最后创建一个目录，在该目录下挂载文件系统。该目录的属主应为将要挂载文件系统的用户。可用 `root` 创建一个由该用户拥有的子目录，如 **/mnt/username**。在以下示例中，将 **用户名** 替换为用户名，将 **用户组** 替换为用户主组：

```sh
# mkdir /mnt/username
# chown 用户名:用户组 /mnt/username
```

此后，普通用户即可挂载 USB 存储设备 **/dev/da0p1**：

```sh
$ mount -t msdosfs -o -m=644,-M=755 /dev/da0p1 /mnt/username
```


---

# 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-23-zhang-cun-chu-guan-li/di-23.1-jie-usb-cun-chu-she-bei.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.
