1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-25 06:03:36 +03:00

F #2095 Support persistant VM snapshots for kvm

This commit is contained in:
Jan Orel 2019-02-01 17:19:22 +01:00 committed by Ruben S. Montero
parent 89f999187d
commit a67217f2c2
6 changed files with 81 additions and 10 deletions

View File

@ -566,7 +566,7 @@ VM_MAD = [
ARGUMENTS = "-t 15 -r 0 kvm",
DEFAULT = "vmm_exec/vmm_exec_kvm.conf",
TYPE = "kvm",
KEEP_SNAPSHOTS = "no",
KEEP_SNAPSHOTS = "yes",
IMPORTED_VMS_ACTIONS = "terminate, terminate-hard, hold, release, suspend,
resume, delete, reboot, reboot-hard, resched, unresched, disk-attach,
disk-detach, nic-attach, nic-detach, snapshot-create, snapshot-delete"

View File

@ -1472,9 +1472,11 @@ int LibVirtDriver::deployment_description_kvm(
// Metadata used by drivers
// ------------------------------------------------------------------------
file << "\t<metadata>\n"
<< "\t\t<system_datastore>"
<< "\t\t<one:vm xmlns:one=\"http://opennebula.org/xmlns/libvirt/1.0\">\n"
<< "\t\t\t<one:system_datastore>"
<< one_util::escape_xml(vm->get_system_dir())
<< "</system_datastore>\n"
<< "</one:system_datastore>\n"
<< "\t\t</one:vm>\n"
// << "\t\t<opennebula>\n" << vm->to_xml(vm_xml) << "\t\t</opennebula>\n"
<< "\t</metadata>\n";

View File

@ -19,16 +19,33 @@
source $(dirname $0)/../../etc/vmm/kvm/kvmrc
source $(dirname $0)/../../scripts_common.sh
domain=$1
DEP_FILE=$1
DEP_FILE_LOCATION=$(dirname $DEP_FILE)
mkdir -p `dirname $domain`
cat > $domain
mkdir -p $DEP_FILE_LOCATION
cat > $DEP_FILE
data=`virsh --connect $LIBVIRT_URI create $domain`
DATA=`virsh --connect $LIBVIRT_URI create $DEP_FILE`
if [ "x$?" = "x0" ]; then
echo $data | sed 's/Domain //' | sed 's/ created from .*$//'
DOMAIN_ID=$(echo $DATA | sed 's/Domain //' | sed 's/ created from .*$//')
echo $DOMAIN_ID
# redefine potential snapshots
for SNAPSHOT_MD_XML in $(ls ${DEP_FILE_LOCATION}/snap-*.xml 2>/dev/null); do
# query UUID, but only once
UUID=${UUID:-$(virsh --connect $LIBVIRT_URI dominfo $DOMAIN_ID | grep UUID: | awk '{print $2}')}
# replace uuid in the snapshot metadata xml
sed -i "s%<uuid>[[:alnum:]-]*</uuid>%<uuid>$UUID</uuid>%" $SNAPSHOT_MD_XML
# redefine the snapshot using the xml metadata file
virsh --connect $LIBVIRT_URI snapshot-create $DOMAIN_ID $SNAPSHOT_MD_XML --redefine > /dev/null || true
done
else
error_message "Could not create domain from $domain"
error_message "Could not create domain from $DEP_FILE"
exit -1
fi

View File

@ -20,6 +20,9 @@ export LIBVIRT_URI=qemu:///system
export QEMU_PROTOCOL=qemu+ssh
export LIBVIRT_MD_URI=http://opennebula.org/xmlns/libvirt/1.0
export LIBVIRT_MD_KEY=one
# Seconds to wait after shutdown until timeout
export SHUTDOWN_TIMEOUT=300

View File

@ -22,9 +22,36 @@ source $(dirname $0)/../../scripts_common.sh
DOMAIN="$1"
SNAP_ID="$2"
data=`virsh --connect $LIBVIRT_URI snapshot-create-as $DOMAIN --name "snap-${SNAP_ID}"`
# -------- Get datastore location from libvirt metadata ------------
DRIVER_PATH=$(dirname $0)
XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"
METADATA_XML=`virsh --connect $LIBVIRT_URI metadata $DOMAIN $LIBVIRT_MD_URI $LIBVIRT_MD_KEY`
unset i XPATH_ELEMENTS
while IFS= read -r -d '' element; do
XPATH_ELEMENTS[i++]="$element"
done < <(echo "$METADATA_XML" | $XPATH /vm/system_datastore/)
unset i
DATASTORE_PATH="${XPATH_ELEMENTS[i++]}"
# -------- Create the snapshot and dump its metadata to xml file ------------
SNAP_NAME="snap-${SNAP_ID}"
data=`virsh --connect $LIBVIRT_URI snapshot-create-as $DOMAIN --name "$SNAP_NAME"`
if [ "$?" = "0" ]; then
SNAP_XML_PATH="${DATASTORE_PATH}/${SNAP_NAME}.xml"
# dump snapshot metadata xml to the VM location
virsh --connect $LIBVIRT_URI snapshot-dumpxml $DOMAIN $SNAP_NAME > $SNAP_XML_PATH || true
echo "$data" | awk '{print $3}'
else
error_message "Could not create snapshot $NAME for domain $DOMAIN."

View File

@ -22,6 +22,28 @@ source $(dirname $0)/../../scripts_common.sh
DOMAIN="$1"
NAME="$2"
# -------- Get datastore location from libvirt metadata ------------
DRIVER_PATH=$(dirname $0)
XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"
METADATA_XML=`virsh --connect $LIBVIRT_URI metadata $DOMAIN $LIBVIRT_MD_URI $LIBVIRT_MD_KEY`
unset i XPATH_ELEMENTS
while IFS= read -r -d '' element; do
XPATH_ELEMENTS[i++]="$element"
done < <(echo "$METADATA_XML" | $XPATH /vm/system_datastore/)
unset i
DATASTORE_PATH="${XPATH_ELEMENTS[i++]}"
# ------ Delete snapshot metadata and the snapshot itself ----------
rm ${DATASTORE_PATH}/${NAME}.xml 2>/dev/null || true
exec_and_log \
"virsh --connect $LIBVIRT_URI snapshot-delete $DOMAIN $NAME" \
"Could not delete snapshot $NAME for domain $DOMAIN."