# 15.3 Linux 文件系统

FreeBSD 提供了对若干 Linux 文件系统的内建支持。本节将演示如何加载这些受支持的 Linux 文件系统，以及如何挂载它们。

为便于演示挂载操作，本节预设一个包含多种 Linux 文件系统分区的示例环境，用于验证各挂载方法的正确性。

```sh
# gpart show -p nda1        # 显示设备 nda1 的分区信息
=>      34  41942973    nda1  GPT  (20G)
        34      2014          - free -  (1.0M)
      2048   1339392  nda1p5  linux-data  (654M) # ext2
   1341440  19630080  nda1p1  linux-data  (9.4G) # ext4
  20971520   8388608  nda1p2  linux-data  (4.0G) # btrfs
  29360128   4194304  nda1p3  ms-basic-data  (2.0G) # fat32
  33554432   8386560  nda1p4  linux-data  (4.0G) # xfs
  41940992      2015          - free -  (1.0M)
```

`-p` 参数用于显示完整设备路径。示例分区中预先配置了一些文件和目录，用于后续验证挂载操作的正确性。

目录结构如下：

```sh
/
├── usr
│   └── ports
│       └── filesystems
│           ├── ext2                  # fusefs-ext2 Ports 目录
│           └── lkl                   # fusefs-lkl Ports 目录
├── home
│   └── ykla
│       ├── test                      # EXT2/3/4 挂载点
│       ├── btrfs                     # Btrfs 挂载点
│       └── xfs                        # XFS 挂载点
└── etc
    └── fstab                          # 持久化挂载配置文件
```

## EXT 系列文件系统

fusefs-ext2 是一款基于 FUSE 的文件系统实现，尽管名称为 ext2，但该模块通过兼容 ext3/ext4 的磁盘格式，同样支持对 ext3 和 ext4 文件系统的访问。

### 安装 fusefs-ext2

* 使用 pkg 安装：

```sh
# pkg install fusefs-ext2
```

* 或使用 Ports 安装：

```sh
# cd /usr/ports/filesystems/ext2/
# make install clean
```

### 加载内核模块

首先，确保已加载 FUSE 内核模块，该模块为所有基于 FUSE 的文件系统提供底层支持。将 ext2fs 和 fusefs 内核模块添加至开机加载列表：

```sh
# sysrc kld_list+="fusefs"
# sysrc kld_list+="ext2fs"
```

### 测试挂载 Ext 分区

* 创建挂载目录 **/home/ykla/test**：

```sh
$ mkdir -p /home/ykla/test # 创建示例挂载目录，请根据实际路径调整，下同
```

* 将 `nda1p1` 分区挂载至 **/home/ykla/test**：

```sh
# fuse-ext2 /dev/nda1p1 /home/ykla/test # 挂载分区，默认只读
Mounting /dev/nda1p1 Read-Only.
Use 'force' or 'rw+' options to enable Read-Write mode
# ls /home/ykla/test/ # 列出挂载目录内容
lost+found	test		test.pdf
```

fusefs-ext2 默认以只读模式挂载，这是为了避免写入操作可能引发的文件系统损坏风险。

* 将 `nda1p1` 分区以读写模式挂载至 **/home/ykla/test**：

```sh
# fuse-ext2 -o rw+ /dev/nda1p1 /home/ykla/test # 挂载磁盘分区为可写
# ls /home/ykla/test/ # 查看挂载目录内容
lost+found	test		test.pdf
# cd /home/ykla/test/ # 切换到挂载路径
root@ykla:/home/ykla/test # touch my.txt # 创建测试文件以验证写入操作
root@ykla:/home/ykla/test # ls # 列出挂载目录内容
lost+found	my.txt		test		test.pdf
```

## Btrfs、XFS 和 ext4 文件系统

fusefs-lkl 是基于 Linux 内核库（LKL，Linux Kernel Library）的 FUSE 实现，通过在用户空间运行 Linux 内核代码，提供对 Btrfs、XFS 等更多 Linux 文件系统的完整支持。与 fusefs-ext2 相比，fusefs-lkl 支持的文件系统类型更广泛，但性能可能略低。此外，FreeBSD 内核原生支持以只读方式挂载 ext2/ext3/ext4（通过 `mount -t ext2fs`），无需安装第三方软件即可直接访问。

### 安装 fusefs-lkl

* 使用 pkg 安装：

```sh
# pkg install fusefs-lkl
```

* 或使用 Ports 安装：

```sh
# cd /usr/ports/filesystems/lkl/
# make install clean
```

### 加载 FUSE 内核模块

fusefs 是 FreeBSD 的 FUSE 框架内核模块，为所有基于 FUSE 的文件系统提供底层支持。在安装 fusefs-ext2 时已加载该模块，如果尚未加载，可执行以下命令：

```sh
# sysrc kld_list+="fusefs"
```

### 测试挂载 Btrfs 分区

使用 `lklfuse` 将 Btrfs 分区 `nda1p2` 挂载至 **/home/ykla/btrfs**：

```sh
# mkdir -p /home/ykla/btrfs # 创建挂载目录
# lklfuse -o type=btrfs /dev/nda1p2 /home/ykla/btrfs # 挂载磁盘分区
# ls /home/ykla/btrfs # 查看挂载目录内容
test1	test2	test3	test4
```

### 测试挂载 XFS 分区

使用 `lklfuse` 将 XFS 分区 `nda1p4` 挂载至 **/home/ykla/xfs**：

```sh
# mkdir -p /home/ykla/xfs # 创建挂载目录
# lklfuse -o type=xfs /dev/nda1p4 /home/ykla/xfs # 挂载磁盘分区
# ls /home/ykla/xfs # 查看挂载目录内容
cfc	test1	test2
```

## 故障排除与未竟事宜

### 文件系统的卸载方法

文件系统卸载操作可通过 `umount` 命令完成，卸载前应确保没有进程正在访问挂载点，否则卸载将失败。若挂载点被占用，可使用 `umount -f` 强制卸载（可能导致正在访问该文件系统的应用程序出现未定义行为）。其基本语法为：

```sh
# umount /home/ykla/test    # 卸载指定挂载点的文件系统
```

### 持久化的挂载配置

持久化挂载可将挂载配置写入 **/etc/fstab** 文件。对 FUSE 文件系统，需特别注意挂载参数与延迟挂载选项 `late` 的使用——`late` 确保文件系统在基本系统启动完成后才挂载，避免因 FUSE 依赖尚未就绪而导致启动失败。

## 参考文献

* FreeBSD Forums. mount linux ext4 drives on Freebsd\[EB/OL]. (2020-03-10)\[2026-03-25]. <https://forums.freebsd.org/threads/mount-linux-ext4-drives-on-freebsd.74414/>. FreeBSD 论坛讨论文章，详述 fusefs-ext2 在 FreeBSD 上挂载 EXT4 文件系统的实践方案。
* FreeBSD Forums. XFS support\[EB/OL]. \[2026-03-25]. <https://forums.freebsd.org/threads/xfs-support.61449/>. FreeBSD 论坛 XFS 支持讨论，探讨 XFS 文件系统在 FreeBSD 上的兼容性与挂载方法。
* FreeBSD Project. fusefs -- Filesystem in Userspace\[EB/OL]. \[2026-04-14]. <https://man.freebsd.org/cgi/man.cgi?query=fusefs&sektion=4>. FUSE 内核接口手册页，描述用户空间文件系统框架。
* FreeBSD Project. ext2fs -- ext2/ext3/ext4 file system\[EB/OL]. \[2026-04-14]. <https://man.freebsd.org/cgi/man.cgi?query=ext2fs&sektion=4>. ext2/ext3/ext4 文件系统手册页，描述 Linux 文件系统在 FreeBSD 上的挂载支持。
* FreeBSD Project. mount -- mount file systems\[EB/OL]. \[2026-04-14]. <https://man.freebsd.org/cgi/man.cgi?query=mount&sektion=8>. 文件系统挂载命令手册页。
* FreeBSD Project. fstab -- static information about the filesystems\[EB/OL]. \[2026-04-14]. <https://man.freebsd.org/cgi/man.cgi?query=fstab&sektion=5>. 文件系统表配置文件格式手册页。


---

# 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://book.bsdcn.org/di-15-zhang-cun-chu-yu-wen-jian-xi-tong-guan-li/di-15.3-jie-linux-wen-jian-xi-tong.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.
