Blob Blame History Raw
From d604830d0da31280c347346343dc880e14965cf8 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Tue, 22 Mar 2022 13:49:20 +0000
Subject: [PATCH] lib, v2v: Move common code for creating v2v directory to
 Utils

I have also renamed the directory in the code from "tmpdir" to
"v2vdir" since tmpdir was a bit generic and didn't accurately describe
what this directory is for.

This is simple refactoring.

(cherry picked from commit 5a60e9a4f6e68d50c6b22eb0c8608aef563bf516)
---
 lib/utils.ml          |  9 +++++++++
 lib/utils.mli         |  3 +++
 v2v/v2v.ml            | 37 ++++++++++++++-----------------------
 v2v/v2v_unit_tests.ml |  1 +
 4 files changed, 27 insertions(+), 23 deletions(-)

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