qemu: block: Create helper for creating data for legacy snapshots

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 <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2018-07-03 14:13:29 +02:00
parent faf769d862
commit bed681d7b5
3 changed files with 45 additions and 15 deletions

View File

@ -19,8 +19,10 @@
#include <config.h>
#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;
}

View File

@ -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__ */

View File

@ -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;
}