-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.sh
executable file
·318 lines (290 loc) · 10.8 KB
/
build.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
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
#!/bin/bash -e
# Runtime vars
###############
BASEDIR="$(readlink -f "$(dirname "$0")")"
CONFIG="config.custom.sh"
NON_INTERACTIVE=0
# Functions
############
log() { echo ">> $1"; }
warn() { echo "WARN: $1" >&2; }
die() { echo "$1" >&2; exit 1; }
err() {
rm -rf initramfs/
die "ERROR: $1"
}
usage() { die "usage: $0 [-c|--config alternate_config.sh] [-N|--non-interactive]"; }
parse_args() {
while [ $# -gt 0 ]; do
case $1 in
-c|--config) CONFIG="$2"; shift ;;
-N|--non-interactive) NON_INTERACTIVE=1 ;;
*) usage ;;
esac
shift
done
}
get_ans() {
local msg=""
case $1 in
"buildroot") [ $NON_INTERACTIVE -ne 1 ] && msg="Update Buildroot tarball (y/N)" || ans="N" ;;
esac
[ $NON_INTERACTIVE -eq 1 ] && return
read -erp ">> $msg? " ans
}
setup_br2() {
if [[ -e "$BR2_TARBALL" && -z "$BR2_SKIP_BUILD" ]]; then
get_ans buildroot
[[ "${ans^^}" = "Y"* ]] && BR2_SKIP_BUILD=0 || BR2_SKIP_BUILD=1
elif [ ! -e "$BR2_TARBALL" ]; then
BR2_SKIP_BUILD=0
fi
if [ $BR2_SKIP_BUILD -eq 0 ]; then
BR2_ARGS=()
[ $NON_INTERACTIVE -eq 1 ] && BR2_ARGS+=("-N")
[ "$CONFIG" != "config.sh" ] && BR2_ARGS+=("-c $CONFIG")
bash -c "$BASEDIR/buildroot/build.sh ${BR2_ARGS[*]}"
else
log "Skipping Buildroot tarball rebuild..."
fi
[ -e "$BR2_TARBALL" ] || err "Buildroot tarball '$BR2_TARBALL' doesn't exist!"
}
item_in_array() {
local item match="$1"
shift
for item; do [ "$item" = "$match" ] && return 0; done
return 1
}
setup_kmodules() {
if [ -z "$KERNEL_MODULES_DIR" ]; then
warn "Skipping kernel modules due to KERNEL_MODULES_DIR being empty!"
return
fi
if [ ! -e "$KERNEL_MODULES_DIR" ]; then
warn "Skipping kernel modules due to '$KERNEL_MODULES_DIR' not existing!"
return
fi
modules_dep="$(find "$KERNEL_MODULES_DIR"/ -type f -name modules.dep 2>/dev/null || :)"
if [ -z "$modules_dep" ]; then
warn "Skipping kernel modules as no modules.dep was found under '$KERNEL_MODULES_DIR'!"
return
elif [ $(echo "$modules_dep" | wc -l) -gt 1 ]; then
err "KERNEL_MODULES_DIR of '$KERNEL_MODULES_DIR' is
too ambiguous as >1 modules.dep files were found as follows:
$(while read line; do echo " ${line/$HOME/\~}"; done < <(echo "$modules_dep"))
"
fi
modules="$(dirname "$modules_dep")" # e.g. ".../lib/modules/5.12.0-msm8998"
modules_len=${#modules} # e.g. 105
kver="$(basename "$modules")" # e.g. "5.12.0-msm8998"
log "Adding configured kernel modules for v$kver..."
all_modules="$(find "$modules" -type f -name "*.ko*")"
builtins_file="$modules/modules.builtin"
builtins="$(<"$builtins_file")"
rd_modules="initramfs/lib/modules/$kver"
probe_modules=()
skip_modules=()
[[ "$builtins" = *"/udc-core.ko"* ]] || \
warn "Couldn't find udc-core (USB_GADGET) as a built-in (=y); USB gadget likely won't work!"
for mod in "${KERNEL_MODULES_COPY[@]}" "${KERNEL_MODULES_PROBE[@]}"; do
item_in_array "$mod" "${skip_modules[@]}" && continue # skip over already processed items
mod_path="$(echo -e "$all_modules" | grep "/$mod.ko" || :)" # ".../kernel/.../mod.ko"
mod_path="${mod_path:$((modules_len+1))}" # drop absolute path prefix
if [ -z "$mod_path" ]; then
[[ "$builtins" = *"/$mod.ko"* ]] || \
warn "Skipping non-existant kernel module '$mod' (which also isn't a built-in)!"
skip_modules+=($mod)
continue # skip modules that are built-ins (or don't exist)
fi
dep_paths="$(sed -n "s|^$mod_path: ||p" "$modules_dep")"
for mod_file in $mod_path $dep_paths; do # e.g. "kernel/.../mod.ko"
[ -e "$rd_modules/$mod_file" ] && continue # skip copying existing modules
mod_dir="$(dirname "$mod_file")" # e.g. "kernel/drivers/usb/gadget"
dst_dir="$rd_modules/$mod_dir"
[ -e "$dst_dir" ] || mkdir -p "$dst_dir"
cp "$modules/$mod_file" "$dst_dir"/
skip_modules+=($mod)
done
if item_in_array "$mod" "${KERNEL_MODULES_PROBE[@]}" \
&& ! item_in_array "$mod" "${probe_modules[@]}"; then
probe_modules+=($mod)
fi
done
log "Copying over modules.builtin to avoid failures when probing built-ins..."
mkdir -p "$rd_modules" # in case there's just built-ins
cp "$builtins_file" "$rd_modules" # {,*.bin}
log "Running depmod to (re)generate modules.dep and such files..."
# avoid warnings about unneeded modules.order file
depmod -b initramfs $kver 2>&1 | grep -v "modules.order" || :
mod_count=$(find $rd_modules -type f -name "*.ko*" | wc -l)
mod_size=$(du -sh "$rd_modules" | awk '{print $1}')
log "Copied $mod_count modules ($mod_size) under /lib/modules!"
if [[ $mod_count -gt 0 && ${#probe_modules[@]} -gt 0 ]]; then
if ! item_in_array "load-modules" "${HOOKS_ENABLE[@]}"; then
log "Including 'load-modules' hook to probe ${#probe_modules[@]} modules on boot"
HOOKS_ENABLE+=(load-modules)
fi
# replace user-defined list with modules *actually* found in the list
KERNEL_MODULES_PROBE=(${probe_modules[@]})
fi
}
hook_present() { item_in_array "$1" "${HOOKS_ENABLE[@]}" "${HOOKS_EXTRA[@]}"; }
hook_get() {
hook_matches="$(find initramfs/hooks -type f 2>/dev/null | grep "$1")"
[ -z "$hook_matches" ] && return # none found
hooks_count=$(echo "$hook_matches" | wc -l)
if [ $hooks_count -eq 1 ]; then
echo "$hook_matches"
else
warn "More than one match for hook '$1' found, returning first of:"
echo "$hook_matches" >&2
echo "$hook_matches" | head -1
fi
}
setup_splash() {
if [ "$BOOT_SPLASH" ] && ! hook_present splash; then
#log "Including 'splash' hook as BOOT_SPLASH is defined"
HOOKS_ENABLE+=(splash)
fi
}
setup_hooks() {
cp -r {hooks,functions} initramfs/
enabled_hooks="${HOOKS_ENABLE[@]}"
extra_hooks="${HOOKS_EXTRA[@]}"
log "Enabled hooks: ${enabled_hooks:-<none>}"
log "Extra hooks: ${extra_hooks:-<none>}"
for hook_file in $(find initramfs/hooks -type f | sort -V); do
hook="${hook_file:16}" # drop "initramfs/hooks/" prefix
hook_dir="$(dirname "$hook")" # e.g. "late" / "."
full_hook_name="$(basename "$hook")" # e.g. "late/00-hang" -> "00-hang"
[[ "$full_hook_name" != [0-9]* ]] && \
err "Using unordered hooks (such as $full_hook_name) creates an unpredictable
loading order & isn't supported; please rename your hooks so they begin with numbers!"
hook_name="$(echo "$full_hook_name" | cut -d'-' -f2-)" # e.g. "00-hang" -> "hang"
hook="$hook_dir/$hook_name" # e.g. "late/hang"
[[ "$hook" = "./"* ]] && hook="${hook:2}" # drop "./" prefix
if ! hook_present "$hook"; then
#log "Dropping unused '$hook' hook from initramfs..."
rm "$hook_file"
continue # avoid extra hook move in this case
fi
if [ -e "extras/$hook" ]; then
extra_files=$(find "extras/$hook/" \( ! -name "deploy" ! -type d \) | wc -l)
if [ $extra_files -gt 0 ]; then
log "Copying extra deployment files for '$hook' hook..."
cp -r "extras/$hook"/* initramfs/
fi
if [ -e "extras/$hook/deploy" ]; then
log "Running extra deployment script for '$hook' hook..."
. "extras/$hook/deploy"
fi
fi
if ! item_in_array "$hook" "${HOOKS_ENABLE[@]}"; then
#log "Moving extra hook '$hook' into /hooks/extra/..."
extra_hook="${hook_file:0:15}/extra/${hook_file:16}" # e.g. "initramfs/hooks/extra/late/50-umtprd"
extra_dir="$(dirname "$extra_hook")" # e.g. "initramfs/hooks/extra/late"
[ -e "$extra_dir" ] || mkdir -p "$extra_dir"
mv "$hook_file" "$extra_dir"
fi
done
hook_present "telnetd" || sed '/telnet_pid/d' -i initramfs/init
hook_present "msm-fb-refresher" || rm -f initramfs/usr/bin/msm-fb-refresher
rmdir initramfs/hooks/late 2>/dev/null && rmdir initramfs/hooks 2>/dev/null || :
rm -f initramfs/deploy # in case any extras/*/deploy scripts were run
}
setup_overlay() {
overlay_items=$(ls -1 overlay | wc -l)
[ $overlay_items -eq 0 ] && return
overlay_size="$(du -sh overlay | awk '{print $1}')"
log "Applying overlay of $overlay_size..."
cp -r overlay/* initramfs/
}
setup_misc() {
if [ $BOOT_DROP_TO_SHELL -eq 1 ]; then
sed -e 's/@HANG_MSG@/Dropping to shell (ash)...\n/' \
-e 's/@HANG_CMD@/shell/' \
-i initramfs/init_functions
else
sed -e 's/@HANG_MSG@/Hanging here forever, please reboot your device!/' \
-e 's/@HANG_CMD@/sleep infinity/' \
-i initramfs/init_functions
fi
if [ $BOOT_DEFAULT_TARGET ]; then
sed -e "s\\@ROOTFS_DEFAULT_INIT@\\[ \$rootfs ] || rootfs=$BOOT_DEFAULT_TARGET\\" -i initramfs/init
else
sed -e '/^@ROOTFS_DEFAULT_INIT@$/d' -i initramfs/init
fi
if $BOOT_OF; then
sed -e 's|@DEVICE_MODEL_SYSFS@|/sys/firmware/devicetree/base/model|' -i initramfs/init
else
sed -e 's|@DEVICE_MODEL_SYSFS@|/sys/devices/virtual/dmi/id/board_name|' -i initramfs/init
fi
sed -e "s/@USB_IFACE@/$BOOT_RNDIS_IFACE/" -i initramfs/init
# add initramfs revision info to /etc/os-release
git_hash="$(git rev-parse --short HEAD 2>/dev/null || :)" # e.g. "c2f1a3b"
# date of either latest commit or current time on dirty trees
if git diff --quiet; then # clean
git_date="$(git log -1 --format="%at" | xargs -I{} date -d @{} +'%Y/%m/%d %H:%M:%S')"
else # dirty
git_date="$(date +'%Y/%m/%d %H:%M:%S')"
git_hash+="+"
fi
git_branch="$(git symbolic-ref --short HEAD 2>/dev/null || :)" # e.g. "master"
if [ "$git_branch" ]; then # regular clones
rd_version="$git_hash on $git_branch @ $git_date"
elif [ "$git_hash" ]; then # shallow clones without branch
rd_version="$git_hash @ $git_date"
else # tarball sources etc.
rd_version="$git_date"
fi
log "Setting initramfs version to '$rd_version'..."
echo "RD_VERSION=\"$rd_version\"" >> initramfs/etc/os-release
# remove placeholder files for previously empty directories
find initramfs/ -type f -name ".keep" -delete
}
create_cpio() {
cpio_name="initramfs${CPIO_EXTRA_NAME}.cpio"
if [ $CPIO_RM_EXISTING -eq 1 ]; then
log "Removing potentially existing '$cpio_name'* files..."
rm -f "$cpio_name"*
fi
if [[ "$CPIO_COMPRESS" = "gz"* ]]; then
compress_cmd="gzip ${CPIO_COMPRESS_ARGS:---best --no-name}"
[ $CPIO_COMPRESS_KEEP_SRC -eq 1 ] && compress_cmd+=" --keep" || compress_cmd+=" --force"
compress_ext=".gz"
elif [[ "$CPIO_COMPRESS" = "lz4"* ]]; then
compress_cmd="lz4 ${CPIO_COMPRESS_ARGS:--l -9 --favor-decSpeed --quiet}"
# FIXME: "lz4 --rm" doesn't appear to delete the source file?
[ $CPIO_COMPRESS_KEEP_SRC -ne 1 ] && compress_cmd+=" --rm"
compress_ext=".lz4"
fi
cpio_final="${cpio_name}${compress_ext}"
log "Creating '$cpio_final'..."
cd initramfs
chmod +x init
find ./* -print0 | cpio --quiet --null --create --format=newc > "$BASEDIR/$cpio_name"
[ "$compress_cmd" ] && $compress_cmd "$BASEDIR/$cpio_name"
cd ..
}
# Script
#########
cd "$BASEDIR"
. config.sh
parse_args "$@"
[ -r "$CONFIG" ] && . "$CONFIG" || CONFIG="config.sh"
setup_br2
[ -d initramfs ] && rm -rf initramfs
mkdir -p extras functions hooks initramfs overlay
log "Extracting Buildroot tarball..."
tar -xf "$BR2_TARBALL" -C initramfs
log "Copying over init scripts & extra directories..."
cp init{,_functions} initramfs/
[[ ${#KERNEL_MODULES_COPY[@]} -gt 0 || ${#KERNEL_MODULES_PROBE[@]} -gt 0 ]] \
&& setup_kmodules
setup_splash
setup_hooks
setup_overlay
setup_misc
create_cpio
rm -rf initramfs
log "Done, size is $(du "$BASEDIR/$cpio_final" | awk '{print $1}')K"