Btrfs Snapshot Integration in KDE

Btrfs Snapshot Integration in KDE

KDE 中的 Btrfs 快照集成

20th of August, 2026 2026 年 8 月 20 日

I have been working on integrating Btrfs snapshots into KDE software. The central part of this work has been realized in the form of KIO Snapshot, which has just been released. Here I want to discuss what it is, how it works, how it was developed, and the surrounding work across KDE. 我一直致力于将 Btrfs 快照集成到 KDE 软件中。这项工作的核心部分已通过刚刚发布的 KIO Snapshot 实现。在此,我想讨论一下它是什么、它是如何工作的、它是如何开发的,以及围绕 KDE 进行的相关工作。

Background

背景

Among the key features of Btrfs is the ability to take efficient snapshots of subvolumes (Subvolumes are independently manageable directory trees within your filesystem. You can snapshot or rollback a subvolume atomically and independently from the rest of your filesystem. Snapshots are just special cases of subvolumes.). They are efficient because Btrfs makes snapshots share file extents with the originals, so snapshots only take up additional space where they differ from the original. Thus it is cheap to take snapshots frequently without worrying about disk space. This can be used to build very handy universal “undo” or “time travel” functionality for the user. Yet, though there are graphical tools to work with Btrfs snapshots such as Btrfs Assistant, these are separate from normal file browsing, and are rather technical tools concerned with the orchestration of snapshots. Direct integration of snapshots into the file browser itself for mundane end-user purposes, like Windows has with Previous Versions or macOS with the famous Time Machine, has been lacking in the Linux world. The aim of KIO Snapshot is to build that kind of direct integration for KDE. Btrfs 的关键特性之一是能够对子卷(Subvolumes)进行高效快照(子卷是文件系统中可独立管理的目录树。你可以原子化且独立于文件系统其余部分地对子卷进行快照或回滚。快照只是子卷的特殊情况)。它们之所以高效,是因为 Btrfs 让快照与原始文件共享文件范围(extents),因此快照仅在与原始文件有差异的地方占用额外空间。因此,频繁进行快照的成本很低,无需担心磁盘空间问题。这可以用来为用户构建非常方便的通用“撤销”或“时光倒流”功能。然而,尽管有一些用于处理 Btrfs 快照的图形化工具(如 Btrfs Assistant),但它们与普通的文件浏览是分离的,且更多是关注快照编排的技术工具。在 Linux 世界中,一直缺乏像 Windows 的“以前的版本”或 macOS 著名的“时间机器”那样,将快照直接集成到文件浏览器中以供普通用户使用的功能。KIO Snapshot 的目标就是为 KDE 构建这种直接集成。

What it is

它是什么

KIO Snapshot lets Dolphin (indeed any KDE software) list and access Btrfs snapshots of a file or subvolume. You can right-click on a file and go to a folder view showing you all the distinct past versions of it as saved in your snapshots. (At first, I wrote the snapshots-for-file case as a dialog with buttons to open or restore (like Windows’ Previous Versions feature), but I changed it to be a full virtual folder, since that would be much more flexible — now a user could select multiple previous versions and open them in a comparison tool, or copy them somewhere, or check their metadata easily, or whatever else they wished.) You also have views into entire directory trees of subvolumes at their various snapshots. Note that it does not take snapshots, it only allows access to them. To take snapshots, you would have to do it manually, or through an orchestrator like Snapper (If you are using Snapper, you should add yourself to the ALLOW_USERS setting for your Snapper config and turn on SYNC_ACL=yes, to allow rootless access to your snapshots. See Snapper-Configs(5) and Snapper(8) § Permissions for details.). KIO Snapshot 让 Dolphin(实际上是任何 KDE 软件)能够列出并访问文件或子卷的 Btrfs 快照。你可以右键点击一个文件,进入一个文件夹视图,查看快照中保存的所有不同历史版本。(起初,我将“文件快照”功能写成了一个带有打开或恢复按钮的对话框(类似于 Windows 的“以前的版本”功能),但我后来将其改为一个完整的虚拟文件夹,因为这样更灵活——现在用户可以选择多个历史版本并在比较工具中打开它们,或者将它们复制到某处,或者轻松检查它们的元数据,以及进行任何他们想要的操作。)你还可以查看子卷在不同快照时间点的整个目录树。请注意,它不负责创建快照,只负责访问它们。若要创建快照,你需要手动操作,或通过像 Snapper 这样的编排工具(如果你正在使用 Snapper,你应该将自己添加到 Snapper 配置的 ALLOW_USERS 设置中,并开启 SYNC_ACL=yes,以允许在无需 root 权限的情况下访问快照。详情请参阅 Snapper-Configs(5) 和 Snapper(8) § Permissions)。

How it works

它是如何工作的

Mainly it uses libbtrfsutil from btrfs-progs to talk to the filesystem, and KDE Frameworks’ Solid to query filesystems and mounts on a more meta level. Now, the Btrfs API is quite conservative in what it allows non-superusers to do with it. Even a question as seemingly innocuous as “what subvolume is this path in?” cannot be answered for a non-superuser directly. The consequence of this is that on my first attempt at building this integration, I had one component running as a system-level DBus service which would let users query stuff like this for files they owned. Luckily, a nudge from Méven Car made me realize that with some working-around, I could build out all the features without anything running as root. For example, while Btrfs is loath to let you get the subvolume ID for a path or vice versa, it will happily give you a listing of the subvolumes (that you can access) under a path. From there you can derive all the information needed. Using this, KIO Snapshot implements a KIO worker that provides the snapshot:// protocol, which will be understood by all programs which use KDE Frameworks. This protocol provides a virtual view into the snapshots in a filesystem along two dimensions: the snapshots for a given subvolume, each of which is a browsable directory tree in itself; and the snapshots for a given file across all snapshots of its containing subvolume. 它主要使用 btrfs-progs 中的 libbtrfsutil 与文件系统通信,并使用 KDE Frameworks 的 Solid 在更宏观的层面查询文件系统和挂载点。目前,Btrfs API 在允许非超级用户操作方面非常保守。即使是“此路径位于哪个子卷中?”这样看似无害的问题,非超级用户也无法直接获得答案。因此,在我第一次尝试构建此集成时,我有一个组件作为系统级 DBus 服务运行,允许用户查询他们拥有的文件的此类信息。幸运的是,Méven Car 的提醒让我意识到,通过一些变通方法,我可以在不以 root 权限运行任何程序的情况下构建所有功能。例如,虽然 Btrfs 不愿让你获取路径的子卷 ID(反之亦然),但它很乐意为你列出路径下(你可以访问的)子卷。由此,你可以推导出所有需要的信息。利用这一点,KIO Snapshot 实现了一个 KIO worker,提供了 snapshot:// 协议,所有使用 KDE Frameworks 的程序都能理解该协议。该协议提供了文件系统中快照的两个维度的虚拟视图:给定子卷的快照(每个快照本身都是一个可浏览的目录树);以及给定文件在其所属子卷的所有快照中的历史版本。

Allied work

相关工作

Aside from KIO Snapshot itself, I also worked on some small things in other parts of KDE to support it. 除了 KIO Snapshot 本身,我还对 KDE 的其他部分进行了一些小改进以支持它。

  • Fixed a bug in KIO which caused inconsistent behavior in Dolphin’s location bar: KIO MR #2305 修复了 KIO 中的一个错误,该错误导致 Dolphin 地址栏出现不一致的行为:KIO MR #2305
  • Fixed how Solid handled Btrfs layouts like the one used in KDE Linux: Solid MR #261 修复了 Solid 处理 Btrfs 布局的方式(例如 KDE Linux 中使用的布局):Solid MR #261
  • Special default view settings for KIO Snapshot’s views in Dolphin: Dolphin MR #1347 为 Dolphin 中 KIO Snapshot 的视图设置了特殊的默认视图设置:Dolphin MR #1347
  • Experimented with adding support for Btrfs subvolumes into Solid itself — this is just a rough proof-of-concept, and maybe this work is more appropriate further upstream in UDisks — but here it is anyway: Solid branch btrfs-subvolumes 尝试将 Btrfs 子卷支持添加到 Solid 本身——这只是一个粗略的概念验证,也许这项工作更适合在 UDisks 上游进行——但无论如何,代码在这里:Solid 分支 btrfs-subvolumes

KDE Linux

KDE Linux

KDE Linux will soon ship with Snapper and KIO Snapshot out-of-the-box. Hadi Chokr did a lot of excellent integration work here: migrating the filesystem layout to make all user homes subvolumes, and automatically integrating and configuring Snapper for all users seamlessly. This is part of a broader initiative in KDE Linux to improve data backup and restore systems, which has also overseen improvements elsewhere, such as in the Kup backup system. KDE Linux 即将开箱即用地提供 Snapper 和 KIO Snapshot。Hadi Chokr 在此做了大量出色的集成工作:迁移文件系统布局以使所有用户主目录成为子卷,并自动为所有用户无缝集成和配置 Snapper。这是 KDE Linux 改进数据备份和恢复系统这一更广泛计划的一部分,该计划还监督了其他方面的改进,例如 Kup 备份系统。

Release

发布

The first stable release of it was made today (thanks to Bhushan Shah for helping with the release process). I imagine it should be getting packaged into distros fairly soon, thanks to the infrastructure that comes with being a KDE project, but even then it should be easy enough to compile from source. Please use it and report bugs and requests, thank you! 今天发布了第一个稳定版本(感谢 Bhushan Shah 在发布过程中提供的帮助)。我想它应该很快就会被打包进各个发行版中,这得益于作为 KDE 项目所拥有的基础设施,即便如此,从源码编译也非常容易。请使用它并报告错误和需求,谢谢!