-
Notifications
You must be signed in to change notification settings - Fork 5
/
build
299 lines (259 loc) · 6.43 KB
/
build
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
PKG_DESC="The initial file system archive"
PKG_BUILD_DEPS="toybox sdaemons loksh lok luufs logfence elfence setfont tamsyn-font termolos ed25519 open-ath9k-htc-firmware"
PKG_DEPS=""
# files present in the initramfs
INITRAMFS_FILES="bin/toybox
bin/init
bin/ksh
bin/awk
bin/daemon
bin/syslog
bin/mkdir
bin/cp
bin/chroot
bin/grep
bin/ls
bin/mount
bin/umount
bin/sleep
bin/clear
bin/cat
bin/poweroff
bin/reboot
bin/blkid
bin/cut
bin/setfont
bin/luufs
bin/logfence
bin/elfence
bin/ed25519
bin/coredump
bin/termolos
share/consolefonts/Tamsyn8x16r.psf
lib/firmware/htc_7010.fw
lib/firmware/htc_9271.fw"
# directories present in the initramfs
INITRAMFS_DIRECTORIES="run
bin
lib/firmware
var/log
proc
dev
mnt/home
mnt/rw
mnt/union
mnt/ro
sys
share/consolefonts"
build() {
# create a temporary directory for the initramfs contents
initramfs_root="$(mktemp -d -u)"
# create the initramfs directories
for directory in $INITRAMFS_DIRECTORIES
do
mkdir -p "$initramfs_root/$directory"
done
# add required programs and scripts to the initramfs
for i in $INITRAMFS_FILES
do
if [ -f "$BASE_DIR/initramfs/$i" ]
then
path="$BASE_DIR/initramfs/$i"
else
path="$SYSROOT/$i"
fi
cp "$path" "$initramfs_root/$i"
done
# create /dev/console
mknod -m 622 "$initramfs_root/dev/console" c 5 1
echo -n '#!/bin/ksh
# set the executable search path
export PATH="/bin"
# clear the screen from boot loader or early kernel messages
clear
# generate a random color scheme
termolos
# set the console font
setfont /share/consolefonts/Tamsyn8x16r.psf
# display an ASCII logo
echo " _ _
____| |___ __| |
| __| / __|/ _ |
| | | \__ \ (_| |
|_| |_|___/\__,_|
"
# mount virtual file systems
echo -n "Mounting virtual file systems ..."
mount -t proc proc /proc
mount -t sysfs sys /sys
mount -t tmpfs -o mode=755,size=20% log /var/log
mount -t tmpfs -o mode=700,size=5% run /run
# if there is at least 256 MB of RAM, copy the root file system image to RAM
if [ 262144 -le $(grep MemTotal: /proc/meminfo | awk "{print \$2}") ]
then
copy=1
else
copy=0
fi
# assign a coredump helper
mkdir /run/crash
echo "|/bin/coredump %e_%s_%P_%t" > /proc/sys/kernel/core_pattern
# read the kernel command-line
home=""
secure=1
for i in $(cat /proc/cmdline)
do
case "$i" in
home=*)
home="${i#*=}"
;;
sleep=*)
interval="${i#*=}"
echo -n " done.
Waiting for $interval seconds ..."
sleep $interval
;;
secure=0)
secure=0
;;
esac
done
# start logging daemons
echo -n " done.
Starting logging services ..."
[ 1 -eq $secure ] && logfence /var/log
daemon syslog
# find the boot partition
echo -n " done.
Searching for the boot partition ..."
cd /sys/class/block
is_found=0
for i in 1 2 3 4 5 6 7 8 9 10
do
for partition in $(ls -d sd*[0-9] mmc*[0-9] sr*[0-9] 2>/dev/null)
do
mount -o ro /dev/$partition /mnt/home 2>/dev/null
if [ 0 -eq $? ]
then
if [ -f /mnt/home/boot/bzImage ] &&
[ -f /mnt/home/boot/initrd.xz ] &&
[ -f /mnt/home/boot/rootfs.sfs ] &&
[ -f /mnt/home/boot/rootfs.sig ]
then
is_found=1
break 2
fi
umount /mnt/home 2>/dev/null
fi
done
sleep 1
done
if [ 0 -eq $is_found ]
then
echo " failure."
exit
fi
# if the home partition was specified by UUID, find the device name
case "$home" in
*-*-*-*-*)
echo -n " done.
Searching for the home partition ..."
home="$(blkid | grep "UUID=\"$home\"" | cut -f 1 -d :)"
if [ -z "$home" ]
then
echo " failure."
exit
fi
;;
auto)
;;
*)
home="/dev/$home"
;;
esac
# copy the root file system image to RAM
if [ 0 -eq $copy ]
then
image_path="/mnt/home/boot/rootfs.sfs"
sig_path="/mnt/home/boot/rootfs.sig"
else
echo -n " done.
Copying the root file system to RAM ..."
cp /mnt/home/boot/rootfs.sfs /rootfs.sfs
[ 1 -eq $secure ] && cp /mnt/home/boot/rootfs.sig /rootfs.sig
umount /mnt/home
image_path="/rootfs.sfs"
sig_path="/rootfs.sig"
fi
# mount the root file system image
echo -n " done.
Mounting the root file system ..."
mount -t squashfs -o loop,ro "$image_path" /mnt/ro
# verify the root file system image signature, using the packlim public key
if [ 1 -eq $secure ]
then
echo -n " done.
Verifying the root file system signature ..."
ed25519 -c "$image_path" "$sig_path" /mnt/ro/etc/packlim/pub_key
if [ 0 -ne $? ]
then
echo " failure."
umount /mnt/ro
exit
fi
fi
# mount a writable file system - if "auto" is specified instead of a block
# device path, use the home partition
echo -n " done.
Setting up a writable system ..."
case "$home" in
"")
mount -t tmpfs save /mnt/rw
;;
auto)
mount "/dev/$partition" /mnt/rw
;;
*)
mount "$home" /mnt/rw
;;
esac
# mount a layered file system
echo -n " done.
Setting up a layered file system ..."
luufs /mnt/ro /mnt/rw /mnt/union
[ 1 -eq $secure ] && elfence /mnt/union/bin
mount /sys /mnt/union/sys
mount /var/log /mnt/union/var/log
mount /run /mnt/union/run
mount /mnt/union/run /mnt/union/var/run
mkdir /mnt/union/run/mnt
mount /mnt/union/run/mnt /mnt/union/mnt
mount /dev /mnt/union/dev
mkdir /mnt/union/dev/pts
mount -t devpts -o mode=620 pts /mnt/union/dev/pts
mkdir /mnt/union/run/shm /mnt/union/dev/shm
mount /mnt/union/run/shm /mnt/union/dev/shm
mount -t tmpfs -o mode=1777,noexec,nosuid,nodev,size=25% tmp /mnt/union/tmp
luufs /mnt/rw /mnt/union/var/rw
echo " done."
# continue the boot process inside the layered file system
if [ 1 -eq $secure ]
then
flag=""
else
mount /proc /mnt/union/proc
flag="-n"
fi
exec chroot /mnt/union newpid $flag rc' > "$initramfs_root/bin/rc"
chmod 755 "$initramfs_root/bin/rc"
# generate an initramfs archive
dir="$(pwd)"
cd "$initramfs_root"
chown -R 0:0 .
find . | cpio -o -H newc | xz -9 -e --check=none > "$dir/initrd.xz"
rm -rf "$initramfs_root"
cd "$dir"
}
package() {
install -D -m 644 initrd.xz "$1/boot/initrd.xz"
}