From 36dc2623071d4876eb5ab454d2ac6796d8e4002b Mon Sep 17 00:00:00 2001 From: Jesse Gardner Date: Tue, 30 Mar 2021 16:22:25 -0700 Subject: [PATCH] Added copy-disks/C flag, added copy disk function when importing/copying templates, slightly improved storage_opt --- qq2clone | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/qq2clone b/qq2clone index c800198..e934b6b 100755 --- a/qq2clone +++ b/qq2clone @@ -428,6 +428,7 @@ for elem in "${opts[@]}"; do OPT["$elem"]="$(sqlite3 \ "select value from CONFIG where name=\"$elem\"")" done +OPT[COPY_DISKS]=0 # Hardcoded default, overriden with --copy-disks/-C } #=========================================================================# write_config () @@ -1624,6 +1625,40 @@ else nohup virt-viewer --uuid "$uuid" &>/dev/null & fi +return 0 +} +#=========================================================================# +copy_disks () +# DESCRIPTION: Go through XML file, find all disk images, and copy them. +# A base name is provided, and new files placed in OPT[STORAGE] +# INPUT: XML file in pipe +# OUTPUT: Altered XML in pipe, and new image files created +# PARAMETERS: $1: Base name for disks +#=========================================================================# +{ +(($#==1)) || unexpected_error copy_disks +local elem i=0 name xml +declare -a disks +xml="$(read_pipe 1)" +[[ -n "$xml" ]] || unexpected_error copy_disks +read_pipe | get_disk_devices + +while read -r line; do disks=("${disks[@]}" "$line"); done < <(read_pipe) +write_pipe 1 <<<"$xml" +RANDOM="$$" + +for elem in "${disks[@]}"; do + ((i++)) + name="${1}.${i}" + while [[ -e "${OPT[STORAGE]}/${name}.qcow2" ]]; do + name="${1}.${i}-${RANDOM}" + done + ((OPT[QUIET])) || echo "Copying disk ${elem}..." + copy_file "$elem" "${OPT[STORAGE]}/${name}.qcow2" + do_virt_xml --edit path="$elem" --disk \ + path="${OPT[STORAGE]}/${name}.qcow2" +done + return 0 } #=========================================================================# @@ -2532,6 +2567,8 @@ write_pipe 1 <"${old}" valid_xml_name_check "$2" do_virt_xml --edit --metadata name="$2" +((OPT[COPY_DISKS])) && copy_disks "$2" + write_file "$new" local md5 @@ -2670,6 +2707,9 @@ read -r name write_pipe 1 <<<"$xml" do_virt_xml --edit --metadata name="$name" + +((OPT[COPY_DISKS])) && copy_disks "$name" + write_file "${OPT[TEMPLATE_DIR]}/${name}.xml" get_disk_devices <"${OPT[TEMPLATE_DIR]}/${name}.xml" @@ -2887,8 +2927,8 @@ if [[ "${args[0]}" =~ ^\-Q ]]; then exec &>/dev/null fi -local short=":c:fghnqrs:St:vV" -local long="connection=c,no-spice=f,use-spice=g,help=h," +local short=":c:Cfghnqrs:St:vV" +local long="connection=c,copy-disks=C,no-spice=f,use-spice=g,help=h," long="${long}no-run=n,quiet=q,run=r,storage=s,spicy=S," long="${long}template=t,verbose=v,virt-viewer=V" @@ -2905,6 +2945,8 @@ while getopts "${short}Q" opt; do { echo "Virsh cannot connect to URI \"$OPTARG\", exiting" >&2; exit "$E_args"; } ;; + C) OPT[COPY_DISKS]=1 + ;; f) OPT[USE_SPICE]=0 ;; g) OPT[USE_SPICE]=1 @@ -3032,7 +3074,6 @@ storage_opt () { if [[ "$1" =~ ^/ ]]; then OPT["STORAGE"]="$1" - check_dir "$1" else virsh pool-info "$1" &>/dev/null || { echo "No such pool \"$1\" exists on current libvirt connection" >&2; @@ -3049,6 +3090,7 @@ else echo "qq2clone at this time" exit "$E_args"; } >&2 fi +check_dir "${OPT[STORAGE]}" return 0 } #=========================================================================#