[Libguestfs] [PATCH] v2v: -o rhev: Don't break if image/metadata directory exists already.

Richard W.M. Jones rjones at redhat.com
Wed Sep 10 14:21:27 UTC 2014


When importing into a data domain, VDSM will create the image and
metadata directories and pass those names to virt-v2v.  (It is up to
VDSM to clean up if virt-v2v fails in this case).  Therefore don't
fail if these directories exist already, and don't delete them if
virt-v2v fails.
---
 v2v/output_RHEV.ml | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/v2v/output_RHEV.ml b/v2v/output_RHEV.ml
index bfcb0cc..5b7a394 100644
--- a/v2v/output_RHEV.ml
+++ b/v2v/output_RHEV.ml
@@ -164,6 +164,20 @@ and get_ostype = function
       typ distro major minor product;
     "Unassigned"
 
+(* This function is like mkdir, but if the directory already exists
+ * then it doesn't fail and returns true.  If the directory was created
+ * it returns false.  If directory creation failed for any other reason
+ * then it throws an exception.
+ *)
+let mkdir_ref dir mode =
+  try mkdir dir mode; false
+  with
+  | Unix_error (EEXIST, _, _) ->
+    if is_directory dir then true
+    else (* Exists but not a directory => raise an exception. *)
+      invalid_arg (sprintf "mkdir: %s exists but is not a directory" dir)
+  | exn -> raise exn
+
 class output_rhev verbose os rhev_params output_alloc =
 object
   inherit output verbose
@@ -349,11 +363,12 @@ object
      * conversion fails for any reason then we delete this directory.
      *)
     image_dir <- esd.mp // esd.uuid // "images" // image_uuid;
-    mkdir image_dir 0o755;
-    at_exit (fun () ->
-      if delete_target_directory then (
-        let cmd = sprintf "rm -rf %s" (quote image_dir) in
-        ignore (Sys.command cmd)
+    if not (mkdir_ref image_dir 0o755) then (
+      at_exit (fun () ->
+        if delete_target_directory then (
+          let cmd = sprintf "rm -rf %s" (quote image_dir) in
+          ignore (Sys.command cmd)
+        )
       )
     );
     if verbose then
@@ -709,7 +724,7 @@ object
 
     (* Write it to the metadata file. *)
     let dir = esd.mp // esd.uuid // "master" // "vms" // vm_uuid in
-    mkdir dir 0o755;
+    ignore (mkdir_ref dir 0o755);
     let file = dir // vm_uuid ^ ".ovf" in
     let chan = open_out file in
     doc_to_chan chan ovf;
-- 
2.0.4




More information about the Libguestfs mailing list