I´ve been experiencing the sudden change in SD Cards installed in Raspberry PIs (3B+ mostly) that go read-only after a sudden loss of power.

As I have to install these devices far away, running a Python system (that writes files to the local storage), I am looking for a way to programatically a) detect if the SD filesystem goes read-only, and b) revert it back to read/write status. This should be done from a Python script or from a Bash script.

Is there any way do accomplish this?

Another way to go would be to install the Python system in a SD, set it as read-only, and also connect a small USB pendrive to the Raspberry to handle the file operations. So normal boot from SD, and file writing to the pendrive... but I´m trying to avoid the extra hardware if possible.

Any advice? Thanks in advance.

  • Does this answer your question? Detect an SD card that became read-only Jun 21 '21 at 23:26
  • Thanks for the tip. I was thinking that the easiest way to detect a read-only SD (or a faulty SD that cannot be written to, for that matter) would be to periodically run a Python scripts that creates a file, writes anything to it, and closes it, and trap the process. Any errors would mean that the SD is not working correctly. What do you think, Dmitry?,
    – PiBer2
    Jun 22 '21 at 10:30
  • Sure, but you have to deal with disk caches somehow. When you create a small file and read it back, you get data from RAM, not from the SD card. It might take a while before the kernel realizes the SD card is not working and you start seeing actual disk access errors. Jun 22 '21 at 14:35

Finding read-only partitions can be done by looking at /proc/mounts:

egrep " ro,|,ro " /proc/mounts

Fixing is a different story. If your partitions become read-only because you often have filesystem errors, then the easiest way would be to enable journalling and unconditional filesystem checks on boot, instead of trying to fix the filesystem which is already mounted as read-only.

sudo tune2fs -O has_journal /dev/mmcblk0p2
sudo tune2fs -c 1 /dev/mmcblk0p2

You may also need to edit the PASS value in /etc/fstab or use fsck.mode=force kernel parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.