Stream: general

Topic: wasmtime-wasi fs examples


view this post on Zulip Dan Soares (Dec 18 2021 at 07:25):

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🤗

view this post on Zulip bjorn3 (Dec 18 2021 at 11:44):

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.

view this post on Zulip Dan Gohman (Dec 18 2021 at 13:48):

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.

view this post on Zulip Pat Hickey (Dec 18 2021 at 17:54):

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

An in-memory filesystem implementation for the wasi-common crate.

view this post on Zulip Pat Hickey (Dec 18 2021 at 17:54):

if your virtfs doesnt need symlinks, this one may work for you

view this post on Zulip Pat Hickey (Dec 18 2021 at 17:56):

i havent touched that PR in a long time. it looks like there are some other incomplete parts as well

view this post on Zulip Pat Hickey (Dec 18 2021 at 17:59):

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