Blame SOURCES/0060-p2v-Allow-p2v-kernel-options-to-override-GUI-configu.patch

e76f14
From fe1e9ab849a7443907dd1968b34e89c58dba91f8 Mon Sep 17 00:00:00 2001
e76f14
From: "Richard W.M. Jones" <rjones@redhat.com>
e76f14
Date: Mon, 18 Apr 2016 14:40:29 +0100
e76f14
Subject: [PATCH] p2v: Allow p2v kernel options to override GUI configuration
e76f14
 (RHBZ#1327488).
e76f14
e76f14
Allow kernel options such as p2v.o=libvirt to override internal
e76f14
defaults, even for GUI configuration.
e76f14
e76f14
The main change is to split up the kernel conversion into two steps:
e76f14
reading the kernel command line configuration, and performing the
e76f14
conversion.  The kernel command line can then be read by the main
e76f14
program and used to initialize the config structure for either kernel
e76f14
conversion or GUI conversion.
e76f14
e76f14
A small adjustment is required in the test because p2v.pre no longer
e76f14
runs before kernel command line parsing.  (The aim is to have
e76f14
p2v.pre/post/fail still only run when doing a kernel conversion, not
e76f14
in the GUI case.)
e76f14
e76f14
(cherry picked from commit fb74a275c1438e6e0569b7fea85d2a4841a07a4e)
e76f14
---
e76f14
 p2v/gui.c                    |  2 +-
e76f14
 p2v/kernel.c                 | 57 +++++++++++++++++++++++++++-----------------
e76f14
 p2v/main.c                   | 27 +++++++++++----------
e76f14
 p2v/p2v.h                    |  5 ++--
e76f14
 p2v/test-virt-p2v-cmdline.sh |  4 +---
e76f14
 5 files changed, 54 insertions(+), 41 deletions(-)
e76f14
e76f14
diff --git a/p2v/gui.c b/p2v/gui.c
e76f14
index 569a295..015fa21 100644
e76f14
--- a/p2v/gui.c
e76f14
+++ b/p2v/gui.c
e76f14
@@ -75,7 +75,7 @@ static GtkWidget *run_dlg,
e76f14
  * Note that gtk_init etc have already been called in main.
e76f14
  */
e76f14
 void
e76f14
-gui_application (struct config *config)
e76f14
+gui_conversion (struct config *config)
e76f14
 {
e76f14
   /* Create the dialogs. */
e76f14
   create_connection_dialog (config);
e76f14
diff --git a/p2v/kernel.c b/p2v/kernel.c
e76f14
index e904502..6d9388b 100644
e76f14
--- a/p2v/kernel.c
e76f14
+++ b/p2v/kernel.c
e76f14
@@ -38,18 +38,19 @@ static void notify_ui_callback (int type, const char *data);
e76f14
 static void run_command (int verbose, const char *stage, const char *command);
e76f14
 
e76f14
 void
e76f14
-kernel_configuration (struct config *config, char **cmdline, int cmdline_source)
e76f14
+update_config_from_kernel_cmdline (struct config *config, char **cmdline)
e76f14
 {
e76f14
   const char *p;
e76f14
 
e76f14
-  p = get_cmdline_key (cmdline, "p2v.pre");
e76f14
+  p = get_cmdline_key (cmdline, "p2v.debug");
e76f14
   if (p)
e76f14
-    run_command (config->verbose, "p2v.pre", p);
e76f14
+    config->verbose = 1;
e76f14
 
e76f14
   p = get_cmdline_key (cmdline, "p2v.server");
e76f14
-  assert (p); /* checked by caller */
e76f14
-  free (config->server);
e76f14
-  config->server = strdup (p);
e76f14
+  if (p) {
e76f14
+    free (config->server);
e76f14
+    config->server = strdup (p);
e76f14
+  }
e76f14
 
e76f14
   p = get_cmdline_key (cmdline, "p2v.port");
e76f14
   if (p) {
e76f14
@@ -83,21 +84,6 @@ kernel_configuration (struct config *config, char **cmdline, int cmdline_source)
e76f14
   if (p)
e76f14
     config->sudo = 1;
e76f14
 
e76f14
-  /* We should now be able to connect and interrogate virt-v2v
e76f14
-   * on the conversion server.
e76f14
-   */
e76f14
-  p = get_cmdline_key (cmdline, "p2v.skip_test_connection");
e76f14
-  if (!p) {
e76f14
-    wait_network_online (config);
e76f14
-    if (test_connection (config) == -1) {
e76f14
-      const char *err = get_ssh_error ();
e76f14
-
e76f14
-      fprintf (stderr, "%s: error opening control connection to %s:%d: %s\n",
e76f14
-               guestfs_int_program_name, config->server, config->port, err);
e76f14
-      exit (EXIT_FAILURE);
e76f14
-    }
e76f14
-  }
e76f14
-
e76f14
   p = get_cmdline_key (cmdline, "p2v.name");
e76f14
   if (p) {
e76f14
     free (config->guestname);
e76f14
@@ -207,12 +193,39 @@ kernel_configuration (struct config *config, char **cmdline, int cmdline_source)
e76f14
     config->output_storage = strdup (p);
e76f14
   }
e76f14
 
e76f14
-  /* Undocumented command line tool used for testing command line parsing. */
e76f14
+  /* Undocumented command line parameter used for testing command line
e76f14
+   * parsing.
e76f14
+   */
e76f14
   p = get_cmdline_key (cmdline, "p2v.dump_config_and_exit");
e76f14
   if (p) {
e76f14
     print_config (config, stdout);
e76f14
     exit (EXIT_SUCCESS);
e76f14
   }
e76f14
+}
e76f14
+
e76f14
+/* Perform conversion using the kernel method. */
e76f14
+void
e76f14
+kernel_conversion (struct config *config, char **cmdline, int cmdline_source)
e76f14
+{
e76f14
+  const char *p;
e76f14
+
e76f14
+  /* Pre-conversion command. */
e76f14
+  p = get_cmdline_key (cmdline, "p2v.pre");
e76f14
+  if (p)
e76f14
+    run_command (config->verbose, "p2v.pre", p);
e76f14
+
e76f14
+  /* Connect to and interrogate virt-v2v on the conversion server. */
e76f14
+  p = get_cmdline_key (cmdline, "p2v.skip_test_connection");
e76f14
+  if (!p) {
e76f14
+    wait_network_online (config);
e76f14
+    if (test_connection (config) == -1) {
e76f14
+      const char *err = get_ssh_error ();
e76f14
+
e76f14
+      error (EXIT_FAILURE, 0,
e76f14
+             "error opening control connection to %s:%d: %s",
e76f14
+             config->server, config->port, err);
e76f14
+    }
e76f14
+  }
e76f14
 
e76f14
   /* Some disks must have been specified for conversion. */
e76f14
   if (config->disks == NULL || guestfs_int_count_strings (config->disks) == 0) {
e76f14
diff --git a/p2v/main.c b/p2v/main.c
e76f14
index f037716..99da3e4 100644
e76f14
--- a/p2v/main.c
e76f14
+++ b/p2v/main.c
e76f14
@@ -191,25 +191,26 @@ main (int argc, char *argv[])
e76f14
 
e76f14
   set_config_defaults (config);
e76f14
 
e76f14
-  /* If /proc/cmdline exists and contains "p2v.server=" then we enable
e76f14
-   * non-interactive configuration.
e76f14
-   * If /proc/cmdline contains p2v.debug then we enable verbose mode
e76f14
-   * even for interactive configuration.
e76f14
+  /* Parse /proc/cmdline (if it exists) or use the --cmdline parameter
e76f14
+   * to initialize the configuration.  This allows defaults to be pass
e76f14
+   * using the kernel command line, with additional GUI configuration
e76f14
+   * later.
e76f14
    */
e76f14
   if (cmdline == NULL) {
e76f14
     cmdline = parse_proc_cmdline ();
e76f14
-    if (cmdline == NULL)
e76f14
-      goto gui;
e76f14
-    cmdline_source = CMDLINE_SOURCE_PROC_CMDLINE;
e76f14
+    if (cmdline != NULL)
e76f14
+      cmdline_source = CMDLINE_SOURCE_PROC_CMDLINE;
e76f14
   }
e76f14
 
e76f14
-  if (get_cmdline_key (cmdline, "p2v.debug") != NULL)
e76f14
-    config->verbose = 1;
e76f14
+  if (cmdline)
e76f14
+    update_config_from_kernel_cmdline (config, cmdline);
e76f14
 
e76f14
-  if (get_cmdline_key (cmdline, "p2v.server") != NULL)
e76f14
-    kernel_configuration (config, cmdline, cmdline_source);
e76f14
+  /* If p2v.server exists, then we use the non-interactive kernel
e76f14
+   * conversion.  Otherwise we run the GUI.
e76f14
+   */
e76f14
+  if (config->server != NULL)
e76f14
+    kernel_conversion (config, cmdline, cmdline_source);
e76f14
   else {
e76f14
-  gui:
e76f14
     if (!gui_possible) {
e76f14
       fprintf (stderr,
e76f14
                _("%s: gtk_init_check returned false, indicating that\n"
e76f14
@@ -217,7 +218,7 @@ main (int argc, char *argv[])
e76f14
                guestfs_int_program_name);
e76f14
       exit (EXIT_FAILURE);
e76f14
     }
e76f14
-    gui_application (config);
e76f14
+    gui_conversion (config);
e76f14
   }
e76f14
 
e76f14
   guestfs_int_free_string_list (cmdline);
e76f14
diff --git a/p2v/p2v.h b/p2v/p2v.h
e76f14
index 097f294..9ccaf0f 100644
e76f14
--- a/p2v/p2v.h
e76f14
+++ b/p2v/p2v.h
e76f14
@@ -106,10 +106,11 @@ extern const char *get_cmdline_key (char **cmdline, const char *key);
e76f14
 #define CMDLINE_SOURCE_PROC_CMDLINE 2 /* /proc/cmdline */
e76f14
 
e76f14
 /* kernel.c */
e76f14
-extern void kernel_configuration (struct config *, char **cmdline, int cmdline_source);
e76f14
+extern void update_config_from_kernel_cmdline (struct config *config, char **cmdline);
e76f14
+extern void kernel_conversion (struct config *, char **cmdline, int cmdline_source);
e76f14
 
e76f14
 /* gui.c */
e76f14
-extern void gui_application (struct config *);
e76f14
+extern void gui_conversion (struct config *);
e76f14
 
e76f14
 /* conversion.c */
e76f14
 extern int start_conversion (struct config *, void (*notify_ui) (int type, const char *data));
e76f14
diff --git a/p2v/test-virt-p2v-cmdline.sh b/p2v/test-virt-p2v-cmdline.sh
e76f14
index bdd51d3..9629a05 100755
e76f14
--- a/p2v/test-virt-p2v-cmdline.sh
e76f14
+++ b/p2v/test-virt-p2v-cmdline.sh
e76f14
@@ -31,14 +31,12 @@ out=test-virt-p2v-cmdline.out
e76f14
 rm -f $out
e76f14
 
e76f14
 # The Linux kernel command line.
e76f14
-virt-p2v --cmdline='p2v.pre="echo 1 2 3" p2v.server=localhost p2v.port=123 p2v.username=user p2v.password=secret p2v.skip_test_connection p2v.name=test p2v.vcpus=4 p2v.memory=1G p2v.disks=sda,sdb,sdc p2v.removable=sdd p2v.interfaces=eth0,eth1 p2v.o=local p2v.oa=sparse p2v.oc=qemu:///session p2v.of=raw p2v.os=/var/tmp p2v.network=em1:wired,other p2v.dump_config_and_exit' > $out
e76f14
+virt-p2v --cmdline='p2v.server=localhost p2v.port=123 p2v.username=user p2v.password=secret p2v.skip_test_connection p2v.name=test p2v.vcpus=4 p2v.memory=1G p2v.disks=sda,sdb,sdc p2v.removable=sdd p2v.interfaces=eth0,eth1 p2v.o=local p2v.oa=sparse p2v.oc=qemu:///session p2v.of=raw p2v.os=/var/tmp p2v.network=em1:wired,other p2v.dump_config_and_exit' > $out
e76f14
 
e76f14
 # For debugging purposes.
e76f14
 cat $out
e76f14
 
e76f14
 # Check the output contains what we expect.
e76f14
-grep "^echo 1 2 3" $out
e76f14
-grep "^1 2 3" $out
e76f14
 grep "^conversion server.*localhost" $out
e76f14
 grep "^port.*123" $out
e76f14
 grep "^username.*user" $out
e76f14
-- 
aa0300
2.7.4
e76f14