Skip to content

Commit 88a09e6

Browse files
fs: Implement fstab devicetree integration for EXT2
Device tree and/or automount support was not present and might be useful when for example the settings module needs early access to a filesystem. This patch adds support to mount an EXT2 filesystem automatically. The implementation chosen is to provide the drive name matching a disk drive specified in the device tree ie on an (e)MMC or SD card. Signed-off-by: Bas van Loon <[email protected]>
1 parent 4a09047 commit 88a09e6

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (C) 2025 Arch-Embedded B.V.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
Pre-defined EXT2 file systems.
6+
7+
compatible: "zephyr,fstab,ext2"
8+
9+
include: zephyr,fstab-common.yaml
10+
11+
properties:
12+
disk-name:
13+
type: string
14+
required: true
15+
description: |
16+
The disk name where the file system is mounted.

subsys/fs/ext2/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,13 @@ config EXT2_SUPERBLOCK_ALIGNMENT
5353
this value if they require alignment. This represents the alignment
5454
of struct ext2_disk_superblock in bytes.
5555

56+
DT_COMPAT_ZEPHYR_FSTAB_EXT2 := zephyr,fstab,ext2
57+
config EXT2_FSTAB_AUTOMOUNT
58+
bool "Support for fstab auto-mounting"
59+
depends on $(dt_compat_enabled,$(DT_COMPAT_ZEPHYR_FSTAB_EXT2))
60+
default y
61+
help
62+
Enable automatic mounting of Ext2 file system at boot time.
63+
5664
endmenu
5765
endif

subsys/fs/ext2/ext2_ops.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,37 @@ static const struct fs_file_system_t ext2_fs = {
647647
#endif
648648
};
649649

650+
#define DT_DRV_COMPAT zephyr_fstab_ext2
651+
652+
#define DEFINE_FS(inst) \
653+
BUILD_ASSERT(DT_INST_PROP(inst, disk_access), "EXT2 needs disk-access"); \
654+
struct fs_mount_t FS_FSTAB_ENTRY(DT_DRV_INST(inst)) = { \
655+
.type = FS_EXT2, \
656+
.mnt_point = DT_INST_PROP(inst, mount_point), \
657+
.storage_dev = DT_INST_PROP(inst, disk_name), \
658+
.flags = FSTAB_ENTRY_DT_MOUNT_FLAGS(DT_DRV_INST(inst)), \
659+
};
660+
661+
DT_INST_FOREACH_STATUS_OKAY(DEFINE_FS);
662+
663+
#ifdef CONFIG_EXT2_FSTAB_AUTOMOUNT
664+
#define REFERENCE_MOUNT(inst) (&FS_FSTAB_ENTRY(DT_DRV_INST(inst))),
665+
666+
static void automount_if_enabled(struct fs_mount_t *mountp)
667+
{
668+
int ret = 0;
669+
670+
if ((mountp->flags & FS_MOUNT_FLAG_AUTOMOUNT) != 0) {
671+
ret = fs_mount(mountp);
672+
if (ret < 0) {
673+
LOG_ERR("Error mounting filesystem: at %s: %d", mountp->mnt_point, ret);
674+
} else {
675+
LOG_DBG("EXT2 Filesystem \"%s\" initialized", mountp->mnt_point);
676+
}
677+
}
678+
}
679+
#endif /* CONFIG_EXT2_FSTAB_AUTOMOUNT */
680+
650681
static int ext2_init(void)
651682
{
652683
int rc = fs_register(FS_EXT2, &ext2_fs);
@@ -656,8 +687,19 @@ static int ext2_init(void)
656687
} else {
657688
LOG_DBG("Ext2 fs registered\n");
658689
}
690+
#ifdef CONFIG_EXT2_FSTAB_AUTOMOUNT
691+
if (rc == 0) {
692+
struct fs_mount_t *partitions[] = {DT_INST_FOREACH_STATUS_OKAY(REFERENCE_MOUNT)};
693+
694+
for (size_t i = 0; i < ARRAY_SIZE(partitions); i++) {
695+
struct fs_mount_t *mpi = partitions[i];
696+
697+
automount_if_enabled(mpi);
698+
}
699+
}
700+
#endif /* CONFIG_EXT2_FSTAB_AUTOMOUNT */
659701

660702
return rc;
661703
}
662704

663-
SYS_INIT(ext2_init, POST_KERNEL, 99);
705+
SYS_INIT(ext2_init, POST_KERNEL, CONFIG_FILE_SYSTEM_INIT_PRIORITY);

0 commit comments

Comments
 (0)