Skip to content

Commit e896ca5

Browse files
committed
ops/filesystem: btrfs filesystem defrag, btrfs-convert, btrfs dedup
1 parent 9b96e8f commit e896ca5

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

docs/ops/storage/filesystem.md

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ test.img3 2623488 16775167 14151680 6.7G Linux filesystem
552552
| exFAT | :fontawesome-solid-check:{: .limegreen } (Linux 5.4+) | :fontawesome-solid-check:{: .limegreen } | :fontawesome-solid-check:{: .limegreen } | 应仅用于不同操作系统交换文件。不支持日志。 |
553553
| [ext4](https://wiki.archlinux.org/title/Ext4) | :fontawesome-solid-check:{: .limegreen } | :fontawesome-solid-xmark:{: .orangered } | :fontawesome-solid-xmark:{: .orangered } | Linux 上最常见的文件系统。 |
554554
| [XFS](https://wiki.archlinux.org/title/XFS) | :fontawesome-solid-check:{: .limegreen } | :fontawesome-solid-xmark:{: .orangered } | :fontawesome-solid-xmark:{: .orangered } | 适用于大文件、大容量的场合。无法随意缩小[^xfs_growfs]|
555-
| ReiserFS | :fontawesome-solid-check:{: .orangered } (deprecated) | :fontawesome-solid-xmark:{: .orangered } | :fontawesome-solid-xmark:{: .orangered } | 适用于存储大量小文件的场合。由于内核主线已经考虑移除支持,如有存储大量小文件需求,可能需要使用其他方案替代。 |
555+
| ReiserFS | :fontawesome-solid-check:{: .orangered } (Linux 2.4.1 ~ 6.12) | :fontawesome-solid-xmark:{: .orangered } | :fontawesome-solid-xmark:{: .orangered } | 适用于存储大量小文件的场合。由于内核最新主线已经移除支持,如有存储大量小文件需求,可能需要使用其他方案替代。 |
556556
| [Btrfs](https://wiki.archlinux.org/title/Btrfs) | :fontawesome-solid-check:{: .limegreen } | :fontawesome-solid-xmark:{: .orangered } | :fontawesome-solid-xmark:{: .orangered } | 内置于 Linux 内核的新一代的 CoW 文件系统,支持快照、透明压缩等高级功能。RAID 5/6 支持不稳定,也有对整体稳定性的争议。 |
557557
| [Bcachefs](https://wiki.archlinux.org/title/Bcachefs) | :fontawesome-solid-check:{: .orangered } (Linux 6.7 ~ 6.17) | :fontawesome-solid-xmark:{: .orangered } | :fontawesome-solid-xmark:{: .orangered } | 基于 [Bcache](https://wiki.archlinux.org/title/Bcache) 的新一代 CoW 文件系统,旨在用更简洁的代码实现 Btrfs 和 ZFS 的功能,采用与 GPL 兼容的许可证。由于与内核社区协作方式的冲突,Bcachefs 在 Linux 6.17 中被标记为「外部维护」,并从下一个版本开始被移除出内核。 |
558558
| [ZFS](https://wiki.archlinux.org/title/ZFS) | :fontawesome-solid-check:{: .limegreen }(需要 kernel module) | :fontawesome-solid-xmark:{: .orangered } | :fontawesome-solid-check:{: .limegreen }[^zfs-win] | 起源于 Solaris 的 CoW 文件系统,适用于存储大量文件、需要高级功能的场合。需要额外的内存和 CPU 资源。 |
@@ -927,7 +927,18 @@ zstd 27% 105G 384G 413G
927927
prealloc 100% 643M 643M 1.0G
928928
```
929929

930-
可以看到,透明压缩特性为该用户节省了 200G 的磁盘空间。
930+
可以看到,透明压缩特性为该用户节省了 200G 的磁盘空间。如果需要给已有的 Btrfs 文件系统上的文件全部重新压缩(例如设置了新的压缩等级,或者给从 ext4 转换到 Btrfs 后的文件系统中的文件做压缩),可以使用碎片整理功能:
931+
932+
```shell
933+
# 以 zstd:3 重新压缩(设置等级需要 6.15 及以上内核)
934+
# 如果不设置 -L,在新内核下压缩等级为 0,在老内核下压缩等级为挂载时的等级
935+
# https://github.com/torvalds/linux/commit/fc5c0c5825874859069ac44c367c724acd7190fb
936+
btrfs filesystem defrag -v -r -czstd -L3 /path/to/btrfs
937+
# 以 zstd 重新压缩(较老的内核)
938+
btrfs filesystem defrag -v -r -czstd /path/to/btrfs
939+
```
940+
941+
注意 defrag 会破坏 reflink 关系——可能会导致占用空间提升。
931942

932943
#### 引用复制 {#btrfs-reflink}
933944

@@ -937,7 +948,37 @@ Btrfs 支持引用复制(reflink)功能,在复制文件时可以共享数
937948
cp -a --reflink=always /path/to/source /path/to/destination
938949
```
939950

940-
<!-- TODO: btrfs 去重功能介绍? -->
951+
#### 从其他文件系统转换到 Btrfs {#btrfs-convert}
952+
953+
[btrfs-convert.8][btrfs-convert.8] 提供了从 ext2/3/4 和 ReiserFS 文件系统离线原地转换为 Btrfs 的功能。在备份数据、解除挂载、使用 fsck 确认文件系统无问题后,即可开始转换:
954+
955+
```shell
956+
btrfs-convert /path/to/device
957+
```
958+
959+
如果文件数量较多,或者文件大小较大的话,需要耐心等待。在转换后,数据会在 top-level subvolume 中。
960+
961+
!!! warning "计算 checksum 的时间开销"
962+
963+
`btrfs-convert` 在运行时会将原来的文件系统的 image 放在单独的 subvolume 里面(例如 ext 系列是 `ext2_saved`,用来之后提供恢复到旧文件系统的功能),并在 Btrfs 文件系统元数据中为每个文件添加对应的 inode。由于 Btrfs 支持数据 checksum,`btrfs-convert` 会花大量的时间计算哈希,而且很不幸的是,这个过程目前不是并行的。这可能会导致转换的大部分时间都卡在 `Create ext2 image file`,给 `ext2_saved` 计算哈希(而可能你之后完全不会使用它)。
964+
965+
尽管 `btrfs-convert` 提供了 `--no-datasum` 参数,但是这个参数也会把普通文件的数据 checksum 关闭,只有在创建了新 inode 之后才能恢复。因此对规模较大的场景,如果想节约转换的时间,可能直接格式化然后从备份复制过来会更快一些。
966+
967+
在确定不需要回滚之后,可以删除 `ext2_saved` subvolume,并进行碎片整理(defrag)和对元数据 balance 操作来优化数据排布。详情可参考 btrfs-convert 手册。另外别忘了修改 `/etc/fstab` 哦!
968+
969+
#### 去重 {#btrfs-dedup}
970+
971+
相比于 [ZFS](./zfs.md) 的去重,Btrfs 需要依赖第三方工具扫描重复项,并将相关信息提交到内核处理。最常用的工具是 [duperemove](https://github.com/markfasheh/duperemove)[bees](https://github.com/Zygo/bees)。前者适用于定时或者手动对 Btrfs(也支持 XFS)执行,而后者是专为 Btrfs 编写的 daemon,会在后台执行自动去重。可以根据实际工作负载的需求选择合适的方案。以 duperemove 为例:
972+
973+
```shell
974+
duperemove --hashfile=example.hashfile -dhr /path/to/btrfs
975+
```
976+
977+
即可扫描 `/path/to/btrfs` 下的文件,搜索重复的块并压缩,并将相关文件的哈希存储到 `example.hashfile` 中,在下次去重的时候加速。
978+
979+
!!! note "其他文件系统的去重"
980+
981+
除了 ZFS 自带去重功能,duperemove 支持 XFS 文件系统(因为 XFS 也提供了 `FIDEDUPRANGE` 的内核接口)以外,其他文件系统可以使用 [jdupes](https://codeberg.org/jbruchon/jdupes),搜索相同的文件,并且做 reflink 或者 hardlink。
941982

942983
#### 常见问题 {#btrfs-faq}
943984

includes/man.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<!-- Note: Debian man pages for useradd(8)/userdel(8) mentions the "low-level" feature and recommends adduser(8)/deluser(8) instead.
9595
Do not link to a "generic" man page for these commands -->
9696
[adduser.8]: https://manpages.debian.org/stable/adduser/adduser.8.en.html
97+
[btrfs-convert.8]: https://man7.org/linux/man-pages/man8/btrfs-convert.8.html
9798
<!-- None of Debian, man7.org or linux.die.net provides conntrack(8), weird -->
9899
[conntrack.8]: https://man.archlinux.org/man/conntrack.8.en
99100
[deluser.8]: https://manpages.debian.org/stable/adduser/deluser.8.en.html

0 commit comments

Comments
 (0)