From 49957f5f69e82589c4523c523bf1574b064806ac Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Mon, 8 Feb 2021 11:19:10 +0100 Subject: [PATCH] qemu: migration_cookie: Add helpers for transforming the cookie into migration params 'qemuMigrationCookieBlockDirtyBitmapsMatchDisks' maps the bitmaps from the migration cookie to actual disk objects definition pointers. 'qemuMigrationCookieBlockDirtyBitmapsToParams' converts the bitmap definitions from the migration cookie into parameters for the 'block-bitmap-mapping' migration parameter. Signed-off-by: Peter Krempa Reviewed-by: Jiri Denemark --- src/qemu/qemu_migration_cookie.c | 115 +++++++++++++++++++++++++++++++ src/qemu/qemu_migration_cookie.h | 8 +++ 2 files changed, 123 insertions(+) diff --git a/src/qemu/qemu_migration_cookie.c b/src/qemu/qemu_migration_cookie.c index 0f8555cbb0..186fe7bc9e 100644 --- a/src/qemu/qemu_migration_cookie.c +++ b/src/qemu/qemu_migration_cookie.c @@ -1580,3 +1580,118 @@ qemuMigrationCookieParse(virQEMUDriverPtr driver, return g_steal_pointer(&mig); } + + +/** + * qemuMigrationCookieBlockDirtyBitmapsMatchDisks: + * @def: domain definition + * @disks: list of qemuMigrationBlockDirtyBitmapsDiskPtr + * + * Matches all of the @disks to the actual domain disk definition objects + * by looking up the target. + */ +int +qemuMigrationCookieBlockDirtyBitmapsMatchDisks(virDomainDefPtr def, + GSList *disks) +{ + GSList *next; + + for (next = disks; next; next = next->next) { + qemuMigrationBlockDirtyBitmapsDiskPtr disk = next->data; + + if (!(disk->disk = virDomainDiskByTarget(def, disk->target))) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Can't find disk '%s' in domain definition"), + disk->target); + return -1; + } + + disk->nodename = disk->disk->src->nodeformat; + } + + return 0; +} + + +/** + * qemuMigrationCookieBlockDirtyBitmapsToParams: + * @disks: list of qemuMigrationBlockDirtyBitmapsDisk + * @mapping: filled with resulting mapping + * + * Converts @disks into the arguments for 'block-bitmap-mapping' migration + * parameter. + */ +int +qemuMigrationCookieBlockDirtyBitmapsToParams(GSList *disks, + virJSONValuePtr *mapping) +{ + g_autoptr(virJSONValue) map = virJSONValueNewArray(); + bool hasDisks = false; + GSList *nextdisk; + + for (nextdisk = disks; nextdisk; nextdisk = nextdisk->next) { + qemuMigrationBlockDirtyBitmapsDiskPtr disk = nextdisk->data; + g_autoptr(virJSONValue) jsondisk = NULL; + g_autoptr(virJSONValue) jsonbitmaps = virJSONValueNewArray(); + bool hasBitmaps = false; + GSList *nextbitmap; + + if (disk->skip || !disk->bitmaps) + continue; + + for (nextbitmap = disk->bitmaps; nextbitmap; nextbitmap = nextbitmap->next) { + qemuMigrationBlockDirtyBitmapsDiskBitmapPtr bitmap = nextbitmap->data; + g_autoptr(virJSONValue) jsonbitmap = NULL; + g_autoptr(virJSONValue) transform = NULL; + const char *bitmapname = bitmap->sourcebitmap; + + if (bitmap->skip) + continue; + + /* if there isn't an override, use the real name */ + if (!bitmapname) + bitmapname = bitmap->bitmapname; + + if (bitmap->persistent == VIR_TRISTATE_BOOL_YES) { + if (virJSONValueObjectCreate(&transform, + "b:persistent", true, NULL) < 0) + return -1; + } + + if (virJSONValueObjectCreate(&jsonbitmap, + "s:name", bitmapname, + "s:alias", bitmap->alias, + "A:transform", &transform, + NULL) < 0) + return -1; + + if (virJSONValueArrayAppend(jsonbitmaps, jsonbitmap) < 0) + return -1; + + jsonbitmap = NULL; + hasBitmaps = true; + } + + if (!hasBitmaps) + continue; + + if (virJSONValueObjectCreate(&jsondisk, + "s:node-name", disk->nodename, + "s:alias", disk->target, + "a:bitmaps", &jsonbitmaps, + NULL) < 0) + return -1; + + if (virJSONValueArrayAppend(map, jsondisk) < 0) + return -1; + + jsondisk = NULL; + hasDisks = true; + } + + if (!hasDisks) + return 0; + + *mapping = g_steal_pointer(&map); + return 0; +} diff --git a/src/qemu/qemu_migration_cookie.h b/src/qemu/qemu_migration_cookie.h index 8636f955da..e50dee7ba7 100644 --- a/src/qemu/qemu_migration_cookie.h +++ b/src/qemu/qemu_migration_cookie.h @@ -226,3 +226,11 @@ qemuMigrationCookieXMLFormat(virQEMUDriverPtr driver, virQEMUCapsPtr qemuCaps, virBufferPtr buf, qemuMigrationCookiePtr mig); + +int +qemuMigrationCookieBlockDirtyBitmapsMatchDisks(virDomainDefPtr def, + GSList *disks); + +int +qemuMigrationCookieBlockDirtyBitmapsToParams(GSList *disks, + virJSONValuePtr *mapping);