From e46157bb227992f5460fdb3247e09dea28c9b7d4 Mon Sep 17 00:00:00 2001 From: onenhansen <92747003+onenhansen@users.noreply.github.com> Date: Thu, 13 Jan 2022 03:26:32 -0700 Subject: [PATCH] F #5448: Move vmdk convert logic (#1697) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit from downloader.sh to vcenter/cp Co-authored-by: Tino Vázquez (cherry picked from commit 7e46da9fe21714f121e70af12de2c47067fd3320) --- src/datastore_mad/remotes/downloader.sh | 23 +------------ src/datastore_mad/remotes/vcenter/cp | 44 ++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/datastore_mad/remotes/downloader.sh b/src/datastore_mad/remotes/downloader.sh index 65fc45cb4d..c1ee3e2479 100755 --- a/src/datastore_mad/remotes/downloader.sh +++ b/src/datastore_mad/remotes/downloader.sh @@ -295,7 +295,7 @@ function get_rbd_cmd echo "ssh '$(esc_sq "$DST_HOST")' \"$RBD export '$(esc_sq "$SOURCE")' -\"" } -TEMP=`getopt -o m:s:l:c:n -l md5:,sha1:,limit:,max-size:,convert:,nodecomp -- "$@"` +TEMP=`getopt -o m:s:l:c:n -l md5:,sha1:,limit:,max-size:,nodecomp -- "$@"` if [ $? != 0 ] ; then echo "Arguments error" >&2 @@ -328,10 +328,6 @@ while true; do export MAX_SIZE="$2" shift 2 ;; - --convert) - export CONVERT="$2" - shift 2 - ;; --) shift break @@ -477,24 +473,7 @@ if [ -n "$HASH_TYPE" ]; then fi fi -function convert_image -{ - original_type=$(qemu-img info $TO | grep "^file format:" | awk '{print $3}' || :) - if [ "$CONVERT" != "$original_type" ]; then - tmpimage=$TO".tmp" - qemu-img convert -f $original_type -O $CONVERT $TO $tmpimage - mv $tmpimage $TO - fi -} - # Unarchive only if the destination is filesystem if [ "$TO" != "-" ]; then unarchive "$TO" - - if [ -n "$CONVERT" ] && [ -f "$TO" ]; then - convert_image - fi - -elif [ -n "$CONVERT" ]; then - convert_image fi diff --git a/src/datastore_mad/remotes/vcenter/cp b/src/datastore_mad/remotes/vcenter/cp index bd1df85156..53bf11fc10 100755 --- a/src/datastore_mad/remotes/vcenter/cp +++ b/src/datastore_mad/remotes/vcenter/cp @@ -51,6 +51,7 @@ $LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' require 'open3' +require 'rexml/document' def file_location?(target_path, f) target_path == File.basename(f) @@ -119,21 +120,56 @@ if VCenterDriver::FileHelper.remote_or_needs_unpack?(img_path) downsh_args << "--sha1 #{sha1} " if sha1 && !sha1.empty? downsh_args << '--nodecomp ' if nodecomp && !nodecomp.empty? downsh_args << "--limit #{limit_bw} " if limit_bw && !limit_bw.empty? - unless VCenterDriver::FileHelper.from_s3?(img_path) - downsh_args << '--convert vmdk' - end + # Define downloader script and driver action variable downloader = "#{File.dirname(__FILE__)}/../downloader.sh #{downsh_args}" b64 = Base64.encode64(drv_action.to_xml).gsub!("\n", '') - rc = system("DRV_ACTION=#{b64} #{downloader} #{img_path} #{temp_file}") + # Fail if download fails if there_is_not_system_error?(rc) STDERR.puts "Error downloading #{img_path}" FileUtils.rm_rf(temp_file) exit(-1) end + # Convert from current format to VMDK + unless VCenterDriver::FileHelper.from_s3?(img_path) + file_type='' + begin + Open3.popen3("qemu-img info #{temp_file}") do + |_stdin, stdout, stderr, wait_thr| + if !stderr.nil? + STDERR.puts "'qemu-img info' stderr: #{stderr}" + raise StandardError + end + file_type=stdout.read.split("\n")[1].split(':')[1].lstrip + if file_type.nil? then raise StandardError end + + wait_thr.join + end + rescue StandardError + STDERR.puts "'qemu-img info' output appears to be invalid.' \ + ' Cannot determine file type." + STDERR.puts "Removing #{temp_file}..." + FileUtils.rm_rf(temp_file) + exit(-1) + end + unless file_type == 'vmdk' + out_file = File.basename(temp_file, '.*')+'.tmp' + convert=system("qemu-img convert -f #{file_type}"\ + " -O vmdk #{temp_file} #{out_file}") + if there_is_not_system_error?(convert) + STDERR.puts "Error converting #{temp_file}." + STDERR.puts "Removing #{temp_file} and #{out_file}." + File.Utils.rm_rf(out_file) + File.Utils.rm_rf(temp_file) + exit(-1) + end + FileUtils.mv(out_file, temp_file) + end + end + img_path = temp_file end