|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Read-only Root-FS for Raspian using overlayfs |
| 4 | +# Version 1.1: |
| 5 | +# Changed to use /proc/mounts rathern than /etc/fstab for deriving the root filesystem. |
| 6 | +# |
| 7 | +# Version 1: |
| 8 | +# Created 2017 by Pascal Suter @ DALCO AG, Switzerland to work on Raspian as custom init script |
| 9 | +# (raspbian does not use an initramfs on boot) |
| 10 | +# |
| 11 | +# This program is free software: you can redistribute it and/or modify |
| 12 | +# it under the terms of the GNU General Public License as published by |
| 13 | +# the Free Software Foundation, either version 3 of the License, or |
| 14 | +# (at your option) any later version. |
| 15 | +# |
| 16 | +# This program is distributed in the hope that it will be useful, |
| 17 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +# GNU General Public License for more details. |
| 20 | +# |
| 21 | +# You should have received a copy of the GNU General Public License |
| 22 | +# along with this program. If not, see |
| 23 | +# <http://www.gnu.org/licenses/>. |
| 24 | +# |
| 25 | +# |
| 26 | +# Tested with Raspbian mini, 2017-01-11 |
| 27 | +# |
| 28 | +# This script will mount the root filesystem read-only and overlay it with a temporary tempfs |
| 29 | +# which is read-write mounted. This is done using the overlayFS which is part of the linux kernel |
| 30 | +# since version 3.18. |
| 31 | +# when this script is in use, all changes made to anywhere in the root filesystem mount will be lost |
| 32 | +# upon reboot of the system. The SD card will only be accessed as read-only drive, which significantly |
| 33 | +# helps to prolong its life and prevent filesystem coruption in environments where the system is usually |
| 34 | +# not shut down properly |
| 35 | +# |
| 36 | +# Install: |
| 37 | +# copy this script to /sbin/overlayRoot.sh and add "init=/sbin/overlayRoot.sh" to the cmdline.txt |
| 38 | +# file in the raspbian image's boot partition. |
| 39 | +# I strongly recommend to disable swapping before using this. it will work with swap but that just does |
| 40 | +# not make sens as the swap file will be stored in the tempfs which again resides in the ram. |
| 41 | +# run these commands on the booted raspberry pi BEFORE you set the init=/sbin/overlayRoot.sh boot option: |
| 42 | +# sudo dphys-swapfile swapoff |
| 43 | +# sudo dphys-swapfile uninstall |
| 44 | +# sudo update-rc.d dphys-swapfile remove |
| 45 | +# |
| 46 | +# To install software, run upgrades and do other changes to the raspberry setup, simply remove the init= |
| 47 | +# entry from the cmdline.txt file and reboot, make the changes, add the init= entry and reboot once more. |
| 48 | + |
| 49 | +fail(){ |
| 50 | + echo -e "$1" |
| 51 | + /bin/bash |
| 52 | +} |
| 53 | + |
| 54 | +createZramDrive () { |
| 55 | + # Check Zram Class created |
| 56 | + modprobe zram |
| 57 | + |
| 58 | + echo "lz4" > /sys/block/zram0/comp_algorithm |
| 59 | + echo "1000M" > /sys/block/zram0/disksize |
| 60 | + echo "333M" > /sys/block/zram0/mem_limit |
| 61 | + mke2fs -v -t ext4 -s 1024 -L zram-config0 /dev/zram0 |
| 62 | + |
| 63 | +} |
| 64 | + |
| 65 | +# load module |
| 66 | +modprobe overlay |
| 67 | +if [ $? -ne 0 ]; then |
| 68 | + fail "ERROR: missing overlay kernel module" |
| 69 | +fi |
| 70 | +# mount /proc |
| 71 | +mount -t proc proc /proc |
| 72 | + |
| 73 | +# create a writable fs to then create our mountpoints |
| 74 | +mount -t tmpfs inittemp /mnt |
| 75 | +if [ $? -ne 0 ]; then |
| 76 | + fail "ERROR: could not create a temporary filesystem to mount the base filesystems for overlayfs" |
| 77 | +fi |
| 78 | +mkdir /mnt/lower |
| 79 | +mkdir /mnt/rw |
| 80 | + |
| 81 | +createZramDrive |
| 82 | +mount -t ext4 /dev/zram0 /mnt/rw |
| 83 | + |
| 84 | +#mount -t tmpfs root-rw /mnt/rw |
| 85 | +if [ $? -ne 0 ]; then |
| 86 | + fail "ERROR: could not create tempfs for upper filesystem" |
| 87 | +fi |
| 88 | +mkdir /mnt/rw/upper |
| 89 | +mkdir /mnt/rw/work |
| 90 | +mkdir /mnt/newroot |
| 91 | + |
| 92 | +# mount root filesystem readonly |
| 93 | +rootDev=`awk '$2 == "/" {print $1}' /proc/mounts` |
| 94 | +rootMountOpt=`awk '$2 == "/" {print $4}' /proc/mounts` |
| 95 | +rootFsType=`awk '$2 == "/" {print $3}' /proc/mounts` |
| 96 | +mount -t ${rootFsType} -o ${rootMountOpt},ro ${rootDev} /mnt/lower |
| 97 | +if [ $? -ne 0 ]; then |
| 98 | + fail "ERROR: could not ro-mount original root partition" |
| 99 | +fi |
| 100 | +mount -t overlay -o lowerdir=/mnt/lower,upperdir=/mnt/rw/upper,workdir=/mnt/rw/work overlayfs-root /mnt/newroot |
| 101 | +if [ $? -ne 0 ]; then |
| 102 | + fail "ERROR: could not mount overlayFS" |
| 103 | +fi |
| 104 | +# create mountpoints inside the new root filesystem-overlay |
| 105 | +mkdir /mnt/newroot/ro |
| 106 | +mkdir /mnt/newroot/rw |
| 107 | +# remove root mount from fstab (this is already a non-permanent modification) |
| 108 | +grep -v "$rootDev" /mnt/lower/etc/fstab > /mnt/newroot/etc/fstab |
| 109 | +echo "#the original root mount has been removed by overlayRoot.sh" >> /mnt/newroot/etc/fstab |
| 110 | +echo "#this is only a temporary modification, the original fstab" >> /mnt/newroot/etc/fstab |
| 111 | +echo "#stored on the disk can be found in /ro/etc/fstab" >> /mnt/newroot/etc/fstab |
| 112 | +# change to the new overlay root |
| 113 | +cd /mnt/newroot |
| 114 | +pivot_root . mnt |
| 115 | +exec chroot . sh -c "$(cat <<END |
| 116 | +# move ro and rw mounts to the new root |
| 117 | +mount --move /mnt/mnt/lower/ /ro |
| 118 | +if [ $? -ne 0 ]; then |
| 119 | + echo "ERROR: could not move ro-root into newroot" |
| 120 | + /bin/bash |
| 121 | +fi |
| 122 | +mount --move /mnt/mnt/rw /rw |
| 123 | +if [ $? -ne 0 ]; then |
| 124 | + echo "ERROR: could not move tempfs rw mount into newroot" |
| 125 | + /bin/bash |
| 126 | +fi |
| 127 | +# unmount unneeded mounts so we can unmout the old readonly root |
| 128 | +umount /mnt/mnt |
| 129 | +umount /mnt/proc |
| 130 | +umount -l -f /mnt/dev |
| 131 | +umount -l -f /mnt |
| 132 | +# continue with regular init |
| 133 | +exec /sbin/init |
| 134 | +END |
| 135 | +)" |
0 commit comments