There are many reasons to make your file system read-only. The primary reason is to prevent file system corruption from sudden shutdowns. I’ve been building a Raspberry-Pi consumer device for the past year and this was one of the requirements: the user should be able to unplug the device while running, with zero consequence. See my other post on how to move /tmp to RAM. This is a continuation of that discussion.


My information comes from the following posts:

http://blog.pi3g.com/2014/04/make-raspbian-system-read-only/

http://openenergymonitor.org/emon/node/5331


sudo cp /etc/default/rcS /etc/default/rcS.orig
sudo sh -c "echo 'RAMTMP=yes' >> /etc/default/rcS"
sudo mv /etc/fstab /etc/fstab.orig
sudo sh -c "echo 'tmpfs /tmp tmpfs nodev,nosuid,size=30M,mode=1777 0 0' >> /etc/fstab"
sudo sh -c "echo 'tmpfs /var/log tmpfs nodev,nosuid,size=30M,mode=1777 0 0' >> /etc/fstab"
sudo sh -c "echo 'proc /proc proc defaults 0 0' >> /etc/fstab"
sudo sh -c "echo '/dev/mmcblk0p1 /boot vfat defaults 0 2' >> /etc/fstab"
sudo sh -c "echo '/dev/mmcblk0p2 / ext4 defaults,ro,noatime,errors=remount-ro 0 1' >> /etc/fstab"
sudo sh -c "echo ' ' >> /etc/fstab"
sudo mv /etc/mtab /etc/mtab.orig
sudo ln -s /proc/self/mounts /etc/mtab

Now create the following two sripts in /usr/bin

sudo nano /usr/bin/rpi-rw

#!/bin/sh
sudo mount -o remount,rw /dev/mmcblk0p2 /
echo "Filesystem is unlocked - Write access"
echo "type ' rpi-ro ' to lock"
sudo nano /usr/bin/rpi-ro

#!/bin/sh
sudo mount -o remount,ro /dev/mmcblk0p2 /
echo "Filesystem is locked - Read Only access"
echo "type ' rpi-rw ' to unlock"

sudo chmod +x /usr/bin/rpi-rw
sudo chmod +x /usr/bin/rpi-ro

After creating the two scripts above, you can now type rpi-rw to make the system temporarily writable, and rpi-ro to lock it down again.