xref: /aosp_15_r20/external/crosvm/docs/book/src/devices/fs.md (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1*bb4ee6a4SAndroid Build Coastguard Worker# Fs
2*bb4ee6a4SAndroid Build Coastguard Worker
3*bb4ee6a4SAndroid Build Coastguard WorkerCrosvm supports
4*bb4ee6a4SAndroid Build Coastguard Worker[virtio-fs](https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-45800011),
5*bb4ee6a4SAndroid Build Coastguard Workera shared file system that lets virtual machines access a directory tree on the host. It allows the
6*bb4ee6a4SAndroid Build Coastguard Workerguest to access files on the host machine. This section will explain how to create a shared
7*bb4ee6a4SAndroid Build Coastguard Workerdirectory. You can also find a runnable sample in `tools/examples/example_fs`.
8*bb4ee6a4SAndroid Build Coastguard Worker
9*bb4ee6a4SAndroid Build Coastguard Worker## Creating a Shared Directory on the Host Machine
10*bb4ee6a4SAndroid Build Coastguard Worker
11*bb4ee6a4SAndroid Build Coastguard WorkerTo create a shared directory, run the following commands in the host machine:
12*bb4ee6a4SAndroid Build Coastguard Worker
13*bb4ee6a4SAndroid Build Coastguard Worker```sh
14*bb4ee6a4SAndroid Build Coastguard Workermkdir host_shared_dir
15*bb4ee6a4SAndroid Build Coastguard WorkerHOST_SHARED_DIR=$(pwd)/host_shared_dir
16*bb4ee6a4SAndroid Build Coastguard Workercrosvm run \
17*bb4ee6a4SAndroid Build Coastguard Worker   --shared-dir "$HOST_SHARED_DIR:my_shared_tag:type=fs" \
18*bb4ee6a4SAndroid Build Coastguard Worker  ... # usual crosvm args
19*bb4ee6a4SAndroid Build Coastguard Worker```
20*bb4ee6a4SAndroid Build Coastguard Worker
21*bb4ee6a4SAndroid Build Coastguard WorkerIn the `--shared-dir` argument:
22*bb4ee6a4SAndroid Build Coastguard Worker
23*bb4ee6a4SAndroid Build Coastguard Worker- The first field is the directory to be shared (`$HOST_SHARED_DIR` in this example).
24*bb4ee6a4SAndroid Build Coastguard Worker- The second field is the tag that the VM will use to identify the device (`my_shared_tag` in this
25*bb4ee6a4SAndroid Build Coastguard Worker  example).
26*bb4ee6a4SAndroid Build Coastguard Worker- The remaining fields are key-value pairs configuring the shared directory.
27*bb4ee6a4SAndroid Build Coastguard Worker
28*bb4ee6a4SAndroid Build Coastguard WorkerTo see available options, run `crosvm run --help`.
29*bb4ee6a4SAndroid Build Coastguard Worker
30*bb4ee6a4SAndroid Build Coastguard Worker## Mount the Shared Directory in the Guest OS
31*bb4ee6a4SAndroid Build Coastguard Worker
32*bb4ee6a4SAndroid Build Coastguard WorkerNext, switch to the guest OS and run the following commands to set up the shared directory:
33*bb4ee6a4SAndroid Build Coastguard Worker
34*bb4ee6a4SAndroid Build Coastguard Worker```sh
35*bb4ee6a4SAndroid Build Coastguard Workersudo su
36*bb4ee6a4SAndroid Build Coastguard Workermkdir /tmp/guest_shared_dir
37*bb4ee6a4SAndroid Build Coastguard Workermount -t virtiofs my_shared_tag /tmp/guest_shared_dir
38*bb4ee6a4SAndroid Build Coastguard Worker```
39*bb4ee6a4SAndroid Build Coastguard Worker
40*bb4ee6a4SAndroid Build Coastguard WorkerYou can now add files to the shared directory. Any files you put in the `guest_shared_dir` will
41*bb4ee6a4SAndroid Build Coastguard Workerappear in the `host_shared_dir` on the host machine, and vice versa.
42*bb4ee6a4SAndroid Build Coastguard Worker
43*bb4ee6a4SAndroid Build Coastguard Worker## Running VirtioFS as root filesystem
44*bb4ee6a4SAndroid Build Coastguard Worker
45*bb4ee6a4SAndroid Build Coastguard WorkerIt is also possible to boot crosvm directly from a virtio-fs directory, as long as the directory
46*bb4ee6a4SAndroid Build Coastguard Workerstructure matches that of a valid rootfs. The outcome is similar to running a chroot but inside a
47*bb4ee6a4SAndroid Build Coastguard WorkerVM.
48*bb4ee6a4SAndroid Build Coastguard Worker
49*bb4ee6a4SAndroid Build Coastguard WorkerRunning VMs with virtio-fs as root filesystem may not be ideal as performance will not be as good as
50*bb4ee6a4SAndroid Build Coastguard Workerrunning a root disk with virtio-block, but it can be useful to run tests and debug while sharing
51*bb4ee6a4SAndroid Build Coastguard Workerfiles between host and guest.
52*bb4ee6a4SAndroid Build Coastguard Worker
53*bb4ee6a4SAndroid Build Coastguard WorkerYou can refer to the [advanced usage](../running_crosvm/advanced_usage.md#with-virtiofs) page for
54*bb4ee6a4SAndroid Build Coastguard Workerthe instructions on how to run virtio-fs as rootfs.
55