Blame SOURCES/0145-p2v-Use-scp-to-copy-the-files-to-remote-debugging-di.patch

e76f14
From a87ea830a0dba6260d9e996ad25f50472200ad8e Mon Sep 17 00:00:00 2001
e76f14
From: "Richard W.M. Jones" <rjones@redhat.com>
e76f14
Date: Thu, 30 Jun 2016 13:42:53 +0100
e76f14
Subject: [PATCH] p2v: Use 'scp' to copy the files to remote debugging
e76f14
 directory.
e76f14
e76f14
Previously we copied / creates files in the remote dir by running
e76f14
complex shell commands like:
e76f14
e76f14
  cat > file <<'EOF'
e76f14
    ## the file was copied in here
e76f14
  EOF
e76f14
e76f14
This was a little hairy, but in particular it doesn't allow us to set
e76f14
the ssh session to cooked mode (so we can send ^C to cancel a
e76f14
conversion).
e76f14
e76f14
A cleaner way to do it is to use 'scp' to copy the files over.
e76f14
e76f14
(cherry picked from commit 6d9bac80b20024a397232f42d136fb48a537a7c4)
e76f14
---
e76f14
 p2v/Makefile.am          |   1 +
e76f14
 p2v/conversion.c         | 279 ++++++++++++++++++++++++-----------------------
e76f14
 p2v/p2v.h                |   3 +-
e76f14
 p2v/ssh.c                | 255 ++++++++++++++++++++++++++++++-------------
e76f14
 p2v/test-virt-p2v-scp.sh |  58 ++++++++++
e76f14
 p2v/test-virt-p2v.sh     |   5 +-
e76f14
 p2v/virt-p2v.pod         |   4 +
e76f14
 7 files changed, 388 insertions(+), 217 deletions(-)
e76f14
 create mode 100755 p2v/test-virt-p2v-scp.sh
e76f14
e76f14
diff --git a/p2v/Makefile.am b/p2v/Makefile.am
e76f14
index 487e198..9bee6f6 100644
e76f14
--- a/p2v/Makefile.am
e76f14
+++ b/p2v/Makefile.am
e76f14
@@ -26,6 +26,7 @@ EXTRA_DIST = \
e76f14
 	p2v.ks.in \
e76f14
 	p2v.service \
e76f14
 	test-virt-p2v-pxe.sshd_config.in \
e76f14
+	test-virt-p2v-scp.sh \
e76f14
 	test-virt-p2v-ssh.sh \
e76f14
 	virt-p2v.pod \
e76f14
 	virt-p2v-make-disk.in \
e76f14
diff --git a/p2v/conversion.c b/p2v/conversion.c
e76f14
index de2a4b2..f9b8350 100644
e76f14
--- a/p2v/conversion.c
e76f14
+++ b/p2v/conversion.c
e76f14
@@ -32,6 +32,7 @@
e76f14
 #include <locale.h>
e76f14
 #include <libintl.h>
e76f14
 #include <netdb.h>
e76f14
+#include <sys/stat.h>
e76f14
 #include <sys/types.h>
e76f14
 #include <sys/wait.h>
e76f14
 
e76f14
@@ -83,9 +84,12 @@ struct data_conn {
e76f14
 static pid_t start_qemu_nbd (int nbd_local_port, const char *device);
e76f14
 static int wait_qemu_nbd (int nbd_local_port, int timeout_seconds);
e76f14
 static void cleanup_data_conns (struct data_conn *data_conns, size_t nr);
e76f14
-static char *generate_libvirt_xml (struct config *, struct data_conn *);
e76f14
-static char *generate_wrapper_script (struct config *, const char *remote_dir);
e76f14
+static void generate_name (struct config *, const char *filename);
e76f14
+static void generate_libvirt_xml (struct config *, struct data_conn *, const char *filename);
e76f14
+static void generate_wrapper_script (struct config *, const char *remote_dir, const char *filename);
e76f14
+static void generate_dmesg_file (const char *filename);
e76f14
 static const char *map_interface_to_network (struct config *, const char *interface);
e76f14
+static void print_quoted (FILE *fp, const char *s);
e76f14
 
e76f14
 static char *conversion_error;
e76f14
 
e76f14
@@ -169,15 +173,16 @@ start_conversion (struct config *config,
e76f14
   int status;
e76f14
   size_t i, len;
e76f14
   const size_t nr_disks = guestfs_int_count_strings (config->disks);
e76f14
-  struct data_conn data_conns[nr_disks];
e76f14
-  CLEANUP_FREE char *remote_dir = NULL, *libvirt_xml = NULL,
e76f14
-    *wrapper_script = NULL;
e76f14
   time_t now;
e76f14
   struct tm tm;
e76f14
   mexp_h *control_h = NULL;
e76f14
-  char dmesg_cmd[] = "dmesg > /tmp/dmesg.XXXXXX", *dmesg_file = &dmesg_cmd[8];
e76f14
-  CLEANUP_FREE char *dmesg = NULL;
e76f14
-  int fd, r;
e76f14
+  struct data_conn data_conns[nr_disks];
e76f14
+  CLEANUP_FREE char *remote_dir = NULL;
e76f14
+  char tmpdir[]           = "/tmp/p2v.XXXXXX";
e76f14
+  char name_file[]        = "/tmp/p2v.XXXXXX/name";
e76f14
+  char libvirt_xml_file[] = "/tmp/p2v.XXXXXX/physical.xml";
e76f14
+  char wrapper_script[]   = "/tmp/p2v.XXXXXX/virt-v2v-wrapper.sh";
e76f14
+  char dmesg_file[]       = "/tmp/p2v.XXXXXX/dmesg";
e76f14
 
e76f14
 #if DEBUG_STDERR
e76f14
   print_config (config, stderr);
e76f14
@@ -273,59 +278,53 @@ start_conversion (struct config *config,
e76f14
   if (notify_ui)
e76f14
     notify_ui (NOTIFY_LOG_DIR, remote_dir);
e76f14
 
e76f14
-  /* Generate the libvirt XML. */
e76f14
-  libvirt_xml = generate_libvirt_xml (config, data_conns);
e76f14
-  if (libvirt_xml == NULL)
e76f14
-    goto out;
e76f14
-
e76f14
-#if DEBUG_STDERR && 0
e76f14
-  fprintf (stderr, "%s: libvirt XML:\n%s",
e76f14
-           guestfs_int_program_name, libvirt_xml);
e76f14
-#endif
e76f14
-
e76f14
-  /* Generate the virt-v2v wrapper script. */
e76f14
-  wrapper_script = generate_wrapper_script (config, remote_dir);
e76f14
-  if (wrapper_script == NULL)
e76f14
-    goto out;
e76f14
-
e76f14
-#if DEBUG_STDERR && 0
e76f14
-  fprintf (stderr, "%s: wrapper script:\n%s",
e76f14
-           guestfs_int_program_name, wrapper_script);
e76f14
-#endif
e76f14
-
e76f14
-  /* Get the output from the 'dmesg' command.  We will store this
e76f14
-   * on the remote server.
e76f14
-   */
e76f14
-  fd = mkstemp (dmesg_file);
e76f14
-  if (fd == -1) {
e76f14
-    perror ("mkstemp");
e76f14
-    goto skip_dmesg;
e76f14
-  }
e76f14
-  close (fd);
e76f14
-  r = system (dmesg_cmd);
e76f14
-  if (r == -1) {
e76f14
-    perror ("system");
e76f14
-    goto skip_dmesg;
e76f14
-  }
e76f14
-  if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) {
e76f14
-    fprintf (stderr, "'dmesg' failed (ignored)\n");
e76f14
-    goto skip_dmesg;
e76f14
+  /* Generate the local temporary directory. */
e76f14
+  if (mkdtemp (tmpdir) == NULL) {
e76f14
+    perror ("mkdtemp");
e76f14
+    cleanup_data_conns (data_conns, nr_disks);
e76f14
+    exit (EXIT_FAILURE);
e76f14
   }
e76f14
+  memcpy (name_file, tmpdir, strlen (tmpdir));
e76f14
+  memcpy (libvirt_xml_file, tmpdir, strlen (tmpdir));
e76f14
+  memcpy (wrapper_script, tmpdir, strlen (tmpdir));
e76f14
+  memcpy (dmesg_file, tmpdir, strlen (tmpdir));
e76f14
 
e76f14
-  ignore_value (read_whole_file (dmesg_file, &dmesg, NULL));
e76f14
- skip_dmesg:
e76f14
+  /* Generate the static files. */
e76f14
+  generate_name (config, name_file);
e76f14
+  generate_libvirt_xml (config, data_conns, libvirt_xml_file);
e76f14
+  generate_wrapper_script (config, remote_dir, wrapper_script);
e76f14
+  generate_dmesg_file (dmesg_file);
e76f14
 
e76f14
-  /* Open the control connection and start conversion */
e76f14
+  /* Open the control connection.  This also creates remote_dir. */
e76f14
   if (notify_ui)
e76f14
     notify_ui (NOTIFY_STATUS, _("Setting up the control connection ..."));
e76f14
 
e76f14
-  control_h = start_remote_connection (config,
e76f14
-                                       remote_dir, libvirt_xml,
e76f14
-                                       wrapper_script, dmesg);
e76f14
+  control_h = start_remote_connection (config, remote_dir);
e76f14
   if (control_h == NULL) {
e76f14
-    const char *err = get_ssh_error ();
e76f14
+    set_conversion_error ("could not open control connection over SSH to the conversion server: %s",
e76f14
+                          get_ssh_error ());
e76f14
+    goto out;
e76f14
+  }
e76f14
 
e76f14
-    set_conversion_error ("could not open control connection over SSH to the conversion server: %s", err);
e76f14
+  /* Copy the static files to the remote dir. */
e76f14
+  if (scp_file (config, name_file, remote_dir) == -1) {
e76f14
+    set_conversion_error ("scp: %s to %s: %s",
e76f14
+                          name_file, remote_dir, get_ssh_error ());
e76f14
+    goto out;
e76f14
+  }
e76f14
+  if (scp_file (config, libvirt_xml_file, remote_dir) == -1) {
e76f14
+    set_conversion_error ("scp: %s to %s: %s",
e76f14
+                          libvirt_xml_file, remote_dir, get_ssh_error ());
e76f14
+    goto out;
e76f14
+  }
e76f14
+  if (scp_file (config, wrapper_script, remote_dir) == -1) {
e76f14
+    set_conversion_error ("scp: %s to %s: %s",
e76f14
+                          wrapper_script, remote_dir, get_ssh_error ());
e76f14
+    goto out;
e76f14
+  }
e76f14
+  if (scp_file (config, dmesg_file, remote_dir) == -1) {
e76f14
+    set_conversion_error ("scp: %s to %s: %s",
e76f14
+                          dmesg_file, remote_dir, get_ssh_error ());
e76f14
     goto out;
e76f14
   }
e76f14
 
e76f14
@@ -667,20 +666,16 @@ cleanup_data_conns (struct data_conn *data_conns, size_t nr)
e76f14
 /* Macros "inspired" by src/launch-libvirt.c */
e76f14
 /* 
e76f14
 #define start_element(element)						\
e76f14
-  if (xmlTextWriterStartElement (xo, BAD_CAST (element)) == -1) {	\
e76f14
-    set_conversion_error ("xmlTextWriterStartElement: %m");		\
e76f14
-    return NULL;							\
e76f14
-  }									\
e76f14
+  if (xmlTextWriterStartElement (xo, BAD_CAST (element)) == -1)         \
e76f14
+    error (EXIT_FAILURE, errno, "xmlTextWriterStartElement");		\
e76f14
   do
e76f14
 
e76f14
 /* finish current </element> */
e76f14
 #define end_element()						\
e76f14
   while (0);							\
e76f14
   do {								\
e76f14
-    if (xmlTextWriterEndElement (xo) == -1) {			\
e76f14
-      set_conversion_error ("xmlTextWriterEndElement: %m");	\
e76f14
-      return NULL;						\
e76f14
-    }								\
e76f14
+    if (xmlTextWriterEndElement (xo) == -1)			\
e76f14
+      error (EXIT_FAILURE, errno, "xmlTextWriterEndElement");	\
e76f14
   } while (0)
e76f14
 
e76f14
 /* <element/> */
e76f14
@@ -689,80 +684,62 @@ cleanup_data_conns (struct data_conn *data_conns, size_t nr)
e76f14
 
e76f14
 /* key=value attribute of the current element. */
e76f14
 #define attribute(key,value)                                            \
e76f14
-  if (xmlTextWriterWriteAttribute (xo, BAD_CAST (key), BAD_CAST (value)) == -1) { \
e76f14
-    set_conversion_error ("xmlTextWriterWriteAttribute: %m");           \
e76f14
-    return NULL;                                                        \
e76f14
-  }
e76f14
+  do {                                                                  \
e76f14
+    if (xmlTextWriterWriteAttribute (xo, BAD_CAST (key), BAD_CAST (value)) == -1) \
e76f14
+    error (EXIT_FAILURE, errno, "xmlTextWriterWriteAttribute");         \
e76f14
+  } while (0)
e76f14
 
e76f14
 /* key=value, but value is a printf-style format string. */
e76f14
 #define attribute_format(key,fs,...)                                    \
e76f14
-  if (xmlTextWriterWriteFormatAttribute (xo, BAD_CAST (key),            \
e76f14
-					 fs, ##__VA_ARGS__) == -1) {	\
e76f14
-    set_conversion_error ("xmlTextWriterWriteFormatAttribute: %m");     \
e76f14
-    return NULL;                                                        \
e76f14
-  }
e76f14
+  do {                                                                  \
e76f14
+    if (xmlTextWriterWriteFormatAttribute (xo, BAD_CAST (key),          \
e76f14
+                                           fs, ##__VA_ARGS__) == -1)	\
e76f14
+      error (EXIT_FAILURE, errno, "xmlTextWriterWriteFormatAttribute"); \
e76f14
+  } while (0)
e76f14
 
e76f14
 /* A string, eg. within an element. */
e76f14
-#define string(str)						\
e76f14
-  if (xmlTextWriterWriteString (xo, BAD_CAST (str)) == -1) {	\
e76f14
-    set_conversion_error ("xmlTextWriterWriteString: %m");	\
e76f14
-    return NULL;						\
e76f14
-  }
e76f14
+#define string(str)                                             \
e76f14
+  do {                                                          \
e76f14
+    if (xmlTextWriterWriteString (xo, BAD_CAST (str)) == -1)	\
e76f14
+      error (EXIT_FAILURE, errno, "xmlTextWriterWriteString");	\
e76f14
+  } while (0)
e76f14
 
e76f14
 /* A string, using printf-style formatting. */
e76f14
 #define string_format(fs,...)                                           \
e76f14
-  if (xmlTextWriterWriteFormatString (xo, fs, ##__VA_ARGS__) == -1) {   \
e76f14
-    set_conversion_error ("xmlTextWriterWriteFormatString: %m");        \
e76f14
-    return NULL;                                                        \
e76f14
-  }
e76f14
+  do {                                                                  \
e76f14
+    if (xmlTextWriterWriteFormatString (xo, fs, ##__VA_ARGS__) == -1)   \
e76f14
+      error (EXIT_FAILURE, errno, "xmlTextWriterWriteFormatString");    \
e76f14
+  } while (0)
e76f14
 
e76f14
 /* An XML comment. */
e76f14
 #define comment(str)						\
e76f14
-  if (xmlTextWriterWriteComment (xo, BAD_CAST (str)) == -1) {	\
e76f14
-    set_conversion_error ("xmlTextWriterWriteComment: %m");	\
e76f14
-    return NULL;						\
e76f14
-  }
e76f14
+  do {                                                          \
e76f14
+    if (xmlTextWriterWriteComment (xo, BAD_CAST (str)) == -1)	\
e76f14
+      error (EXIT_FAILURE, errno, "xmlTextWriterWriteComment");	\
e76f14
+  } while (0)
e76f14
 
e76f14
 /* Write the libvirt XML for this physical machine.  Note this is not
e76f14
  * actually input for libvirt.  It's input for virt-v2v on the
e76f14
  * conversion server, and virt-v2v will (if necessary) generate the
e76f14
  * final libvirt XML.
e76f14
  */
e76f14
-static char *
e76f14
-generate_libvirt_xml (struct config *config, struct data_conn *data_conns)
e76f14
+static void
e76f14
+generate_libvirt_xml (struct config *config, struct data_conn *data_conns,
e76f14
+                      const char *filename)
e76f14
 {
e76f14
   uint64_t memkb;
e76f14
-  char *ret;
e76f14
-  CLEANUP_XMLBUFFERFREE xmlBufferPtr xb = NULL;
e76f14
-  xmlOutputBufferPtr ob;
e76f14
   CLEANUP_XMLFREETEXTWRITER xmlTextWriterPtr xo = NULL;
e76f14
   size_t i;
e76f14
 
e76f14
-  xb = xmlBufferCreate ();
e76f14
-  if (xb == NULL) {
e76f14
-    set_conversion_error ("xmlBufferCreate: %m");
e76f14
-    return NULL;
e76f14
-  }
e76f14
-  ob = xmlOutputBufferCreateBuffer (xb, NULL);
e76f14
-  if (ob == NULL) {
e76f14
-    set_conversion_error ("xmlOutputBufferCreateBuffer: %m");
e76f14
-    return NULL;
e76f14
-  }
e76f14
-  xo = xmlNewTextWriter (ob);
e76f14
-  if (xo == NULL) {
e76f14
-    set_conversion_error ("xmlNewTextWriter: %m");
e76f14
-    return NULL;
e76f14
-  }
e76f14
+  xo = xmlNewTextWriterFilename (filename, 0);
e76f14
+  if (xo == NULL)
e76f14
+    error (EXIT_FAILURE, errno, "xmlNewTextWriterFilename");
e76f14
 
e76f14
   if (xmlTextWriterSetIndent (xo, 1) == -1 ||
e76f14
-      xmlTextWriterSetIndentString (xo, BAD_CAST "  ") == -1) {
e76f14
-    set_conversion_error ("could not set XML indent: %m");
e76f14
-    return NULL;
e76f14
-  }
e76f14
-  if (xmlTextWriterStartDocument (xo, NULL, NULL, NULL) == -1) {
e76f14
-    set_conversion_error ("xmlTextWriterStartDocument: %m");
e76f14
-    return NULL;
e76f14
-  }
e76f14
+      xmlTextWriterSetIndentString (xo, BAD_CAST "  ") == -1)
e76f14
+    error (EXIT_FAILURE, errno, "could not set XML indent");
e76f14
+  if (xmlTextWriterStartDocument (xo, NULL, NULL, NULL) == -1)
e76f14
+    error (EXIT_FAILURE, errno, "xmlTextWriterStartDocument");
e76f14
 
e76f14
   memkb = config->memory / 1024;
e76f14
 
e76f14
@@ -907,17 +884,8 @@ generate_libvirt_xml (struct config *config, struct data_conn *data_conns)
e76f14
 
e76f14
   } end_element (); /* </domain> */
e76f14
 
e76f14
-  if (xmlTextWriterEndDocument (xo) == -1) {
e76f14
-    set_conversion_error ("xmlTextWriterEndDocument: %m");
e76f14
-    return NULL;
e76f14
-  }
e76f14
-  ret = (char *) xmlBufferDetach (xb); /* caller frees */
e76f14
-  if (ret == NULL) {
e76f14
-    set_conversion_error ("xmlBufferDetach: %m");
e76f14
-    return NULL;
e76f14
-  }
e76f14
-
e76f14
-  return ret;
e76f14
+  if (xmlTextWriterEndDocument (xo) == -1)
e76f14
+    error (EXIT_FAILURE, errno, "xmlTextWriterEndDocument");
e76f14
 }
e76f14
 
e76f14
 /* Using config->network_map, map the interface to a target network
e76f14
@@ -952,19 +920,18 @@ map_interface_to_network (struct config *config, const char *interface)
e76f14
 }
e76f14
 
e76f14
 /**
e76f14
- * Print a shell-quoted string on C<fp>.
e76f14
+ * Write the guest name into C<filename>.
e76f14
  */
e76f14
 static void
e76f14
-print_quoted (FILE *fp, const char *s)
e76f14
+generate_name (struct config *config, const char *filename)
e76f14
 {
e76f14
-  fprintf (fp, "\"");
e76f14
-  while (*s) {
e76f14
-    if (*s == '$' || *s == '`' || *s == '\\' || *s == '"')
e76f14
-      fprintf (fp, "\\");
e76f14
-    fprintf (fp, "%c", *s);
e76f14
-    ++s;
e76f14
-  }
e76f14
-  fprintf (fp, "\"");
e76f14
+  FILE *fp;
e76f14
+
e76f14
+  fp = fopen (filename, "w");
e76f14
+  if (fp == NULL)
e76f14
+    error (EXIT_FAILURE, errno, "fopen: %s", filename);
e76f14
+  fprintf (fp, "%s\n", config->guestname);
e76f14
+  fclose (fp);
e76f14
 }
e76f14
 
e76f14
 /**
e76f14
@@ -974,16 +941,15 @@ print_quoted (FILE *fp, const char *s)
e76f14
  * to "type" a long and complex single command line into the ssh
e76f14
  * connection when we start the conversion.
e76f14
  */
e76f14
-static char *
e76f14
-generate_wrapper_script (struct config *config, const char *remote_dir)
e76f14
+static void
e76f14
+generate_wrapper_script (struct config *config, const char *remote_dir,
e76f14
+                         const char *filename)
e76f14
 {
e76f14
   FILE *fp;
e76f14
-  char *output = NULL;
e76f14
-  size_t output_len = 0;
e76f14
 
e76f14
-  fp = open_memstream (&output, &output_len);
e76f14
+  fp = fopen (filename, "w");
e76f14
   if (fp == NULL)
e76f14
-    error (EXIT_FAILURE, errno, "open_memstream");
e76f14
+    error (EXIT_FAILURE, errno, "fopen: %s", filename);
e76f14
 
e76f14
   fprintf (fp, "#!/bin/bash -\n");
e76f14
   fprintf (fp, "\n");
e76f14
@@ -1082,7 +1048,42 @@ generate_wrapper_script (struct config *config, const char *remote_dir)
e76f14
            "fi\n",
e76f14
            remote_dir);
e76f14
 
e76f14
+  fprintf (fp, "\n");
e76f14
+  fprintf (fp, "# EOF\n");
e76f14
   fclose (fp);
e76f14
 
e76f14
-  return output;                /* caller frees */
e76f14
+  if (chmod (filename, 0755) == -1)
e76f14
+    error (EXIT_FAILURE, errno, "chmod: %s", filename);
e76f14
+}
e76f14
+
e76f14
+/**
e76f14
+ * Print a shell-quoted string on C<fp>.
e76f14
+ */
e76f14
+static void
e76f14
+print_quoted (FILE *fp, const char *s)
e76f14
+{
e76f14
+  fprintf (fp, "\"");
e76f14
+  while (*s) {
e76f14
+    if (*s == '$' || *s == '`' || *s == '\\' || *s == '"')
e76f14
+      fprintf (fp, "\\");
e76f14
+    fprintf (fp, "%c", *s);
e76f14
+    ++s;
e76f14
+  }
e76f14
+  fprintf (fp, "\"");
e76f14
+}
e76f14
+
e76f14
+/**
e76f14
+ * Put the output of the C<dmesg> command into C<filename>.
e76f14
+ *
e76f14
+ * If the command fails, this is non-fatal.
e76f14
+ */
e76f14
+static void
e76f14
+generate_dmesg_file (const char *filename)
e76f14
+{
e76f14
+  CLEANUP_FREE char *cmd = NULL;
e76f14
+
e76f14
+  if (asprintf (&cmd, "dmesg >%s 2>&1", filename) == -1)
e76f14
+    error (EXIT_FAILURE, errno, "asprintf");
e76f14
+
e76f14
+  ignore_value (system (cmd));
e76f14
 }
e76f14
diff --git a/p2v/p2v.h b/p2v/p2v.h
e76f14
index 52d5ccb..da30340 100644
e76f14
--- a/p2v/p2v.h
e76f14
+++ b/p2v/p2v.h
e76f14
@@ -130,8 +130,9 @@ extern int conversion_is_running (void);
e76f14
 /* ssh.c */
e76f14
 extern int test_connection (struct config *);
e76f14
 extern mexp_h *open_data_connection (struct config *, int *local_port, int *remote_port);
e76f14
-extern mexp_h *start_remote_connection (struct config *, const char *remote_dir, const char *libvirt_xml, const char *wrapper_script, const char *dmesg);
e76f14
+extern mexp_h *start_remote_connection (struct config *, const char *remote_dir);
e76f14
 extern const char *get_ssh_error (void);
e76f14
+extern int scp_file (struct config *config, const char *localfile, const char *remotefile);
e76f14
 
e76f14
 /* utils.c */
e76f14
 extern uint64_t get_blockdev_size (const char *dev);
e76f14
diff --git a/p2v/ssh.c b/p2v/ssh.c
e76f14
index f4adde4..270f1e5 100644
e76f14
--- a/p2v/ssh.c
e76f14
+++ b/p2v/ssh.c
e76f14
@@ -565,6 +565,182 @@ start_ssh (struct config *config, char **extra_args, int wait_prompt)
e76f14
   return h;
e76f14
 }
e76f14
 
e76f14
+/**
e76f14
+ * Upload a file to remote using L<scp(1)>.
e76f14
+ *
e76f14
+ * This is a simplified version of L</start_ssh> above.
e76f14
+ */
e76f14
+int
e76f14
+scp_file (struct config *config, const char *localfile, const char *remotefile)
e76f14
+{
e76f14
+  size_t j, nr_args;
e76f14
+  char port_str[64];
e76f14
+  char connect_timeout_str[128];
e76f14
+  CLEANUP_FREE char *remote = NULL;
e76f14
+  CLEANUP_FREE /* [sic] */ const char **args = NULL;
e76f14
+  mexp_h *h;
e76f14
+  const int ovecsize = 12;
e76f14
+  int ovector[ovecsize];
e76f14
+  int using_password_auth;
e76f14
+
e76f14
+  if (cache_ssh_identity (config) == -1)
e76f14
+    return -1;
e76f14
+
e76f14
+  /* Are we using password or identity authentication? */
e76f14
+  using_password_auth = config->identity_file == NULL;
e76f14
+
e76f14
+  /* Create the scp argument array. */
e76f14
+  if (using_password_auth)
e76f14
+    nr_args = 12;
e76f14
+  else
e76f14
+    nr_args = 14;
e76f14
+  args = malloc (sizeof (char *) * nr_args);
e76f14
+  if (args == NULL)
e76f14
+    error (EXIT_FAILURE, errno, "malloc");
e76f14
+
e76f14
+  j = 0;
e76f14
+  args[j++] = "scp";
e76f14
+  args[j++] = "-P";             /* Port. */
e76f14
+  snprintf (port_str, sizeof port_str, "%d", config->port);
e76f14
+  args[j++] = port_str;
e76f14
+  args[j++] = "-o";             /* Host key will always be novel. */
e76f14
+  args[j++] = "StrictHostKeyChecking=no";
e76f14
+  args[j++] = "-o";             /* ConnectTimeout */
e76f14
+  snprintf (connect_timeout_str, sizeof connect_timeout_str,
e76f14
+            "ConnectTimeout=%d", SSH_TIMEOUT);
e76f14
+  args[j++] = connect_timeout_str;
e76f14
+  if (using_password_auth) {
e76f14
+    /* Only use password authentication. */
e76f14
+    args[j++] = "-o";
e76f14
+    args[j++] = "PreferredAuthentications=keyboard-interactive,password";
e76f14
+  }
e76f14
+  else {
e76f14
+    /* Use identity file (private key). */
e76f14
+    args[j++] = "-o";
e76f14
+    args[j++] = "PreferredAuthentications=publickey";
e76f14
+    args[j++] = "-i";
e76f14
+    args[j++] = config->identity_file;
e76f14
+  }
e76f14
+  args[j++] = localfile;
e76f14
+  if (asprintf (&remote, "%s@%s:%s",
e76f14
+                config->username ? config->username : "root",
e76f14
+                config->server, remotefile) == -1)
e76f14
+    error (EXIT_FAILURE, errno, "asprintf");
e76f14
+  args[j++] = remote;
e76f14
+  args[j++] = NULL;
e76f14
+  assert (j == nr_args);
e76f14
+
e76f14
+#if DEBUG_STDERR && 0
e76f14
+  size_t i;
e76f14
+
e76f14
+  fputs ("scp command: ", stderr);
e76f14
+  for (i = 0; i < nr_args - 1; ++i) {
e76f14
+    if (i > 0) fputc (' ', stderr);
e76f14
+    fputs (args[i], stderr);
e76f14
+  }
e76f14
+  fputc ('\n', stderr);
e76f14
+#endif
e76f14
+
e76f14
+  /* Create the miniexpect handle. */
e76f14
+  h = mexp_spawnv ("scp", (char **) args);
e76f14
+  if (h == NULL) {
e76f14
+    set_ssh_internal_error ("scp: mexp_spawnv: %m");
e76f14
+    return -1;
e76f14
+  }
e76f14
+
e76f14
+  /* We want the ssh ConnectTimeout to be less than the miniexpect
e76f14
+   * timeout, so that if the server is completely unresponsive we
e76f14
+   * still see the error from ssh, not a timeout from miniexpect.  The
e76f14
+   * obvious solution to this is to set ConnectTimeout (above) and to
e76f14
+   * set the miniexpect timeout to be a little bit larger.
e76f14
+   */
e76f14
+  mexp_set_timeout (h, SSH_TIMEOUT + 20);
e76f14
+
e76f14
+  if (using_password_auth &&
e76f14
+      config->password && strlen (config->password) > 0) {
e76f14
+    CLEANUP_FREE char *ssh_message = NULL;
e76f14
+
e76f14
+    /* Wait for the password prompt. */
e76f14
+  wait_password_again:
e76f14
+    switch (mexp_expect (h,
e76f14
+                         (mexp_regexp[]) {
e76f14
+                           { 100, .re = password_re },
e76f14
+                           { 101, .re = ssh_message_re },
e76f14
+                           { 0 }
e76f14
+                         }, ovector, ovecsize)) {
e76f14
+    case 100:                   /* Got password prompt. */
e76f14
+      if (mexp_printf (h, "%s\n", config->password) == -1) {
e76f14
+        set_ssh_mexp_error ("mexp_printf");
e76f14
+        mexp_close (h);
e76f14
+        return -1;
e76f14
+      }
e76f14
+      break;
e76f14
+
e76f14
+    case 101:
e76f14
+      free (ssh_message);
e76f14
+      ssh_message = strndup (&h->buffer[ovector[2]], ovector[3]-ovector[2]);
e76f14
+      goto wait_password_again;
e76f14
+
e76f14
+    case MEXP_EOF:
e76f14
+      /* This is where we get to if the user enters an incorrect or
e76f14
+       * impossible hostname or port number.  Hopefully scp printed an
e76f14
+       * error message, and we picked it up and put it in
e76f14
+       * 'ssh_message' in case 101 above.  If not we have to print a
e76f14
+       * generic error instead.
e76f14
+       */
e76f14
+      if (ssh_message)
e76f14
+        set_ssh_error ("%s", ssh_message);
e76f14
+      else
e76f14
+        set_ssh_error ("scp closed the connection without printing an error.");
e76f14
+      mexp_close (h);
e76f14
+      return -1;
e76f14
+
e76f14
+    case MEXP_TIMEOUT:
e76f14
+      set_ssh_unexpected_timeout ("password prompt");
e76f14
+      mexp_close (h);
e76f14
+      return -1;
e76f14
+
e76f14
+    case MEXP_ERROR:
e76f14
+      set_ssh_mexp_error ("mexp_expect");
e76f14
+      mexp_close (h);
e76f14
+      return -1;
e76f14
+
e76f14
+    case MEXP_PCRE_ERROR:
e76f14
+      set_ssh_pcre_error ();
e76f14
+      mexp_close (h);
e76f14
+      return -1;
e76f14
+    }
e76f14
+  }
e76f14
+
e76f14
+  /* Wait for the scp subprocess to finish. */
e76f14
+  switch (mexp_expect (h, NULL, NULL, 0)) {
e76f14
+  case MEXP_EOF:
e76f14
+    break;
e76f14
+
e76f14
+  case MEXP_TIMEOUT:
e76f14
+    set_ssh_unexpected_timeout ("copying (scp) file");
e76f14
+    mexp_close (h);
e76f14
+    return -1;
e76f14
+
e76f14
+  case MEXP_ERROR:
e76f14
+    set_ssh_mexp_error ("mexp_expect");
e76f14
+    mexp_close (h);
e76f14
+    return -1;
e76f14
+
e76f14
+  case MEXP_PCRE_ERROR:
e76f14
+    set_ssh_pcre_error ();
e76f14
+    mexp_close (h);
e76f14
+    return -1;
e76f14
+  }
e76f14
+
e76f14
+  if (mexp_close (h) == -1) {
e76f14
+    set_ssh_internal_error ("scp: mexp_close: %m");
e76f14
+    return -1;
e76f14
+  }
e76f14
+
e76f14
+  return 0;
e76f14
+}
e76f14
+
e76f14
 static void add_input_driver (const char *name, size_t len);
e76f14
 static void add_output_driver (const char *name, size_t len);
e76f14
 static int compatible_version (const char *v2v_version);
e76f14
@@ -971,17 +1147,9 @@ wait_for_prompt (mexp_h *h)
e76f14
 }
e76f14
 
e76f14
 mexp_h *
e76f14
-start_remote_connection (struct config *config,
e76f14
-                         const char *remote_dir, const char *libvirt_xml,
e76f14
-                         const char *wrapper_script, const char *dmesg)
e76f14
+start_remote_connection (struct config *config, const char *remote_dir)
e76f14
 {
e76f14
   mexp_h *h;
e76f14
-  char magic[9];
e76f14
-
e76f14
-  if (guestfs_int_random_string (magic, 8) == -1) {
e76f14
-    perror ("random_string");
e76f14
-    return NULL;
e76f14
-  }
e76f14
 
e76f14
   h = start_ssh (config, NULL, 1);
e76f14
   if (h == NULL)
e76f14
@@ -996,16 +1164,9 @@ start_remote_connection (struct config *config,
e76f14
   if (wait_for_prompt (h) == -1)
e76f14
     goto error;
e76f14
 
e76f14
-  /* Write some useful config information to files in the remote directory. */
e76f14
-  if (mexp_printf (h, "echo '%s' > %s/name\n",
e76f14
-                   config->guestname, remote_dir) == -1) {
e76f14
-    set_ssh_mexp_error ("mexp_printf");
e76f14
-    goto error;
e76f14
-  }
e76f14
-
e76f14
-  if (wait_for_prompt (h) == -1)
e76f14
-    goto error;
e76f14
-
e76f14
+  /* It's simplest to create the remote 'time' file by running the date
e76f14
+   * command, as that won't send any special control characters.
e76f14
+   */
e76f14
   if (mexp_printf (h, "date > %s/time\n", remote_dir) == -1) {
e76f14
     set_ssh_mexp_error ("mexp_printf");
e76f14
     goto error;
e76f14
@@ -1014,62 +1175,6 @@ start_remote_connection (struct config *config,
e76f14
   if (wait_for_prompt (h) == -1)
e76f14
     goto error;
e76f14
 
e76f14
-  /* Upload the guest libvirt XML to the remote directory. */
e76f14
-  if (mexp_printf (h,
e76f14
-                   "cat > '%s/physical.xml' << '__%s__'\n"
e76f14
-                   "%s"
e76f14
-                   "__%s__\n",
e76f14
-                   remote_dir, magic,
e76f14
-                   libvirt_xml,
e76f14
-                   magic) == -1) {
e76f14
-    set_ssh_mexp_error ("mexp_printf");
e76f14
-    goto error;
e76f14
-  }
e76f14
-
e76f14
-  if (wait_for_prompt (h) == -1)
e76f14
-    goto error;
e76f14
-
e76f14
-  /* Upload the wrapper script to the remote directory. */
e76f14
-  if (mexp_printf (h,
e76f14
-                   "cat > '%s/virt-v2v-wrapper.sh' << '__%s__'\n"
e76f14
-                   "%s"
e76f14
-                   "__%s__\n",
e76f14
-                   remote_dir, magic,
e76f14
-                   wrapper_script,
e76f14
-                   magic) == -1) {
e76f14
-    set_ssh_mexp_error ("mexp_printf");
e76f14
-    goto error;
e76f14
-  }
e76f14
-
e76f14
-  if (wait_for_prompt (h) == -1)
e76f14
-    goto error;
e76f14
-
e76f14
-  if (mexp_printf (h, "chmod +x %s/virt-v2v-wrapper.sh\n", remote_dir) == -1) {
e76f14
-    set_ssh_mexp_error ("mexp_printf");
e76f14
-    goto error;
e76f14
-  }
e76f14
-
e76f14
-  if (wait_for_prompt (h) == -1)
e76f14
-    goto error;
e76f14
-
e76f14
-  if (dmesg != NULL) {
e76f14
-    /* Upload the physical host dmesg to the remote directory. */
e76f14
-    if (mexp_printf (h,
e76f14
-                     "cat > '%s/dmesg' << '__%s__'\n"
e76f14
-                     "%s"
e76f14
-                     "\n"
e76f14
-                     "__%s__\n",
e76f14
-                     remote_dir, magic,
e76f14
-                     dmesg,
e76f14
-                     magic) == -1) {
e76f14
-      set_ssh_mexp_error ("mexp_printf");
e76f14
-      goto error;
e76f14
-    }
e76f14
-
e76f14
-    if (wait_for_prompt (h) == -1)
e76f14
-      goto error;
e76f14
-  }
e76f14
-
e76f14
   return h;
e76f14
 
e76f14
  error:
e76f14
diff --git a/p2v/test-virt-p2v-scp.sh b/p2v/test-virt-p2v-scp.sh
e76f14
new file mode 100755
e76f14
index 0000000..be37a03
e76f14
--- /dev/null
e76f14
+++ b/p2v/test-virt-p2v-scp.sh
e76f14
@@ -0,0 +1,58 @@
e76f14
+#!/bin/bash -
e76f14
+# Copyright (C) 2014-2016 Red Hat Inc.
e76f14
+#
e76f14
+# This program is free software; you can redistribute it and/or modify
e76f14
+# it under the terms of the GNU General Public License as published by
e76f14
+# the Free Software Foundation; either version 2 of the License, or
e76f14
+# (at your option) any later version.
e76f14
+#
e76f14
+# This program is distributed in the hope that it will be useful,
e76f14
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
e76f14
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e76f14
+# GNU General Public License for more details.
e76f14
+#
e76f14
+# You should have received a copy of the GNU General Public License
e76f14
+# along with this program; if not, write to the Free Software
e76f14
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
e76f14
+
e76f14
+# This is an scp substitute used by test-virt-p2v.sh.
e76f14
+
e76f14
+TEMP=`getopt \
e76f14
+        -o 'o:P:' \
e76f14
+        -- "$@"`
e76f14
+if [ $? != 0 ]; then
e76f14
+    echo "$0: problem parsing the command line arguments"
e76f14
+    exit 1
e76f14
+fi
e76f14
+eval set -- "$TEMP"
e76f14
+
e76f14
+while true ; do
e76f14
+    case "$1" in
e76f14
+        # Regular arguments that we can just ignore.
e76f14
+        -o|-P)
e76f14
+            shift 2
e76f14
+            ;;
e76f14
+
e76f14
+        --)
e76f14
+            shift
e76f14
+	    break
e76f14
+            ;;
e76f14
+        *)
e76f14
+            echo "$0: internal error ($1)"
e76f14
+            exit 1
e76f14
+            ;;
e76f14
+    esac
e76f14
+done
e76f14
+
e76f14
+# Hopefully there are two arguments left, the source (local) file
e76f14
+# and a remote file of the form user@server:remote.
e76f14
+if [ $# -ne 2 ]; then
e76f14
+    echo "$0: incorrect number of arguments found:" "$@"
e76f14
+    exit 1
e76f14
+fi
e76f14
+
e76f14
+local="$1"
e76f14
+remote="$(echo $2 | awk -F: '{print $2}')"
e76f14
+
e76f14
+# Use the copy command.
e76f14
+exec cp "$local" "$remote"
e76f14
diff --git a/p2v/test-virt-p2v.sh b/p2v/test-virt-p2v.sh
e76f14
index 692894b..e232d42 100755
e76f14
--- a/p2v/test-virt-p2v.sh
e76f14
+++ b/p2v/test-virt-p2v.sh
e76f14
@@ -50,10 +50,11 @@ d=test-virt-p2v.d
e76f14
 rm -rf $d
e76f14
 mkdir $d
e76f14
 
e76f14
-# We don't want the program under test to run real 'ssh'.  It's
e76f14
-# unlikely to work.  Therefore create a dummy 'ssh' binary.
e76f14
+# We don't want the program under test to run real 'ssh' or 'scp'.
e76f14
+# They won't work.  Therefore create dummy 'ssh' and 'scp' binaries.
e76f14
 pushd $d
e76f14
 ln -sf ../test-virt-p2v-ssh.sh ssh
e76f14
+ln -sf ../test-virt-p2v-scp.sh scp
e76f14
 popd
e76f14
 export PATH=$d:$PATH
e76f14
 
e76f14
diff --git a/p2v/virt-p2v.pod b/p2v/virt-p2v.pod
e76f14
index 247acce..ef6a9db 100644
e76f14
--- a/p2v/virt-p2v.pod
e76f14
+++ b/p2v/virt-p2v.pod
e76f14
@@ -56,6 +56,10 @@ by virt-p2v, and it will not work if this is disabled on the
e76f14
 conversion server.  (C<AllowTcpForwarding> must be C<yes> in the
e76f14
 L<sshd_config(5)> file on the conversion server).
e76f14
 
e76f14
+The scp (secure copy) feature of ssh is required by virt-p2v so it can
e76f14
+send over small files (this is I<not> the method by which disks are
e76f14
+copied).
e76f14
+
e76f14
 The conversion server does not need to be a physical machine.  It
e76f14
 could be a virtual machine, as long as it has sufficient memory and
e76f14
 disk space to do the conversion, and as long as the physical machine
e76f14
-- 
e76f14
1.8.3.1
e76f14