Mount Host Files

Serve file systems from the host into the guest.

import { createKernel, MemoryFs, nodeFs } from "unkernel";

const mem = new MemoryFs();
mem.onChange((c) => console.log(c.type, c.path)); // what the guest changed

const vm = await createKernel({
  mounts: {
    "/mnt/host": { "hello.txt": "hi", dir: { "a.bin": bytes }, "now.txt": () => Date() },
    "/mnt/mem": mem,
  },
});
await vm.exec("cp", ["/etc/os-release", "/mnt/mem/"]);
console.log(mem.readText("/os-release"));

const work = await vm.mount("/work", nodeFs("."), { readOnly: true }); // Node only
await work.unmount();

A file system is one of:

  • A plain object tree. Strings and bytes are files, objects are directories, and a function is a computed file, evaluated once per open.
  • A MemoryFs, which you read and write from the host.
  • nodeFs(dir) (Node), a host directory.
  • Your own Fs: getattr, readdir and open by path, sync or async, plus whichever change methods you want.

Mounts are served over 9p on vsock. Each guest write reaches the host as it happens, and a host change shows in the guest within a second.