-
Notifications
You must be signed in to change notification settings - Fork 7.6k
virtio: add virtiofs #86768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fkokosinski
wants to merge
5
commits into
zephyrproject-rtos:main
Choose a base branch
from
antmicro:66707-virtiofs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,818
−0
Open
virtio: add virtiofs #86768
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
910d44d
fs: add fuse primitives
jmichalski-ant 5a23b2d
fs: add virtiofs operations
jmichalski-ant 3fb1603
fs: implement zephyr fs api for virtiofs
jmichalski-ant b599a36
fs: virtiofs: add virtiofs sample
jmichalski-ant d535276
docs: virtio: add section about available drivers and samples
jmichalski-ant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) 2025 Antmicro <www.antmicro.com> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef ZEPHYR_INCLUDE_FS_VIRTIOFS_H_ | ||
#define ZEPHYR_INCLUDE_FS_VIRTIOFS_H_ | ||
#include <stdint.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
struct virtiofs_fs_data { | ||
uint32_t max_write; | ||
}; | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* ZEPHYR_INCLUDE_FS_VIRTIOFS_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
|
||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(virtiofs) | ||
|
||
target_sources(app PRIVATE src/main.c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
.. zephyr:code-sample:: virtiofs | ||
:name: virtiofs filesystem | ||
:relevant-api: file_system_api | ||
|
||
Use file system API over virtiofs. | ||
|
||
Overview | ||
******** | ||
|
||
This sample app demonstrates the use of Zephyr's :ref:`file system API | ||
<file_system_api>` over `virtiofs <https://virtio-fs.gitlab.io/>`_ by reading, creating and listing files and directories. | ||
In the case of virtiofs the mounted filesystem is a directory on the host. | ||
|
||
Requirements | ||
************ | ||
This sample requires `virtiofsd <https://gitlab.com/virtio-fs/virtiofsd>`_ to run. | ||
|
||
Building | ||
******** | ||
.. zephyr-app-commands:: | ||
:zephyr-app: samples/subsys/fs/virtiofs | ||
:board: qemu_x86_64 | ||
:goals: build | ||
:compact: | ||
|
||
|
||
Running | ||
******* | ||
Before launching QEMU ``virtiofsd`` has to be running. QEMU's arguments are embedded using :code:`CONFIG_QEMU_EXTRA_FLAGS` and socket path is set to :code:`/tmp/vhostqemu`, so ``virtiofsd`` has to be launched using | ||
|
||
.. code-block:: | ||
|
||
virtiofsd --socket-path=/tmp/vhostqemu -o source=shared_dir_path | ||
|
||
where :code:`shared_dir_path` is a directory that will be mounted on Zephyr side. | ||
Then you can launch QEMU using: | ||
|
||
.. code-block:: | ||
|
||
west build -t run | ||
|
||
This sample will list the files and directories in the mounted filesystem and print the contents of the file :code:`file` in the mounted directory. | ||
This sample will also create some files and directories. | ||
You can create the sample directory using :code:`prepare_sample_directory.sh`. | ||
|
||
Example output: | ||
|
||
.. code-block:: | ||
|
||
*** Booting Zephyr OS build v4.1.0-rc1-28-gc6816316fc50 *** | ||
/virtiofs directory tree: | ||
- dir2 (type=dir) | ||
- b (type=file, size=3) | ||
- a (type=file, size=2) | ||
- c (type=file, size=4) | ||
- dir (type=dir) | ||
- some_file (type=file, size=0) | ||
- nested_dir (type=dir) | ||
- some_other_file (type=file, size=0) | ||
- file (type=file, size=27) | ||
|
||
/virtiofs/file content: | ||
this is a file on the host | ||
|
||
|
||
After running the sample you can check the created files: | ||
|
||
.. code-block:: console | ||
|
||
shared_dir_path$ cat file_created_by_zephyr | ||
hello world | ||
shared_dir_path$ cat second_file_created_by_zephyr | ||
lorem ipsum |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
&pcie0 { | ||
virtio_pci: virtio_pci { | ||
compatible = "virtio,pci"; | ||
|
||
vendor-id = <0x1af4>; | ||
device-id = <0x105a>; | ||
|
||
interrupts = <0xb 0x0 0x0>; | ||
interrupt-parent = <&intc>; | ||
|
||
status = "okay"; | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
&pcie0 { | ||
virtio_pci: virtio_pci { | ||
compatible = "virtio,pci"; | ||
|
||
vendor-id = <0x1af4>; | ||
device-id = <0x105a>; | ||
|
||
interrupts = <0xb 0x0 0x0>; | ||
interrupt-parent = <&intc>; | ||
|
||
status = "okay"; | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Copyright (c) 2025 Antmicro <www.antmicro.com> | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
mkdir -p dir/nested_dir | ||
touch dir/some_file | ||
touch dir/nested_dir/some_other_file | ||
mkdir dir2 | ||
echo "a" > dir2/a | ||
echo "bb" > dir2/b | ||
echo "ccc" > dir2/c | ||
echo "this is a file on the host" > file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CONFIG_VIRTIO=y | ||
CONFIG_PCIE=y | ||
CONFIG_FILE_SYSTEM=y | ||
CONFIG_FILE_SYSTEM_VIRTIOFS=y | ||
CONFIG_HEAP_MEM_POOL_SIZE=100000 | ||
CONFIG_MAIN_STACK_SIZE=16384 | ||
CONFIG_QEMU_EXTRA_FLAGS="-chardev socket,id=char0,path=/tmp/vhostqemu -device vhost-user-fs-pci,queue-size=1024,chardev=char0,tag=myfs -m 32M -object memory-backend-memfd,id=mem,size=32M,share=on -numa node,memdev=mem" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
sample: | ||
name: virtio filesystem sample | ||
common: | ||
tags: | ||
- filesystem | ||
- virtio | ||
tests: | ||
sample.filesystem.virtiofs: | ||
build_only: true | ||
filter: CONFIG_DT_HAS_VIRTIO_PCI_ENABLED |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Copyright (c) 2025 Antmicro <www.antmicro.com> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
#include <zephyr/device.h> | ||
#include <zephyr/fs/fs.h> | ||
#include <zephyr/fs/virtiofs.h> | ||
#include <zephyr/kernel.h> | ||
|
||
#define VIRTIO_DEV DEVICE_DT_GET(DT_NODELABEL(virtio_pci)) | ||
|
||
#define MOUNT_POINT "/virtiofs" | ||
|
||
struct virtiofs_fs_data fs_data; | ||
|
||
static struct fs_mount_t mp = { | ||
.type = FS_VIRTIOFS, | ||
.fs_data = &fs_data, | ||
.flags = 0, | ||
.storage_dev = (void *)VIRTIO_DEV, | ||
.mnt_point = MOUNT_POINT, | ||
}; | ||
|
||
void dirtree(const char *path, int indent) | ||
{ | ||
struct fs_dir_t dir; | ||
|
||
fs_dir_t_init(&dir); | ||
if (fs_opendir(&dir, path) == 0) { | ||
while (1) { | ||
struct fs_dirent entry; | ||
|
||
if (fs_readdir(&dir, &entry) == 0) { | ||
if (entry.name[0] == '\0') { | ||
break; | ||
} | ||
if (entry.type == FS_DIR_ENTRY_DIR) { | ||
printf("%*s- %s (type=dir)\n", indent * 2, "", entry.name); | ||
char *subdir_path = k_malloc( | ||
strlen(path) + strlen(entry.name) + 2 | ||
); | ||
|
||
if (subdir_path == NULL) { | ||
printf("failed to allocate subdir path\n"); | ||
continue; | ||
} | ||
strcpy(subdir_path, path); | ||
strcat(subdir_path, "/"); | ||
strcat(subdir_path, entry.name); | ||
dirtree(subdir_path, indent + 1); | ||
k_free(subdir_path); | ||
} else { | ||
printf( | ||
"%*s- %s (type=file, size=%zu)\n", | ||
indent * 2, "", entry.name, entry.size | ||
); | ||
} | ||
} else { | ||
printf("failed to readdir %s\n", path); | ||
break; | ||
} | ||
}; | ||
|
||
fs_closedir(&dir); | ||
} else { | ||
printf("failed to opendir %s\n", path); | ||
} | ||
} | ||
|
||
void create_file(const char *path, const char *content) | ||
{ | ||
struct fs_file_t file; | ||
|
||
fs_file_t_init(&file); | ||
if (fs_open(&file, path, FS_O_CREATE | FS_O_WRITE) == 0) { | ||
fs_write(&file, content, strlen(content) + 1); | ||
} else { | ||
printf("failed to create %s\n", path); | ||
} | ||
fs_close(&file); | ||
} | ||
|
||
void print_file(const char *path) | ||
{ | ||
struct fs_file_t file; | ||
|
||
fs_file_t_init(&file); | ||
if (fs_open(&file, path, FS_O_READ) == 0) { | ||
char buf[256] = "\0"; | ||
int read_c = fs_read(&file, buf, sizeof(buf)); | ||
|
||
if (read_c >= 0) { | ||
buf[read_c] = 0; | ||
|
||
printf( | ||
"%s content:\n" | ||
"%s\n", | ||
path, buf | ||
); | ||
} else { | ||
printf("failed to read from %s\n", path); | ||
} | ||
|
||
fs_close(&file); | ||
} else { | ||
printf("failed to open %s\n", path); | ||
} | ||
} | ||
|
||
int main(void) | ||
{ | ||
if (fs_mount(&mp) == 0) { | ||
printf("%s directory tree:\n", MOUNT_POINT); | ||
dirtree(MOUNT_POINT, 0); | ||
printf("\n"); | ||
|
||
print_file(MOUNT_POINT"/file"); | ||
|
||
create_file("/virtiofs/file_created_by_zephyr", "hello world\n"); | ||
|
||
create_file("/virtiofs/second_file_created_by_zephyr", "lorem ipsum\n"); | ||
|
||
fs_mkdir("/virtiofs/dir_created_by_zephyr"); | ||
} else { | ||
printf("failed to mount %s\n", MOUNT_POINT); | ||
} | ||
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.