[PATCH 3/3] commands: findmnt: add option to print kernel root option

Ahmad Fatoum a.fatoum at pengutronix.de
Fri Apr 4 11:06:40 PDT 2025


There exists currently no way to view the kernel root option that
barebox would generate when instructed to via global.bootm.root_dev,
but without actually mounting the file system.

Add a new -K option that can print this. This can be useful in scripts
that want to fixup the root line, so it's interpreted by the rdinit, but
not by the kernel.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 commands/Kconfig   |  3 ++-
 commands/findmnt.c | 29 ++++++++++++++++++++++++++---
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/commands/Kconfig b/commands/Kconfig
index f36dcf02a8ea..58e0a8b0bfe4 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -690,7 +690,7 @@ config CMD_FINDMNT
 	help
 	  Find a file system
 
-	  Usage: findmnt [ DEVICE | -T FILE ] [-v VARIABLE]
+	  Usage: findmnt [-K] [ DEVICE | -T FILE ] [-v VARIABLE]
 
 	  findmnt will list all mounted filesystems or search
 	  for a filesystem when given the mountpoint or the
@@ -699,6 +699,7 @@ config CMD_FINDMNT
 	  Options:
 		-T		mount target file path
 		-v VARIABLE	export target to specified VARIABLE
+		-K		output kernel rootarg line
 
 config CMD_PARTED
 	tristate
diff --git a/commands/findmnt.c b/commands/findmnt.c
index c64ef8760684..21868fcc0e56 100644
--- a/commands/findmnt.c
+++ b/commands/findmnt.c
@@ -6,6 +6,7 @@
 #include <fs.h>
 #include <errno.h>
 #include <getopt.h>
+#include <block.h>
 
 static void print_header(bool *header_printed)
 {
@@ -38,9 +39,13 @@ static int do_findmnt(int argc, char *argv[])
 	char *device = NULL;
 	int opt, dirfd = AT_FDCWD;
 	const char *variable = NULL;
+	bool kernelopt = false;
 
-	while ((opt = getopt(argc, argv, "T:v:")) > 0) {
+	while ((opt = getopt(argc, argv, "KT:v:")) > 0) {
 		switch(opt) {
+		case 'K':
+			kernelopt = true;
+			break;
 		case 'v':
 			variable = optarg;
 			break;
@@ -57,7 +62,8 @@ static int do_findmnt(int argc, char *argv[])
 	argv += optind;
 	argc -= optind;
 
-	if ((target && argc > 0) || (!target && argc > 1))
+	if ((target && argc > 0) || (!target && argc > 1) ||
+	    (target && kernelopt) || (kernelopt && argc != 1))
 		return COMMAND_ERROR_USAGE;
 
 	if (variable)
@@ -77,6 +83,22 @@ static int do_findmnt(int argc, char *argv[])
 		return COMMAND_ERROR_USAGE;
 	}
 
+	if (kernelopt) {
+		struct cdev *cdev;
+		char *root_arg;
+		int ret;
+
+		cdev = cdev_open_by_name(device, O_RDONLY);
+		if (!cdev)
+			return COMMAND_ERROR;;
+
+		root_arg = cdev_get_linux_rootarg(cdev);
+		ret = cmd_export_val(variable, root_arg);
+		free(root_arg);
+
+		return ret;
+	}
+
 	for_each_fs_device(target) {
 		if (!device || streq_ptr(target->path, device) ||
 		    streq_ptr(target->backingstore, device)) {
@@ -115,12 +137,13 @@ BAREBOX_CMD_HELP_TEXT("")
 BAREBOX_CMD_HELP_TEXT("Options:")
 BAREBOX_CMD_HELP_OPT ("-T",  "mount target file path")
 BAREBOX_CMD_HELP_OPT ("-v VARIABLE",  "export target to specified VARIABLE")
+BAREBOX_CMD_HELP_OPT ("-K",  "output kernel rootarg line")
 BAREBOX_CMD_HELP_END
 
 BAREBOX_CMD_START(findmnt)
 	.cmd		= do_findmnt,
 	BAREBOX_CMD_DESC("find a file system")
-	BAREBOX_CMD_OPTS("[ DEVICE | -T FILE ] [-v VAR]")
+	BAREBOX_CMD_OPTS("[-K] [ DEVICE | -T FILE ] [-v VAR]")
 	BAREBOX_CMD_GROUP(CMD_GRP_FILE)
 	BAREBOX_CMD_HELP(cmd_findmnt_help)
 BAREBOX_CMD_END
-- 
2.39.5




More information about the barebox mailing list