The idea is to use a read only file system as you have it on a LiveCD together with a union filesystem.
(quote [1]) Generally speaking, a union filesystem combines multiple filesystems into a single virtual filesystem. There are mainly two union filesystems: overlayfs
and aufs
. Both have the same basic model. The following is a dramatic simplification:
- Filesystems are stacked vertically.
- Read accesses are attempted on each filesystem in turn from top to bottom. The first filesystem that contains the file being read is used for the read operation.
- Write accesses are performed similarly, but files that are written to are stored in the top-most writable filesystem. This usually means there is a single writable layer in the union. If files that exist in a read-only layer are written to, they are first copied to the next highest writable layer.
If you use your default read only filesystem and overlay it with a read/writable tmpfs then all changes (they are only stored in tmpfs) will be lost on a reboot.
I prefer to use overlayfs
because it is simple and part of the kernel. We only have to activate it. There are several scripts and HowTos on the web to do that but I prefer overlayroot. It is small, well documented, up to date and has nice features. You can simply update your read only filesystem and you can use semi permanent overlays like USB-Sticks, but default is to use tmpfs. If you add skipoverlay
to /boot/cmdline.txt
you can disable this.
Installing it is done in some minutes. Download the scripts and unzip them:
rpi ~$ curl -L https://github.com/chesty/overlayroot/archive/master.zip >overlayroot.zip
rpi ~$ unzip overlayroot.zip
Now follow the instructions from overlayroot or in overlayroot-master/README.md
. I do not repeat it here to have it in one place because it may change in some time.
For debugging you can check the generated init.gz for its content [5]:
rpi ~$ mkdir /tmp/initramfs
rpi ~$ cd /tmp/initramfs
rpi ~$ gunzip -c /boot/init.gz | cpio -i -d -H newc --no-absolute-filenames
references:
[1] How To: Build A Read-Only Linux System
[2] aufsRootFileSystemOnUsbFlash
[3] How do I use OverlayFS?
[4] Raspberry Pi Overlay Root Filesystem
[5] initramfs-tools
[6] root-ro
[7] overlayroot