Skip to content

Commit

Permalink
B #6487: Fixed bug on the cloning scripts where the SPARSE parameter …
Browse files Browse the repository at this point in the history
…from the Datastore was ignored.

- Added function get_ds_attribute to tm_common.sh to get a given attribute from a Datastore
- Using falloc preallocation mode on the qemu-img commands since it is faster than using full preallocation
- Note the cloning script when using the TM_MAD = ssh does not use qemu-img command. Instead it uses the tar command, hence it ignores the QCOW2_OPTIONS parameter

Signed-off-by: Alejandro Mosteiro <[email protected]>
  • Loading branch information
Aletibazo committed Apr 12, 2024
1 parent 7831e0e commit ab468c4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
16 changes: 14 additions & 2 deletions src/tm_mad/qcow2/clone
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,29 @@ done < <(onevm show -x $VMID| $XPATH \
/VM/TEMPLATE/DISK\[DISK_ID=$DISK_ID\]/SIZE \
/VM/TEMPLATE/DISK\[DISK_ID=$DISK_ID\]/FORMAT \
/VM/TEMPLATE/DISK\[DISK_ID=$DISK_ID\]/ORIGINAL_SIZE \
/VM/TEMPLATE/DISK\[DISK_ID=$DISK_ID\]/QCOW2_STANDALONE)
/VM/TEMPLATE/DISK\[DISK_ID=$DISK_ID\]/QCOW2_STANDALONE \
/VM/HISTORY_RECORDS/HISTORY[last\(\)]/DS_ID)

SIZE="${XPATH_ELEMENTS[j++]}"
FORMAT="${XPATH_ELEMENTS[j++]}"
ORIGINAL_SIZE="${XPATH_ELEMENTS[j++]}"
QCOW2_STANDALONE="${XPATH_ELEMENTS[j++]}"
SYS_DS_ID="${XPATH_ELEMENTS[j++]}"

ssh_make_path $DST_HOST $DST_DIR
disable_local_monitoring $DST_HOST $DST_DIR

#-------------------------------------------------------------------------------
# Clone (cp) SRC into DST
#-------------------------------------------------------------------------------
SPARSE=$(get_ds_attribute $SYS_DS_ID "SPARSE")

if [ -n "$ORIGINAL_SIZE" ] && [ "$SIZE" -gt "$ORIGINAL_SIZE" ]; then
RESIZE_CMD="qemu-img resize ${DST_PATH} ${SIZE}M"
if [[ "${SPARSE}" =~ ^(no|NO)$ ]]; then
RESIZE_CMD="qemu-img resize --preallocation=falloc ${DST_PATH} ${SIZE}M"
else
RESIZE_CMD="qemu-img resize ${DST_PATH} ${SIZE}M"
fi
fi

if [ "$FORMAT" = "qcow2" ] && is_yes "${QCOW2_STANDALONE}"; then
Expand All @@ -94,6 +101,11 @@ else
CLONE_CMD="cp $SRC_PATH $DST_PATH"
fi

if [[ "${SPARSE}" =~ ^(no|NO)$ ]]; then
CLONE_CMD=$(qcow_dir_cmd "$SRC_PATH" "$DST_PATH" "convert")
CLONE_CMD="${CLONE_CMD/-O qcow2/-O qcow2 -S 0}"
fi

CLONE_RESIZE_CMD=$(cat <<EOF
$CLONE_CMD
$RESIZE_CMD
Expand Down
16 changes: 14 additions & 2 deletions src/tm_mad/qcow2/clone.ssh
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,25 @@ while IFS= read -r -d '' element; do
done < <(onevm show -x $VMID| $XPATH \
/VM/TEMPLATE/DISK\[DISK_ID=$DISK_ID\]/SIZE \
/VM/TEMPLATE/DISK\[DISK_ID=$DISK_ID\]/FORMAT \
/VM/TEMPLATE/DISK\[DISK_ID=$DISK_ID\]/ORIGINAL_SIZE)
/VM/TEMPLATE/DISK\[DISK_ID=$DISK_ID\]/ORIGINAL_SIZE \
/VM/HISTORY_RECORDS/HISTORY[last\(\)]/DS_ID)

SIZE="${XPATH_ELEMENTS[j++]}"
FORMAT="${XPATH_ELEMENTS[j++]}"
ORIGINAL_SIZE="${XPATH_ELEMENTS[j++]}"
SYS_DS_ID="${XPATH_ELEMENTS[j++]}"

#-------------------------------------------------------------------------------
# Copy files to the remote host
#-------------------------------------------------------------------------------
SPARSE=$(get_ds_attribute $SYS_DS_ID "SPARSE")

if [ -n "$ORIGINAL_SIZE" ] && [ "$SIZE" -gt "$ORIGINAL_SIZE" ]; then
RESIZE_CMD="qemu-img resize ${DST_PATH} ${SIZE}M"
if [[ "${SPARSE}" =~ ^(no|NO)$ ]]; then
RESIZE_CMD="qemu-img resize --preallocation=falloc ${DST_PATH} ${SIZE}M"
else
RESIZE_CMD="qemu-img resize ${DST_PATH} ${SIZE}M"
fi
fi

if [ "$FORMAT" = "qcow2" ]; then
Expand All @@ -86,6 +93,11 @@ else
CLONE_CMD="cp $SRC_PATH $DST_PATH"
fi

if [[ "${SPARSE}" =~ ^(no|NO)$ ]]; then
CLONE_CMD=$(qcow_dir_cmd "$SRC_PATH" "$DST_PATH" "convert")
CLONE_CMD="${CLONE_CMD/-O qcow2/-O qcow2 -S 0}"
fi

CLONE_RESIZE_CMD=$(cat <<EOF
$CLONE_CMD
$RESIZE_CMD
Expand Down
21 changes: 16 additions & 5 deletions src/tm_mad/ssh/clone
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ fi
# Copy files to the remote host
#-------------------------------------------------------------------------------
log "Cloning $SRC_PATH in $DST_PATH"
SPARSE=$(get_ds_attribute $SYS_DS_ID "SPARSE")

COPY_CMD=$(cat <<EOF
set -e -o pipefail
Expand All @@ -107,16 +108,26 @@ COPY_CMD=$(cat <<EOF
SRC_SNAP="${SRC_FILE}.snap"
fi
$TAR -C $SRC_DIR --transform="flags=r;s|$SRC_FILE|$DST_FILE|" -cSf - $SRC_FILE \$SRC_SNAP | \
$SSH $DST_HOST "$TAR -xSf - -C $DST_DIR"
if [[ "${SPARSE}" =~ ^(no|NO)$ ]]; then
$TAR -C $SRC_DIR --transform="flags=r;s|$SRC_FILE|$DST_FILE|" -cf - $SRC_FILE \$SRC_SNAP | \
$SSH $DST_HOST "$TAR -xf - -C $DST_DIR"
else
$TAR -C $SRC_DIR --transform="flags=r;s|$SRC_FILE|$DST_FILE|" -cSf - $SRC_FILE \$SRC_SNAP | \
$SSH $DST_HOST "$TAR -xSf - -C $DST_DIR"
fi
EOF
)

ssh_forward ssh_exec_and_log "$SRC_HOST" "$COPY_CMD" \
"Error copying $SRC_PATH to $DST"

if [ -n "$ORIGINAL_SIZE" ] && [ "$SIZE" -gt "$ORIGINAL_SIZE" ]; then
RESIZE_CMD="qemu-img resize ${DST_PATH} ${SIZE}M"
ssh_exec_and_log "$DST_HOST" "$RESIZE_CMD" \
"Error resizing image $DST"
if [[ "${SPARSE}" =~ ^(no|NO)$ ]]; then
RESIZE_CMD="qemu-img resize --preallocation=falloc ${DST_PATH} ${SIZE}M"
else
RESIZE_CMD="qemu-img resize ${DST_PATH} ${SIZE}M"
fi

ssh_exec_and_log "$DST_HOST" "$RESIZE_CMD" \
"Error resizing image $DST"
fi
13 changes: 13 additions & 0 deletions src/tm_mad/tm_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,16 @@ function is_yes
{
[[ "$1" =~ ^(yes|YES|true|TRUE)$ ]]
}

# ------------------------------------------------------------------------------
# Returns given attribute value from DATASTORE template
# ------------------------------------------------------------------------------
function get_ds_attribute {
local DS_ID=$1
local ATTRIBUTE=$2

XPATH="${ONE_LOCAL_VAR}/remotes/datastore/xpath.rb --stdin"
LOC="/DATASTORE/TEMPLATE/$ATTRIBUTE"

echo "$(awk 'gsub(/[\0]/, x)' <(onedatastore show $DS_ID -x|$XPATH $LOC))"
}

0 comments on commit ab468c4

Please sign in to comment.