Richard W.M. Jones
2014-Sep-10 14:21 UTC
[Libguestfs] [PATCH] v2v: -o rhev: Don't break if image/metadata directory exists
As explained in the commit summary, if VDSM is going to create metadata and image directories, then virt-v2v should not mkdir those same directories (and hence fail). I'm not really convinced this is the right approach. I think instead of adding more and more hacks on top of '-o rhev' which should split this output mode into two sub modes, one for outputting to the RHEV Export Storage Domain (ie. "traditional" imports) and one which can handle the --rhev* parameters and the Data domain ('-o vdsm' perhaps?) Rich.
Richard W.M. Jones
2014-Sep-10 14:21 UTC
[Libguestfs] [PATCH] v2v: -o rhev: Don't break if image/metadata directory exists already.
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