0

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.

PiBer2
  • 11
  • 3

1 Answers1

1

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.

Dmitry Grigoryev
  • 28,277
  • 6
  • 54
  • 147