[Libguestfs] [PATCH v2 1/3] p2v: Add --test-disk option for testing conversions.

Richard W.M. Jones rjones at redhat.com
Mon May 30 20:48:58 UTC 2016


Use, for example:

  ./run virt-p2v --test-disk=$(pwd)/test-data/phony-guests/windows.img

to test conversions using a file of test data instead of the real host
/dev/sda.
---
 docs/guestfs-hacking.pod | 13 ++++++++-----
 p2v/main.c               | 32 +++++++++++++++++++++++++++++++-
 p2v/virt-p2v.pod         |  5 +++++
 3 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/docs/guestfs-hacking.pod b/docs/guestfs-hacking.pod
index c8db955..4e325d6 100644
--- a/docs/guestfs-hacking.pod
+++ b/docs/guestfs-hacking.pod
@@ -757,12 +757,15 @@ in virt-v2v.  They are converted in the same way as foreign VMs.
 
 =head2 Running virt-p2v
 
-You can run the F<p2v/virt-p2v> binary directly, although it's not
-really recommended, but it's OK for quick tests of the GUI (but don't
-try doing a conversion that way).
+You can run the F<p2v/virt-p2v> binary directly, but it will try to
+convert your machine's real F</dev/sda> which is unlikely to work
+well.  However virt-p2v also has a test mode in which you can supply a
+test disk:
 
-A better way is to run virt-p2v inside a VM on the local machine.  To
-do that, do:
+ ./run virt-p2v --test-disk=../test-data/phony-guests/windows.img
+
+A more realistic test is to run virt-p2v inside a VM on the local
+machine.  To do that, do:
 
  make -C p2v run-virt-p2v
 
diff --git a/p2v/main.c b/p2v/main.c
index 40b1ff2..e1be311 100644
--- a/p2v/main.c
+++ b/p2v/main.c
@@ -43,6 +43,8 @@ char **all_disks;
 char **all_removable;
 char **all_interfaces;
 
+static const char *test_disk = NULL;
+
 static void udevadm_settle (void);
 static void set_config_defaults (struct config *config);
 static void find_all_disks (void);
@@ -56,6 +58,7 @@ static const struct option long_options[] = {
   { "cmdline", 1, 0, 0 },
   { "long-options", 0, 0, 0 },
   { "short-options", 0, 0, 0 },
+  { "test-disk", 1, 0, 0 },
   { "verbose", 0, 0, 'v' },
   { "version", 0, 0, 'V' },
   { 0, 0, 0, 0 }
@@ -155,6 +158,15 @@ main (int argc, char *argv[])
         cmdline = parse_cmdline_string (optarg);
         cmdline_source = CMDLINE_SOURCE_COMMAND_LINE;
       }
+      else if (STREQ (long_options[option_index].name, "test-disk")) {
+        if (test_disk != NULL)
+          error (EXIT_FAILURE, 0,
+                 _("only a single --test-disk option can be used"));
+        if (optarg[0] != '/')
+          error (EXIT_FAILURE, 0,
+                 _("--test-disk must be an absolute path"));
+        test_disk = optarg;
+      }
       else
         error (EXIT_FAILURE, 0,
                _("unknown long option: %s (%d)"),
@@ -298,11 +310,29 @@ set_config_defaults (struct config *config)
   else
     config->flags = 0;
 
-  find_all_disks ();
+  /* Find all block devices in the system. */
+  if (!test_disk)
+    find_all_disks ();
+  else {
+    /* For testing and debugging purposes, you can use
+     * --test-disk=/path/to/disk.img
+     */
+    all_disks = malloc (2 * sizeof (char *));
+    if (all_disks == NULL)
+      error (EXIT_FAILURE, errno, "realloc");
+    all_disks[0] = strdup (test_disk);
+    if (all_disks[0] == NULL)
+      error (EXIT_FAILURE, errno, "strdup");
+    all_disks[1] = NULL;
+  }
   if (all_disks)
     config->disks = guestfs_int_copy_string_list (all_disks);
+
+  /* Find all removable devices in the system. */
   if (all_removable)
     config->removable = guestfs_int_copy_string_list (all_removable);
+
+  /* Find all network interfaces in the system. */
   find_all_interfaces ();
   if (all_interfaces)
     config->interfaces = guestfs_int_copy_string_list (all_interfaces);
diff --git a/p2v/virt-p2v.pod b/p2v/virt-p2v.pod
index b4598d9..013efc2 100644
--- a/p2v/virt-p2v.pod
+++ b/p2v/virt-p2v.pod
@@ -581,6 +581,11 @@ Display help.
 This is used for debugging. Instead of parsing the kernel command line
 from F</proc/cmdline>, parse the string parameter C<CMDLINE>.
 
+=item B<--test-disk=/PATH/TO/DISK.IMG>
+
+For testing or debugging purposes, replace F</dev/sda> with a local
+file.  You must use an absolute path.
+
 =item B<-v>
 
 =item B<--verbose>
-- 
2.7.4




More information about the Libguestfs mailing list