Skip to contentSkip to conversation

nvmetcli: allow running arbitrary commands non-interactively

PATCH Applied Maurizio Lombardi 3 participants 3 messages Jul 28, 2025 – Jul 8 NVMe

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmetcli: allow running arbitrary commands non-interactively

This patch refactors the command-line argument handling
to allow for the execution of any valid shell command directly from the system's command line, without entering the interactive mode.

Previously, only a limited, hardcoded set of
commands (save, restore, clear, ls) could be run non-interactively.
This change removes the special case for the ls command and introduces a generic mechanism that concatenates all command-line arguments and executes them as a single command within the nvmetcli shell.

This enhancement improves the scriptability of the tool.

For example, users can now run complex commands like:

$ nvmetcli /subsystems create nqn.2024-07.org.example:sub1
$ nvmetcli /ports ls

The usage information has been updated to reflect this new functionality.

---
 nvmetcli | 33 +++++++++++++++++----------------
 1 file changed, 17 insertions(+), 16 deletions(-)
nvmetcli +17 -16
index d949891..044b24c 100755
@@ -666,7 +666,8 @@ def usage():
     print("syntax: %s save [file_to_save_to]" % sys.argv[0])
     print("        %s restore [file_to_restore_from]" % sys.argv[0])
     print("        %s clear" % sys.argv[0])
-    print("        %s ls" % sys.argv[0])
+    print("        %s --help  (Print this information)" % sys.argv[0])
+    print("        %s <CMD>   (Run shell command and exit)" % sys.argv[0])
     sys.exit(-1)
 
 
@@ -703,14 +704,14 @@ def clear(unused):
     nvme.Root().clear_existing()
 
 
-def ls(unused):
+def execute_cmd(cmd):
     shell = configshell.shell.ConfigShell('~/.nvmetcli')
     UIRootNode(shell)
-    shell.run_cmdline("ls")
+    shell.run_cmdline(cmd)
     sys.exit(0)
 
 
-funcs = dict(save=save, restore=restore, clear=clear, ls=ls)
+funcs = dict(save=save, restore=restore, clear=clear)
 
 
 def main():
@@ -718,23 +719,23 @@ def main():
         print("%s: must run as root." % sys.argv[0], file=sys.stderr)
         sys.exit(-1)
 
-    if len(sys.argv) > 3:
-        usage()
-
-    if len(sys.argv) == 2 or len(sys.argv) == 3:
+    if len(sys.argv) > 1:
         if sys.argv[1] == "--help":
             usage()
 
-        if sys.argv[1] not in funcs.keys():
-            usage()
+        if sys.argv[1] in funcs.keys():
+            if len(sys.argv) > 3:
+                usage()
+            if len(sys.argv) == 3:
+                savefile = sys.argv[2]
+            else:
+                savefile = None
 
-        if len(sys.argv) == 3:
-            savefile = sys.argv[2]
+            funcs[sys.argv[1]](savefile)
+            return
         else:
-            savefile = None
-
-        funcs[sys.argv[1]](savefile)
-        return
+            concat_args = ' '.join(sys.argv[1:])
+            execute_cmd(concat_args)
 
     try:
         shell = configshell.shell.ConfigShell('~/.nvmetcli')
Signature · 1 line
2.47.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Maurizio Lombardi

This patch refactors the command-line argument handling
to allow for the execution of any valid shell command directly from the system's command line, without entering the interactive mode.

Previously, only a limited, hardcoded set of
commands (save, restore, clear, ls) could be run non-interactively.
This change removes the special case for the ls command and introduces a generic mechanism that concatenates all command-line arguments and executes them as a single command within the nvmetcli shell.

This enhancement improves the scriptability of the tool.

Quoted 7 lines

For example, users can now run complex commands like:

$ nvmetcli /subsystems create nqn.2024-07.org.example:sub1
$ nvmetcli /ports ls

The usage information has been updated to reflect this new functionality.

IMO this is a useful addition to the nvmetcli tool. I have also tested changes and looks good to me:

#2 lore raw
Quoted 19 lines · Nilay Shroff
Quoted 18 lines · Maurizio Lombardi

This patch refactors the command-line argument handling
to allow for the execution of any valid shell command directly from the system's command line, without entering the interactive mode.

Previously, only a limited, hardcoded set of
commands (save, restore, clear, ls) could be run non-interactively.
This change removes the special case for the ls command and introduces a generic mechanism that concatenates all command-line arguments and executes them as a single command within the nvmetcli shell.

This enhancement improves the scriptability of the tool.

For example, users can now run complex commands like:

$ nvmetcli /subsystems create nqn.2024-07.org.example:sub1
$ nvmetcli /ports ls
Quoted 8 lines
Quoted 3 lines

The usage information has been updated to reflect this new functionality.

IMO this is a useful addition to the nvmetcli tool. I have also tested changes and looks good to me:

I've applied the patch to the integration tree.

Thanks,
Daniel