[Libguestfs] [PATCH 1/3] p2v: In non-ISO environment, don't use hard-coded port 50123.

Richard W.M. Jones rjones at redhat.com
Thu Jan 26 15:53:04 UTC 2017


When testing virt-p2v (eg. on the host machine) this prevents us from
testing two instances of virt-p2v at the same time because both will
try to use the same port.
---
 p2v/main.c | 16 ++++++++++++++++
 p2v/p2v.h  |  5 +++++
 p2v/ssh.c  |  3 ---
 3 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/p2v/main.c b/p2v/main.c
index 6aba331..8f8955c 100644
--- a/p2v/main.c
+++ b/p2v/main.c
@@ -51,6 +51,7 @@ char **all_disks;
 char **all_removable;
 char **all_interfaces;
 int is_iso_environment = 0;
+int nbd_local_port;
 int feature_colours_option = 0;
 int force_colour = 0;
 
@@ -143,6 +144,9 @@ main (int argc, char *argv[])
   bindtextdomain (PACKAGE, LOCALEBASEDIR);
   textdomain (PACKAGE);
 
+  /* We may use random(3) in this program. */
+  srandom (time (NULL) + getpid ());
+
   /* There is some raciness between slow devices being discovered by
    * the kernel and udev and virt-p2v running.  This is a partial
    * workaround, but a real fix involves handling hotplug events
@@ -216,6 +220,18 @@ main (int argc, char *argv[])
     usage (EXIT_FAILURE);
   }
 
+  if (is_iso_environment)
+    /* The p2v ISO should allow us to open up just about any port, so
+     * we can fix a port number in that case.  Using a predictable
+     * port number in this case should avoid rare errors if the port
+     * colides with another (ie. it'll either always fail or never
+     * fail).
+     */
+    nbd_local_port = 50123;
+  else
+    /* When testing on the local machine, choose a random port. */
+    nbd_local_port = 50000 + (random () % 10000);
+
   set_config_defaults (config);
 
   /* Parse /proc/cmdline (if it exists) or use the --cmdline parameter
diff --git a/p2v/p2v.h b/p2v/p2v.h
index e75d46e..df23898 100644
--- a/p2v/p2v.h
+++ b/p2v/p2v.h
@@ -52,6 +52,11 @@ extern char **all_interfaces;
  */
 extern int is_iso_environment;
 
+/* The local port that the NBD server listens on (incremented for
+ * each server which is started).
+ */
+extern int nbd_local_port;
+
 /* True if virt-v2v supports the --colours option. */
 extern int feature_colours_option;
 
diff --git a/p2v/ssh.c b/p2v/ssh.c
index 2dbc109..74ae126 100644
--- a/p2v/ssh.c
+++ b/p2v/ssh.c
@@ -1038,9 +1038,6 @@ compatible_version (const char *v2v_version)
   return 1;                     /* compatible */
 }
 
-/* The p2v ISO should allow us to open up just about any port. */
-static int nbd_local_port = 50123;
-
 mexp_h *
 open_data_connection (struct config *config, int *local_port, int *remote_port)
 {
-- 
2.9.3




More information about the Libguestfs mailing list