mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-07 05:47:24 +08:00
Sessions and the console run as user in /home/user on Debian and Alpine (doas with a sudo shim there); the host asks for another user with SpawnOptions.user, the CLI's --user or kw.user=. The shims, the command-not-found installers, poweroff and the page's launcher and demos reach root through sudo, and apt, apt-get and apk typed by the user are aliases to it. /usr/lib/kw/shrc gives nested shells, sudo -s and sudo -i the prompt, the command-not-found handler and PATH that only login shells had. The framebuffer, input and sound devices are group-accessible, and host mounts show as the user's.
152 lines
5.6 KiB
TypeScript
152 lines
5.6 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// The interface of a host file system mounted in the guest (vm.mount(),
|
|
// createKernel({ mounts })): what a file system implements (Fs, FsFile)
|
|
// and the shapes it takes and returns. MemoryFs (memfs.ts) and nodeFs()
|
|
// (node/nodefs.ts) implement it; the server that answers the guest with it
|
|
// is 9p.ts.
|
|
|
|
import type { MaybePromise } from "./block.ts";
|
|
|
|
/** @internal Linux (asm-generic) errno values by name, for FsError and Node's error codes. */
|
|
export const ERRNO: Readonly<Record<string, number>> = {
|
|
EPERM: 1, ENOENT: 2, EINTR: 4, EIO: 5, ENXIO: 6, E2BIG: 7, ENOEXEC: 8, EBADF: 9, EAGAIN: 11,
|
|
EWOULDBLOCK: 11, ENOMEM: 12, EACCES: 13, EFAULT: 14, EBUSY: 16, EEXIST: 17, EXDEV: 18, ENODEV: 19,
|
|
ENOTDIR: 20, EISDIR: 21, EINVAL: 22, ENFILE: 23, EMFILE: 24, ENOTTY: 25, ETXTBSY: 26, EFBIG: 27,
|
|
ENOSPC: 28, ESPIPE: 29, EROFS: 30, EMLINK: 31, EPIPE: 32, ERANGE: 34, EDEADLK: 35, ENAMETOOLONG: 36,
|
|
ENOLCK: 37, ENOSYS: 38, ENOTEMPTY: 39, ELOOP: 40, ENODATA: 61, EPROTO: 71, EOVERFLOW: 75, EILSEQ: 84,
|
|
EOPNOTSUPP: 95, ENOTSUP: 95, ECONNABORTED: 103, ECONNRESET: 104, ENOTCONN: 107, ETIMEDOUT: 110,
|
|
ESTALE: 116, EDQUOT: 122, ECANCELED: 125,
|
|
};
|
|
|
|
/** Throw one from a handler to answer the guest with that errno: `code` is its name ("ENOENT"). */
|
|
export class FsError extends Error {
|
|
readonly code: string;
|
|
readonly errno: number;
|
|
constructor(code: string, message?: string) {
|
|
super(message === undefined || message === code ? code : `${code}: ${message}`);
|
|
this.name = "FsError";
|
|
this.code = code;
|
|
this.errno = ERRNO[code] ?? ERRNO.EIO!;
|
|
}
|
|
}
|
|
|
|
export type FsFileType = "file" | "dir" | "symlink";
|
|
|
|
export interface FsAttr {
|
|
type: FsFileType;
|
|
/** Permission bits (default 0o644 files, 0o755 directories, 0o777 links). */
|
|
mode?: number;
|
|
size?: number;
|
|
/** The owner (default FS_DEFAULT_OWNER). */
|
|
uid?: number;
|
|
gid?: number;
|
|
nlink?: number;
|
|
atimeMs?: number;
|
|
mtimeMs?: number;
|
|
ctimeMs?: number;
|
|
/** Inode number (with `dev`). Without it one is derived from the path. */
|
|
ino?: number | bigint;
|
|
/** Device number, combined with `ino` (only needed when one tree spans devices). */
|
|
dev?: number | bigint;
|
|
/**
|
|
* A change counter: changes when the data changes. Default: derived
|
|
* from mtimeMs and size. 0 means the guest never caches this file's
|
|
* data, so each of its reads reaches the file (for computed files,
|
|
* whose size may change).
|
|
*/
|
|
version?: number;
|
|
}
|
|
|
|
/**
|
|
* The uid and gid the guest sees for a file whose backend reports none
|
|
* (nodeFs; MemoryFs's files until chowned): 1000, the distro images'
|
|
* user, so that the guest's default user may write where the permission
|
|
* bits let the owner. Root may anyway; the base shell shows the number.
|
|
*/
|
|
export const FS_DEFAULT_OWNER = 1000;
|
|
|
|
export interface FsDirEntry {
|
|
name: string;
|
|
type: FsFileType;
|
|
/** The inode number and device, as getattr gives them (else one is derived from the path). */
|
|
ino?: number | bigint;
|
|
dev?: number | bigint;
|
|
}
|
|
|
|
export interface FsSetAttr {
|
|
mode?: number;
|
|
uid?: number;
|
|
gid?: number;
|
|
size?: number;
|
|
atimeMs?: number;
|
|
mtimeMs?: number;
|
|
}
|
|
|
|
export interface FsStatFs {
|
|
/** Block size (default 4096). */
|
|
bsize?: number;
|
|
blocks?: number;
|
|
bfree?: number;
|
|
/** Default: bfree. */
|
|
bavail?: number;
|
|
files?: number;
|
|
ffree?: number;
|
|
/** Longest name (default 255). */
|
|
namelen?: number;
|
|
}
|
|
|
|
/** How the guest opens a file. */
|
|
export interface FsOpenFlags {
|
|
read: boolean;
|
|
write: boolean;
|
|
/** O_TRUNC: truncate to 0 first. */
|
|
truncate: boolean;
|
|
append: boolean;
|
|
}
|
|
|
|
/**
|
|
* An open file (Fs.open/create), used for the guest's reads, writes,
|
|
* getattr, truncation and fsync until it closes it. It must keep working
|
|
* after the path is renamed or removed. Without `write` the file cannot be
|
|
* opened for writing (EACCES).
|
|
*/
|
|
export interface FsFile {
|
|
read(offset: number, length: number): MaybePromise<Uint8Array>;
|
|
/** Returns the bytes written. */
|
|
write?(offset: number, data: Uint8Array): MaybePromise<number>;
|
|
getattr?(): MaybePromise<FsAttr>;
|
|
setattr?(attr: FsSetAttr): MaybePromise<void>;
|
|
fsync?(): MaybePromise<void>;
|
|
close?(): MaybePromise<void>;
|
|
}
|
|
|
|
/**
|
|
* A file system by path: every path absolute and normalised ("/" the
|
|
* root, no "." or ".." parts, names never contain "/"). getattr, readdir
|
|
* and open are required. A missing change method answers EROFS (symlink
|
|
* EOPNOTSUPP); hard links are not supported (EPERM). Handlers may be async
|
|
* and throw an FsError, or any Error with an errno `code` as Node's fs
|
|
* errors have, to answer with that errno; anything else is EIO and logged.
|
|
*/
|
|
export interface Fs {
|
|
getattr(path: string): MaybePromise<FsAttr>;
|
|
readdir(path: string): MaybePromise<FsDirEntry[]>;
|
|
/** Open a regular file. */
|
|
open(path: string, flags: FsOpenFlags): MaybePromise<FsFile>;
|
|
/** Create a regular file (EEXIST if the name exists) and return it open for reading and writing. */
|
|
create?(path: string, mode: number): MaybePromise<FsFile>;
|
|
mkdir?(path: string, mode: number): MaybePromise<void>;
|
|
symlink?(path: string, target: string): MaybePromise<void>;
|
|
readlink?(path: string): MaybePromise<string>;
|
|
/** `dir`: rmdir (ENOTEMPTY unless empty), else unlink. */
|
|
remove?(path: string, dir: boolean): MaybePromise<void>;
|
|
/** Replaces `to` if it exists (an empty directory, or a non-directory over a non-directory). */
|
|
rename?(from: string, to: string): MaybePromise<void>;
|
|
/** Change a path that is not open (chmod, utimes, truncate by name). */
|
|
setattr?(path: string, attr: FsSetAttr): MaybePromise<void>;
|
|
statfs?(): MaybePromise<FsStatFs>;
|
|
/** Called once the last mount of this file system is gone (unmounted, or the VM exited). */
|
|
close?(): MaybePromise<void>;
|
|
}
|