Separated function get_format from clone
This commit is contained in:
parent
c36d9ef1c8
commit
9e35cd6df3
33
qq2clone
33
qq2clone
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue