Recover a Read-Only Root Filesystem with fsck

In progress Linux Kubernetes

Goal

Bring a Linux node whose root filesystem was remounted read-only (after journal or block errors) back to read-write, without reinstalling it.

Prerequisites

  • Console or SSH access to the node, with sudo.
  • The root filesystem currently mounted read-only: check with findmnt -n -o OPTIONS / (it must show ro).

Steps

  1. Rule out the look-alike first. If / is read-only right after every boot, check that /etc/fstab still has an active entry for /; if it doesn't, there is nothing to repair with fsck (see With No Active Root Entry in fstab, systemd Leaves the Root Filesystem Read-Only on Every Boot):
    grep -v '^#' /etc/fstab                                   # a line for "/" must be there
    sudo journalctl -k -b -1 | grep -iE 'EXT4-fs error|I/O error'   # errors here = a real fault
    
    Then check the disk before repairing it: lsblk -no PKNAME "$(findmnt -n -o SOURCE /)" names the disk under the root partition (e.g. sda), and sudo smartctl -H -A /dev/sda reports its health (reallocated, pending or uncorrectable sectors mean replace the drive).
  2. (Optional) If you must run automation against the locked node first (to stop it cleanly, for example), send Ansible's temporary files to RAM-backed /tmp, which stays writable when it's a tmpfs:
    ansible all -b -e ansible_async_dir=/tmp/.ansible_async \
      -m shell -a "shutdown -h now" -B 1 -P 0
    
  3. Find the root device instead of guessing /dev/sda1:
    ROOT_DEV=$(findmnt -n -o SOURCE /)
    
  4. Check and repair, answering yes to every fix (there can be thousands):
    sudo fsck -y "$ROOT_DEV"
    
  5. Once it reports the filesystem clean, reboot (sudo reboot; if it hangs on the read-only root, hold the power button). The kernel still holds its own view of the filesystem it had mounted, so remounting read-write without a reboot risks writing over what fsck just repaired. Remount (sudo mount -o remount,rw /) only if you must reach the node before it can be rebooted, then reboot as soon as possible.

Verify

  • findmnt -n -o OPTIONS / shows rw.
  • touch /var/tmp/write-test && rm /var/tmp/write-test succeeds (on the disk itself, not on /tmp, which may be in RAM).
  • dmesg | grep -i -E "ext4|I/O error" shows no new errors.

Troubleshooting

  • fsck says clean, yet / is read-only again after every reboot: without -f, e2fsck only reads the clean flag; the filesystem is probably fine and the fstab root entry is missing (step 0).
  • Remount fails with "write-protected" after a clean fsck: the drive itself has switched to read-only, typical of a dying SSD. Replace it.
  • Errors come back after a few days: check the drive's health with smartctl -a "$ROOT_DEV" before trusting the node again.

References