Skip to content

Commit

Permalink
F #3244: Improve qcow2.ssh mixed-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
xorel committed Nov 6, 2019
1 parent c92db8e commit 45e3465
Show file tree
Hide file tree
Showing 10 changed files with 468 additions and 73 deletions.
9 changes: 9 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ VAR_DIRS="$VAR_LOCATION/remotes \
$VAR_LOCATION/remotes/datastore/dummy \
$VAR_LOCATION/remotes/datastore/fs \
$VAR_LOCATION/remotes/datastore/ceph \
$VAR_LOCATION/remotes/datastore/qcow2 \
$VAR_LOCATION/remotes/datastore/dev \
$VAR_LOCATION/remotes/datastore/vcenter \
$VAR_LOCATION/remotes/market \
Expand Down Expand Up @@ -502,6 +503,7 @@ INSTALL_FILES=(
DATASTORE_DRIVER_FS_SCRIPTS:$VAR_LOCATION/remotes/datastore/fs
DATASTORE_DRIVER_ETC_FS_SCRIPTS:$VAR_LOCATION/remotes/etc/datastore/fs
DATASTORE_DRIVER_CEPH_SCRIPTS:$VAR_LOCATION/remotes/datastore/ceph
DATASTORE_DRIVER_QCOW2_SCRIPTS:$VAR_LOCATION/remotes/datastore/qcow2
DATASTORE_DRIVER_ETC_CEPH_SCRIPTS:$VAR_LOCATION/remotes/etc/datastore/ceph
DATASTORE_DRIVER_DEV_SCRIPTS:$VAR_LOCATION/remotes/datastore/dev
DATASTORE_DRIVER_VCENTER_SCRIPTS:$VAR_LOCATION/remotes/datastore/vcenter
Expand Down Expand Up @@ -1289,16 +1291,21 @@ TM_QCOW2_FILES="src/tm_mad/qcow2/clone \
src/tm_mad/qcow2/mkswap \
src/tm_mad/qcow2/mkimage \
src/tm_mad/qcow2/mv \
src/tm_mad/qcow2/mv.ssh \
src/tm_mad/qcow2/context \
src/tm_mad/qcow2/premigrate \
src/tm_mad/qcow2/postmigrate \
src/tm_mad/qcow2/failmigrate \
src/tm_mad/qcow2/mvds \
src/tm_mad/qcow2/mvds.ssh \
src/tm_mad/qcow2/snap_create \
src/tm_mad/qcow2/snap_create.ssh \
src/tm_mad/qcow2/snap_create_live \
src/tm_mad/qcow2/snap_create_live.ssh \
src/tm_mad/qcow2/snap_delete \
src/tm_mad/qcow2/snap_delete.ssh \
src/tm_mad/qcow2/snap_revert \
src/tm_mad/qcow2/snap_revert.ssh \
src/tm_mad/qcow2/cpds \
src/tm_mad/qcow2/cpds.ssh \
src/tm_mad/qcow2/resize"
Expand Down Expand Up @@ -1466,6 +1473,8 @@ DATASTORE_DRIVER_CEPH_SCRIPTS="src/datastore_mad/remotes/ceph/cp \
src/datastore_mad/remotes/ceph/ceph_utils.sh \
src/datastore_mad/remotes/ceph/export"

DATASTORE_DRIVER_QCOW2_SCRIPTS="src/datastore_mad/remotes/qcow2/qcow2_utils.sh"

DATASTORE_DRIVER_ETC_CEPH_SCRIPTS="src/datastore_mad/remotes/ceph/ceph.conf"

DATASTORE_DRIVER_DEV_SCRIPTS="src/datastore_mad/remotes/dev/cp \
Expand Down
35 changes: 35 additions & 0 deletions src/datastore_mad/remotes/qcow2/qcow2_utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# ----------------------------------------------------------------------------- #
# Copyright 2002-2019, OpenNebula Project (OpenNebula.org), C12G Labs #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#------------------------------------------------------------------------------ #

#--------------------------------------------------------------------------------
# Rebase backing files of snapshots in current directory
# @param $1 name of the backing_file symlink used internally
#--------------------------------------------------------------------------------

rebase_backing_files() {
local DST_FILE=$1

for SNAP_ID in $(find * -maxdepth 0 -type f -print); do
INFO=$(qemu-img info --output=json $SNAP_ID)

if [[ $INFO =~ "backing-filename" ]]; then
BACKING_FILE=${INFO/*backing-filename\": \"/}
BACKING_FILE=${BACKING_FILE/\"*/}
BACKING_FILE=$(basename ${BACKING_FILE})
qemu-img rebase -f qcow2 -u -b "${DST_FILE}.snap/$BACKING_FILE" $SNAP_ID
fi
done
}
7 changes: 1 addition & 6 deletions src/tm_mad/qcow2/cpds.ssh
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,11 @@ while IFS= read -r -d '' element; do
done < <(onevm show -x $VMID| $XPATH \
/VM/DEPLOY_ID \
/VM/LCM_STATE \
/VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/TARGET \
/VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/SOURCE \
/VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CLONE)
/VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/TARGET)

DEPLOY_ID="${XPATH_ELEMENTS[j++]}"
LCM_STATE="${XPATH_ELEMENTS[j++]}"
DISK_TARGET="${XPATH_ELEMENTS[j++]}"
DISK_SRC="${XPATH_ELEMENTS[j++]}"
CLONE="${XPATH_ELEMENTS[j++]}"

#-------------------------------------------------------------------------------
# Set dst path and dir
Expand All @@ -82,7 +78,6 @@ DST_DS_PATH="$(dirname $(dirname $DST_ARG_PATH))"
DST_PATH="${SRC_DS_PATH}${DST_ARG_PATH##$DST_DS_PATH}"

if [ "$SNAP_ID" != "-1" ]; then
[ "$CLONE" != "YES" ] && SRC_PATH="$DISK_SRC"
SRC_PATH="$SRC_PATH.snap/$SNAP_ID"
fi

Expand Down
69 changes: 31 additions & 38 deletions src/tm_mad/qcow2/ln.ssh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

SRC=$1
DST=$2

VMID=$3
DSID=$4

Expand All @@ -36,25 +35,23 @@ else
TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
fi

. $TMCOMMON

DRIVER_PATH=$(dirname $0)
LIBFS=${DRIVER_PATH}/../../datastore/libfs.sh

. $TMCOMMON

#-------------------------------------------------------------------------------
# Set dst path and dir
#-------------------------------------------------------------------------------
DST_PATH=`arg_path $DST`
DST_HOST=`arg_host $DST`
DST_PATH=`arg_path $DST`
DST_SNAP_DIR=${DST_PATH}.snap
DST_FILE=`basename $DST_PATH`
DST_DIR=`dirname $DST_PATH`

SRC_ARG_PATH=`arg_path $SRC`

DST_DS_PATH="$(dirname $(dirname $(dirname $DST_PATH)))"
SRC_DS_PATH="$(dirname $(dirname $SRC_ARG_PATH))"

SRC_PATH="${DST_DS_PATH}${SRC_ARG_PATH##$SRC_DS_PATH}"
SNAP_DIR="${SRC_PATH}.snap"
SNAP_NAME="$(basename $SNAP_DIR)"
SRC_HOST=`arg_host $SRC`
SRC_PATH=`arg_path $SRC`
SRC_SNAP_DIR=${SRC_PATH}.snap

#-------------------------------------------------------------------------------
# Create DST path
Expand All @@ -63,42 +60,38 @@ SNAP_NAME="$(basename $SNAP_DIR)"
ssh_make_path $DST_HOST $DST_DIR 'ssh'

#-------------------------------------------------------------------------------
# Link (ln) SRC into DST
# Copy SRC into DST
#-------------------------------------------------------------------------------
log "Linking $SRC_PATH in $DST"
log "Copying $SRC_PATH in $DST"

type="$(file -b --mime-type $SRC_PATH)"

if [ "$type" == "application/x-iso9660-image" ]; then
CMD="ln -sf ${SRC_PATH} ${DST_PATH}"
else
CMD=$(cat <<EOT
CMD=$(cat <<EOT
set -ex -o pipefail
mkdir -p "${SNAP_DIR}"
if [ ! -L "${SNAP_DIR}/${SNAP_NAME}" ]; then
ln -s "${SNAP_DIR}" "${SNAP_DIR}/${SNAP_NAME}"
fi
SNAP="\$(ls ${SNAP_DIR} | grep '^[[:digit:]]*$' | sort -n | tail -n 1 || :)"
if [ -z "\${SNAP}" ]; then
SNAP=0
ln -sf "$SRC_PATH" "$SNAP_DIR/0"
mkdir -p "${DST_SNAP_DIR}"
cd "${DST_SNAP_DIR}"
if [ -d ${SRC_SNAP_DIR} ]; then
for F in \$(ls ${SRC_SNAP_DIR}); do
if [ -f ${SRC_SNAP_DIR}/\$F ]; then
cp ${SRC_SNAP_DIR}/\$F .
fi
done
else
cp ${SRC_PATH} 0
fi
rm -f "${DST_PATH}"
rm -f "${DST_FILE}.snap"
ln -sf "." "${DST_FILE}.snap"
ln -sf "$SNAP_DIR/\$SNAP" "$DST_PATH"
rebase_backing_files "$DST_FILE"
rm -rf "${DST_PATH}.snap"
SNAP="\$(ls | grep '^[[:digit:]]*$' | sort -n | tail -n 1 || :)"
cd ..
rm -f "${DST_FILE}"
ln -sf "${DST_FILE}.snap/\$SNAP" "${DST_FILE}"
ln -sf "$SNAP_DIR" "${DST_PATH}.snap"
EOT
)
fi

ssh_exec_and_log $DST_HOST "$CMD" "Error linking $SRC to $DST"
ssh_exec_and_log_stdin $DST_HOST "$CMD" "$LIBFS" "Error copying $SRC to $DST"

exit 0
1 change: 1 addition & 0 deletions src/tm_mad/qcow2/mv.ssh
58 changes: 29 additions & 29 deletions src/tm_mad/qcow2/mvds.ssh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

SRC=$1
DST=$2

VMID=$3
DSID=$4

Expand All @@ -36,48 +35,49 @@ else
TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
fi

. $TMCOMMON

DRIVER_PATH=$(dirname $0)
LIBFS=${DRIVER_PATH}/../../datastore/libfs.sh

#-------------------------------------------------------------------------------
# Get Image information
#-------------------------------------------------------------------------------

DISK_ID=$(basename ${SRC} | cut -d. -f2)

XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"

unset i j XPATH_ELEMENTS

while IFS= read -r -d '' element; do
XPATH_ELEMENTS[i++]="$element"
done < <(onevm show -x $VMID| $XPATH \
/VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CLONE)

CLONE="${XPATH_ELEMENTS[j++]}"
if [ "$CLONE" != "YES" ]; then
# it's persistent image, nothing to do
exit 0
fi
. $TMCOMMON

#-------------------------------------------------------------------------------
# Set dst path and dir
#-------------------------------------------------------------------------------

SRC_PATH="$(arg_path $SRC)"
SRC_HOST="$(arg_host $SRC)"
SRC_HOST=`arg_host $SRC`
SRC_PATH=`arg_path $SRC`
SRC_SNAP_DIR=${SRC_PATH}.snap

DST_PATH="$(arg_path $DST)"
DST_PATH="$DST"
DST_FILE=`basename $DST`
DST_SNAP_DIR=${DST_PATH}.snap
DST_DIR=`dirname $DST_PATH`

#-------------------------------------------------------------------------------
# Move the image back to the datastore
#-------------------------------------------------------------------------------

log "Moving $SRC to datastore as $DST"

ssh_exec_and_log "${SRC_HOST}" \
"cp -r ${SRC_PATH} ${DST_PATH}" \
"Error copying ${SRC} to ${DST}" >&2
MV_CMD=$(cat <<EOT
set -ex -o pipefail
rm -rf ${DST_SNAP_DIR}
mkdir -p ${DST_SNAP_DIR}
mv ${SRC_SNAP_DIR}/[0-9]* ${DST_SNAP_DIR}
cd "${DST_SNAP_DIR}"
ln -sf "." "${DST_FILE}.snap"
rebase_backing_files "$DST_FILE"
SNAP="\$(ls | grep '^[[:digit:]]*$' | sort -n | tail -n 1 || :)"
cd ..
rm "${DST_FILE}"
ln -sf "${DST_FILE}.snap/\$SNAP" "${DST_FILE}"
rm -rf ${SRC_PATH}
EOT
)

ssh_exec_and_log_stdin "$SRC_HOST" "$MV_CMD" "$LIBFS" "Error moving disk ${SRC_PATH} to ${DST_PATH} on $SRC_HOST"

exit 0
85 changes: 85 additions & 0 deletions src/tm_mad/qcow2/snap_create.ssh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2019, OpenNebula Project, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #

# snap_create host:parent_image snap_id vmid ds_id

SRC=$1
SNAP_ID=$2
VMID=$3
DSID=$4

if [ -z "${ONE_LOCATION}" ]; then
TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh
DATASTORES=/var/lib/one/datastores
else
TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
DATASTORES=$ONE_LOCATION/var/datastores
fi

DRIVER_PATH=$(dirname $0)

. $TMCOMMON

SRC_PATH=$(arg_path $SRC)
SRC_HOST=$(arg_host $SRC)

#-------------------------------------------------------------------------------
# Get Image information
#-------------------------------------------------------------------------------

DISK_ID=$(basename ${SRC} | cut -d. -f2)


SYSTEM_DS_PATH=$(dirname ${SRC_PATH})

SYSTEM_DS_DISK_PATH="${SYSTEM_DS_PATH}/disk.${DISK_ID}"

# for TM_MAD=ssh create the snapshot always on system DS
DISK_PATH="${SYSTEM_DS_DISK_PATH}"
DISK_PATH_SHORT="$(basename ${SYSTEM_DS_DISK_PATH})"

NEXT_SNAP_ID=$(($SNAP_ID+1))

SNAP_DIR="${DISK_PATH}.snap"
SNAP_DIR_SHORT="${DISK_PATH_SHORT}.snap"

SNAP_PATH="${SNAP_DIR}/${NEXT_SNAP_ID}"
SNAP_PATH_SHORT="${SNAP_DIR_SHORT}/${NEXT_SNAP_ID}"

SNAP_CMD=$(cat <<EOT
set -ex -o pipefail
mkdir -p "${SNAP_DIR}"
PREVIOUS_SNAP=\$(readlink $SYSTEM_DS_DISK_PATH)
cd "${SNAP_DIR}"
if [ -e "${SNAP_PATH}" ]; then
echo "Snapshot file ${SNAP_PATH} already exists." >&2
exit 1
fi
qemu-img create -f qcow2 -o backing_fmt=qcow2 -b "\${PREVIOUS_SNAP}" "${SNAP_PATH}"
ln -sf $SNAP_PATH_SHORT $SYSTEM_DS_DISK_PATH
EOT
)

ssh_exec_and_log "${SRC_HOST}" "${SNAP_CMD}" \
"Error creating snapshot ${SNAP_PATH}"

Loading

0 comments on commit 45e3465

Please sign in to comment.