The Regex That Commented Out My Root Filesystem: Six Months of Read-Only Boots

In progress Linux Ansible Kubernetes

One of my homelab nodes, an old Dell laptop, had a reputation: its SSD "kept locking itself read-only". Twice I diagnosed it, twice I fixed it with fsck and a remount, and twice I wrote down a plausible cause. In November 2026, while turning that laptop into the first control plane of the rebuilt cluster, I finally found the real one: a single regular expression in my own bootstrap playbook had commented out the root filesystem's line in /etc/fstab, six months earlier.

Impact

  • Actual: from 2026-05-30, the node booted with / read-only every time. containerd and the kubelet couldn't write, so the node dropped out of the cluster after every reboot until someone remounted the root by hand. A recovery script made that routine, which hid the pattern.
  • Misdiagnosis: the June lockout was logged as "a dirty ext4 journal from a hard shutdown". The hardware was suspected throughout.
  • Collateral: the same task had commented out /boot/efi on the GPU laptop. That node still booted, but bootloader updates were being written to an empty directory on the root disk instead of the EFI partition.
  • Blast radius: every node the playbook had run on, on Debian's default fstab layout.

Timeline

  • 2026-05-23 to 26 - Real read-only lockouts with EXT4-fs errors after hard power-offs; repaired with fsck. These predate the bug.
  • 2026-05-30 10:15 - Playbook 02-bootstrap-debian.yaml runs on the Dell. Its "disable swap in fstab" task edits /etc/fstab and leaves a backup, /etc/fstab.<pid>.2026-05-30@10:15:04~. From the next boot on, / stays read-only.
  • 2026-06-01 to 02 - Node NotReady after a power-on; containerd fails with read-only file system. Logged as a dirty journal, "fixed" with recover-node.sh (fsck, mount -o remount,rw /). The fix holds until the next reboot.
  • 2026-11-22 21:49 - kubeadm reset on the Dell fails on every file: read-only file system.
  • 21:50 - recover-node.sh again: fsck says clean, remount works.
  • 22:00 - After a reboot / is ro again. SMART: PASSED, 0 reallocated and 0 uncorrectable sectors. The kernel log of the "previous boot" is from May: nothing had been persisted since.
  • 22:02 - cat /etc/fstab: the root entry starts with # . The swap line is gone entirely.
  • 22:05 - Root line restored, findmnt --verify clean, reboot: / comes up rw. The Ansible backup's timestamp dates the edit.
  • 22:08 - Same check on the GPU laptop: / fine, /boot/efi commented out. Restored, mounted, grub-install re-run.

Root cause

The regex. The task was meant to comment out swap entries:

- name: Disable swap permanently in /etc/fstab
  ansible.builtin.replace:
    path: /etc/fstab
    regexp: '^(\s*[^#\n]+\s+\S+\s+swap\s+.*)$'
    replace: '# \1'

ansible.builtin.replace runs the pattern over the whole file in multiline mode, and \s matches newlines. Debian's installer writes a comment after each entry, so the file read:

UUID=... /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=... none            swap    sw              0       0

The pattern started on the root line, \s+ crossed the line break, \S+ matched #, and swap matched the comment. The match began on the root line, so the root line got the # . (See In a Regex, the Whitespace Class Also Matches Newlines, So One Pattern Can Span Two Lines.)

Why it looked like a disk. Debian boots with ro on the kernel command line, and systemd-remount-fs later applies the fstab options for /. With no active / entry it had nothing to apply, exited successfully, and left the root read-only. Nothing logged an error; the persistent journal couldn't even be written (see With No Active Root Entry in fstab, systemd Leaves the Root Filesystem Read-Only on Every Boot).

Why it lasted six months. The recovery script ran fsck and remounted read-write, which worked every time. Its own swap step, sed -i '/swap/d' /etc/fstab, then deleted every line containing "swap", including the evidence. And a real ext4 incident a week earlier made "the disk again" the obvious story.

Actions

  • Restore the root entry on the Dell and /boot/efi on the GPU laptop; re-run grub-install there.
  • Rewrite the regex with [ \t] instead of \s, matching only lines whose third field is swap.
  • Assert after the edit that /etc/fstab still has an active entry for / (with the is search test: ansible-core 2.19 rejects a conditional that evaluates to a string).
  • recover-node.sh comments out active swap entries instead of deleting every line containing "swap".
  • Add the pattern to the homelab troubleshooting guide, and correct the earlier diagnoses.
  • Check the other replace and lineinfile tasks for patterns that can cross lines.

Lessons learned

  • Read the boot, not the repair. "Read-only after every reboot, fsck clean, SMART clean" is not a disk fault. A recurring symptom with clean diagnostics points at configuration.
  • A recovery script can hide a bug. fsck plus a remount fixed the symptom so reliably that nobody looked at why it came back.
  • Test text edits on the real file. The regex was right for a single line. The file has comments between the lines.
  • Assert the end state. The playbook was green for six months; an assert on "the root still has an entry" would have failed on the first run. It's the same lesson as The Playbook That Said ok, from another angle.
  • Keep backups dated. Ansible's backup: yes file was the only timestamp linking the incident to its cause.

References