My dark factory ran so well it forgot to clean up after itself. The result: full Btrfs metadata, a read-only filesystem, and a laptop with no graphics. And that was with 175 GB of free disk space. No catastrophe, just a few hours of fixing. But it's worth knowing why it happened.
I recently wrote about how I started a dark factory in two sentences. The main Claude Code session organizes subagents, each one runs in its own git worktree, and PRs go through E2E and the gates all the way into the merge queue. And it keeps going overnight. It works. I described how the whole thing turned out in a separate article.
Just one thing didn't work. The line produces, but nobody cleans up after it. Claude has worktree cleanup built into the setup, it just didn't do it. And with something like fifty worktrees, you could really tell. Each worktree is another copy of the repo, with vendor dependencies and a lot of small files. Every run adds Docker images, build cache, containers and their logs. And on my laptop all of that sat on a single Btrfs. Docker was also in the system subvolume @, which Timeshift snapshots.
So this isn't an article about AI being evil. It's more about how even vibe engineering can make a mess you'd never think of (I've got plenty of disk space, after all), but then it costs you almost a whole Sunday of fixing.
How much was there?
I only saw the numbers once I started cleaning up. Docker reported:
Images: 847, 269.8 GB (reclaimable 162.5 GB) Local Volumes: 315, 25.11 GB (reclaimable 25.04 GB) Build Cache: 7822 položek, 226.7 GB 847 images and 226 GB of build cache. On top of that, dozens of old containers and JSON logs, the largest of which was over 20 GB. And in /home, around fifty old worktrees. Plus eight Timeshift snapshots of @, which were still holding on to Docker data that should have been gone a long time ago.
I don't know exactly how much each of those sources contributed to the full metadata. I didn't profile it. But it was definitely the combination of worktrees, Docker and the snapshots.
Why free space isn't enough
Here's the important part. Btrfs doesn't have one big bag for everything. It splits the disk into chunks (block groups), and each chunk is either for data or for metadata (plus small system chunks). Metadata consists of trees with information about files, directories, extents, snapshots. Lots of small files and lots of snapshots = lots of metadata.
Once a chunk is allocated for data, metadata can't go into it. And the other way around. A new metadata chunk can only come from unallocated space, meaning the part of the disk that doesn't belong to any chunk yet.
In my case it looked like this:
| Metric | Approximately |
|---|---|
| Device size | 1.82 TiB |
| Allocated for data | 1.74 TiB |
| Free space inside data chunks | 175 GiB |
| Metadata – allocated | 42 GiB |
| Metadata – used | 41.50 GiB |
| Unallocated space | 1.05 MiB |
Those 175 GiB are free space inside the data chunks. Fine for new files, useless for metadata. And because the metadata was in the DUP profile (two copies on the same disk), those 42 GiB of metadata took up roughly 84 GiB physically. What was left unallocated was one megabyte. There was nowhere to take a new metadata chunk from.
df -h, meanwhile, was cheerfully showing hundreds of gigs. That's what makes it so tricky.
Normally btrfs balance takes care of this. It rearranges the half-empty data chunks, frees them, and they become unallocated space. But balance itself needs some space to work with. -dusage=0 didn't find a single empty chunk, and -dusage=10 ended like this:
balance: start -dusage=10 36 enospc errors during balance balance: ended with status: -28 What happened
In two words: nothing would write. I couldn't truncate a 20GB log (truncate needs metadata too), and Docker wouldn't start.
The Claude I was working through this with tried to work around it by adding a temporary device. It created a 16GiB image on an external ext4 disk, attached it via a loop device, and ran btrfs device add on its own, so the metadata would have somewhere to grow. It ended like this:
03:11:18 loop0: detected capacity change from 0 to 33554432 03:11:30 BTRFS error: Transaction 1802515 aborted (error -2) 03:11:30 BTRFS error in do_chunk_alloc: errno=-2 No such entry 03:11:30 BTRFS info: forced readonly Transaction aborted and the filesystem went read-only. What's interesting is that the error was ENOENT (-2), not ENOSPC. There was a suspicion of a race condition between device add and chunk allocation, but I don't have that confirmed.
Then I needed to find out whether the second device had actually been written. btrfs filesystem show was showing two devices, but that's the state of the running filesystem in memory. The superblocks on the NVMe (btrfs inspect-internal dump-super -a) all agreed on num_devices 1, and the loop image had zero allocated blocks. So a reboot without it should go through.
Before the reboot I still made a rescue backup of home with Borg from the console. It finished with a warning, but the archive was there.
Root came up, graphics didn't. A lot of services were falling over with No space left on device, SDDM didn't start the X server. I could work over SSH, even though Zsh was throwing ENOSPC when saving history. BTW, exec bash --noprofile --norc is pretty useful in a situation like that.
How I fixed it
By gradually deleting things.
First the Timeshift snapshots. Deleting the oldest one freed roughly 9 GiB and metadata dropped from 41.50 to 41.21 GiB. After two more, down to 40.38 GiB. Unallocated space was still 1 MiB, though, and after a reboot the metadata jumped back up.
What actually helped was cleaning up the old worktrees in /home. Exactly what Claude was supposed to be doing as it went. Those weren't in the snapshots, so deleting them showed up immediately. Metadata dropped to 33 GiB, /home was writable again and KDE came up. After some more cleanup I got to this:
Device unallocated: 8.00 GiB Free (estimated): 422.22 GiB Metadata used: 29.48 GiB / 42 GiB Eight gigs of unallocated space. Btrfs had something to draw from again.
How I changed the setup
I didn't want to deal with this again in a month, so the second half of Sunday went to Docker.
Cleanup. docker container prune (almost 18 GB), then unused images and the BuildKit cache. And because these are development environments I can rebuild, I also ran docker volume prune -a for old projects. That left 11 images and a zero build cache.
Logs. I switched the default logging driver from json-file to local, with rotation:
"log-driver": "local", "log-opts": { "max-size": "10m", "max-file": "3" } Existing containers keep the old driver, so you have to recreate them.
Docker outside the snapshots. A new top-level subvolume @docker, mounted directly on /var/lib/docker. Images, cache, containers and volumes are thus outside the @ snapshots. I added a safety check to the Docker systemd unit:
[Unit] RequiresMountsFor=/var/lib/docker AssertPathIsMountPoint=/var/lib/docker If the subvolume doesn't get mounted, Docker won't start and won't start filling an empty directory in the root filesystem.
Regular cleanup. A systemd timer, docker-gc.timer, once a week cleans stopped containers and unused images older than 30 days, and build cache older than 14 days, with a target of around 20 GB. Never volumes automatically. The first run happened on Sunday, 22 November, at four in the morning and took about twenty seconds. It deleted two unused images (postgres and busybox) and freed just under half a gig. Docker then reported 20 images at 41 GB, build cache under half a gig, and volumes around one gig. Compared to 847 images and 226 GB of build cache, that's a completely different number. But I had done the main cleanup by hand a week earlier, so this run didn't have much work to do. The real test will come when the factory is running at full speed again.
After the final reboot: one NVMe, 8 GiB unallocated, metadata 30.95 GiB of 42 GiB, Docker running.
One important thing. @docker is still on the same Btrfs. It separates Docker from the snapshots, but it shares space and metadata with the system. If the factory floods it again, it'll be a problem again.
Ideally on its own disk
The best solution is simple. Docker, including volumes, and all the working worktrees on a separate disk that's meant for that. If it fills up, it fills up. The system, KDE and /home keep going.
On a laptop, though, that doesn't really work. I have external disks, but none of them can stay plugged into the laptop permanently. So for now it's a subvolume, cleanup, and watching the numbers.
Check your Btrfs
If you use Btrfs and you run agents that build containers and create worktrees, take a look at this:
sudo btrfs filesystem usage -T / Don't pay attention to Free (estimated). Look at:
-
Device unallocated– when it's getting close to zero, there won't be anywhere to take new chunks from, -
Metadata– how much isusedout of the allocatedtotal.
If there's still a bit of unallocated space and the data chunks are half-empty, a balance with a low filter helps, for example:
sudo btrfs balance start -dusage=10 / I found out this isn't just theory 14 days after the incident, while writing this article. The metadata was fine (19 GiB used out of 42), but unallocated space was down to just 9 GiB again. And that was with 460 GiB of free space. The data chunks took up 1.73 TiB, but only 1.29 TiB of that was actually used. One balance with -dusage=10 brought the unallocated space back to 138 GiB.
But do it before it's too late. Once the metadata is full, balance won't go through on its own, as I found out.
Do use worktrees, parallel agents don't work without them. Just don't assume the cleanup in the setup actually ran. Not for worktrees, and not for Docker either. Check it once in a while:
docker system df # how much images, containers, volumes and cache take up docker builder prune # build cache docker image prune -a # unused images git worktree list # how many worktrees are still hanging around git worktree remove <path> git worktree prune # cleans up records after deleted directories Plus Docker log rotation, and watch out for whether Timeshift (or snapper) is snapshotting /var/lib/docker.
Docker now has its own subvolume, rotated logs and a weekly cleanup. And worktrees? I'll keep using those, the factory wouldn't run without them. You just have to watch whether Claude actually cleaned up after itself :-)
Do you run agents on Btrfs? And do you watch unallocated space, or do you only look at what df tells you?