-
Notifications
You must be signed in to change notification settings - Fork 13
/
compiler
355 lines (319 loc) · 7.84 KB
/
compiler
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/bin/bash
# build the oroot install hook
cat <<EOF > oroot_install
#!/bin/bash
build() {
add_dir /lroot
add_dir /troot
add_binary zramctl
add_binary mkswap
add_binary swapon
add_binary free
EOF
# check which binary to add to oroot install hook
if [ "$1" = "btrfs" ]; then
cat <<EOF >> oroot_install
add_binary mkfs.btrfs
add_binary btrfs
EOF
fi
# finish writing the oroot install hook
cat <<EOF >>oroot_install
add_binary mkfs.ext2
add_binary nproc
add_runscript
}
help() {
cat <<HELPEOF
This hook overlays a tmpfs on top of root removing persistent writes to root
HELPEOF
}
EOF
# output header for oroot hook + overlay_flush header
cat <<EOF > oroot
#!/bin/bash
run_hook() {
if [ "\${oroot}" ]; then
fsck_root() {
echo &>/dev/null
}
if [ ! -b "/dev/zram0" ]; then
modprobe zram num_devices=\$((\$(nproc)+2))
fi
echo "#!/bin/bash
mkdir /run/oroot
EOF
# generate part of the overlay_flush script
genOverlayFlush() {
read b
# sort the kernel cmdline parameters alphabetically
for a in $(sort <(for c in $b; do echo $c; done)); do
case "$a" in
cryptdevice*UUID*)
crypt_root="${a#cryptdevice=UUID=}"
cat <<EOF >> oroot
if [ \"\$oroot\" = \"live\" ]; then
cryptsetup open \"/dev/disk/by-uuid/${crypt_root%:*}\" \"${crypt_root#*:}\" \\
EOF
;;
cryptdevice)
crypt_root="${a#cryptdevice=}"
cat <<EOF >> oroot
if [ \"\$oroot\" = \"live\" ]; then
cryptsetup open \"${crypt_root%:*}\" \"${crypt_root#*:}\" \\
EOF
;;
cryptkey*UUID)
cryptkey_root="${a#cryptkey=UUID=}"
cat <<EOF >> oroot
--key-file \\\$(mkdir \"/run/oroot-key\"; mount -U \"${cryptkey_root%%:*}\" \"/run/oroot-key\"; echo \"/run/oroot${cryptkey_root##*:}\")
fi
EOF
unset cryptkey_root
;;
cryptkey*)
cryptkey_root="${a#cryptkey=}"
cat <<EOF >> oroot
--key-file \\\$(mkdir \"/run/oroot-key\"; mount \"${cryptkey_root%%:*}\" \"/run/oroot-key\"; echo \"/run/oroot${cryptkey_root##*:}\")
EOF
unset cryptkey_root
;;
root*UUID*)
# need to newline before fi for cryptsetup to break \
if [ "$crypt_root" ]; then
cat <<EOF >> oroot
fi
mount -U \"${a#root=UUID=}\" \\
EOF
else
cat <<EOF >> oroot
mount -U \"${a#root=UUID=}\" \\
EOF
fi
;;
root*)
if [ "$crypt_root" ]; then
cat <<EOF >> oroot
fi
mount --source \"${a#root=}\" \\
EOF
else
cat <<EOF >> oroot
mount --source \"${a#root=}\" \\
EOF
fi
;;
esac
done
unset b
}
# interpret kernel cmdline options and pass to above loop
genOverlayFlush </proc/cmdline
# use mount options
cat <<EOF >> oroot
-o $(awk '{if ($2 == "/") {print $4}}' /etc/fstab) \"/run/oroot\"
EOF
if [ "$1" = "btrfs" ]; then
cat <<EOF >> oroot
if [ \"\$oroot\" = \"live\" ]; then
btrfs subvolume snapshot / /oroot-snap
cd /oroot-snap
else
cd /
fi
EOF
else
cat <<EOF >> oroot
cd /
EOF
fi
# rsync code
cat <<EOF >> oroot
rsync -ax --delete --no-whole-file --inplace \\\$PWD/ /run/oroot \\
--exclude boot --exclude dev \\
--exclude mnt --exclude proc --exclude run \\
--exclude sys --exclude tmp --exclude usr/bin/overlay_flush;
EOF
if [ "$1" = "btrfs" ]; then
cat <<EOF >> oroot
btrfs subvolume snapshot -r \"/run/oroot\" \"/run/oroot/snapshots/\\\$(date +%s)\"
if [ \"\$oroot\" = \"live\" ]; then
btrfs subvolume delete /oroot-snap
fi
EOF
fi
# unmount and remove drive
cat <<EOF >> oroot
umount /run/oroot &&
rm -r /run/oroot &&
EOF
# close the device in overlay_flush
closeOverlayFlush() {
read b
for a in $b; do
case "$a" in
cryptkey*)
cat <<EOF >> oroot
if [ \"\$oroot\" = \"live\" ]; then
umount \"/run/oroot-key\"
rm -r \"/run/oroot-key\"
fi
EOF
;;
cryptdevice*UUID*)
cat <<EOF >> oroot
if [ \"\$oroot\" = \"live\" ]; then
cryptsetup close \"${a#cryptdevice=UUID=*:}\"
fi
EOF
;;
cryptdevice*)
cat <<EOF >> oroot
if [ \"\$oroot\" = \"live\" ]; then
cryptsetup close \"${a#cryptdevice=*:}\"
fi
EOF
;;
esac
done;
unset b
}
# interpret kernel cmdline options and pass to above loop
closeOverlayFlush </proc/cmdline
# setuid for overlay_flush
cat <<EOF >> oroot
" > /overlay_flush
chmod ug+x /overlay_flush
EOF
# check and mount root device
checkRootDev() {
read b
for a in $b; do
case "$a" in
root*UUID*)
cat <<EOF >> oroot
poll_device "/dev/disk/by-uuid/${a#root=UUID=}" 20
mount -U "${a#root=UUID=}" /lroot
EOF
;;
root*)
cat <<EOF >> oroot
poll_device "${a#root=}" 20
mount "${a#root=}" /lroot
EOF
;;
esac
done;
unset b
}
checkRootDev </proc/cmdline
if [ "$1" = "btrfs" ]; then
cat <<EOF >> oroot
root_snap=\$(ls /lroot/snapshots | sort -nr | head -1)
export root_snap
EOF
fi
# read $oroot and generate ram device
cat <<EOF >> oroot
case "\$oroot" in
live)
zdevice=\$(zramctl -f -s \$(free -m | awk '/Mem/ {print int(\$2*2)"M"}') -a lzo -t \$(nproc))
EOF
# generate either btrfs or ext2 device
if [ "$1" = "btrfs" ]; then
cat <<EOF >> oroot
mkfs.btrfs "\$zdevice"
EOF
else
cat <<EOF >> oroot
mkfs.ext2 -q "\$zdevice"
EOF
fi
# mount zdevice
cat <<EOF >> oroot
mount "\$zdevice" /troot
export zdevice
EOF
# if using btrfs + oroot=live; load most recent snapshot
if [ "$1" = "btrfs" ]; then
cat <<EOF >> oroot
btrfs send "/lroot/snapshots/\$root_snap" | \\
btrfs receive /troot
btrfs subvolume snapshot "/troot/\$root_snap" \\
"/troot/\${root_snap}_root"
EOF
else
cat <<EOF >> oroot
cp -a /lroot/* /troot/
EOF
fi
# unmount /troot when using oroot=live
cat <<EOF >> oroot
umount /troot
umount /lroot
EOF
# close device if oroot=live
closeDevice() {
read b
for a in $b; do
case "$a" in
cryptdevice*UUID*)
cat <<EOF >> oroot
cryptsetup close "${a#cryptdevice=UUID=*:}"
EOF
;;
cryptdevice*)
cat <<EOF >> oroot
cryptsetup close "${a#cryptdevice=*:}"
EOF
;;
esac
done
unset b
}
closeDevice </proc/cmdline
# finish setting up ram device
cat <<EOF >> oroot
oroot_mount() {
EOF
if [ "$1" = "btrfs" ]; then
cat <<EOF >> oroot
mount -o subvol="\${root_snap}_root" "\$zdevice" "\$1"
EOF
else
cat <<EOF >> oroot
mount "\$zdevice" "\$1"
EOF
fi
cat <<EOF >> oroot
mv /overlay_flush \$1/usr/bin/
}
;;
compressed)
zdevice=\$(zramctl -f -s \$(free -m | awk '/Mem/ {print int(\$2*2)"M"}') -a lzo -t \$(nproc))
mkfs.ext2 -q "\$zdevice"
mount "\$zdevice" "/troot"
mkdir /troot/upper /troot/work
oroot_mount() {
EOF
cat <<EOF >> oroot
mount oroot -t overlay -o lowerdir=/lroot,upperdir=/troot/upper,workdir=/troot/work "\$1"
mv /overlay_flush \$1/usr/bin/
}
;;
*)
mount troot -t tmpfs -o size=\$(free -m | awk '/Mem/ {print int(\$2)"M"}') /troot
mkdir /troot/upper /troot/work
oroot_mount() {
mount oroot -t overlay -o lowerdir=/lroot,upperdir=/troot/upper,workdir=/troot/work "\$1"
mv /overlay_flush \$1/usr/bin/
}
;;
esac
EOF
# close oroot hook
cat <<EOF >> oroot
mount_handler=oroot_mount
fi;
}
EOF