Blame SOURCES/0186-p2v-Add-p2v.pre-p2v.post-p2v.fail-commands-RHBZ-1229.patch

ffd6ed
From 47455b13d2defac1654d3ba5e28026be72ecbe39 Mon Sep 17 00:00:00 2001
ffd6ed
From: "Richard W.M. Jones" <rjones@redhat.com>
ffd6ed
Date: Tue, 9 Jun 2015 17:06:33 +0100
ffd6ed
Subject: [PATCH] p2v: Add p2v.pre, p2v.post, p2v.fail commands (RHBZ#1229385).
ffd6ed
ffd6ed
The default p2v.post command, when run from the ISO, is to poweroff
ffd6ed
the machine after successful conversion.
ffd6ed
ffd6ed
(cherry picked from commit b45f6a04359b4b64256b7b21a04416fa772c9423)
ffd6ed
---
ffd6ed
 p2v/kernel.c         | 43 +++++++++++++++++++++++++++++++++++++++++++
ffd6ed
 p2v/test-virt-p2v.sh |  2 +-
ffd6ed
 p2v/virt-p2v.pod     | 35 +++++++++++++++++++++++++++++++++++
ffd6ed
 3 files changed, 79 insertions(+), 1 deletion(-)
ffd6ed
ffd6ed
diff --git a/p2v/kernel.c b/p2v/kernel.c
ffd6ed
index 88d18bd..d7664d5 100644
ffd6ed
--- a/p2v/kernel.c
ffd6ed
+++ b/p2v/kernel.c
ffd6ed
@@ -29,16 +29,23 @@
ffd6ed
 #include <assert.h>
ffd6ed
 #include <locale.h>
ffd6ed
 #include <libintl.h>
ffd6ed
+#include <sys/types.h>
ffd6ed
+#include <sys/wait.h>
ffd6ed
 
ffd6ed
 #include "p2v.h"
ffd6ed
 
ffd6ed
 static void notify_ui_callback (int type, const char *data);
ffd6ed
+static void run_command (int verbose, const char *stage, const char *command);
ffd6ed
 
ffd6ed
 void
ffd6ed
 kernel_configuration (struct config *config, char **cmdline, int cmdline_source)
ffd6ed
 {
ffd6ed
   const char *p;
ffd6ed
 
ffd6ed
+  p = get_cmdline_key (cmdline, "p2v.pre");
ffd6ed
+  if (p)
ffd6ed
+    run_command (config->verbose, "p2v.pre", p);
ffd6ed
+
ffd6ed
   p = get_cmdline_key (cmdline, "p2v.server");
ffd6ed
   assert (p); /* checked by caller */
ffd6ed
   free (config->server);
ffd6ed
@@ -193,8 +200,21 @@ kernel_configuration (struct config *config, char **cmdline, int cmdline_source)
ffd6ed
 
ffd6ed
     fprintf (stderr, "%s: error during conversion: %s\n",
ffd6ed
              guestfs_int_program_name, err);
ffd6ed
+
ffd6ed
+    p = get_cmdline_key (cmdline, "p2v.fail");
ffd6ed
+    if (p)
ffd6ed
+      run_command (config->verbose, "p2v.fail", p);
ffd6ed
+
ffd6ed
     exit (EXIT_FAILURE);
ffd6ed
   }
ffd6ed
+
ffd6ed
+  p = get_cmdline_key (cmdline, "p2v.post");
ffd6ed
+  if (!p) {
ffd6ed
+    if (geteuid () == 0 && cmdline_source == CMDLINE_SOURCE_PROC_CMDLINE)
ffd6ed
+      p = "poweroff";
ffd6ed
+  }
ffd6ed
+  if (p)
ffd6ed
+    run_command (config->verbose, "p2v.post", p);
ffd6ed
 }
ffd6ed
 
ffd6ed
 static void
ffd6ed
@@ -218,3 +238,26 @@ notify_ui_callback (int type, const char *data)
ffd6ed
             guestfs_int_program_name, type, data);
ffd6ed
   }
ffd6ed
 }
ffd6ed
+
ffd6ed
+static void
ffd6ed
+run_command (int verbose, const char *stage, const char *command)
ffd6ed
+{
ffd6ed
+  int r;
ffd6ed
+
ffd6ed
+  if (STREQ (command, ""))
ffd6ed
+    return;
ffd6ed
+
ffd6ed
+  if (verbose)
ffd6ed
+    printf ("%s\n", command);
ffd6ed
+
ffd6ed
+  r = system (command);
ffd6ed
+  if (r == -1) {
ffd6ed
+    perror ("system");
ffd6ed
+    exit (EXIT_FAILURE);
ffd6ed
+  }
ffd6ed
+  if ((WIFEXITED (r) && WEXITSTATUS (r) != 0) || !WIFEXITED (r)) {
ffd6ed
+    fprintf (stderr, "%s: %s: unexpected failure of external command\n",
ffd6ed
+             guestfs_int_program_name, stage);
ffd6ed
+    exit (EXIT_FAILURE);
ffd6ed
+  }
ffd6ed
+}
ffd6ed
diff --git a/p2v/test-virt-p2v.sh b/p2v/test-virt-p2v.sh
ffd6ed
index f4d28d1..5184dab 100755
ffd6ed
--- a/p2v/test-virt-p2v.sh
ffd6ed
+++ b/p2v/test-virt-p2v.sh
ffd6ed
@@ -59,7 +59,7 @@ export PATH=$d:$PATH
ffd6ed
 # under test (because of the ./run script).
ffd6ed
 
ffd6ed
 # The Linux kernel command line.
ffd6ed
-cmdline="p2v.server=localhost p2v.name=windows p2v.debug p2v.disks=$f p2v.o=local p2v.os=$d p2v.network=em1:wired,other"
ffd6ed
+cmdline="p2v.server=localhost p2v.name=windows p2v.debug p2v.disks=$f p2v.o=local p2v.os=$d p2v.network=em1:wired,other p2v.post="
ffd6ed
 
ffd6ed
 virt-p2v --cmdline="$cmdline"
ffd6ed
 
ffd6ed
diff --git a/p2v/virt-p2v.pod b/p2v/virt-p2v.pod
ffd6ed
index 9c1dba1..dd44a1c 100644
ffd6ed
--- a/p2v/virt-p2v.pod
ffd6ed
+++ b/p2v/virt-p2v.pod
ffd6ed
@@ -422,6 +422,41 @@ option.  See L<virt-v2v(1)/OPTIONS>.
ffd6ed
 
ffd6ed
 If not specified, the default is C (on the conversion server).
ffd6ed
 
ffd6ed
+=item B<p2v.pre=COMMAND>
ffd6ed
+
ffd6ed
+=item B<p2v.pre="COMMAND ARG ...">
ffd6ed
+
ffd6ed
+Select a pre-conversion command to run.  Any command or script can be
ffd6ed
+specified here.  If the command contains spaces, you must quote the
ffd6ed
+whole command with double quotes.  The default is not to run any
ffd6ed
+command.
ffd6ed
+
ffd6ed
+=item B<p2v.post=poweroff>
ffd6ed
+
ffd6ed
+=item B<p2v.post=reboot>
ffd6ed
+
ffd6ed
+=item B<p2v.post=COMMAND>
ffd6ed
+
ffd6ed
+=item B<p2v.post="COMMAND ARG ...">
ffd6ed
+
ffd6ed
+Select a post-conversion command to run if conversion is successful.
ffd6ed
+This can be any command or script.  If the command contains spaces,
ffd6ed
+you must quote the whole command with double quotes.
ffd6ed
+
ffd6ed
+I<If> virt-p2v is running as root, I<and> the command line was set
ffd6ed
+from C</proc/cmdline> (not I<--cmdline>), then the default is to run
ffd6ed
+the L<poweroff(8)> command.  Otherwise the default is not to run any
ffd6ed
+command.
ffd6ed
+
ffd6ed
+=item B<p2v.fail=COMMAND>
ffd6ed
+
ffd6ed
+=item B<p2v.fail="COMMAND ARG ...">
ffd6ed
+
ffd6ed
+Select a post-conversion command to run if conversion fails.  Any
ffd6ed
+command or script can be specified here.  If the command contains
ffd6ed
+spaces, you must quote the whole command with double quotes.  The
ffd6ed
+default is not to run any command.
ffd6ed
+
ffd6ed
 =item B<ip=dhcp>
ffd6ed
 
ffd6ed
 Use DHCP for configuring the network interface (this is the default).
ffd6ed
-- 
ffd6ed
1.8.3.1
ffd6ed