Something went wrong. Try again.
An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307#!/bin/sh# SPDX-License-Identifier: GPL-2.0-or-later# Copyright (C) 2026 Tsiry Sandratraina## Build the OpenBSD kernel for wasm (.agents/openbsd.md): the OpenBSD# source tree (sparse) with kernel/patches/openbsd/ applied, OpenBSD's# config(8) built for the host, the GENERIC kernel as a wasm64 module with# kwemu linked in, and disks from OpenBSD/arm64's base set.## kernel/openbsd.sh src clone OpenBSD (sparse) into vendor/src/openbsd, apply the patches# kernel/openbsd.sh tools build config(8) into vendor/build/openbsd-tools# kernel/openbsd.sh kernel zig build (kwemu), config + build GENERIC and GENERIC.MP# (wasm64), lowered to vendor/build/openbsd/openbsd32.wasm# and openbsd-mp32.wasm# kernel/openbsd.sh rootfs vendor/build/openbsd/root.fs: FFS image of the base set's# /bin and /sbin (static), for rd0# kernel/openbsd.sh image vendor/build/openbsd/openbsd.img: FFS disk of the base set# kernel/openbsd.sh all src, tools, kernel, rootfs, image# kernel/openbsd.sh run [ARGS] boot single-user from root.fs on the SDK host# kernel/openbsd.sh boot [ARGS] boot multi-user from openbsd.img (--disk) to a login prompt# CPUS=N kernel/openbsd.sh boot|run the same on N CPUs (GENERIC.MP, --cpus N)# MEM=MiB kernel/openbsd.sh boot|run memory (default 1024)# kernel/openbsd.sh export re-export kernel/patches/openbsd from branch kw-wasm## Needs clang and lld 22 from Homebrew (LLVM_BIN, WASM_LD override them),# bmake, yacc and lex, Zig 0.16, Node 24 with JSPI (--experimental-wasm-jspi),# for image on macOS hdiutil (the base set needs a case-sensitive file# system to stage; Linux file systems are, so a plain directory does there).
set -eroot=$(cd "$(dirname "$0")/.." && pwd)src=$root/vendor/src/openbsdtools=$root/vendor/build/openbsd-toolsout=$root/vendor/build/openbsdCPUS=${CPUS:-1}MEM=${MEM:-1024} # MiB: OpenBSD with a few CPUs pages hard in 256OPENBSD_REPO=${OPENBSD_REPO:-https://github.com/openbsd/src}OPENBSD_COMMIT=${OPENBSD_COMMIT:-53a9b6751d0d} # master, 2026-11-28LLVM_BIN=${LLVM_BIN:-/opt/homebrew/opt/llvm/bin}WASM_LD=${WASM_LD:-/opt/homebrew/bin/wasm-ld}WASM_OPT=${WASM_OPT:-wasm-opt} # Binaryen >= 133 (--table64-lowering)BMAKE=${BMAKE:-bmake}MAKEFS=$tools/bin/makefs# User space: OpenBSD 8.0/arm64's base set (etc.tgz, /etc, is inside it).BASE_URL=${BASE_URL:-https://cdn.openbsd.org/pub/OpenBSD/8.0/arm64/base80.tgz}BASE_SHA256=92cfeda3da21d14f10f0687d194a3a6ec99fbf89140f927369e37f917ca35ed9
# What the kernel and config(8) need from the tree.sparse="sys usr.sbin/config usr.sbin/makefs"
do_src() { if [ ! -d "$src/.git" ]; then mkdir -p "$root/vendor/src" git clone --filter=blob:none --no-checkout "$OPENBSD_REPO" "$src" git -C "$src" sparse-checkout init --cone # shellcheck disable=SC2086 git -C "$src" sparse-checkout set $sparse git -C "$src" checkout -q "$OPENBSD_COMMIT" fi if ! git -C "$src" rev-parse -q --verify kw-wasm >/dev/null; then git -C "$src" checkout -q -b kw-wasm "$OPENBSD_COMMIT" git -C "$src" am "$root"/kernel/patches/openbsd/*.patch fi git -C "$src" checkout -q kw-wasm}
# config(8) for the host: a yacc grammar, a lex scanner and C, with shims# for what OpenBSD's libc has (kernel/tools/openbsd-config-compat.*);# -e, which edits a kernel binary, is left out.do_tools() { c=$src/usr.sbin/config b=$tools/config rm -rf "$b" mkdir -p "$b" "$tools/bin" (cd "$b" && yacc -d -o gram.c "$c/gram.y" && lex -o scan.c "$c/scan.l" && for f in gram.c scan.c "$c"/files.c "$c"/hash.c "$c"/main.c \ "$c"/misc.c "$c"/mkheaders.c "$c"/mkioconf.c "$c"/mkmakefile.c \ "$c"/mkswap.c "$c"/pack.c "$c"/sem.c "$c"/util.c \ "$root/kernel/tools/openbsd-config-compat.c"; do cc -c -I. -I"$c" -include "$root/kernel/tools/openbsd-config-compat.h" \ -Werror=implicit-function-declaration -Wno-everything "$f" || exit 1 done && cc -o "$tools/bin/config" ./*.o) # makefs(8), FFS only (cd9660 and msdos are stubbed out), with the # OpenBSD headers it needs, and shims (kernel/tools/openbsd-makefs-compat.*). m=$src/usr.sbin/makefs b=$tools/makefs rm -rf "$b" mkdir -p "$b/inc/sys" "$b/inc/machine" ln -s "$src/sys/ufs" "$b/inc/ufs" cp "$src/sys/sys/disklabel.h" "$b/inc/sys/" cp "$src/sys/arch/arm64/include/disklabel.h" "$b/inc/machine/" # OpenBSD's struct uuid for <sys/disklabel.h>, without its uuid_t # (the host has its own). cat > "$b/inc/sys/uuid.h" <<'UUID'#define _UUID_NODE_LEN 6struct uuid { uint32_t time_low; uint16_t time_mid; uint16_t time_hi_and_version; uint8_t clock_seq_hi_and_reserved; uint8_t clock_seq_low; uint8_t node[_UUID_NODE_LEN];};UUID # <util.h>: glibc has none; what makefs uses of it is in the shims. echo '/* OpenBSD libutil: see kernel/tools/openbsd-makefs-compat.h */' > "$b/inc/util.h" (cd "$b" && for f in "$m"/makefs.c "$m"/walk.c "$m"/xmalloc.c "$m"/ffs.c "$m"/ffs/*.c \ "$root/kernel/tools/openbsd-makefs-compat.c"; do cc -c -I"$m" -Iinc -include "$root/kernel/tools/openbsd-makefs-compat.h" \ -Werror=implicit-function-declaration -Wno-everything "$f" || exit 1 done && cc -o "$tools/bin/makefs" ./*.o) ls -l "$tools/bin/makefs"}
# build_kernel CONF KWEMU OUT: config, make, lower to OUT.build_kernel() { compile=$out/compile/$1 mkdir -p "$compile" (cd "$src/sys/arch/wasm/conf" && "$tools/bin/config" -s "$src/sys" -b "$compile" "$1") (cd "$compile" && unset CPPFLAGS CFLAGS LDFLAGS && "$BMAKE" -j8 LLVM_BIN="$LLVM_BIN" WASM_LD="$WASM_LD" KWEMU="$2") # The 32-bit-memory build, as for Linux, NetBSD and FreeBSD. "$WASM_OPT" -g --memory64-lowering --table64-lowering -O2 --enable-bulk-memory \ --enable-sign-ext --enable-nontrapping-float-to-int --enable-mutable-globals \ --enable-threads "$compile/bsd" -o "$3" ls -l "$compile/bsd" "$3"}
do_kernel() { # kwemu with its JIT (unkernel.kw_host_jit_*, which the SDK host serves), # and with atomics for GENERIC.MP's shared memory. (cd "$root" && zig build -Dkwemu-jit=true) (cd "$root" && zig build -Dkwemu-jit=true -Dkwemu-atomics=true --prefix vendor/build/kwemu-smp) cp "$root/include/kw_emu.h" "$src/sys/arch/wasm/include/kw_emu.h" build_kernel GENERIC "$root/zig-out/lib/libkwemu.a" "$out/openbsd32.wasm" build_kernel GENERIC.MP "$root/vendor/build/kwemu-smp/lib/libkwemu.a" "$out/openbsd-mp32.wasm"}
kernel_args() { if [ "$CPUS" -gt 1 ]; then echo "--kernel $out/openbsd-mp32.wasm --cpus $CPUS" else echo "--kernel $out/openbsd32.wasm" fi}
fetch_base() { mkdir -p "$out" set=$out/base80.tgz if [ ! -f "$set" ] || [ "$(shasum -a 256 "$set" | cut -d' ' -f1)" != "$BASE_SHA256" ]; then curl -fL -o "$set.tmp" "$BASE_URL" [ "$(shasum -a 256 "$set.tmp" | cut -d' ' -f1)" = "$BASE_SHA256" ] || { echo "base80.tgz: checksum mismatch" >&2; exit 1; } mv "$set.tmp" "$set" fi}
# An FFS2 image of STAGE (SIZE), with owners, modes and device nodes from# SPEC (makefs -F, kernel/tools/openbsd-spec.py) and a disklabel (rdroot:# partition a over the image; OpenBSD opens no disk without one). Blocks# and fragments are newfs's defaults, an inode per 8 KiB; rdroot leaves# no reserve (minfree=0).make_ffs() { rm -f "$4" mkdir -p "$1/dev/fd" "$MAKEFS" -t ffs -o version=2,rdroot,minfree=0,bsize=16384,fsize=2048,density=8192 \ -s "$3" -F "$2" "$4" "$1" ls -l "$4"}
# root.fs: the base set's /bin and /sbin (static), uname(1) with ld.so and# libc (the one dynamic program, for checks), and the password files, for# single-user (init asks for a shell; RETURN takes /bin/sh). The kernel# reads it as the host's "initrd" into rd0, the root.do_rootfs() { fetch_base stage=$out/root rm -rf "$stage" "$out/etc-set" mkdir -p "$stage/etc" "$stage/tmp" "$stage/root" "$stage/mnt" "$out/etc-set" chmod 1777 "$stage/tmp" tar xzf "$set" -C "$stage" ./bin ./sbin ./var/sysmerge/etc.tgz \ ./usr/bin/uname ./usr/libexec/ld.so \ "$(tar tzf "$set" | grep '^\./usr/lib/libc\.so\.' | head -1)" tar xzf "$stage/var/sysmerge/etc.tgz" -C "$out/etc-set" rm -rf "$stage/var" for f in master.passwd passwd pwd.db spwd.db group login.conf; do cp "$out/etc-set/etc/$f" "$stage/etc/$f" done python3 -I "$root/kernel/tools/openbsd-spec.py" "$stage" > "$out/root.spec" make_ffs "$stage" "$out/root.spec" 48m "$out/root.fs"}
# openbsd.img: the base set and its /etc on FFS, configured for this# machine: root on kwd0a, a getty on the console (kwcons, which has com's# major), no relinking at boot (it would take minutes emulated), and no# network services that need keys generated first. Staged on a# case-sensitive volume: the set has names that differ in case only (on# macOS an APFS sparse image; elsewhere the file system is case-sensitive# already, so a directory of $out).do_image() { fetch_base if [ "$(uname)" = Darwin ]; then vol=$out/stage.sparseimage mnt=$out/stage [ -f "$vol" ] || hdiutil create -size 4g -fs 'Case-sensitive APFS' \ -type SPARSE -volname obstage "$vol" >/dev/null mkdir -p "$mnt" mount | grep -q " on $mnt " || hdiutil attach -nobrowse -mountpoint "$mnt" "$vol" >/dev/null stage=$mnt/image else mnt= stage=$out/image fi [ ! -d "$stage" ] || chmod -R u+rwX "$stage" rm -rf "$stage" mkdir -p "$stage" tar xzf "$set" -C "$stage" chmod -R u+rwX "$stage" # the spec has the real modes tar xzf "$stage/var/sysmerge/etc.tgz" -C "$stage" chmod -R u+rwX "$stage" rm -rf "$stage/usr/share/relink" echo openbsd > "$stage/etc/myname" cat > "$stage/etc/fstab" <<'FSTAB'/dev/kwd0a / ffs rw 1 1FSTAB cat > "$stage/etc/rc.conf.local" <<'RC'# unkernel (kernel/openbsd.sh image)library_aslr=NOpf=NOsshd_flags=NOsmtpd_flags=NOsndiod_flags=NOntpd_flags=NORC echo "inet autoconf" > "$stage/etc/hostname.kwnet0" echo "https://cdn.openbsd.org/pub/OpenBSD" > "$stage/etc/installurl" # pkg_add's mirror cat >> "$stage/root/.profile" <<'PROFILE'
# unkernel (kernel/openbsd.sh image): neofetch at login[ -x /usr/local/bin/neofetch ] && neofetchPROFILE python3 -I "$root/kernel/tools/openbsd-spec.py" "$stage" \ "$set" "$stage/var/sysmerge/etc.tgz" > "$out/image.spec" make_ffs "$stage" "$out/image.spec" 2g "$out/openbsd.img" [ -z "$mnt" ] || hdiutil detach "$mnt" >/dev/null install_pkgs}
# The packages the image ships (IMAGE_PKGS; empty: none), installed by# pkg_add itself in one boot of the fresh image, which writes the disk in# place (a network is needed). Root's .profile runs neofetch at login.IMAGE_PKGS=${IMAGE_PKGS-neofetch}install_pkgs() { [ -n "$IMAGE_PKGS" ] || return 0 local log=$out/pkgs.log echo "image: installing $IMAGE_PKGS with pkg_add in a boot of openbsd.img ($log)" (cd "$root" && node --experimental-wasm-jspi --stack-trace-limit=40 js/cli.ts \ $(kernel_args) --initrd none --disk "$out/openbsd.img" \ --distro none --mem "$MEM" \ --timeout 1500 --input "rootpkg_add -I $IMAGE_PKGS && echo KW-PKGS-\"OK\"; halt -p" --input-delay 120 < /dev/null > "$log" 2>&1) || true # The marker is quoted in the typed (echoed) command, whole in the output. grep -q '^KW-PKGS-OK' "$log" || { echo "image: installing $IMAGE_PKGS failed (see $log)" >&2; exit 1; }}
# Multi-user from openbsd.img: rc, then getty and login on the console.# The SDK writes the image in place; --snapshot keeps the writes in memory.do_boot() { cd "$root" exec node --experimental-wasm-jspi --stack-trace-limit=40 js/cli.ts \ $(kernel_args) --initrd none --disk "$out/openbsd.img" \ --distro none --mem "$MEM" "$@"}
# Single user (-s): init asks for a shell; RETURN takes /bin/sh.do_run() { cd "$root" exec node --experimental-wasm-jspi --stack-trace-limit=40 js/cli.ts \ $(kernel_args) --initrd "$out/root.fs" --append "-s" \ --distro none --no-net --mem "$MEM" "$@"}
do_export() { rm -f "$root"/kernel/patches/openbsd/*.patch git -C "$src" format-patch -N -o "$root/kernel/patches/openbsd" "$OPENBSD_COMMIT..kw-wasm"}
case "${1:-all}" insrc) do_src ;;tools) do_tools ;;kernel) do_kernel ;;rootfs) do_rootfs ;;image) do_image ;;all) do_src; do_tools; do_kernel; do_rootfs; do_image ;;run) shift; do_run "$@" ;;boot) shift; do_boot "$@" ;;export) do_export ;;*) echo "usage: $0 src|tools|kernel|rootfs|image|all|run [ARGS]|boot [ARGS]|export" >&2; exit 2 ;;esac