Skip to content

Make Raspbian Read Only

Brandon Butler edited this page Jul 24, 2014 · 2 revisions

Overview

Once you have Hyperion setup and properly configured, it is useful to make the file-system read only. This way you can abruptly shut off the raspberry pi without causing any harm to the SD Card's file-system.

Steps

Disable Swap

dphys-swapfile swapoff
dphys-swapfile uninstall
update-rc.d dphys-swapfile disable

Install UnionFS

aptitude install unionfs-fuse

Create UnionFS Mount Script

nano /usr/local/bin/mount_unionfs

add these lines to the file:

[ -z "$1" ] && exit 1 || DIR=$1
ROOT_MOUNT=$(grep -v "^#" /etc/fstab | awk '$2=="/" {print substr($4,1,2)}')
if [ "$ROOT_MOUNT" != "ro" ]; then
/bin/mount --bind ${DIR}_org ${DIR}
else
/bin/mount -t tmpfs ramdisk ${DIR}_rw
/usr/bin/unionfs-fuse -o cow,allow_other,suid,dev,nonempty ${DIR}_rw=RW:${DIR}_org=RO ${DIR}
fi

Make the file executable:

chmod +x /usr/local/bin/mount_unionfs

Update your fstab to look like this:

proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    ro                0       2
/dev/mmcblk0p2  /               ext4    ro,noatime        0       1
mount_unionfs   /etc            fuse    defaults          0       0
mount_unionfs   /var            fuse    defaults          0       0
none            /tmp            tmpfs   defaults          0       0

Prepare these directories:

cp -al /etc /etc_org
mv /var /var_org
mkdir /etc_rw
mkdir /var /var_rw
reboot

Finished!

End

Now you don't have to worry if your raspberry pi loses power suddenly, all should be well :)

If for some reason you need to make changes you can remount the filesystem as read/write with this command: mount -o remount,rw /

Enjoy

I give Max via the pi3g Blog credit for this solution

BaM provided a better mount_unionfs script in the comments