mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-03 16:19:40 +08:00
binfmt_misc hands a wasm module to kwwasi (kernel/guest/kwwasi.c), which connects to the host over AF_VSOCK port 1021 and serves the program's file I/O with its own descriptors, cwd and credentials. The host (js/core/wasi-exec.ts, serveWasiPrograms(), the CLI's --wasi) compiles the module once and runs each program in a worker of its own (js/core/wasi-program.ts) with WASI preview 1, or preview 0 adapted. Requests cross through a SharedArrayBuffer where one can be used, else with JSPI and messages. kwwasi sends a module only when the host has not compiled it for that file, resolves paths against the cwd or / by what exists, and maps open flags from their asm-generic values to the guest's. alpine.sh builds kwwasi and kw-binfmt.sh registers it; no image with them has been built yet, so test/wasi-binfmt.ts brings kwwasi in through a mount.
52 lines
2.5 KiB
TypeScript
52 lines
2.5 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// unkernel for Node (>= 24): the universal SDK plus file disks, direct
|
|
// networking over host sockets, host directories as guest mounts
|
|
// (nodeFs), and the bundled kernel read from disk.
|
|
//
|
|
// import { createKernel } from "unkernel";
|
|
// const vm = await createKernel(); // bundled Alpine, no network
|
|
// vm.onOutput((b) => process.stdout.write(b));
|
|
// vm.write("uname -a; poweroff\n");
|
|
// const { code } = await vm.exited;
|
|
//
|
|
// const vm = await createKernel({ worker: true }); // same, in a worker thread
|
|
//
|
|
// Nothing else imports this module (the CLI uses node/platform.ts), so the
|
|
// bundled kernel's URL below stays relative to dist/node.mjs.
|
|
|
|
import { createKernelOn, type CreateKernelOptions, type KernelHandle, type KernelInstance, type WorkerOption } from "./core/instance.ts";
|
|
import type { WorkerKernel } from "./core/worker-host.ts";
|
|
import { nodePlatform } from "./node/platform.ts";
|
|
|
|
export * from "./core/index.ts";
|
|
export { FileBase, FileDisk, createImageFromKwsparse, openDisk } from "./node/disk.ts";
|
|
export type { OpenDiskOptions } from "./node/disk.ts";
|
|
export { nodeFs } from "./node/nodefs.ts";
|
|
export { serveWasiPrograms } from "./node/wasi.ts";
|
|
// wispBackend here sends Origin: http://localhost by default (it shadows the core export).
|
|
export { NODE_WISP_HEADERS, directBackend, wispBackend } from "./node/net.ts";
|
|
|
|
/**
|
|
* The bundled kernel: vmlinux32.wasm with the Alpine root file system and
|
|
* its command line in custom sections. In a source checkout this resolves
|
|
* (through a symlink) to vendor/build/bundle/unkernel-alpine.wasm.
|
|
*/
|
|
export const bundledKernelUrl: URL = new URL("./unkernel-alpine.wasm", import.meta.url);
|
|
|
|
const platform = nodePlatform(bundledKernelUrl);
|
|
|
|
/**
|
|
* Load, compile and boot a kernel (default: the bundled Alpine) and return
|
|
* the running instance. Rejects with a BootError if it cannot start. With
|
|
* `worker: true` the kernel is loaded, compiled and run in a worker thread
|
|
* and the result is a WorkerKernel (no low-level `kernel`; see
|
|
* WorkerOption for what it does not support yet).
|
|
*/
|
|
export function createKernel(opts: CreateKernelOptions & { worker: true }): Promise<WorkerKernel>;
|
|
export function createKernel(opts?: CreateKernelOptions & { worker?: false }): Promise<KernelInstance>;
|
|
export function createKernel(opts?: CreateKernelOptions & WorkerOption): Promise<KernelHandle>;
|
|
export function createKernel(opts: CreateKernelOptions & WorkerOption = {}): Promise<KernelHandle> {
|
|
return createKernelOn(platform, opts);
|
|
}
|