[Libguestfs] [PATCH 3/7] daemon: Add --cmdline option for testing.

Richard W.M. Jones rjones at redhat.com
Tue Jun 23 22:49:51 UTC 2015


This allows the Linux kernel command line to be specified on the
program command line.
---
 daemon/guestfsd.c   | 22 ++++++++++++++++++----
 daemon/guestfsd.pod |  6 ++++++
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c
index eef24d5..190ade2 100644
--- a/daemon/guestfsd.c
+++ b/daemon/guestfsd.c
@@ -130,7 +130,7 @@ static void
 usage (void)
 {
   fprintf (stderr,
-    "guestfsd [-r] [-v|--verbose]\n");
+    "guestfsd [--cmdline CMDLINE] [-r] [-v|--verbose]\n");
 }
 
 int
@@ -138,12 +138,14 @@ main (int argc, char *argv[])
 {
   static const char *options = "rv?";
   static const struct option long_options[] = {
+    { "cmdline", 1, 0, 0 },
     { "help", 0, 0, '?' },
     { "verbose", 0, 0, 'v' },
     { 0, 0, 0, 0 }
   };
   int c;
-  char *cmdline;
+  char *cmdline = NULL;
+  int option_index;
 
   ignore_value (chdir ("/"));
 
@@ -178,10 +180,21 @@ main (int argc, char *argv[])
     root_device = statbuf.st_dev;
 
   for (;;) {
-    c = getopt_long (argc, argv, options, long_options, NULL);
+    c = getopt_long (argc, argv, options, long_options, &option_index);
     if (c == -1) break;
 
     switch (c) {
+    case 0:                     /* options which are long only */
+      if (STREQ (long_options[option_index].name, "cmdline")) {
+        cmdline = strdup (optarg);
+        if (!cmdline)
+          error (EXIT_FAILURE, errno, "strdup");
+      }
+      else
+        error (EXIT_FAILURE, 0, "unknown long option: %s (%d)\n",
+               long_options[option_index].name, option_index);
+      break;
+
       /* The -r flag is used when running standalone.  It changes
        * several aspects of the daemon.
        */
@@ -210,7 +223,8 @@ main (int argc, char *argv[])
     exit (EXIT_FAILURE);
   }
 
-  cmdline = read_cmdline ();
+  if (!cmdline)
+    cmdline = read_cmdline ();
 
   /* Set the verbose flag. */
   verbose = verbose ||
diff --git a/daemon/guestfsd.pod b/daemon/guestfsd.pod
index 1ed31a9..96bbbd6 100644
--- a/daemon/guestfsd.pod
+++ b/daemon/guestfsd.pod
@@ -50,6 +50,12 @@ removed.
 
 Display brief help.
 
+=item B<--cmdline> CMDLINE
+
+This option is used for testing.  Instead of reading the Linux kernel
+command line from F</proc/cmdline>, use the value given on the
+program's command line.
+
 =item B<-r>
 
 Set the root filesystem to be F</> (instead of the default which is
-- 
2.3.1




More information about the Libguestfs mailing list