> 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-26-zhang-qi-ta-wen-jian-xi-tong/di-26.2-jie-linux-wen-jian-xi-tong.md).

# 26.2 Linux 文件系统

FreeBSD 提供了对部分 Linux 文件系统的原生支持。本节演示如何加载和挂载这些受支持的 Linux 文件系统。

## EXT 系列文件系统

从 FreeBSD 2.2 起，内核便已支持扩展文件系统（EXT）系列中的 ext2 文件系统。FreeBSD 9.0 从 NetBSD 的净室实现中合并了重要的分配代码，使 ext2fs 驱动采用 BSD 许可。FreeBSD 12.0-RELEASE 起，ext2fs 驱动原生支持以读写方式挂载 ext4 文件系统，但不支持日志记录和加密功能。

| FreeBSD 版本           | 里程碑                                       |
| -------------------- | ----------------------------------------- |
| FreeBSD 2.2          | ext2 只读支持（内核原生）                           |
| FreeBSD 9.0          | 合并 NetBSD 净室实现（BSD 许可），ext2fs 驱动采用 BSD 许可 |
| FreeBSD 12.0-RELEASE | ext4 原生读写支持（不支持日志和加密功能）                   |

> **警告**
>
> 由于 FreeBSD 的 ext2fs 驱动不支持 ext4 的日志（journal）功能，以读写模式挂载 ext4 文件系统时，若发生异常断电、系统崩溃或非正常关机，文件系统可能遭受数据损坏或元数据不一致，严重时可能导致文件系统无法挂载。请勿在存放关键数据的 ext4 卷上执行写操作，或确保设备已连接 UPS 等断电保护装置。

### 格式化磁盘为 Ext4 文件系统

需要自行安装 Port **filesystems/e2fsprogs**，其依赖提供了工具 `mke2fs` 用于格式化磁盘为 ext 格式。

* 创建 GPT 分区表

```sh
# gpart create -s gpt ada0
```

* 创建一个包含所有剩余空间的分区

```sh
# gpart add -t linux-data ada0
```

* 格式化为 Ext4

```sh
# mke2fs -t ext4 /dev/ada0p1
```

* 验证 **/dev/ada0p1** 分区使用的文件系统：

```sh
# fstyp /dev/ada0p1
ext2fs
```

### 测试挂载 Ext4 文件系统

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

```sh
$ mkdir -p /home/ykla/test
```

请根据实际路径调整，下同。

* 将分区 **/dev/ada0p1** 挂载至 **/home/ykla/test**：

```sh
# mount -t ext2fs /dev/ada0p1 /home/ykla/test
```

* 验证挂载情况

```sh
$ df -hl
Filesystem            Size    Used   Avail Capacity  Mounted on
/dev/ada0p1            20G    128M     19G     1%    /home/ykla/test

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

* 尝试读写一些文件：

```sh
# touch test
# mkdir -p test2/test2
# ls -al
total 33
drwxr-xr-x  4 root wheel  4096 May 29 17:20 .
drwxr-xr-x  3 ykla ykla     10 May 29 17:08 ..
drwx------  2 root wheel 16384 May 29 17:12 lost+found
-rw-r--r--  1 root wheel     0 May 29 17:19 test
drwxr-xr-x  3 root wheel  4096 May 29 17:20 test2
```

## Btrfs 和 XFS 文件系统

fusefs-lkl 是基于 Linux 内核库（LKL，Linux Kernel Library）的 FUSE 实现，通过在用户空间运行 Linux 内核代码，提供对 Btrfs、XFS 等 Linux 文件系统的完整支持。与 fusefs-ext2 相比，fusefs-lkl 支持的文件系统类型更广泛，但性能略低。

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

显示 **nda1** 设备的分区信息：

```sh
# gpart show -p nda1
=>      34  41942973    nda1  GPT  (20G)

			……省略无关输出……

  20971520   8388608  nda1p2  linux-data  (4.0G) # btrfs
  33554432   8386560  nda1p4  linux-data  (4.0G) # xfs
```

`-p` 参数用于显示完整设备路径。

示例分区中预先配置了文件和目录，用于后续验证挂载操作。

相关目录结构如下：

```sh
/
├── home
│   └── ykla
│       ├── btrfs                     # Btrfs 挂载点
│       └── xfs                        # XFS 挂载点
└── etc
    └── fstab                          # 持久化挂载配置文件
```

### 安装 fusefs-lkl

* 使用 pkg 安装：

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

* 或使用 Ports 安装：

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

### 加载 FUSE 内核模块

fusefs 是 FreeBSD 的 FUSE 框架内核模块，为所有基于 FUSE 的文件系统提供底层支持，执行以下命令将其添加至系统启动加载列表：

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

上述命令使配置永久生效，若要立即生效，执行：

```sh
# kldload 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
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-26-zhang-qi-ta-wen-jian-xi-tong/di-26.2-jie-linux-wen-jian-xi-tong.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.
