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

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