Recovering from BTRFS error open_ctree failed: -17
I recently ran into an error during a reboot that resulted in my not being able to get into NixOS. The machine POSTed successfully and grub presented the usual choices of 10 of the latest NixOS generations. All of my drives are LUKS encrypted and I was prompted to provide the decryption passphrase as usual. However, after unlocking the rest of the boot process failed with the following error:
BTRFS: Transaction aborted (error -17)
BTRFS: error (device dm-0 state A) in __btrfs_run_delayed_items:1168: errno=-17 Object already exists
BTRFS: error (device dm-0 state EA) in btrfs_replay_log:2100: errno=-17 Object already exists (Failed to recover ...
BTRFS error (device dm-0 state EA): open_ctree failed: -17
mount: /mnt: fsconfig() failed: File exists.
The error seems to be the result of a corrupted log tree in the BTRFS filesystem and there was no way to fallback into a recovery shell. The option provided to do so simply resulted in the same error.
The problem arose likely due to a bug that was re-introduced in somewhat recent kernel updates (as of this article being published of course), according to some information provided here: https://lore.kernel.org/linux-btrfs/fce139db-4458-4788-bb97-c29acf6cb1df@cachyos.org/
The solution in my case was to boot into a NixOS Installation ISO and run the following command as root. If you don't have a fully encrypted system, you may be able to get into a shell without having to boot into an ISO.
btrfs rescue zero-log /dev/foo
where foo
is the device where the failure occurred.
The command will clear the log tree along with the corrupted data. The only downside is that you may lose up to 1 minute of data that was written to the device prior to the system being rebooted. Not to bad under most circumstances.
The following references may be helpful:
- https://en.opensuse.org/SDB:BTRFS#How_to_repair_a_broken/unmountable_btrfs_filesystem - information about BTRFS recovery options, including the solution I used above.
- https://discussion.fedoraproject.org/t/boot-fails-btrfs-errno-17-object-already-exists/90508/2 - information about the error occurring under slightly different circumstances as well as additional context about the solution.
- https://discourse.nixos.org/t/solved-post-formerly-titled-as-btrfs-issue-open-ctree-failed-bad-tree-block-start-unmountable/59604 - information about the error occurring under slightly different circumstances on a NixOS system. I did respond to this post with much the same information as posted here.
I'm publishing this article mainly as a reference for myself, in case I encounter the issue again in the future and can't remember the solution, but also to potentionallly help someone else who runs into the same thing. Tracking down a solution took longer than it should have, due in part to the year being 2025 when searching for useful information online is all but broken unless you're interested in whatever nonsense has been engineered to the top of the "SEO" shit pile.