#!/usr/bin/env node // SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // Bundle a root file system (and a default command line) into a kernel module. // // node kernel/tools/bundle.mjs -o // [--cmdline "console=hvc0 rdinit=/init"] [--datadisk kwdata.kwsparse.gz] [--no-gzip] // [--smp [--smp-patch ] [--smp-optional]] // // The initramfs goes into a custom section "unkernel.initrd", the command // line into "unkernel.cmdline", and the data disk template (a gzipped // kwsparse image, kernel/build.sh datadisk) into "unkernel.datadisk". // --smp (.agents/js.md, CPU threads) adds the patch that turns // this kernel into the given SMP build of the same tree // ("unkernel.kernel.smp.kwpatch", kwdiff.mjs --remap, last), which the // loader applies when asked for more than one CPU (js/core/kpatch.ts). // It refuses two kernels that are not the UP and SMP builds of one tree // (smpPairError: their banners must differ only in "SMP", which with // kernel/build.sh's build stamps means the same tree and kwemu sources; // the same KW_ABI_VERSION in their kwemu; only the SMP one importing // wasm_cpu_start; the same memory index type); with --smp-optional it // warns and leaves the patch out instead. --smp-patch caches the patch // (making it takes about 15 s): a patch in that file made from these two // kernels (its header's SHA-256s) is reused, else a new one is written there. // Custom sections are ignored by the engine, so // the module is otherwise unchanged (same code, imports and memory type), and // the kernel needs no rebuild. The host runtime (js/core/kernel.ts, see // bundledBoot) uses them when it is given no initrd / command line of its own: // // node js/cli.ts vendor/build/bundle/unkernel.wasm // // The cpio is gzipped (CONFIG_RD_GZIP) unless it already is or --no-gzip is // given. Existing unkernel.* sections are replaced, so re-bundling works. import { readFileSync, writeFileSync, renameSync, existsSync } from "node:fs"; import { createHash } from "node:crypto"; import { gzipSync } from "node:zlib"; import { makePatch } from "./kwdiff.mjs"; import { kernelPatchInfo } from "../../js/core/kpatch.ts"; export const INITRD_SECTION = "unkernel.initrd"; export const CMDLINE_SECTION = "unkernel.cmdline"; export const DATADISK_SECTION = "unkernel.datadisk"; export const SMP_PATCH_SECTION = "unkernel.kernel.smp.kwpatch"; function uleb(n) { const out = []; do { let b = n & 0x7f; n = Math.floor(n / 128); if (n) b |= 0x80; out.push(b); } while (n); return Uint8Array.from(out); } function customSection(name, payload) { const nm = new TextEncoder().encode(name); const body = [uleb(nm.length), nm, payload]; const size = body.reduce((s, b) => s + b.length, 0); return [Uint8Array.of(0), uleb(size), ...body]; } /** Split a wasm binary into its sections: [{id, name?, start, end}]. */ export function sections(wasm) { if (wasm.length < 8 || wasm[0] !== 0 || wasm[1] !== 0x61 || wasm[2] !== 0x73 || wasm[3] !== 0x6d) throw new Error("not a WebAssembly module"); let p = 8; const leb = () => { let v = 0, s = 1, c; do { c = wasm[p++]; v += (c & 0x7f) * s; s *= 128; } while (c & 0x80); return v; }; const out = []; while (p < wasm.length) { const start = p, id = wasm[p++], size = leb(), body = p; let name; if (id === 0) { const n = leb(); name = new TextDecoder().decode(wasm.subarray(p, p + n)); } p = body + size; if (p > wasm.length) throw new Error("truncated section"); out.push({ id, name, start, end: p }); } return out; } /** * What identifies a kernel build: { banner, smp, abi, cpuStart, memory64 }. * banner is linux_banner without its newline and smp whether its version * part ("# SMP