Something went wrong. Try again.
An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117#!/usr/bin/env python3# SPDX-License-Identifier: GPL-2.0-or-later# Copyright (c) 2026 Tsiry Sandratraina## An mtree spec for NetBSD's makefs (kernel/openbsd.sh rootfs, image): the# staged tree STAGE with owners and modes from the OpenBSD sets it was# extracted from (TARBALLs, newest last; the host may not keep them), the# files kernel/openbsd.sh added, and /dev: OpenBSD has no devfs, so the# image carries the nodes MAKEDEV would make, with the majors of# OpenBSD/arm64 that the wasm kernel uses (arch/wasm/wasm/conf.c).## Usage: openbsd-spec.py STAGE [TARBALL...] > SPEC
import osimport statimport sysimport tarfile
WHEEL, KMEM, TTY, OPERATOR = 0, 2, 4, 5
def makedev(major, minor): """OpenBSD's makedev(): an 8-bit major, a 24-bit minor.""" return ((major & 0xff) << 8) | (minor & 0xff) | ((minor & 0xffff00) << 8)
def devices(): nodes = []
def node(name, kind, major, minor, mode, gid=WHEEL): nodes.append((name, kind, makedev(major, minor), mode, gid))
node("console", "char", 0, 0, 0o600) node("tty", "char", 1, 0, 0o666) node("mem", "char", 2, 0, 0o640, KMEM) node("kmem", "char", 2, 1, 0o640, KMEM) node("null", "char", 2, 2, 0o666) node("zero", "char", 2, 12, 0o666) node("klog", "char", 7, 0, 0o600) node("tty00", "char", 8, 0, 0o660) node("cua00", "char", 8, 128, 0o660) node("stdin", "char", 22, 0, 0o666) node("stdout", "char", 22, 1, 0o666) node("stderr", "char", 22, 2, 0o666) for i in range(64): node(f"fd/{i}", "char", 22, i, 0o666) node("bpf", "char", 23, 0, 0o600) for i in range(4): node(f"tun{i}", "char", 40, i, 0o600) node(f"tap{i}", "char", 93, i, 0o600) node("random", "char", 45, 0, 0o644) node("urandom", "char", 45, 0, 0o644) node("kstat", "char", 51, 0, 0o640, KMEM) node("pf", "char", 73, 0, 0o600) node("ptm", "char", 81, 0, 0o666) node("diskmap", "char", 90, 0, 0o640, OPERATOR) for i, c in enumerate("0123456789abcdef"): node(f"ttyp{c}", "char", 5, i, 0o666, TTY) node(f"ptyp{c}", "char", 6, i, 0o666, TTY) parts = "abcdefghijklmnop" for unit in range(4): for i, p in enumerate(parts): minor = unit * 16 + i node(f"kwd{unit}{p}", "block", 4, minor, 0o640, OPERATOR) node(f"rkwd{unit}{p}", "char", 13, minor, 0o640, OPERATOR) node(f"vnd{unit}{p}", "block", 14, minor, 0o640, OPERATOR) node(f"rvnd{unit}{p}", "char", 41, minor, 0o640, OPERATOR) for i, p in enumerate("abc"): node(f"rd0{p}", "block", 17, i, 0o640, OPERATOR) node(f"rrd0{p}", "char", 47, i, 0o640, OPERATOR) return nodes
def main(): stage = sys.argv[1] meta = {} for tar in sys.argv[2:]: with tarfile.open(tar) as t: for m in t: name = m.name[2:] if m.name.startswith("./") else m.name if name: meta[name] = m
print("/set uid=0 gid=0") print(". type=dir mode=0755") for dp, dns, fns in os.walk(stage): dns.sort() rel = os.path.relpath(dp, stage) for n in sorted(dns + fns): r = n if rel == "." else os.path.join(rel, n) if r == "dev" or r.startswith("dev/"): continue # below: the nodes MAKEDEV would make p = os.path.join(stage, r) st = os.lstat(p) m = meta.get(r) mode = stat.S_IMODE(m.mode if m is not None else st.st_mode) own = f"uid={m.uid} gid={m.gid}" if m is not None else "uid=0 gid=0" if stat.S_ISLNK(st.st_mode): print(f"./{r} type=link {own} mode={mode:04o} " f"link={os.readlink(p)}") elif stat.S_ISDIR(st.st_mode): print(f"./{r} type=dir {own} mode={mode:04o}") elif stat.S_ISREG(st.st_mode): print(f"./{r} type=file {own} mode={mode:04o}")
print("./dev type=dir uid=0 gid=0 mode=0755") print("./dev/fd type=dir uid=0 gid=0 mode=0555") if os.path.exists(os.path.join(stage, "dev/MAKEDEV")): print("./dev/MAKEDEV type=file uid=0 gid=0 mode=0555") for name, kind, dev, mode, gid in devices(): print(f"./dev/{name} type={kind} uid=0 gid={gid} mode={mode:04o} " f"device={dev}")
if __name__ == "__main__": main()