Making a PI Read Only
In order to not corrupt the SD boot card of the raspberry pi when it is running these scripts and being abruptly powered down. These steps are specifically for the Ubuntu Mate OS, but the instructions should be similar for most Linux operating systems. We will set up our card with a temporary file system using unionfs that will log files to the RAM on the pi. We also want to configure the pi to be able to easily switch between read only and read-write modes.
First let's download the packages that we need.
apt-get install unionfs-fuse
Now let's mount our card as read-only. We will need to edit our /etc/fstab file. Change the this line:
/dev/mmcblk0p2 / ext4 defaults,noatime,ro 0 1
No we need to make and mount our temporary directory. Add a line to your /etc/fstab file. You can change the size of the directory you would like to put your user data in by changing the 'size=' line.
tempfs /tmp tmpfs defaults,size=30M 0 0
Now we need to disable some logging services that ubuntu mate uses.
service rsyslog disable
Now we need to move some files around for our temporary file system.
mkdir /ro
mv /var /ro
mv /home /ro
Now we need once again edit /etc/fstab
unionfs-fuse#/tmp=rw:/ro/var=ro /var fuse cow,allow_other,nonempty
unionfs-fuse#/tmp=rw:/ro/home=ro /home fuse cow,allow_other
You can change between read-only and read-write by executing this line.
sudo mount / -o remount,rw
To change back to read-only you can use this line.
sudo mount / -o remount,ro
When you want to switch to read-only you need to reboot but switching to read-write does not require a reboot. You can set the lines to switch between read-only and read-write as aliases for convenience.