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
  • 173
  • 1
  • 4
  • 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
  • So, to sum it up I just need to add "ro" in /boot/cmdline.txt so the system is mounted readonly at boot up and add e.g. /dev/mmcblk0p3 /home/pi/Desktop/data vfat defaults,uid=1000,gid=1000 0 0 at the end of /etc/fstab to mount data folder as writable ? From your fstab I need to add "ro" also in /dev/mmcblk0p2 / ext4 defaults,ro,noatime 0 1 right? This way I prevent OS corrupting but could I damage the microSD by interrupting the power since I still have writable partitions? – Miky Dec 11 '18 at 22:21
  • VFAT filesystem is less prone to data corruption than ext4 (no journaling, etc). That's why my writeable partition is in VFAT. RootFS is made read-only and can be shutdown at any time, with no corruption. Temp files that are usually written on disk (/tmp, /run, /var) are mounted in tmpfs so you also have to add that in your fstab. That is the main change to truly be able to switch between ro and rw mode. – tqhien Dec 12 '18 at 09:28
  • what if I don't put in my fstab /tmp, /run, /var, they will not be written on disk so can I omit these if I don't need temp files? – Miky Dec 12 '18 at 10:19
  • Even if YOU don't need temp files, the Operating System DOES. And if you want to switch between ro and rw WITHOUT rebooting everytime, it is better to move temp dir to tempfs. – tqhien Dec 13 '18 at 11:51
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,107
  • 20
  • 85
  • 197
  • If I switch off the power when I am in read write mode on a partition, do I risk damages to the microSD only if it happens during the writing process (e.g. storing an image) on that partition or independently? – Miky Dec 11 '18 at 22:30
  • @Miky I have updated my answer. – Ingo Dec 11 '18 at 22:51
  • So in short in read write mode I'll always risk, in read only mode I can prevent power-interrupting damages, but if the OS needs the disk for buffering , caching and other, it can write anyway (damaging the SD?) or it use RAM? – Miky Dec 11 '18 at 23:35
  • In ro mode the OS only writes to the (overlaying) RAMdisk, but reads from the SD Card if it does not find it in the RAMdisk. This way it never touches the SD Card by writing to it. You can also write and read back any files to the SD Card but that isn't stored there. On the next reboot it is lost. – Ingo Dec 12 '18 at 00:22
  • and does this affect the RAM performance? – Miky Dec 12 '18 at 00:44
  • @Miky RAM is the fastest storrage on a computer, no matter how much is stored in RAM. The only limit is amout of data to store there. If this is an issue you can use an USB stick instead. – Ingo Dec 12 '18 at 01:10
  • I know, I meant if this increase the OS usage of RAM as it isn't able to write on the SD. yes, I'd probably better use a USB stick – Miky Dec 12 '18 at 01:45
  • last question: the USB stick would still be damaged in case of sudden shutdowns? – Miky Dec 19 '18 at 12:42
  • @Miky The USB stick wouldn't be damaged but maybe a file just stored gets corrupted but it doesn't matter. It is not used anymore because the system boots from the underlaying clean read only filesystem and all files are new written from that on. – Ingo Dec 19 '18 at 14:34