Blame SOURCES/0024-v2v-Allow-large-temporary-directory-to-be-set-on-a-g.patch

62f9b7
From 186c237ac1cb6f6830cfe2d08dfdcfdbdffab264 Mon Sep 17 00:00:00 2001
62f9b7
From: "Richard W.M. Jones" <rjones@redhat.com>
62f9b7
Date: Mon, 6 Apr 2020 10:19:12 +0100
62f9b7
Subject: [PATCH] v2v: Allow large temporary directory to be set on a global
62f9b7
 basis.
62f9b7
62f9b7
Previously we placed large files in g#get_cachedir () (usually
62f9b7
/var/tmp).  However the problem is this ties the libguestfs appliance
62f9b7
and the virt-v2v overlay files to the same location.
62f9b7
62f9b7
When virt-v2v is run in a container, or any other situation where
62f9b7
local storage is limited, it's helpful to be able to put the overlay
62f9b7
files on an externally mounted PVC, which might be using NFS and
62f9b7
shared between containers.  But putting the libguestfs appliance on
62f9b7
NFS in a shared location is certainly not recommended.
62f9b7
62f9b7
This allows the two locations to be set separately:
62f9b7
62f9b7
  VIRT_V2V_TMPDIR - location of large temporary files, can use NFS
62f9b7
                    and may be shared
62f9b7
62f9b7
  LIBGUESTFS_CACHEDIR - location of libguestfs appliance
62f9b7
62f9b7
Another motivation for this patch is to allow more reliable cleanup of
62f9b7
large temporary files by an external process, as described in the
62f9b7
updated documentation.
62f9b7
62f9b7
Small temporary files are placed in $TMPDIR (usually /tmp).  I cleaned
62f9b7
up some existing code which used /var/tmp for small temporaries.
62f9b7
62f9b7
(cherry picked from commit 717b808bc5cb632778973eb000600e87eaf5c31a)
62f9b7
---
62f9b7
 docs/virt-v2v.pod        | 27 +++++++++++++++++++--------
62f9b7
 v2v/input_ova.ml         |  4 ++--
62f9b7
 v2v/input_vmx.ml         |  3 +--
62f9b7
 v2v/output_glance.ml     |  3 +--
62f9b7
 v2v/output_null.ml       |  3 +--
62f9b7
 v2v/output_rhv_upload.ml | 29 ++++++++++++++++-------------
62f9b7
 v2v/parse_ova.ml         |  6 ++----
62f9b7
 v2v/python_script.ml     | 12 +++---------
62f9b7
 v2v/python_script.mli    |  5 +----
62f9b7
 v2v/utils.ml             |  6 +++++-
62f9b7
 v2v/utils.mli            |  5 +++++
62f9b7
 v2v/v2v.ml               | 14 ++++++--------
62f9b7
 12 files changed, 62 insertions(+), 55 deletions(-)
62f9b7
62f9b7
diff --git a/docs/virt-v2v.pod b/docs/virt-v2v.pod
62f9b7
index 6f9f323e..af69d633 100644
62f9b7
--- a/docs/virt-v2v.pod
62f9b7
+++ b/docs/virt-v2v.pod
62f9b7
@@ -1172,8 +1172,8 @@ possible.
62f9b7
 =head3 Disk space
62f9b7
 
62f9b7
 Virt-v2v places potentially large temporary files in
62f9b7
-C<$LIBGUESTFS_CACHEDIR> (which is F if you don't set it).
62f9b7
-Using tmpfs is a bad idea.
62f9b7
+C<$VIRT_V2V_TMPDIR> (usually F, see also
62f9b7
+L</ENVIRONMENT VARIBLES> below).  Using tmpfs is a bad idea.
62f9b7
 
62f9b7
 For each guest disk, an overlay is stored temporarily.  This stores
62f9b7
 the changes made during conversion, and is used as a cache.  The
62f9b7
@@ -1186,12 +1186,12 @@ and output methods may use disk space, as outlined in the table below.
62f9b7
 =item I<-i ova>
62f9b7
 
62f9b7
 This temporarily places a full copy of the uncompressed source disks
62f9b7
-in C<$LIBGUESTFS_CACHEDIR> (or F).
62f9b7
+in C<$VIRT_V2V_TMPDIR> (or F).
62f9b7
 
62f9b7
 =item I<-o glance>
62f9b7
 
62f9b7
 This temporarily places a full copy of the output disks in
62f9b7
-C<$LIBGUESTFS_CACHEDIR> (or F).
62f9b7
+C<$VIRT_V2V_TMPDIR> (or F).
62f9b7
 
62f9b7
 =item I<-o local>
62f9b7
 
62f9b7
@@ -1311,7 +1311,7 @@ have at least 100 available inodes.
62f9b7
 =head3 Minimum free space check in the host
62f9b7
 
62f9b7
 You must have sufficient free space in the host directory used to
62f9b7
-store temporary overlays.  To find out
62f9b7
+store large temporary overlays.  To find out
62f9b7
 which directory this is, use:
62f9b7
 
62f9b7
  $ df -h "`guestfish get-cachedir`"
62f9b7
@@ -1319,9 +1319,12 @@ which directory this is, use:
62f9b7
  /dev/mapper/root   50G   40G  6.8G  86% /
62f9b7
 
62f9b7
 and look under the C<Avail> column.  Virt-v2v will refuse to do the
62f9b7
-conversion at all unless at least 1GB is available there.
62f9b7
+conversion at all unless at least 1GB is available there.  You can
62f9b7
+change the directory that virt-v2v uses by setting
62f9b7
+C<$VIRT_V2V_TMPDIR>.
62f9b7
 
62f9b7
-See also L</Resource requirements> above.
62f9b7
+See also L</Resource requirements> above and L</ENVIRONMENT VARIABLES>
62f9b7
+below.
62f9b7
 
62f9b7
 =head2 Running virt-v2v as root or non-root
62f9b7
 
62f9b7
@@ -1496,10 +1499,18 @@ conversion.
62f9b7
 
62f9b7
 =over 4
62f9b7
 
62f9b7
+=item C<VIRT_V2V_TMPDIR>
62f9b7
+
62f9b7
 =item C<LIBGUESTFS_CACHEDIR>
62f9b7
 
62f9b7
 Location of the temporary directory used for the potentially large
62f9b7
-temporary overlay file.  If not set, F is used.
62f9b7
+temporary overlay file.  If neither environment variable is set then
62f9b7
+F is used.
62f9b7
+
62f9b7
+To reliably ensure large temporary files are cleaned up (for example
62f9b7
+in case virt-v2v crashes) you should create a randomly named directory
62f9b7
+under F, set C<VIRT_V2V_TMPDIR> to point to this directory,
62f9b7
+then when virt-v2v exits remove the directory.
62f9b7
 
62f9b7
 See the L</Disk space> section above.
62f9b7
 
62f9b7
diff --git a/v2v/input_ova.ml b/v2v/input_ova.ml
62f9b7
index 5d3bece1..d78a5ce8 100644
62f9b7
--- a/v2v/input_ova.ml
62f9b7
+++ b/v2v/input_ova.ml
62f9b7
@@ -132,8 +132,8 @@ class input_ova ova = object
62f9b7
            (* The spec allows the file to be gzip-compressed, in
62f9b7
             * which case we must uncompress it into a temporary.
62f9b7
             *)
62f9b7
-           let temp_dir = (open_guestfs ())#get_cachedir () in
62f9b7
-           let new_filename = Filename.temp_file ~temp_dir "ova" ".vmdk" in
62f9b7
+           let new_filename =
62f9b7
+             Filename.temp_file ~temp_dir:Utils.large_tmpdir "ova" ".vmdk" in
62f9b7
            unlink_on_exit new_filename;
62f9b7
            let cmd =
62f9b7
              sprintf "zcat %s > %s" (quote filename) (quote new_filename) in
62f9b7
diff --git a/v2v/input_vmx.ml b/v2v/input_vmx.ml
62f9b7
index f1d143e9..7a7647e5 100644
62f9b7
--- a/v2v/input_vmx.ml
62f9b7
+++ b/v2v/input_vmx.ml
62f9b7
@@ -389,8 +389,7 @@ and find_nics vmx =
62f9b7
 
62f9b7
 class input_vmx input_password input_transport arg =
62f9b7
   let tmpdir =
62f9b7
-    let base_dir = (open_guestfs ())#get_cachedir () in
62f9b7
-    let t = Mkdtemp.temp_dir ~base_dir "vmx." in
62f9b7
+    let t = Mkdtemp.temp_dir "vmx." in
62f9b7
     rmdir_on_exit t;
62f9b7
     t in
62f9b7
 object
62f9b7
diff --git a/v2v/output_glance.ml b/v2v/output_glance.ml
62f9b7
index 0a9e9181..e8facd0a 100644
62f9b7
--- a/v2v/output_glance.ml
62f9b7
+++ b/v2v/output_glance.ml
62f9b7
@@ -33,8 +33,7 @@ class output_glance () =
62f9b7
    * to write to a temporary file.  XXX
62f9b7
    *)
62f9b7
   let tmpdir =
62f9b7
-    let base_dir = (open_guestfs ())#get_cachedir () in
62f9b7
-    let t = Mkdtemp.temp_dir ~base_dir "glance." in
62f9b7
+    let t = Mkdtemp.temp_dir ~base_dir:large_tmpdir "glance." in
62f9b7
     rmdir_on_exit t;
62f9b7
     t in
62f9b7
 object
62f9b7
diff --git a/v2v/output_null.ml b/v2v/output_null.ml
62f9b7
index 3528da50..edb749ea 100644
62f9b7
--- a/v2v/output_null.ml
62f9b7
+++ b/v2v/output_null.ml
62f9b7
@@ -75,8 +75,7 @@ class output_null =
62f9b7
    * the null-co device w/ a JSON URL.
62f9b7
    *)
62f9b7
   let tmpdir =
62f9b7
-    let base_dir = (open_guestfs ())#get_cachedir () in
62f9b7
-    let t = Mkdtemp.temp_dir ~base_dir "null." in
62f9b7
+    let t = Mkdtemp.temp_dir ~base_dir:large_tmpdir "null." in
62f9b7
     rmdir_on_exit t;
62f9b7
     t in
62f9b7
 object
62f9b7
diff --git a/v2v/output_rhv_upload.ml b/v2v/output_rhv_upload.ml
62f9b7
index 81896e53..913992d9 100644
62f9b7
--- a/v2v/output_rhv_upload.ml
62f9b7
+++ b/v2v/output_rhv_upload.ml
62f9b7
@@ -148,25 +148,28 @@ class output_rhv_upload output_alloc output_conn
62f9b7
                         rhv_options =
62f9b7
   (* Create a temporary directory which will be deleted on exit. *)
62f9b7
   let tmpdir =
62f9b7
-    let base_dir = (open_guestfs ())#get_cachedir () in
62f9b7
-    let t = Mkdtemp.temp_dir ~base_dir "rhvupload." in
62f9b7
+    let t = Mkdtemp.temp_dir "rhvupload." in
62f9b7
     rmdir_on_exit t;
62f9b7
     t in
62f9b7
 
62f9b7
   let diskid_file_of_id id = tmpdir // sprintf "diskid.%d" id in
62f9b7
 
62f9b7
   (* Create Python scripts for precheck, vmcheck, plugin and create VM. *)
62f9b7
-  let py_create = Python_script.create ~tmpdir in
62f9b7
-  let precheck_script = py_create ~name:"rhv-upload-precheck.py"
62f9b7
-                        Output_rhv_upload_precheck_source.code in
62f9b7
-  let vmcheck_script = py_create ~name:"rhv-upload-vmcheck.py"
62f9b7
-                       Output_rhv_upload_vmcheck_source.code in
62f9b7
-  let plugin_script = py_create ~name:"rhv-upload-plugin.py"
62f9b7
-                      Output_rhv_upload_plugin_source.code in
62f9b7
-  let createvm_script = py_create ~name:"rhv-upload-createvm.py"
62f9b7
-                        Output_rhv_upload_createvm_source.code in
62f9b7
-  let deletedisks_script = py_create ~name:"rhv-upload-deletedisks.py"
62f9b7
-                           Output_rhv_upload_deletedisks_source.code in
62f9b7
+  let precheck_script =
62f9b7
+    Python_script.create ~name:"rhv-upload-precheck.py"
62f9b7
+      Output_rhv_upload_precheck_source.code in
62f9b7
+  let vmcheck_script =
62f9b7
+    Python_script.create ~name:"rhv-upload-vmcheck.py"
62f9b7
+      Output_rhv_upload_vmcheck_source.code in
62f9b7
+  let plugin_script =
62f9b7
+    Python_script.create ~name:"rhv-upload-plugin.py"
62f9b7
+      Output_rhv_upload_plugin_source.code in
62f9b7
+  let createvm_script =
62f9b7
+    Python_script.create ~name:"rhv-upload-createvm.py"
62f9b7
+      Output_rhv_upload_createvm_source.code in
62f9b7
+  let deletedisks_script =
62f9b7
+    Python_script.create ~name:"rhv-upload-deletedisks.py"
62f9b7
+      Output_rhv_upload_deletedisks_source.code in
62f9b7
 
62f9b7
   (* JSON parameters which are invariant between disks. *)
62f9b7
   let json_params = [
62f9b7
diff --git a/v2v/parse_ova.ml b/v2v/parse_ova.ml
62f9b7
index 0b939ac4..568ac5fa 100644
62f9b7
--- a/v2v/parse_ova.ml
62f9b7
+++ b/v2v/parse_ova.ml
62f9b7
@@ -71,8 +71,7 @@ let rec parse_ova ova =
62f9b7
     if is_directory ova then ova, Directory
62f9b7
     else (
62f9b7
       let tmpdir =
62f9b7
-        let base_dir = (open_guestfs ())#get_cachedir () in
62f9b7
-        let t = Mkdtemp.temp_dir ~base_dir "ova." in
62f9b7
+        let t = Mkdtemp.temp_dir ~base_dir:large_tmpdir "ova." in
62f9b7
         rmdir_on_exit t;
62f9b7
         t in
62f9b7
 
62f9b7
@@ -221,8 +220,7 @@ and uncompress_head format file =
62f9b7
  *)
62f9b7
 and uncompressed_type format file =
62f9b7
   let head, headlen = uncompress_head format file in
62f9b7
-  let tmpfile, chan =
62f9b7
-    Filename.open_temp_file "ova.file." "" in
62f9b7
+  let tmpfile, chan = Filename.open_temp_file "ova.file." "" in
62f9b7
   output chan head 0 headlen;
62f9b7
   close_out chan;
62f9b7
   let ret = detect_file_type tmpfile in
62f9b7
diff --git a/v2v/python_script.ml b/v2v/python_script.ml
62f9b7
index b1ea8f9d..212c8e1b 100644
62f9b7
--- a/v2v/python_script.ml
62f9b7
+++ b/v2v/python_script.ml
62f9b7
@@ -31,15 +31,9 @@ type script = {
62f9b7
   path : string;                (* Path to script. *)
62f9b7
 }
62f9b7
 
62f9b7
-let create ?(name = "script.py") ?tmpdir code =
62f9b7
-  let tmpdir =
62f9b7
-    match tmpdir with
62f9b7
-    | None ->
62f9b7
-      let base_dir = (open_guestfs ())#get_cachedir () in
62f9b7
-      let t = Mkdtemp.temp_dir ~base_dir "v2v." in
62f9b7
-      rmdir_on_exit t;
62f9b7
-      t
62f9b7
-    | Some dir -> dir in
62f9b7
+let create ?(name = "script.py") code =
62f9b7
+  let tmpdir = Mkdtemp.temp_dir "v2v." in
62f9b7
+  rmdir_on_exit tmpdir;
62f9b7
   let path = tmpdir // name in
62f9b7
   with_open_out path (fun chan -> output_string chan code);
62f9b7
   { tmpdir; path }
62f9b7
diff --git a/v2v/python_script.mli b/v2v/python_script.mli
62f9b7
index 6bf77e34..fdf73514 100644
62f9b7
--- a/v2v/python_script.mli
62f9b7
+++ b/v2v/python_script.mli
62f9b7
@@ -20,14 +20,11 @@
62f9b7
 
62f9b7
 type script
62f9b7
 
62f9b7
-val create : ?name:string -> ?tmpdir:string -> string -> script
62f9b7
+val create : ?name:string -> string -> script
62f9b7
 (** Create a Python script object.
62f9b7
 
62f9b7
     The optional parameter [?name] is a hint for the name of the script.
62f9b7
 
62f9b7
-    The optional parameter [?tmpdir] is the temporary directory to use
62f9b7
-    (instead of creating a new one).
62f9b7
-
62f9b7
     The parameter is the Python code.  Usually this is
62f9b7
     [Some_source.code] where [some_source.ml] is generated from
62f9b7
     the Python file by [v2v/embed.sh] (see also [v2v/Makefile.am]). *)
62f9b7
diff --git a/v2v/utils.ml b/v2v/utils.ml
62f9b7
index c2940582..a6c359f0 100644
62f9b7
--- a/v2v/utils.ml
62f9b7
+++ b/v2v/utils.ml
62f9b7
@@ -24,6 +24,10 @@ open Std_utils
62f9b7
 open Tools_utils
62f9b7
 open Common_gettext.Gettext
62f9b7
 
62f9b7
+let large_tmpdir =
62f9b7
+  try Sys.getenv "VIRT_V2V_TMPDIR"
62f9b7
+  with Not_found -> (open_guestfs ())#get_cachedir ()
62f9b7
+
62f9b7
 (* Is SELinux enabled and enforcing on the host? *)
62f9b7
 let have_selinux =
62f9b7
   0 = Sys.command "getenforce 2>/dev/null | grep -isq Enforcing"
62f9b7
@@ -114,6 +118,7 @@ let qemu_img_supports_offset_and_size () =
62f9b7
    * file that has an offset and size.
62f9b7
    *)
62f9b7
   let tmp = Filename.temp_file "v2vqemuimgtst" ".img" in
62f9b7
+  unlink_on_exit tmp;
62f9b7
   Unix.truncate tmp 1024;
62f9b7
 
62f9b7
   let json = [
62f9b7
@@ -133,7 +138,6 @@ let qemu_img_supports_offset_and_size () =
62f9b7
             (if verbose () then "" else " 2>&1") in
62f9b7
   debug "%s" cmd;
62f9b7
   let r = 0 = Sys.command cmd in
62f9b7
-  Unix.unlink tmp;
62f9b7
   debug "qemu-img supports \"offset\" and \"size\" in json URLs: %b" r;
62f9b7
   r
62f9b7
 
62f9b7
diff --git a/v2v/utils.mli b/v2v/utils.mli
62f9b7
index 937e2b9b..d86ca507 100644
62f9b7
--- a/v2v/utils.mli
62f9b7
+++ b/v2v/utils.mli
62f9b7
@@ -18,6 +18,11 @@
62f9b7
 
62f9b7
 (** Utilities used in virt-v2v only. *)
62f9b7
 
62f9b7
+val large_tmpdir : string
62f9b7
+(** [VIRT_V2V_TMPDIR] or [/var/tmp].  Create all large temporary files
62f9b7
+    such as overlays in this directory.  Small temporary files can
62f9b7
+    use the default behaviour eg. of {!Filename.temp_file} *)
62f9b7
+
62f9b7
 val have_selinux : bool
62f9b7
 (** True if SELinux is enabled and enforcing on the host. *)
62f9b7
 
62f9b7
diff --git a/v2v/v2v.ml b/v2v/v2v.ml
62f9b7
index 73edff2c..a58ff433 100644
62f9b7
--- a/v2v/v2v.ml
62f9b7
+++ b/v2v/v2v.ml
62f9b7
@@ -264,8 +264,6 @@ and set_source_networks_and_bridges cmdline source =
62f9b7
   let nics = List.map (Networks.map cmdline.network_map) source.s_nics in
62f9b7
   { source with s_nics = nics }
62f9b7
 
62f9b7
-and overlay_dir = (open_guestfs ())#get_cachedir ()
62f9b7
-
62f9b7
 (* Conversion can fail or hang if there is insufficient free space in
62f9b7
  * the temporary directory used to store overlays on the host
62f9b7
  * (RHBZ#1316479).  Although only a few hundred MB is actually
62f9b7
@@ -273,12 +271,12 @@ and overlay_dir = (open_guestfs ())#get_cachedir ()
62f9b7
  * guestfs appliance which is also stored here.
62f9b7
  *)
62f9b7
 and check_host_free_space () =
62f9b7
-  let free_space = StatVFS.free_space (StatVFS.statvfs overlay_dir) in
62f9b7
-  debug "check_host_free_space: overlay_dir=%s free_space=%Ld"
62f9b7
-        overlay_dir free_space;
62f9b7
+  let free_space = StatVFS.free_space (StatVFS.statvfs large_tmpdir) in
62f9b7
+  debug "check_host_free_space: large_tmpdir=%s free_space=%Ld"
62f9b7
+        large_tmpdir free_space;
62f9b7
   if free_space < 1_073_741_824L then
62f9b7
     error (f_"insufficient free space in the conversion server temporary directory %s (%s).\n\nEither free up space in that directory, or set the LIBGUESTFS_CACHEDIR environment variable to point to another directory with more than 1GB of free space.\n\nSee also the virt-v2v(1) manual, section \"Minimum free space check in the host\".")
62f9b7
-          overlay_dir (human_size free_space)
62f9b7
+          large_tmpdir (human_size free_space)
62f9b7
 
62f9b7
 (* Create a qcow2 v3 overlay to protect the source image(s). *)
62f9b7
 and create_overlays source_disks =
62f9b7
@@ -286,7 +284,7 @@ and create_overlays source_disks =
62f9b7
   List.mapi (
62f9b7
     fun i ({ s_qemu_uri = qemu_uri; s_format = format } as source) ->
62f9b7
       let overlay_file =
62f9b7
-        Filename.temp_file ~temp_dir:overlay_dir "v2vovl" ".qcow2" in
62f9b7
+        Filename.temp_file ~temp_dir:large_tmpdir "v2vovl" ".qcow2" in
62f9b7
       unlink_on_exit overlay_file;
62f9b7
 
62f9b7
       (* There is a specific reason to use the newer qcow2 variant:
62f9b7
@@ -823,7 +821,7 @@ and preserve_overlays overlays src_name =
62f9b7
   List.iter (
62f9b7
     fun ov ->
62f9b7
       let saved_filename =
62f9b7
-        sprintf "%s/%s-%s.qcow2" overlay_dir src_name ov.ov_sd in
62f9b7
+        sprintf "%s/%s-%s.qcow2" large_tmpdir src_name ov.ov_sd in
62f9b7
       rename ov.ov_overlay_file saved_filename;
62f9b7
       info (f_"Overlay saved as %s [--debug-overlays]") saved_filename
62f9b7
   ) overlays
62f9b7
-- 
0602f3
2.31.1
62f9b7