1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-24 02:03:52 +03:00

Feature #3782: Add ceph disk snapshot actions

This commit is contained in:
Jaime Melis 2015-05-28 17:19:48 +02:00
parent 83ac6188de
commit b0f72d5ad4
5 changed files with 331 additions and 2 deletions

View File

@ -1094,6 +1094,9 @@ TM_CEPH_FILES="src/tm_mad/ceph/clone \
src/tm_mad/ceph/cpds \
src/tm_mad/ceph/premigrate \
src/tm_mad/ceph/postmigrate \
src/tm_mad/ceph/snap_create \
src/tm_mad/ceph/snap_delete \
src/tm_mad/ceph/snap_revert \
src/tm_mad/ceph/delete"
TM_DEV_FILES="src/tm_mad/dev/clone \

View File

@ -97,11 +97,43 @@ log "Deleting $DST_PATH"
# drivers, is executed in the worker node and not in the CEPH frontend.
DELETE_CMD=$(cat <<EOF
set -e
rm_children(){
local rbd_snap rbd snap_id child snap_list
rbd_snap=\$1
rbd=\${rbd_snap%%@*}
snap_list=\$(set +e; rbd snap ls \$rbd)
if [ -n "\$snap_list" ]; then
CHILDREN=\$(set +e; rbd children \$rbd_snap 2>/dev/null)
for child in \$CHILDREN; do
snap_id=\${child##*-}
child=\$child@\$snap_id
rm_children \$child
done
$RBD snap unprotect \$rbd_snap
$RBD snap rm \$rbd_snap
fi
$RBD rm \$rbd
}
RBD_FORMAT=\$($RBD info $RBD_SRC | sed -n 's/.*format: // p')
$RBD rm $RBD_SRC
if [ "\$RBD_FORMAT" = "2" ]; then
has_snap_shots=\$($RBD info $RBD_SRC-0@0 2>/dev/null)
if [ -n "\$has_snap_shots" ]; then
rm_children $RBD_SRC-0@0
else
$RBD rm $RBD_SRC
fi
else
$RBD rm $RBD_SRC
fi
if [ "\$RBD_FORMAT" = "2" ]; then
$RBD snap unprotect $SRC@$RBD_SNAP

99
src/tm_mad/ceph/snap_create Executable file
View File

@ -0,0 +1,99 @@
#!/bin/bash
# -------------------------------------------------------------------------- #
# Copyright 2002-2015, 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. #
#--------------------------------------------------------------------------- #
# snap_create host:parent_image snap_id vmid ds_id
SRC=$1
SNAP_ID=$2
VM_ID=$3
DS_ID=$4
# Example
# SRC=ceph0:/var/lib/one//datastores/0/76/disk.0
# SNAP_ID=1
# VM_ID=76
# DS_ID=100
if [ -z "${ONE_LOCATION}" ]; then
TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh
else
TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
fi
DRIVER_PATH=$(dirname $0)
source $TMCOMMON
source ${DRIVER_PATH}/../../datastore/ceph/ceph.conf
#-------------------------------------------------------------------------------
# Set dst path and dir
#-------------------------------------------------------------------------------
SRC_HOST=`arg_host $SRC`
SRC_PATH=`arg_path $SRC`
#-------------------------------------------------------------------------------
# Get Image information
#-------------------------------------------------------------------------------
DISK_ID=$(echo "$SRC_PATH" | $AWK -F. '{print $NF}')
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 $VM_ID| $XPATH \
/VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/SOURCE \
/VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CEPH_USER)
RBD_SRC="${XPATH_ELEMENTS[j++]}"
CEPH_USER="${XPATH_ELEMENTS[j++]}"
RBD_DST="${RBD_SRC}-${VM_ID}-${DISK_ID}"
#-------------------------------------------------------------------------------
# Create snapshots
#-------------------------------------------------------------------------------
if [ -n "$CEPH_USER" ]; then
RBD="$RBD --id ${CEPH_USER}"
fi
SNAP_CREATE_CMD=$(cat <<EOF
set -e
RBD_FORMAT=\$($RBD info $RBD_SRC | sed -n 's/.*format: // p')
if [ "\${RBD_FORMAT}" != "2" ]; then
echo "Only RBD Format 2 is supported for this operation" >&2
exit 1
fi
$RBD snap create $RBD_DST@$SNAP_ID
$RBD snap protect $RBD_DST@$SNAP_ID
$RBD rename $RBD_DST $RBD_DST-$SNAP_ID
$RBD clone $RBD_DST-$SNAP_ID@$SNAP_ID $RBD_DST
EOF
)
ssh_exec_and_log "$SRC_HOST" "$SNAP_CREATE_CMD" \
"Error creating snapshot $RBD_DST@$SNAP_ID"
exit 0

98
src/tm_mad/ceph/snap_delete Executable file
View File

@ -0,0 +1,98 @@
#!/bin/bash
# -------------------------------------------------------------------------- #
# Copyright 2002-2015, 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. #
#--------------------------------------------------------------------------- #
# snap_delete host:parent_image snap_id vmid ds_id
SRC=$1
SNAP_ID=$2
VM_ID=$3
DS_ID=$4
# Example
# SRC=ceph0:/var/lib/one//datastores/0/76/disk.0
# SNAP_ID=1
# VM_ID=76
# DS_ID=100
if [ -z "${ONE_LOCATION}" ]; then
TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh
else
TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
fi
DRIVER_PATH=$(dirname $0)
source $TMCOMMON
source ${DRIVER_PATH}/../../datastore/ceph/ceph.conf
#-------------------------------------------------------------------------------
# Set dst path and dir
#-------------------------------------------------------------------------------
SRC_HOST=`arg_host $SRC`
SRC_PATH=`arg_path $SRC`
#-------------------------------------------------------------------------------
# Get Image information
#-------------------------------------------------------------------------------
DISK_ID=$(echo "$SRC_PATH" | $AWK -F. '{print $NF}')
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 $VM_ID| $XPATH \
/VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/SOURCE \
/VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CEPH_USER)
RBD_SRC="${XPATH_ELEMENTS[j++]}"
CEPH_USER="${XPATH_ELEMENTS[j++]}"
RBD_DST="${RBD_SRC}-${VM_ID}-${DISK_ID}"
#-------------------------------------------------------------------------------
# Create snapshots
#-------------------------------------------------------------------------------
if [ -n "$CEPH_USER" ]; then
RBD="$RBD --id ${CEPH_USER}"
fi
SNAP_DELETE_CMD=$(cat <<EOF
set -e
RBD_FORMAT=\$($RBD info $RBD_SRC | sed -n 's/.*format: // p')
if [ "\${RBD_FORMAT}" != "2" ]; then
echo "Only RBD Format 2 is supported for this operation" >&2
exit 1
fi
$RBD snap unprotect $RBD_DST-$SNAP_ID@$SNAP_ID
$RBD snap rm $RBD_DST-$SNAP_ID@$SNAP_ID
$RBD rm $RBD_DST-$SNAP_ID
EOF
)
ssh_exec_and_log "$SRC_HOST" "$SNAP_DELETE_CMD" \
"Error deleting snapshot $RBD_DST-$SNAP_ID@$SNAP_ID"
exit 0

97
src/tm_mad/ceph/snap_revert Executable file
View File

@ -0,0 +1,97 @@
#!/bin/bash
# -------------------------------------------------------------------------- #
# Copyright 2002-2015, 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. #
#--------------------------------------------------------------------------- #
# snap_revert host:parent_image snap_id vmid ds_id
SRC=$1
SNAP_ID=$2
VM_ID=$3
DS_ID=$4
# Example
# SRC=ceph0:/var/lib/one//datastores/0/76/disk.0
# SNAP_ID=1
# VM_ID=76
# DS_ID=100
if [ -z "${ONE_LOCATION}" ]; then
TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh
else
TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
fi
DRIVER_PATH=$(dirname $0)
source $TMCOMMON
source ${DRIVER_PATH}/../../datastore/ceph/ceph.conf
#-------------------------------------------------------------------------------
# Set dst path and dir
#-------------------------------------------------------------------------------
SRC_HOST=`arg_host $SRC`
SRC_PATH=`arg_path $SRC`
#-------------------------------------------------------------------------------
# Get Image information
#-------------------------------------------------------------------------------
DISK_ID=$(echo "$SRC_PATH" | $AWK -F. '{print $NF}')
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 $VM_ID| $XPATH \
/VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/SOURCE \
/VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CEPH_USER)
RBD_SRC="${XPATH_ELEMENTS[j++]}"
CEPH_USER="${XPATH_ELEMENTS[j++]}"
RBD_DST="${RBD_SRC}-${VM_ID}-${DISK_ID}"
#-------------------------------------------------------------------------------
# Create snapshots
#-------------------------------------------------------------------------------
if [ -n "$CEPH_USER" ]; then
RBD="$RBD --id ${CEPH_USER}"
fi
SNAP_REVERT_CMD=$(cat <<EOF
set -e
RBD_FORMAT=\$($RBD info $RBD_SRC | sed -n 's/.*format: // p')
if [ "\${RBD_FORMAT}" != "2" ]; then
echo "Only RBD Format 2 is supported for this operation" >&2
exit 1
fi
$RBD rm $RBD_DST
$RBD clone $RBD_DST-$SNAP_ID@$SNAP_ID $RBD_DST
EOF
)
ssh_exec_and_log "$SRC_HOST" "$SNAP_REVERT_CMD" \
"Error reverting snapshot $RBD_DST-$SNAP_ID@$SNAP_ID"
exit 0