Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,9 @@ faster, in the origin private file system (`backend: "opfs"`, for a kernel
in a worker: `{ kind: "data", template: "bundled", backend: "opfs" }`);
`migrateDataDisk()` moves one from IndexedDB to OPFS. Anything that implements
`BlockDevice` works too, and a disk of your own can be the root:
`cmdline: "console=hvc0 root=/dev/wda rw"` with `initrd: null`.
`cmdline: "console=hvc0 root=/dev/wda rw"` with `initrd: null`. The kernel
takes at most 16 disks, `/dev/wda` to `/dev/wdp`, the distro image
included; `createKernel()` rejects more with a `BootError`.

`distro` takes an image of your own too: an erofs or ext4 file system
labelled `kwroot` with an aarch64 user space, a working `/bin/sh`, and an
Expand Down
2 changes: 2 additions & 0 deletions docs/1.sdk/6.disks.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const vm = await createKernel({
});
```

The guest sees the disks in order as `/dev/wda`, `/dev/wdb` and so on, the distro image first. The kernel takes at most 16, the distro image included: `createKernel()` rejects more with a `BootError`.

## The data disk

A disk labelled `kwdata` is the guest's persistent data disk: the root file system is an overlay of the read-only distro image under your changes, and with a data disk attached each distro keeps its changes in `/data/distros/<name>/` on it.
Expand Down
3 changes: 2 additions & 1 deletion js/core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export const IRQ_BLOCK = 3;
const OP_READ = 0, OP_WRITE = 1, OP_FLUSH = 2, OP_DISCARD = 3, OP_WRITE_ZEROES = 4;
export const F_RO = 1, F_FLUSH = 2, F_DISCARD = 4, F_WRITE_ZEROES = 8;
const EIO = 5, EROFS = 30, ENODEV = 19, EOPNOTSUPP = 95;
const MAX_DEVS = 16;
/** The most disks the kernel takes (KWBLK_MAX_DEVS): /dev/wda to /dev/wdp. */
export const MAX_DEVS = 16;

// struct kwblk_req (packed): dev u32 @0, op u32 @4, sector u64 @8, bytes u64 @16,
// nr_segs u32 @24, status s32 @28, segs[] {addr u64, len u64} @32.
Expand Down
10 changes: 9 additions & 1 deletion js/core/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// kernel is, how to read local files and how to start a worker. With
// `worker: true` the kernel runs in a worker (worker-host.ts) instead.

import type { BlockDevice, BlockStats } from "./block.ts";
import { MAX_DEVS, type BlockDevice, type BlockStats } from "./block.ts";
import type { CpuBlocker, CpuEndpoint, CpuInit, CpuLimit } from "./cpu.ts";
import type { DisplayFrame, DisplayHost, DisplayMode, DisplayOption, DisplaySize, DisplayStats } from "./display.ts";
import type { Evdev, InputHost, InputState, InputStats } from "./input.ts";
Expand Down Expand Up @@ -1257,6 +1257,14 @@ export async function createKernelOn(platform: Platform, opts: CreateKernelOptio
const owned = built.disks.filter((d) => !items.includes(d));
if (distro?.owned) owned.push(distro.disk);
const disks = distro ? [distro.disk, ...built.disks] : built.disks;
if (disks.length > MAX_DEVS) {
for (const d of owned) {
try {
(d as BlockDevice & { close?: () => void }).close?.();
} catch {}
}
throw new BootError("boot", `disks: ${disks.length} disks${distro ? " (the distro image included)" : ""}, and the kernel takes at most ${MAX_DEVS}`);
}
const cmdline = opts.cmdline ?? (initrdBytes === undefined && bundled.initrd && bundled.cmdline ? bundled.cmdline
: disks.length && !initrd ? "console=hvc0 root=/dev/wda rw" : "console=hvc0 rdinit=/init");

Expand Down
20 changes: 20 additions & 0 deletions test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,26 @@ console.log(stdout.trim());
}));
});

test("more disks than the kernel takes (16) is a BootError, inline and in a worker; 16 boot", async (t) => {
const dir = mkdtempSync(path.join(tmpdir(), "kw-disks-"));
t.after(() => rmSync(dir, { recursive: true, force: true }));
const file = path.join(dir, "d.img");
writeFileSync(file, new Uint8Array(1 << 16));
const closed: number[] = [];
const stub = (i: number) => ({ size: 1 << 16, readOnly: true, read: () => {}, close: () => { closed.push(i); } }) as unknown as import("../js/node.ts").BlockDevice;
const tooMany = /disks: 17 disks, and the kernel takes at most 16/;
await Promise.all([
assert.rejects(createKernel({ kernel: testKernel, disks: Array.from({ length: 17 }, (_, i) => stub(i)), log: () => {} }),
(e: Error) => e.name === "BootError" && tooMany.test(e.message)),
assert.rejects(createKernel({ kernel: testKernel, worker: true, disks: Array.from({ length: 17 }, () => ({ kind: "file" as const, path: file, readOnly: true })), log: () => {} }),
(e: Error) => e.name === "BootError" && tooMany.test(e.message)),
]);
assert.deepEqual(closed, [], "the caller's own disks are left open");
const vm = await boot(t, { disks: Array.from({ length: 16 }, () => ({ kind: "file" as const, path: file, readOnly: true })), log: () => {} });
const { stdout } = await vm.exec("sh", ["-c", "ls /sys/block | grep -c '^wd'"]);
assert.equal(stdout.trim(), "16");
});

test("worker mode rejects device objects (naming the spec instead)", async (t) => {
const disk = { size: 1 << 20, read: () => {} } as unknown as import("../js/node.ts").BlockDevice;
await assert.rejects(createKernel({ worker: true, disks: [{ kind: "file", path: "/nonexistent" }, disk] }), (e: Error) =>
Expand Down