From bed681d7b53dace3793087ddca5b4b21233195f9 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Tue, 3 Jul 2018 14:13:29 +0200 Subject: [PATCH] qemu: block: Create helper for creating data for legacy snapshots MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With 'transaction' support we don't need to keep around the multipurpose code which would create the snapshot if 'transaction' is not supported. To simplify this add a new helper that just wraps the arguments for 'blockdev-snapshot-sync' operation in 'transaction' and use it instead of qemuBlockSnapshotAddLegacy. Additionally this allows to format the arguments prior to creating the file for simpler cleanup. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- src/qemu/qemu_block.c | 36 ++++++++++++++++++++++++++++++++++++ src/qemu/qemu_block.h | 6 ++++++ src/qemu/qemu_driver.c | 18 +++--------------- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c index 7ad79c7e7d..509b0a5210 100644 --- a/src/qemu/qemu_block.c +++ b/src/qemu/qemu_block.c @@ -19,8 +19,10 @@ #include #include "qemu_block.h" +#include "qemu_command.h" #include "qemu_domain.h" #include "qemu_alias.h" +#include "qemu_monitor_json.h" #include "viralloc.h" #include "virstring.h" @@ -1707,3 +1709,37 @@ qemuBlockStorageSourceDetachOneBlockdev(virQEMUDriverPtr driver, return ret; } + + +int +qemuBlockSnapshotAddLegacy(virJSONValuePtr actions, + virDomainDiskDefPtr disk, + virStorageSourcePtr newsrc, + bool reuse) +{ + const char *format = virStorageFileFormatTypeToString(newsrc->format); + char *device = NULL; + char *source = NULL; + int ret = -1; + + if (!(device = qemuAliasDiskDriveFromDisk(disk))) + goto cleanup; + + if (qemuGetDriveSourceString(newsrc, NULL, &source) < 0) + goto cleanup; + + if (qemuMonitorJSONTransactionAdd(actions, "blockdev-snapshot-sync", + "s:device", device, + "s:snapshot-file", source, + "s:format", format, + "S:mode", reuse ? "existing" : NULL, + NULL) < 0) + goto cleanup; + + ret = 0; + + cleanup: + VIR_FREE(device); + VIR_FREE(source); + return ret; +} diff --git a/src/qemu/qemu_block.h b/src/qemu/qemu_block.h index 418b5064b5..fd8984e60b 100644 --- a/src/qemu/qemu_block.h +++ b/src/qemu/qemu_block.h @@ -117,4 +117,10 @@ qemuBlockStorageSourceDetachOneBlockdev(virQEMUDriverPtr driver, qemuDomainAsyncJob asyncJob, virStorageSourcePtr src); +int +qemuBlockSnapshotAddLegacy(virJSONValuePtr actions, + virDomainDiskDefPtr disk, + virStorageSourcePtr newsrc, + bool reuse); + #endif /* __QEMU_BLOCK_H__ */ diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 0ef252d71b..8cdb04e987 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -14935,23 +14935,16 @@ qemuDomainSnapshotCreateSingleDiskActive(virQEMUDriverPtr driver, virJSONValuePtr actions, bool reuse) { - qemuDomainObjPrivatePtr priv = vm->privateData; - char *device = NULL; - char *source = NULL; - const char *formatStr = NULL; int ret = -1; - if (!(device = qemuAliasDiskDriveFromDisk(dd->disk))) - goto cleanup; - - if (qemuGetDriveSourceString(dd->src, NULL, &source) < 0) + if (qemuBlockSnapshotAddLegacy(actions, dd->disk, dd->src, reuse) < 0) goto cleanup; /* pre-create the image file so that we can label it before handing it to qemu */ if (!reuse && dd->src->type != VIR_STORAGE_TYPE_BLOCK) { if (virStorageFileCreate(dd->src) < 0) { virReportSystemError(errno, _("failed to create image file '%s'"), - source); + NULLSTR(dd->src->path)); goto cleanup; } dd->created = true; @@ -14965,14 +14958,9 @@ qemuDomainSnapshotCreateSingleDiskActive(virQEMUDriverPtr driver, dd->prepared = true; - formatStr = virStorageFileFormatTypeToString(dd->src->format); - - ret = qemuMonitorDiskSnapshot(priv->mon, actions, device, source, - formatStr, reuse); + ret = 0; cleanup: - VIR_FREE(device); - VIR_FREE(source); return ret; } -- GitLab