Hi guys!
I’ve been playing around with rust, wasmtime and the wasi api for learning(and maybe future projects).
Something I can’t see to figure out is how to implement a virtual fs using wasi. While searching I found that wasi used to have a virtfs module that -maybe- could fit in my use case, but it looks like it was removed from the crate. What I basically want to do involves the following structure:
/foo - an ordinary, preopened-directory, with read and write capabilities.
/bar - an in-memory directory.
/bar/hello.txt - an in-memory file the guest can read to.
Sorry if this sounds like a noob question, I’m just starting in Rust + Wasm world and any starting point or help would be highly apreciated🤗
You can implement WasiFile
and WasiDir
for your own type to represent /bar
and /bar/hello.txt
. You can then mount it by calling .push_preopened_dir()
on the WasiCtx
.
Hi! There are ways to do this, as bjorn3 mentioned. It also turns out that, as convenient as they can be to get things started with one wasm component, filesystem-based APIs make it difficult to have multiple wasm components that work together. The wasm component model is designed to support virtualization of APIs, with the idea being that instead of writing filesystem-based APIs, you can just write regular APIs, using the new wit and wit-bindgen, and have the same ability to swap out implementations.
dan and I rewrote the wasi-common crate last winter. the virtfs did end up getting removed as part of the rewrite because all of the internal interfaces changed. if you want a starting point, i have one here, its complete except for symlinks (which turn out to be a pretty big part of getting a filesystem implementation right under wasi) https://github.com/bytecodealliance/wasmtime/pull/2635
if your virtfs doesnt need symlinks, this one may work for you
i havent touched that PR in a long time. it looks like there are some other incomplete parts as well
but at any rate, the approach taken by that PR is to implement the virtfs as native rust code. the component model, which is in progress but not quite ready for production use yet, will make it possible to implement a virtfs as webassembly code.
Last updated: Nov 22 2024 at 16:03 UTC