Repository navigation
Expand file tree
/
Copy pathbuild-kernel.sh
More file actions
executable file
·145 lines (135 loc) · 6.95 KB
/
Copy pathbuild-kernel.sh
File metadata and controls
executable file
·145 lines (135 loc) · 6.95 KB
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
#!/usr/bin/env bash
# Build the unLAB kernel: upstream unkernel's source at the commit of the
# pinned npm package, with unLAB's config (kernel/kw_defconfig, and
# kernel/kw_smp.config for the multi-CPU kernel) and patches: those in
# kernel/patches/*.patch to upstream's repository, and those in
# kernel/patches/linux/*.patch to Linux after upstream's series, each in name
# order.
# It is packed as upstream packs its own unkernel.wasm: the one-CPU kernel,
# with the patch to the multi-CPU kernel, the base initramfs and the data
# disk template in custom sections. The last two are copied from the pinned
# package's unkernel.wasm, so the kernel is the only thing that differs.
#
# scripts/build-kernel.sh
#
# Writes to KERNEL_OUT (default .kernel/out):
# unlab-kernel.wasm the kernel
# unlab-kernel-source.tar.gz the upstream commit, the config files and both sets of patches
# report.md, report.json size, SHA-256 and boot time beside upstream's own file,
# and beside the pinned kernel when npm run distros mirrored it
#
# Needs what upstream's kernel/build.sh needs: clang 22 with the WebAssembly
# target, lld and the llvm tools, Zig 0.16, cmake, ninja, flex, bison, bc, m4,
# python3 and perl; and public/distros/alpine.erofs (npm run distros -- alpine)
# to boot the result. Upstream's checkout and its build caches stay in
# KERNEL_WORK (default .kernel). With nothing cached, the patched wasm-ld alone
# takes about an hour on two cores.
set -euo pipefail
root="$(cd "$(dirname "$0")/.." && pwd)"
work="$(realpath -m "${KERNEL_WORK:-$root/.kernel}")"
out="$(realpath -m "${KERNEL_OUT:-$work/out}")"
src="$work/unkernel"
ksrc="$src/vendor/src/linux-wasm-kernel"
distro="$root/public/distros/alpine.erofs"
packaged="$root/node_modules/unkernel/dist/unkernel.wasm"
pinned="$root/public/distros/unlab-kernel.wasm"
version="$(node -p 'require(process.argv[1]).dependencies.unkernel' "$root/package.json")"
installed="$(node -p 'require(process.argv[1]).version' "$root/node_modules/unkernel/package.json")"
if [ "$installed" != "$version" ]; then
echo "node_modules has unkernel $installed and package.json pins $version: run npm ci" >&2
exit 1
fi
[ -f "$distro" ] || { echo "no $distro to boot the kernel on: run npm run distros -- alpine" >&2; exit 1; }
tag="v$version"
# A checkout of another version has another tree, toolchain and series, so
# it goes with its caches. Caches restored before the first checkout stay.
if [ ! -d "$src/.git" ] || ! git -C "$src" rev-parse -q --verify "refs/tags/$tag" >/dev/null; then
[ ! -d "$src/.git" ] || rm -rf "$src"
mkdir -p "$src"
git -C "$src" init -q
git -C "$src" fetch -q --depth 1 https://github.com/pithings/unkernel.git "refs/tags/$tag:refs/tags/$tag"
fi
commit="$(git -C "$src" rev-parse "refs/tags/$tag^{commit}")"
case "$commit" in
"${version##*-}"*) ;;
*) echo "upstream's $tag is $commit, not the commit its version names" >&2; exit 1 ;;
esac
git -C "$src" am --abort 2>/dev/null || true
git -C "$src" -c advice.detachedHead=false checkout -q --force "$tag"
patches=("$root"/kernel/patches/*.patch)
if [ -e "${patches[0]}" ]; then
git -C "$src" -c user.name=unLAB -c user.email=unlab@localhost am -q "${patches[@]}"
fi
# Linux at upstream's base commit with upstream's series, made as upstream's
# kernel/build.sh makes it, then unLAB's patches. A tree made from other
# patches is made again.
linux_patches=("$root"/kernel/patches/linux/*.patch)
[ -e "${linux_patches[0]}" ] || linux_patches=()
series="$(cat "$src"/kernel/patches/linux/*.patch "${linux_patches[@]}" | sha256sum | cut -d' ' -f1)"
if [ -e "$ksrc/.git" ] && [ "$(cat "$ksrc/.git/unlab-series" 2>/dev/null)" != "$series" ]; then
rm -rf "$ksrc"
fi
if [ ! -e "$ksrc/.git" ]; then
rm -rf "$src/vendor/build/kernel" "$src/vendor/build/kernel-smp"
linux_repo="$(sed -n 's/^LINUX_REPO="${LINUX_REPO:-\(.*\)}"$/\1/p' "$src/kernel/build.sh")"
linux_base="$(sed -n 's/^LINUX_BASE=//p' "$src/kernel/build.sh")"
rm -rf "$ksrc.tmp"
mkdir -p "$ksrc.tmp"
git -C "$ksrc.tmp" init -q
git -C "$ksrc.tmp" fetch -q --depth 1 "$linux_repo" "$linux_base"
git -C "$ksrc.tmp" checkout -q --detach FETCH_HEAD
git -C "$ksrc.tmp" -c user.name=unkernel -c user.email=unkernel@localhost am -q "$src"/kernel/patches/linux/*.patch
git -C "$ksrc.tmp" checkout -q -b kw-arm64-user
# Left uncommitted, as the config is below: the banner's build id covers
# them, and upstream's check counts only its own series' commits.
[ ${#linux_patches[@]} -eq 0 ] || git -C "$ksrc.tmp" apply "${linux_patches[@]}"
echo "$series" > "$ksrc.tmp/.git/unlab-series"
mv "$ksrc.tmp" "$ksrc"
fi
# Left uncommitted, so the build id in the kernel's banner covers it, and
# upstream's config gives upstream's banner. Copied on every run, so it is
# newer than any .config it built: upstream's build configures, and checks
# that no option was dropped, only when it is.
cp "$root/kernel/kw_defconfig" "$ksrc/arch/wasm/configs/kw_defconfig"
# The multi-CPU kernel's options are upstream's kw_smp.config and then unLAB's.
git -C "$ksrc" checkout -q -- arch/wasm/configs/kw_smp.config
cat "$root/kernel/kw_smp.config" >> "$ksrc/arch/wasm/configs/kw_smp.config"
node -e '
const { mkdirSync, readFileSync, writeFileSync } = require("node:fs");
const { dirname } = require("node:path");
const [bundle, ...pairs] = process.argv.slice(1);
const module = new WebAssembly.Module(readFileSync(bundle));
for (let i = 0; i < pairs.length; i += 2) {
const [section] = WebAssembly.Module.customSections(module, pairs[i]);
if (!section) throw new Error(`${bundle} has no ${pairs[i]} section`);
mkdirSync(dirname(pairs[i + 1]), { recursive: true });
writeFileSync(pairs[i + 1], new Uint8Array(section));
}
' "$packaged" \
unkernel.initrd "$src/vendor/build/initramfs/base.cpio" \
unkernel.datadisk "$src/vendor/build/disk/kwdata.kwsparse.gz"
# The multi-CPU kernel first: the one-CPU build then bundles both, and with
# KW_SMP=1 fails unless the two are builds of one tree.
"$src/kernel/build.sh" smp
KW_SMP=1 "$src/kernel/build.sh" kernel
mkdir -p "$out"
cp "$src/vendor/build/bundle/unkernel.wasm" "$out/unlab-kernel.wasm"
stage="$work/source"
rm -rf "$stage"
mkdir -p "$stage"
printf 'repository https://github.com/pithings/unkernel\nversion %s\ncommit %s\n' "$version" "$commit" > "$stage/UPSTREAM"
cp "$root/kernel/kw_defconfig" "$root/kernel/kw_smp.config" "$stage/"
if [ -e "${patches[0]}" ]; then
mkdir "$stage/patches"
cp "${patches[@]}" "$stage/patches/"
fi
if [ ${#linux_patches[@]} -gt 0 ]; then
mkdir -p "$stage/patches/linux"
cp "${linux_patches[@]}" "$stage/patches/linux/"
fi
tar --sort=name --mtime=@0 --owner=0 --group=0 --numeric-owner -C "$stage" -cf - . | gzip -9n > "$out/unlab-kernel-source.tar.gz"
# The pinned kernel, as npm run distros mirrors it, is the release before this one.
previous=()
[ ! -f "$pinned" ] || previous=(--previous "$pinned")
node "$root/scripts/kernel-report.ts" "$out/unlab-kernel.wasm" "$packaged" "${previous[@]}" \
--unkernel "$src" --distro "$distro" --version "$version" --commit "$commit" --out "$out"