-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoverlayRoot.sh
executable file
·117 lines (92 loc) · 3.59 KB
/
overlayRoot.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/sh
# Copyright (c) 2021 Kim Hendrikse
# Inspired by and based in part on this work http://wiki.psuter.ch/doku.php?id=solve_raspbian_sd_card_corruption_issues_with_read-only_mounted_root_partition
#
# Jetson Xavier AGX version
#
fail(){
echo -e "$1"
exec /bin/bash
}
# Find from the extlinux.conf file, /proc/cmdline is not always reliable
found_root=$(perl -n -e 'print $1, "\n" if m%^\s*APPEND .*\broot=(\S+)%' /boot/extlinux/extlinux.conf)
found_init=$(perl -n -e 'print $1, "\n" if m%^\s*APPEND .*\binit=(\S+)%' /boot/extlinux/extlinux.conf)
found_sbtsroot=$(perl -n -e 'print $1, "\n" if m%^\s*APPEND .*\bsbtsroot=(\S+)%' /boot/extlinux/extlinux.conf)
if [ "$found_init" != "/sbin/overlayRoot.sh" -a -z "$found_sbtsroot" ] ; then
exec /lib/systemd/systemd
fi
if [ "$found_init" != "/sbin/overlayRoot.sh" -a ! -z "$found_sbtsroot" ] ; then
mkdir /mnt/newroot || fail "Can't create /mnt/newroot"
mount $found_sbtsroot /mnt/newroot || fail "Can't mount $found_sbtsroot on /mnt"
cd /mnt/newroot || fail "Can't change to /mnt/newroot"
pivot_root . mnt
exec chroot . sh -c "$(cat <<END
mount --move /mnt/proc /proc
mount --move /mnt/sys /sys
mount --move /mnt/dev /dev
rmdir /mnt/mnt/newroot
umount /mnt/proc
umount /mnt
# Should be the same as what /sbin/init used to link to
exec /lib/systemd/systemd
END
)"
fi
if [ -z "$found_sbtsroot" ] ; then
found_sbtsroot=$found_root
fi
# load module
modprobe overlay || fail "ERROR: missing overlay kernel module"
# create a writable fs to then create our mountpoints
mount -t tmpfs inittemp /mnt || fail "ERROR: could not create a temporary filesystem to mount the base filesystems for overlayfs"
mkdir /mnt/lower || fail "ERROR: Can't create /mnt/lower"
mkdir /mnt/rw || fail "ERROR: Can't create /mnt/rw"
mount -t tmpfs root-rw /mnt/rw || fail "ERROR: could not create tempfs for upper filesystem"
mkdir /mnt/rw/upper
mkdir /mnt/rw/work
mkdir /mnt/newroot
# mount root filesystem readonly
rootDev=$found_sbtsroot
rootMountOpt=defaults
rootFsType=ext4
mount -t ${rootFsType} -o ${rootMountOpt},ro ${rootDev} /mnt/lower || fail "ERROR: could not ro-mount original root partition"
mount -t overlay -o lowerdir=/mnt/lower,upperdir=/mnt/rw/upper,workdir=/mnt/rw/work overlayfs-root /mnt/newroot
if [ $? -ne 0 ]; then
fail "ERROR: could not mount overlayFS"
fi
# create mountpoints inside the new root filesystem-overlay
mkdir /mnt/newroot/ro
mkdir /mnt/newroot/rw
# remove root mount from fstab (this is already a non-permanent modification)
awk '{if ($0 ~ /^#/ || $2 != "/") print}' /mnt/lower/etc/fstab > /mnt/newroot/etc/fstab
# HACK # > /mnt/newroot/etc/fstab
echo "#the original root mount has been removed by overlayRoot.sh" >> /mnt/newroot/etc/fstab
echo "#this is only a temporary modification, the original fstab" >> /mnt/newroot/etc/fstab
echo "#stored on the disk can be found in /ro/etc/fstab" >> /mnt/newroot/etc/fstab
# change to the new overlay root
cd /mnt/newroot
pivot_root . mnt
exec chroot . sh -c "$(cat <<END
# move ro and rw mounts to the new root
mount --move /mnt/mnt/lower/ /ro
if [ $? -ne 0 ]; then
echo "ERROR: could not move ro-root into newroot"
/bin/bash
fi
mount --move /mnt/mnt/rw /rw
if [ $? -ne 0 ]; then
echo "ERROR: could not move tempfs rw mount into newroot"
/bin/bash
fi
# Move mounts needed so we can unmount the old root mount
mount --move /mnt/proc /proc
mount --move /mnt/sys /sys
mount --move /mnt/dev /dev
umount /mnt/mnt
umount /mnt/proc
umount /mnt
# continue with regular init
# Should be the same as what /sbin/init used to link to
exec /lib/systemd/systemd
END
)"