Skip to content

Commit d8bd0f9

Browse files
samples: fs: Add EXT2 automount sample.
Add a simple sample that tests the functionality of the EXT2 automount feature. The sample creates a ramdisk and writes some content and tries to read it back after unmount and mount operation. Signed-off-by: Bas van Loon <[email protected]>
1 parent 88a09e6 commit d8bd0f9

File tree

6 files changed

+173
-0
lines changed

6 files changed

+173
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2025 Arch-Embedded B.V.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(ext2_fstab_sample)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.. zephyr:code-sample:: ext2-fstab
2+
:name: EXT2 filesystem fstab
3+
:relevant-api: file_system_api
4+
5+
Define ext2 filesystems in the devicetree.
6+
7+
Overview
8+
***********
9+
10+
This sample shows how to define an ext2 fstab entry in the devicetree.
11+
The sample is run on the ``native_sim`` board and RAM disk is used for ext2 storage.
12+
13+
Building and running
14+
********************
15+
16+
To run this sample, build it for the ``native_sim`` board
17+
and afterwards run the generated executable file within the build folder.
18+
19+
The RAM disk sample for the ``native_sim`` board can be built as follows:
20+
21+
.. zephyr-app-commands::
22+
:zephyr-app: samples/subsys/fs/ext2_fstab
23+
:host-os: unix
24+
:board: native_sim
25+
:gen-args: -DDTC_OVERLAY_FILE=ext2_fstab.overlay
26+
:goals: build
27+
:compact:
28+
29+
Sample Output
30+
=============
31+
32+
When the sample runs successfully you should see following message on the screen:
33+
.. code-block:: console
34+
35+
I: Filesystem access successful
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2025 Arch-Embedded B.V.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
fstab {
9+
compatible = "zephyr,fstab";
10+
ext2fs1: ext2fs1 {
11+
compatible = "zephyr,fstab,ext2";
12+
automount;
13+
disk-access;
14+
disk-name = "RAM";
15+
mount-point = "/ext2";
16+
};
17+
};
18+
19+
ramdisk0 {
20+
compatible = "zephyr,ram-disk";
21+
disk-name = "RAM";
22+
sector-size = <512>;
23+
sector-count = <128>;
24+
};
25+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CONFIG_DISK_ACCESS=y
2+
CONFIG_DISK_DRIVERS=y
3+
4+
CONFIG_LOG=y
5+
CONFIG_LOG_MODE_MINIMAL=y
6+
7+
CONFIG_FILE_SYSTEM=y
8+
CONFIG_FILE_SYSTEM_MKFS=y
9+
CONFIG_FILE_SYSTEM_EXT2=y
10+
CONFIG_EXT2_FSTAB_AUTOMOUNT=y
11+
12+
# Needed for MKFS
13+
CONFIG_ENTROPY_GENERATOR=y
14+
CONFIG_XOSHIRO_RANDOM_GENERATOR=y
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sample:
2+
name: EXT2 fstab sample
3+
tests:
4+
sample.filesystem.ext2.fstab:
5+
platform_allow:
6+
- native_sim
7+
extra_args:
8+
- EXTRA_DTC_OVERLAY_FILE="ext2_fstab.overlay"
9+
tags: filesystem
10+
harness: console
11+
harness_config:
12+
type: one_line
13+
regex:
14+
- "I: Filesystem access successful"
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2025 Endress+Hauser AG
3+
* Copyright (c) 2025 Arch-Embedded B.V.
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include <zephyr/device.h>
9+
#include <zephyr/fs/fs.h>
10+
#include <zephyr/kernel.h>
11+
#include <zephyr/logging/log.h>
12+
#include <zephyr/storage/disk_access.h>
13+
14+
LOG_MODULE_REGISTER(main, LOG_LEVEL_INF);
15+
16+
#define AUTOMOUNT_NODE DT_NODELABEL(ext2fs1)
17+
#define MOUNT_POINT DT_PROP(AUTOMOUNT_NODE, mount_point)
18+
19+
FS_FSTAB_DECLARE_ENTRY(AUTOMOUNT_NODE);
20+
21+
#define FILE_PATH MOUNT_POINT "/hello.txt"
22+
23+
int main(void)
24+
{
25+
struct fs_file_t file;
26+
int rc;
27+
static const char data[] = "Hello";
28+
/* You can get direct mount point to automounted node too */
29+
struct fs_mount_t *auto_mount_point = &FS_FSTAB_ENTRY(AUTOMOUNT_NODE);
30+
struct fs_dirent stat;
31+
32+
fs_file_t_init(&file);
33+
34+
rc = fs_open(&file, FILE_PATH, FS_O_CREATE | FS_O_WRITE);
35+
if (rc != 0) {
36+
LOG_ERR("Accessing filesystem failed");
37+
return rc;
38+
}
39+
40+
rc = fs_write(&file, data, strlen(data));
41+
if (rc != strlen(data)) {
42+
LOG_ERR("Writing filesystem failed");
43+
return rc;
44+
}
45+
46+
rc = fs_close(&file);
47+
if (rc != 0) {
48+
LOG_ERR("Closing file failed");
49+
return rc;
50+
}
51+
52+
/* You can unmount the automount node */
53+
rc = fs_unmount(auto_mount_point);
54+
if (rc != 0) {
55+
LOG_ERR("Failed to do unmount");
56+
return rc;
57+
};
58+
59+
/* And mount it back */
60+
rc = fs_mount(auto_mount_point);
61+
if (rc != 0) {
62+
LOG_ERR("Failed to remount the auto-mount node");
63+
return rc;
64+
}
65+
66+
/* Is the file still there? */
67+
rc = fs_stat(FILE_PATH, &stat);
68+
if (rc != 0) {
69+
LOG_ERR("File status check failed %d", rc);
70+
return rc;
71+
}
72+
73+
LOG_INF("Filesystem access successful");
74+
75+
return 0;
76+
}

0 commit comments

Comments
 (0)