* [PATCH 1/3] commands: devlookup: factor out cmd_export_val
2025-04-04 18:06 [PATCH 0/3] commands: findmnt: add option to print kernel root option Ahmad Fatoum
@ 2025-04-04 18:06 ` Ahmad Fatoum
2025-04-04 18:06 ` [PATCH 2/3] commands: findmnt: add support for exporting target as variable Ahmad Fatoum
2025-04-04 18:06 ` [PATCH 3/3] commands: findmnt: add option to print kernel root option Ahmad Fatoum
2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2025-04-04 18:06 UTC (permalink / raw)
To: barebox; +Cc: bst, Ahmad Fatoum
Until we have support for capturing command output into a variable,
future commands will continue to have a -v parameter or similar for
printing the output of a command into a variable.
Add a helper for this that either prints it to the variable or to
stdout. This will allow easy migration to command output capture once we
have it.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
commands/devlookup.c | 16 ++--------------
common/command.c | 13 +++++++++++++
include/command.h | 2 ++
3 files changed, 17 insertions(+), 14 deletions(-)
diff --git a/commands/devlookup.c b/commands/devlookup.c
index bc9bd9461443..4a2c9b773661 100644
--- a/commands/devlookup.c
+++ b/commands/devlookup.c
@@ -9,18 +9,6 @@
#include <linux/ctype.h>
#include <environment.h>
-static int report(const char *variable, const char *val)
-{
- if (!val)
- return -(errno ?: EINVAL);
-
- if (variable)
- return setenv(variable, val);
-
- printf("%s\n", val);
- return 0;
-}
-
static int do_devlookup(int argc, char *argv[])
{
const char *variable = NULL, *devicefile, *paramname;
@@ -56,9 +44,9 @@ static int do_devlookup(int argc, char *argv[])
}
if (paramname)
- ret = report(variable, dev_get_param(cdev->dev, paramname));
+ ret = cmd_export_val(variable, dev_get_param(cdev->dev, paramname));
else
- ret = report(variable, dev_name(cdev->dev));
+ ret = cmd_export_val(variable, dev_name(cdev->dev));
out:
cdev_close(cdev);
diff --git a/common/command.c b/common/command.c
index 014b85f9a35f..a119b0f41b3a 100644
--- a/common/command.c
+++ b/common/command.c
@@ -128,6 +128,19 @@ struct command *find_cmd (const char *cmd)
}
EXPORT_SYMBOL(find_cmd);
+int cmd_export_val(const char *variable, const char *val)
+{
+ if (!val)
+ return -EINVAL;
+
+ if (variable)
+ return setenv(variable, val);
+
+ printf("%s\n", val);
+ return 0;
+}
+EXPORT_SYMBOL(cmd_export_val);
+
/*
* Put all commands into a linked list. Without module support we could use
* the raw command array, but with module support a list is easier to handle.
diff --git a/include/command.h b/include/command.h
index 26e06077b071..03ac270d1305 100644
--- a/include/command.h
+++ b/include/command.h
@@ -52,6 +52,7 @@ extern struct command * const __barebox_cmd_end[];
/* common/command.c */
#ifdef CONFIG_COMMAND_SUPPORT
struct command *find_cmd(const char *cmd);
+int cmd_export_val(const char *variable, const char *val);
int execute_command(int argc, char **argv);
void barebox_cmd_usage(struct command *cmdtp);
int run_command(const char *cmd);
@@ -59,6 +60,7 @@ int run_command(const char *cmd);
static inline struct command *find_cmd(const char *cmd) { return NULL; }
static inline int execute_command(int argc, char **argv) { return -ENOSYS; }
static inline void barebox_cmd_usage(struct command *cmdtp) {}
+static inline int cmd_export_val(const char *variable, const char *val) { return -ENOSYS; }
static inline int run_command(const char *cmd) { return -ENOSYS; }
#endif
--
2.39.5
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 2/3] commands: findmnt: add support for exporting target as variable
2025-04-04 18:06 [PATCH 0/3] commands: findmnt: add option to print kernel root option Ahmad Fatoum
2025-04-04 18:06 ` [PATCH 1/3] commands: devlookup: factor out cmd_export_val Ahmad Fatoum
@ 2025-04-04 18:06 ` Ahmad Fatoum
2025-04-04 18:06 ` [PATCH 3/3] commands: findmnt: add option to print kernel root option Ahmad Fatoum
2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2025-04-04 18:06 UTC (permalink / raw)
To: barebox; +Cc: bst, Ahmad Fatoum
This can be useful information in a script, so let's allow exporting it
into a variable.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
commands/Kconfig | 3 ++-
commands/findmnt.c | 31 ++++++++++++++++++++++++-------
2 files changed, 26 insertions(+), 8 deletions(-)
diff --git a/commands/Kconfig b/commands/Kconfig
index 313910d6649b..f36dcf02a8ea 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -690,7 +690,7 @@ config CMD_FINDMNT
help
Find a file system
- Usage: findmnt [ DEVICE | -T FILE ]
+ Usage: findmnt [ DEVICE | -T FILE ] [-v VARIABLE]
findmnt will list all mounted filesystems or search
for a filesystem when given the mountpoint or the
@@ -698,6 +698,7 @@ config CMD_FINDMNT
Options:
-T mount target file path
+ -v VARIABLE export target to specified VARIABLE
config CMD_PARTED
tristate
diff --git a/commands/findmnt.c b/commands/findmnt.c
index 3c9a520b8500..c64ef8760684 100644
--- a/commands/findmnt.c
+++ b/commands/findmnt.c
@@ -15,12 +15,18 @@ static void print_header(bool *header_printed)
*header_printed = true;
}
-static void report_findmnt(const struct fs_device *fsdev)
+static void report_findmnt(const struct fs_device *fsdev,
+ const char *variable)
{
const char *backingstore;
backingstore = fsdev->backingstore ?: cdev_name(fsdev->cdev) ?: "none";
+ if (variable) {
+ cmd_export_val(variable, backingstore);
+ return;
+ }
+
printf("%-20s%-25s%-10s%-20s\n", fsdev->path, backingstore,
fsdev->driver->drv.name, fsdev->options);
}
@@ -31,9 +37,13 @@ static int do_findmnt(int argc, char *argv[])
struct fs_device *target = NULL;
char *device = NULL;
int opt, dirfd = AT_FDCWD;
+ const char *variable = NULL;
- while ((opt = getopt(argc, argv, "T:")) > 0) {
+ while ((opt = getopt(argc, argv, "T:v:")) > 0) {
switch(opt) {
+ case 'v':
+ variable = optarg;
+ break;
case 'T':
target = get_fsdevice_by_path(dirfd, optarg);
if (!target)
@@ -50,9 +60,12 @@ static int do_findmnt(int argc, char *argv[])
if ((target && argc > 0) || (!target && argc > 1))
return COMMAND_ERROR_USAGE;
+ if (variable)
+ header_printed = true;
+
if (target) {
print_header(&header_printed);
- report_findmnt(target);
+ report_findmnt(target, variable);
return 0;
}
@@ -60,13 +73,16 @@ static int do_findmnt(int argc, char *argv[])
device = canonicalize_path(dirfd, argv[0]);
if (!device)
return COMMAND_ERROR;
+ } else if (variable) {
+ return COMMAND_ERROR_USAGE;
}
for_each_fs_device(target) {
if (!device || streq_ptr(target->path, device) ||
streq_ptr(target->backingstore, device)) {
- print_header(&header_printed);
- report_findmnt(target);
+ if (!variable)
+ print_header(&header_printed);
+ report_findmnt(target, variable);
} else {
const char *backingstore;
struct cdev *cdev;
@@ -80,7 +96,7 @@ static int do_findmnt(int argc, char *argv[])
if (streq_ptr(backingstore, cdev->name)) {
print_header(&header_printed);
- report_findmnt(target);
+ report_findmnt(target, variable);
}
cdev_close(cdev);
}
@@ -98,12 +114,13 @@ BAREBOX_CMD_HELP_TEXT("source device as an argument")
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_END
BAREBOX_CMD_START(findmnt)
.cmd = do_findmnt,
BAREBOX_CMD_DESC("find a file system")
- BAREBOX_CMD_OPTS("[ DEVICE | -T FILE ]")
+ BAREBOX_CMD_OPTS("[ DEVICE | -T FILE ] [-v VAR]")
BAREBOX_CMD_GROUP(CMD_GRP_FILE)
BAREBOX_CMD_HELP(cmd_findmnt_help)
BAREBOX_CMD_END
--
2.39.5
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 3/3] commands: findmnt: add option to print kernel root option
2025-04-04 18:06 [PATCH 0/3] commands: findmnt: add option to print kernel root option Ahmad Fatoum
2025-04-04 18:06 ` [PATCH 1/3] commands: devlookup: factor out cmd_export_val Ahmad Fatoum
2025-04-04 18:06 ` [PATCH 2/3] commands: findmnt: add support for exporting target as variable Ahmad Fatoum
@ 2025-04-04 18:06 ` Ahmad Fatoum
2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2025-04-04 18:06 UTC (permalink / raw)
To: barebox; +Cc: bst, Ahmad Fatoum
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@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
^ permalink raw reply [flat|nested] 4+ messages in thread