0

is it true that if I set Raspbian as read only I can prevent any microSD corruption due to power faults, so that I can simply unplug the battery instead of the clean "Menu > Shutdown"?

if it is true how can I switch quickly from read only to writable e.g. for code editing?

or can I partition the microSD to have the OS on the read only partition and the code on the writable partition and how?

Miky
  • 155
  • 1
  • 5
  • 14

2 Answers2

1

You can mount your root fs read only, and switch between RO and RW.

For switching : I have 2 scripts in /usr/local/bin:

/usr/local/bin/rw with inside

#! /bin/sh

mount / -o rw,remount

/usr/local/bin/ro with

#! /bin/sh

mount / -o ro,remount

You then make them executable :

sudo chmod +x /usr/local/bin/ro /usr/local/bin/rw

Then you also have to change some config files :

  • /boot/cmdline.txt : add "ro" in the line so the system is mounted readonly at boot up

  • Some file are kept open when you start your system (that's why you can switch only once on the other answer). Mainly they are log files.

You can find which ones with :

fuser -v -m / 2>&1 | awk '($3 ~ /F.*/){ print "/proc/"$2"/fd"}' | xargs ls -l| grep '^l.w' | grep -v socket: | grep -v /dev/ | grep -v "/proc" | grep -v anon_inode | grep -v pipe

It is /var/log ! So one way is to change the link to a tmpfs, for example :

rm -rf /var/log

ln -s /run/log /var/log

  • in your fstab file (/etc/fstab), you have to move some directory (temp,run) to ram memory : here is my fstab file :

/dev/mmcblk0p1 /boot vfat defaults 0 2

/dev/mmcblk0p2 / ext4 defaults,ro,noatime 0 1

proc /proc proc defaults 0 0

devpts /dev/pts devpts defaults,gid=5,mode=620,ptmxmode=0666 0 0

tmpfs /dev/shm tmpfs mode=0777 0 0

tmpfs /tmp tmpfs mode=1777 0 0

tmpfs /run tmpfs mode=0755,nosuid,nodev 0 0

sysfs /sys sysfs defaults 0 0

/dev/mmcblk0p3 /home/rpi vfat defaults,uid=1000,gid=1000 0 0

And finally, in that fstab, I mount my third partition as VFAT in /home/rpi

tqhien
  • 111
  • 2
0

You should know that unix/linux isn't made failsafe in general. There are special distributions mainly for embedded systems which are made failsafe. But making the SD Card read only prevents corruption of the filesystem on it so you are on a very safe side that it will start next time without errors if you interrupt the power.

For example you can switch the boot partition to read only and vice versa with:

rpi ~$ sudo mount -o remount,ro /boot
rpi ~$ sudo mount -o remount,rw /boot

# Check with
rpi ~$ findmnt /boot

This is not a problem because the boot partition isn't used on a running system. But if you try to set the root partition read only you will get:

rpi ~$ sudo mount -o remount,ro /
mount: / is busy

If you boot the root partition read only then you can set it read/write, one time. So this is not a usable way. There is also another issue. A normal running operating system, not running in emergency mode, needs the disk for buffering data, storing temporary files caching and so on. So if we start the system read only we have to fake the operating system (OS) that there is a normal read/writable disk.

Fortunately there is a very efficient way to do it. We can use a union filesystem that is also supported by the kernel. It overlays the main read only filesystem with a ramdisk so the OS can write to the disk as normal. Of course, if you reboot then all changes stored in the ramdisk are lost and the system starts from the clean read only filesystem as before. But there is a way to switch to read/write to the underlaying main system so it will boot next time with the changes.

But you should not switch off the power just when you are in read write mode. Then you always risk to corrupt the filesystem, not only when you are just writing to it. To speed up media access linux extensively buffers data and write it later to the disk when the system is idle. This is in the range of some or more seconds. With the ext4 filesystem and journaling it is taken much effort to minimize corruption but this is only for one time disaster recovery. You should never provoke it.

How to setup this you can look at How do I make the OS reset itself every time it boots up?.

Ingo
  • 42,961
  • 20
  • 87
  • 207