Skip to content

Commit 8f10252

Browse files
zephemeral boot added
1 parent 07a0288 commit 8f10252

File tree

3 files changed

+191
-5
lines changed

3 files changed

+191
-5
lines changed

install.sh

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ install -m 644 ztab /etc/ztab
1818
mkdir -p /usr/local/share/zram-config
1919
mkdir -p /usr/local/share/zram-config/log
2020
install -m 644 uninstall.sh /usr/local/share/zram-config/uninstall.sh
21+
install -m 644 ro-root.sh /usr/local/share/zram-config/ro-root.sh
2122
install -m 644 zram-config.logrotate /etc/logrotate.d/zram-config
2223
mkdir -p /usr/local/lib/zram-config/
2324
install -m 755 overlayfs-tools/overlay /usr/local/lib/zram-config/overlay

ro-root.sh

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
)"

zram-config

+55-5
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,42 @@ removeZswap () {
194194
echo "/dev$ZRAM_DEV removed" >>${ZLOG}
195195
}
196196

197+
enableZephemeral () {
198+
if [ "$ZEPHEMERAL" = false ] ; then
199+
sudo sed -i '/root=PARTUUID/ s|$| init=/bin/ro-root.sh|' /boot/cmdline.txt
200+
echo "initramfs initrd followkernel" >> /boot/config.txt
201+
echo "ramfsfile=initrd" >> /boot/config.txt
202+
echo "ramfsaddr=-1" >> /boot/config.txt
203+
cp $ZSHARE/ro-root.sh /bin/ro-root.sh
204+
chown root:root /bin/ro-root.sh
205+
chmod a+x /bin/ro-root.sh
206+
echo "Zephemeral enabled, after reboot all writes & changes will not be persistent"
207+
else
208+
echo "Zephemeral is already enabled"
209+
fi
210+
}
211+
212+
disableZephemeral () {
213+
if [ "$ZEPHEMERAL" = true ] ; then
214+
sed -i 's| init=/bin/ro-root.sh||g' /boot/cmdline.txt
215+
sed -i '/initramfs initrd followkernel/d' /boot/config.txt
216+
sed -i '/ramfsfile=initrd/d' /boot/config.txt
217+
sed -i '/ramfsaddr=-1/d' /boot/config.txt
218+
rm /bin/ro-root.sh
219+
echo "Zephemeral enabled, after reboot system will be write and change enabled"
220+
else
221+
echo "Zephemeral is already disabled"
222+
fi
223+
}
224+
197225
ZSHARE=/usr/local/share/zram-config
198226
ZLOG=${ZSHARE}/log/zram-config.log
199227
ZDIR=/opt/zram
228+
if grep -q init=/bin/ro-root.sh /boot/cmdline.txt;then
229+
ZEPHEMERAL=true
230+
else
231+
ZEPHEMERAL=false
232+
fi
200233

201234
case "$1" in
202235
start)
@@ -234,13 +267,17 @@ case "$1" in
234267
dir)
235268
TARGET_DIR=$5
236269
BIND_DIR=$6
237-
createZdir
270+
if [ "$ZEPHEMERAL" = false ] ; then
271+
createZdir
272+
fi
238273
;;
239274
log)
240275
TARGET_DIR=$5
241276
BIND_DIR=$6
242277
OLDLOG_DIR=$7
243-
createZlog
278+
if [ "$ZEPHEMERAL" = false ] ; then
279+
createZlog
280+
fi
244281
;;
245282
esac
246283
;;
@@ -281,23 +318,36 @@ case "$1" in
281318
ZRAM_DEV=$2
282319
TARGET_DIR=$3
283320
BIND_DIR=$4
284-
removeZdir
321+
if [ "$ZEPHEMERAL" = false ] ; then
322+
removeZdir
323+
fi
285324
;;
286325
log)
287326
ZTYPE=$1
288327
ZRAM_DEV=$2
289328
TARGET_DIR=$3
290329
BIND_DIR=$4
291-
removeZlog
330+
if [ "$ZEPHEMERAL" = false ] ; then
331+
removeZlog
332+
fi
292333
;;
293334
esac
294335
;;
295336
esac
296337
done < "$file"
297338
rm -v ${ZSHARE}/zram-device-list.rev >>${ZLOG}
298339
;;
340+
341+
enable-ephemeral)
342+
enableZephemeral
343+
;;
344+
345+
disable-ephemeral)
346+
disableZephemeral
347+
;;
348+
299349
*)
300-
echo "Usage: zram-config {start|stop}" >&2
350+
echo "Usage: zram-config {start|stop|enable-ephemeral|disable-ephemeral}" >&2
301351
exit 1
302352
;;
303353
esac

0 commit comments

Comments
 (0)