The approach taken here is to mount the sdcard read only and then overlay a read-write tmpfs. This means all filesystem writes are stored to RAM and conveniently cleared when the system is power cycled. Persistent state between boots could be stored on the network or on a separate thumb drive; however, this is out of scope for the present discussion.
The procedure described here was developed using a new install of Raspbian Pixel downloaded September 29, 2016.
First, get a root shell by typing "sudo bash" or equivalent. Now change directory to /usr/share/initramfs-tools and as root edit the file hook-functions. Around line 528 add overlay to the list of modules to be include in the default initramfs. The relevant part of the file should now look like
Code: Select all
for arg in "$@" ; do
case "$arg" in
base)
modules="$modules ehci-pci ehci-orion ehci-hcd ohci-hcd ohci-pci uhci-hcd usbhid overlay"
modules="$modules xhci xhci-pci xhci-hcd"
modules="$modules btrfs ext2 ext3 ext4 ext4dev "
Code: Select all
# cd /usr/share/initramfs-tools/scripts
# cp local overlay
# cp -rp local-premount overlay-premount
# cp -rp local-bottom overlay-bottom
Code: Select all
# if [ "${readonly}" = "y" ]; then
roflag=-r
# else
# roflag=-w
# fi
# FIXME This has no error checking
modprobe ${FSTYPE}
checkfs ${ROOT} root
# FIXME This has no error checking
# Mount root
mkdir /upper /lower
if [ "${FSTYPE}" != "unknown" ]; then
mount ${roflag} -t ${FSTYPE} ${ROOTFLAGS} ${ROOT} /lower
else
mount ${roflag} ${ROOTFLAGS} ${ROOT} /lower
fi
modprobe overlay
mount -t tmpfs tmpfs /upper
mkdir /upper/data /upper/work
mount -t overlay \
-olowerdir=/lower,upperdir=/upper/data,workdir=/upper/work \
overlay ${rootmnt}
Code: Select all
# uname -a
Linux raspberry 4.4.21-v7+ #911 SMP Thu Sep 15 14:22:38 BST 2016 armv7l GNU/Linux
# update-initramfs -c -k 4.4.21-v7+
# cd /boot
# mv initrd.img-4.4.21-v7+ initrd7.img
It is now time to enable the initramfs. If after making the following changes your Pi fails to boot, turn the power off, remove the sdcard and mount it in a working computer. Simply revert the changes to config.txt and cmdline.txt described below and your Pi should work normally as before. The reversion can even be done using a Windows PC as the /boot partition is FAT formatted. Now, to enable the initramfs edit /boot/config.txt and add the lines
Code: Select all
kernel=kernel7.img
initramfs initrd7.img
Code: Select all
boot=overlay dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait plymouth.ignore-serial-consoles
Note that running with boot=overlay means all writes to the root file system will be stored in RAM. This is intended for protecting sdcards running embedded applications that have modest memory usage patterns. While a full Raspian desktop will still boot, since RAM is scarce on the Pi, the use of web-browsers, Mathematica and office software may go badly. If available RAM becomes an issue, it would be possible to use similar techniques to put the writable overlay on a separate USB stick without impacting the reliability of the sdcard.