Blame SOURCES/0011-lib-v2v-Move-common-code-for-creating-v2v-directory-.patch

7bddab
From d604830d0da31280c347346343dc880e14965cf8 Mon Sep 17 00:00:00 2001
7bddab
From: "Richard W.M. Jones" <rjones@redhat.com>
7bddab
Date: Tue, 22 Mar 2022 13:49:20 +0000
7bddab
Subject: [PATCH] lib, v2v: Move common code for creating v2v directory to
7bddab
 Utils
7bddab
7bddab
I have also renamed the directory in the code from "tmpdir" to
7bddab
"v2vdir" since tmpdir was a bit generic and didn't accurately describe
7bddab
what this directory is for.
7bddab
7bddab
This is simple refactoring.
7bddab
7bddab
(cherry picked from commit 5a60e9a4f6e68d50c6b22eb0c8608aef563bf516)
7bddab
---
7bddab
 lib/utils.ml          |  9 +++++++++
7bddab
 lib/utils.mli         |  3 +++
7bddab
 v2v/v2v.ml            | 37 ++++++++++++++-----------------------
7bddab
 v2v/v2v_unit_tests.ml |  1 +
7bddab
 4 files changed, 27 insertions(+), 23 deletions(-)
7bddab
7bddab
diff --git a/lib/utils.ml b/lib/utils.ml
7bddab
index 4f0ff67a..876a44c6 100644
7bddab
--- a/lib/utils.ml
7bddab
+++ b/lib/utils.ml
7bddab
@@ -22,6 +22,7 @@ open Printf
7bddab
 
7bddab
 open Std_utils
7bddab
 open Tools_utils
7bddab
+open Unix_utils
7bddab
 open Common_gettext.Gettext
7bddab
 
7bddab
 let large_tmpdir =
7bddab
@@ -155,6 +156,14 @@ let error_if_no_ssh_agent () =
7bddab
   with Not_found ->
7bddab
     error (f_"ssh-agent authentication has not been set up ($SSH_AUTH_SOCK is not set).  This is required by qemu to do passwordless ssh access.  See the virt-v2v(1) man page for more information.")
7bddab
 
7bddab
+(* Create the directory containing inX and outX sockets. *)
7bddab
+let create_v2v_directory () =
7bddab
+  let d = Mkdtemp.temp_dir "v2v." in
7bddab
+  let running_as_root = Unix.geteuid () = 0 in
7bddab
+  if running_as_root then Unix.chmod d 0o711;
7bddab
+  On_exit.rmdir d;
7bddab
+  d
7bddab
+
7bddab
 (* Wait for a file to appear until a timeout. *)
7bddab
 let rec wait_for_file filename timeout =
7bddab
   if Sys.file_exists filename then true
7bddab
diff --git a/lib/utils.mli b/lib/utils.mli
7bddab
index 3f8e4b3c..c571cca5 100644
7bddab
--- a/lib/utils.mli
7bddab
+++ b/lib/utils.mli
7bddab
@@ -63,6 +63,9 @@ val backend_is_libvirt : unit -> bool
7bddab
 
7bddab
 val error_if_no_ssh_agent : unit -> unit
7bddab
 
7bddab
+val create_v2v_directory : unit -> string
7bddab
+(** Create the directory containing inX and outX sockets. *)
7bddab
+
7bddab
 val wait_for_file : string -> int -> bool
7bddab
 (** [wait_for_file filename timeout] waits up to [timeout] seconds for
7bddab
     [filename] to appear.  It returns [true] if the file appeared. *)
7bddab
diff --git a/v2v/v2v.ml b/v2v/v2v.ml
7bddab
index 6859a02c..71dd1c4d 100644
7bddab
--- a/v2v/v2v.ml
7bddab
+++ b/v2v/v2v.ml
7bddab
@@ -37,17 +37,8 @@ open Utils
7bddab
 let mac_re = PCRE.compile ~anchored:true "([[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}):(network|bridge|ip):(.*)"
7bddab
 let mac_ip_re = PCRE.compile ~anchored:true "([[:xdigit:]]|:|\\.)+"
7bddab
 
7bddab
-(* Create the temporary directory to control conversion.
7bddab
- *
7bddab
- * Because it contains sockets, if we're running as root then
7bddab
- * we must make it executable by world.
7bddab
- *)
7bddab
-let tmpdir =
7bddab
-  let tmpdir = Mkdtemp.temp_dir "v2v." in
7bddab
-  let running_as_root = geteuid () = 0 in
7bddab
-  if running_as_root then chmod tmpdir 0o711;
7bddab
-  On_exit.rmdir tmpdir;
7bddab
-  tmpdir
7bddab
+(* Create the temporary directory to control conversion. *)
7bddab
+let v2vdir = create_v2v_directory ()
7bddab
 
7bddab
 let rec main () =
7bddab
   let set_string_option_once optname optref arg =
7bddab
@@ -523,7 +514,7 @@ read the man page virt-v2v(1).
7bddab
   (* Start the input module (runs an NBD server in the background). *)
7bddab
   message (f_"Setting up the source: %s")
7bddab
     (Input_module.to_string input_options args);
7bddab
-  let source = Input_module.setup tmpdir input_options args in
7bddab
+  let source = Input_module.setup v2vdir input_options args in
7bddab
 
7bddab
   (* If --print-source then print the source metadata and exit. *)
7bddab
   if print_source then (
7bddab
@@ -540,28 +531,28 @@ read the man page virt-v2v(1).
7bddab
   let output_poptions = Output_module.parse_options output_options source in
7bddab
 
7bddab
   (* Do the conversion. *)
7bddab
-  with_open_out (tmpdir // "convert") (fun _ -> ());
7bddab
-  let inspect, target_meta = Convert.convert tmpdir conv_options source in
7bddab
-  unlink (tmpdir // "convert");
7bddab
+  with_open_out (v2vdir // "convert") (fun _ -> ());
7bddab
+  let inspect, target_meta = Convert.convert v2vdir conv_options source in
7bddab
+  unlink (v2vdir // "convert");
7bddab
 
7bddab
   (* Start the output module (runs an NBD server in the background). *)
7bddab
   message (f_"Setting up the destination: %s")
7bddab
     (Output_module.to_string output_options);
7bddab
-  let output_t = Output_module.setup tmpdir output_poptions source in
7bddab
+  let output_t = Output_module.setup v2vdir output_poptions source in
7bddab
 
7bddab
   (* Debug the v2vdir. *)
7bddab
   if verbose () then (
7bddab
-    let cmd = sprintf "ls -alZ %s 1>&2" (quote tmpdir) in
7bddab
+    let cmd = sprintf "ls -alZ %s 1>&2" (quote v2vdir) in
7bddab
     ignore (Sys.command cmd)
7bddab
   );
7bddab
 
7bddab
   (* Do the copy. *)
7bddab
-  with_open_out (tmpdir // "copy") (fun _ -> ());
7bddab
+  with_open_out (v2vdir // "copy") (fun _ -> ());
7bddab
 
7bddab
   (* Get the list of disks and corresponding sockets. *)
7bddab
   let rec loop acc i =
7bddab
-    let input_socket = sprintf "%s/in%d" tmpdir i
7bddab
-    and output_socket = sprintf "%s/out%d" tmpdir i in
7bddab
+    let input_socket = sprintf "%s/in%d" v2vdir i
7bddab
+    and output_socket = sprintf "%s/out%d" v2vdir i in
7bddab
     if Sys.file_exists input_socket && Sys.file_exists output_socket then
7bddab
       loop ((i, input_socket, output_socket) :: acc) (i+1)
7bddab
     else
7bddab
@@ -591,11 +582,11 @@ read the man page virt-v2v(1).
7bddab
   ) disks;
7bddab
 
7bddab
   (* End of copying phase. *)
7bddab
-  unlink (tmpdir // "copy");
7bddab
+  unlink (v2vdir // "copy");
7bddab
 
7bddab
   (* Do the finalization step. *)
7bddab
   message (f_"Creating output metadata");
7bddab
-  Output_module.finalize tmpdir output_poptions output_t
7bddab
+  Output_module.finalize v2vdir output_poptions output_t
7bddab
     source inspect target_meta;
7bddab
 
7bddab
   message (f_"Finishing off");
7bddab
@@ -604,7 +595,7 @@ read the man page virt-v2v(1).
7bddab
    * use the presence or absence of the file to determine if
7bddab
    * on-success or on-fail cleanup is required.
7bddab
    *)
7bddab
-  with_open_out (tmpdir // "done") (fun _ -> ())
7bddab
+  with_open_out (v2vdir // "done") (fun _ -> ())
7bddab
 
7bddab
 (* Conversion can fail or hang if there is insufficient free space in
7bddab
  * the large temporary directory.  Some input modules use large_tmpdir
7bddab
diff --git a/v2v/v2v_unit_tests.ml b/v2v/v2v_unit_tests.ml
7bddab
index 889f7998..bf5306c4 100644
7bddab
--- a/v2v/v2v_unit_tests.ml
7bddab
+++ b/v2v/v2v_unit_tests.ml
7bddab
@@ -26,6 +26,7 @@ open Std_utils
7bddab
 open Tools_utils
7bddab
 
7bddab
 open Types
7bddab
+open Utils
7bddab
 
7bddab
 let inspect_defaults = {
7bddab
   i_type = ""; i_distro = ""; i_osinfo = ""; i_arch = "";
7bddab
-- 
7bddab
2.31.1
7bddab