{"slug": "a-dark-factory-filled-up-my-btrfs-metadata", "title": "A dark factory filled up my Btrfs metadata", "summary": "A developer running an autonomous \"dark factory\" of Claude Code subagents, each working in its own git worktree, found the setup filled the Btrfs filesystem's metadata despite 175 GiB of free data space, forcing the disk read-only and leaving the laptop without graphics. Cleanup revealed 847 Docker images (269.8 GB), 315 volumes, 226.7 GB of build cache, dozens of stale containers and logs, roughly fifty leftover worktrees and eight Timeshift snapshots, which together exhausted the 1.05 MiB of unallocated space Btrfs needed to allocate new metadata chunks. The developer notes the agent had worktree cleanup built in but never ran it, and that btrfs balance failed with 36 ENOSPC errors.", "body_md": "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.\n\nI recently wrote about how I started a [dark factory in two sentences](https://vojta.biberle.cz/en/blog/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](https://vojta.biberle.cz/en/blog/what-i-wont-hand-to-an-agent-without-understanding) in a separate article.\n\nJust 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.\n\nSo 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.\n\nI only saw the numbers once I started cleaning up. Docker reported:\n\n```\nImages:        847, 269.8 GB (reclaimable 162.5 GB)\nLocal Volumes: 315, 25.11 GB (reclaimable 25.04 GB)\nBuild Cache:   7822 položek, 226.7 GB\n```\n\n847 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.\n\nI 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.\n\nHere'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.\n\nOnce 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.\n\nIn my case it looked like this:\n\n| Metric | Approximately | \n|---|---|\n| Device size | 1.82 TiB | \n| Allocated for data | 1.74 TiB | \n| Free space inside data chunks | 175 GiB | \n| Metadata – allocated | 42 GiB | \n| Metadata – used | 41.50 GiB | \n| Unallocated space | **1.05 MiB** | \n\nThose 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.\n\n`df -h`, meanwhile, was cheerfully showing hundreds of gigs. That's what makes it so tricky.\n\nNormally `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:\n\n```\nbalance: start -dusage=10\n36 enospc errors during balance\nbalance: ended with status: -28\n```\n\nIn two words: nothing would write. I couldn't truncate a 20GB log (`truncate` needs metadata too), and Docker wouldn't start.\n\nThe 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:\n\n```\n03:11:18  loop0: detected capacity change from 0 to 33554432\n03:11:30  BTRFS error: Transaction 1802515 aborted (error -2)\n03:11:30  BTRFS error in do_chunk_alloc: errno=-2 No such entry\n03:11:30  BTRFS info: forced readonly\n```\n\nTransaction 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.\n\nThen 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.\n\nBefore 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.\n\nRoot 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.\n\nBy gradually deleting things.\n\nFirst 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.\n\nWhat 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:\n\n```\nDevice unallocated: 8.00 GiB\nFree (estimated):  422.22 GiB\nMetadata used:     29.48 GiB / 42 GiB\n```\n\nEight gigs of unallocated space. Btrfs had something to draw from again.\n\nI didn't want to deal with this again in a month, so the second half of Sunday went to Docker.\n\n**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.\n\n**Logs.** I switched the default logging driver from `json-file` to `local`, with rotation:\n\n```\n\"log-driver\": \"local\",\n\"log-opts\": {\n  \"max-size\": \"10m\",\n  \"max-file\": \"3\"\n}\n```\n\nExisting containers keep the old driver, so you have to recreate them.\n\n**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:\n\n```\n[Unit]\nRequiresMountsFor=/var/lib/docker\nAssertPathIsMountPoint=/var/lib/docker\n```\n\nIf the subvolume doesn't get mounted, Docker won't start and won't start filling an empty directory in the root filesystem.\n\n**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.\n\nAfter the final reboot: one NVMe, 8 GiB unallocated, metadata 30.95 GiB of 42 GiB, Docker running.\n\nOne 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.\n\nThe 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.\n\nOn 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.\n\nIf you use Btrfs and you run agents that build containers and create worktrees, take a look at this:\n\n```\nsudo btrfs filesystem usage -T /\n```\n\nDon't pay attention to `Free (estimated)`. Look at:\n\n`Device unallocated` – when it's getting close to zero, there won't be anywhere to take new chunks from,`Metadata` – how much is `used` out of the allocated `total`.\nIf there's still a bit of unallocated space and the data chunks are half-empty, a balance with a low filter helps, for example:\n\n```\nsudo btrfs balance start -dusage=10 /\n```\n\nI 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.\n\nBut do it before it's too late. Once the metadata is full, balance won't go through on its own, as I found out.\n\nDo 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:\n\n```\ndocker system df          # how much images, containers, volumes and cache take up\ndocker builder prune      # build cache\ndocker image prune -a     # unused images\ngit worktree list         # how many worktrees are still hanging around\ngit worktree remove <path>\ngit worktree prune        # cleans up records after deleted directories\n```\n\nPlus Docker log rotation, and watch out for whether Timeshift (or snapper) is snapshotting `/var/lib/docker`.\n\nDocker 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 :-)\n\nDo you run agents on Btrfs? And do you watch unallocated space, or do you only look at what df tells you?", "url": "https://wpnews.pro/news/a-dark-factory-filled-up-my-btrfs-metadata", "canonical_source": "https://dev.to/vojtabiberle/a-dark-factory-filled-up-my-btrfs-metadata-2n08", "published_at": "2026-11-28 06:03:49+00:00", "updated_at": "2026-11-28 06:13:26.617035+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools"], "entities": ["Claude Code", "Docker", "Btrfs", "Timeshift", "Anthropic"], "also_reported_by": [], "takeaway": "Long-running agent fleets that create per-task git worktrees and Docker builds can exhaust Btrfs metadata chunks even with 175 GiB of free data space, so cleanup of worktrees, images, build cache and snapshots must be enforced rather than left to the agent.", "recommendation": {"advice": "Automate pruning of agent-created worktrees, Docker images, build cache and snapshots, and monitor Btrfs unallocated space rather than df free space, since metadata allocation fails while df still shows hundreds of gigabytes free.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/a-dark-factory-filled-up-my-btrfs-metadata", "markdown": "https://wpnews.pro/news/a-dark-factory-filled-up-my-btrfs-metadata.md", "text": "https://wpnews.pro/news/a-dark-factory-filled-up-my-btrfs-metadata.txt", "jsonld": "https://wpnews.pro/news/a-dark-factory-filled-up-my-btrfs-metadata.jsonld"}}