[PATCH] nvmetcli: allow running arbitrary commands non-interactively

Maurizio Lombardi mlombard at redhat.com
Mon Jul 28 08:55:04 PDT 2025


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.

Signed-off-by: Maurizio Lombardi <mlombard at redhat.com>
---
 nvmetcli | 33 +++++++++++++++++----------------
 1 file changed, 17 insertions(+), 16 deletions(-)

diff --git a/nvmetcli b/nvmetcli
index d949891..044b24c 100755
--- a/nvmetcli
+++ b/nvmetcli
@@ -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')
-- 
2.47.1




More information about the Linux-nvme mailing list