Added copy-disks/C flag, added copy disk function when importing/copying templates, slightly improved storage_opt

This commit is contained in:
Jesse Gardner 2021-03-30 16:22:25 -07:00
parent 9e35cd6df3
commit 36dc262307
1 changed files with 45 additions and 3 deletions

View File

@ -428,6 +428,7 @@ for elem in "${opts[@]}"; do
OPT["$elem"]="$(sqlite3 \ OPT["$elem"]="$(sqlite3 \
"select value from CONFIG where name=\"$elem\"")" "select value from CONFIG where name=\"$elem\"")"
done done
OPT[COPY_DISKS]=0 # Hardcoded default, overriden with --copy-disks/-C
} }
#=========================================================================# #=========================================================================#
write_config () write_config ()
@ -1624,6 +1625,40 @@ else
nohup virt-viewer --uuid "$uuid" &>/dev/null & nohup virt-viewer --uuid "$uuid" &>/dev/null &
fi 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 return 0
} }
#=========================================================================# #=========================================================================#
@ -2532,6 +2567,8 @@ write_pipe 1 <"${old}"
valid_xml_name_check "$2" valid_xml_name_check "$2"
do_virt_xml --edit --metadata name="$2" do_virt_xml --edit --metadata name="$2"
((OPT[COPY_DISKS])) && copy_disks "$2"
write_file "$new" write_file "$new"
local md5 local md5
@ -2670,6 +2707,9 @@ read -r name
write_pipe 1 <<<"$xml" write_pipe 1 <<<"$xml"
do_virt_xml --edit --metadata name="$name" do_virt_xml --edit --metadata name="$name"
((OPT[COPY_DISKS])) && copy_disks "$name"
write_file "${OPT[TEMPLATE_DIR]}/${name}.xml" write_file "${OPT[TEMPLATE_DIR]}/${name}.xml"
get_disk_devices <"${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 exec &>/dev/null
fi fi
local short=":c:fghnqrs:St:vV" local short=":c:Cfghnqrs:St:vV"
local long="connection=c,no-spice=f,use-spice=g,help=h," 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}no-run=n,quiet=q,run=r,storage=s,spicy=S,"
long="${long}template=t,verbose=v,virt-viewer=V" 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; { echo "Virsh cannot connect to URI \"$OPTARG\", exiting" >&2;
exit "$E_args"; } exit "$E_args"; }
;; ;;
C) OPT[COPY_DISKS]=1
;;
f) OPT[USE_SPICE]=0 f) OPT[USE_SPICE]=0
;; ;;
g) OPT[USE_SPICE]=1 g) OPT[USE_SPICE]=1
@ -3032,7 +3074,6 @@ storage_opt ()
{ {
if [[ "$1" =~ ^/ ]]; then if [[ "$1" =~ ^/ ]]; then
OPT["STORAGE"]="$1" OPT["STORAGE"]="$1"
check_dir "$1"
else else
virsh pool-info "$1" &>/dev/null || virsh pool-info "$1" &>/dev/null ||
{ echo "No such pool \"$1\" exists on current libvirt connection" >&2; { echo "No such pool \"$1\" exists on current libvirt connection" >&2;
@ -3049,6 +3090,7 @@ else
echo "qq2clone at this time" echo "qq2clone at this time"
exit "$E_args"; } >&2 exit "$E_args"; } >&2
fi fi
check_dir "${OPT[STORAGE]}"
return 0 return 0
} }
#=========================================================================# #=========================================================================#