Separated function get_format from clone

This commit is contained in:
Jesse Gardner 2021-03-30 13:59:04 -07:00
parent c36d9ef1c8
commit 9e35cd6df3
1 changed files with 22 additions and 11 deletions

View File

@ -1531,7 +1531,7 @@ local name uuid
local storage="${OPT[STORAGE]}"
declare -a f_arr
local img new_img j=-1 type level
local img new_img j=-1 type
local disks
disks="$(sqlite3 "select disks from TEMPLATES where\
@ -1541,15 +1541,7 @@ trap 'rm -f "${storage}/${name}.${uuid:?}.*"' INT
while read -r img; do
((j++))
new_img="${storage}/${name}.${uuid}.${j}.qcow2"
level=0
while read -r line; do
[[ "$line" =~ \{[[:space:]]*$ ]] && { ((level++)); continue; }
[[ "$line" =~ \},?[[:space:]]*$ ]] && { ((level--)); continue; }
if ((level == 1)); then
[[ "$line" =~ \"format\":[[:space:]]*\"(.*)\" ]] &&
type="${BASH_REMATCH[1]}"
fi
done < <(qemu-img info --output=json "$img")
type="$(get_format "$img")"
qemu-img create -f qcow2 -F "$type" -b "$img" "$new_img"\
&>/dev/null ||
{ rm -f "${storage}/${name}.${uuid:?}.*";
@ -1558,7 +1550,6 @@ while read -r img; do
f_arr[${#f_arr[@]}]="$new_img"
done < <(echo "$disks")
virt-clone --original-xml "$txml" --name "$name" --uuid "$uuid"\
--preserve-data "${f_arr[@]}" &>/dev/null ||
{ rm -f "${storage}/${name}.${uuid:?}.*";
@ -1776,6 +1767,26 @@ trap 'exit' INT
return 0
}
#=========================================================================#
get_format ()
# DESCRIPTION: Find format of given virtual machine image file
# INPUT: Absolute filepath to a virtual machine image file
# OUTPUT: Echoes the name of the format
# PARAMETERS: $1: Filepath
#=========================================================================#
{
local line level=0
while read -r line; do
[[ "$line" =~ \{[[:space:]]*$ ]] && { ((level++)); continue; }
[[ "$line" =~ \},?[[:space:]]*$ ]] && { ((level--)); continue; }
if ((level == 1)); then
[[ "$line" =~ \"format\":[[:space:]]*\"(.*)\" ]] &&
{ echo "${BASH_REMATCH[1]}";
return 0; }
fi
done < <(qemu-img info --output=json "$1")
return 1
}
#=========================================================================#
get_template_list ()
# DESCRIPTION: List existing templates, alphabetically ordered, one per
# line