[libvirt] [PATCH v7 0/4] Handling of undefine and re define snapshots with VirtualBox 4.2 or higher

Hello, This is a new series of patches in order to support undefining and redefining snapshots with VirtualBox 4.2 or higher. These patches are based on Manuel Vives' patches, taking into account Daniel P. Berrange's remarks. The VirtualBox API provides only high level operations to manipulate snapshots, so it not possible to support flags like VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE and VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY with only API calls. Following an IRC talk with Eric Blake, the decision was made to emulate these behaviours by manipulating directly the .vbox XML files. The first patch adds extra details in the snapshot XML returned by libvirt. We will need those details in order to redefine the snapshots. The second patch adds a new API to manipulate the VirtualBox XML file. It provides several structs describing the VirtualBox XML file nodes and functions which can manipulate these structs. The third patch adds support of the VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE and VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT flags in virDomainSnapshotCreateXML. The idea is to unregister the machine, add the snapshot in the Virtualbox XML file and re-register the machine. However, VirtualBox forbids a machine to have snapshots but no current snapshot. So, if the real current snapshot has not been redefined yet, we create fake disks, allowing us to have an intermediate state in order to not corrupt the snapshot's read-write disks. These fake disks will be deleted during the next redefine. The fourth and last patch adds support of the VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY flag in virDomainSnapshotDelete. As in the third patch, we also create fake disks to not corrupt the snapshot's read-write disks. The patches were only tested with VirtualBox 4.3.10 and VirtualBox 4.2.24. Regards Yohan BELLEGUIC v7: * Add vbox_snapshot_conf.{h,c} files to (de)serialize VirtualBox XML files * Update the code to use the API exposed by vbox_snapshot_conf.h * Handle the fact that VirtualBox forbids a machine to have snapshots but no current snapshot v6: * Rebased because of a massive change in vbox_tmpl.c due to changes in the handling of different versions of VirtualBox v5: * The patches are modified according to a first review by Laine Stump: * renamed virSearchUuid to virSearchRegex and moved it from viruuid.{c,h} to virstring.{c,h}. * Various fixes. v4: * The code is compliant with Virtualbox 4.3 API * Some minor modifications in order to satisfy "make syntax-check" v3: * Use of STREQ_NULLABLE instead of STREQ in one case * Fix the method for finding uuids according to Ján Tomko review v2: * Fix a licence problem with the method for string replacement Manuel VIVES (1): vbox_tmpl.c: Better XML description for snapshots Yohan BELLEGUIC (3): Add vbox_snapshot_conf struct vbox_tmpl.c: Patch for redefining snapshots vbox_tmpl.c: Add function for undefining snapshot po/POTFILES.in | 1 + src/Makefile.am | 1 + src/vbox/vbox_snapshot_conf.c | 1383 +++++++++++++++++++++++++ src/vbox/vbox_snapshot_conf.h | 100 ++ src/vbox/vbox_tmpl.c | 2225 +++++++++++++++++++++++++++++++++++++---- 5 files changed, 3538 insertions(+), 172 deletions(-) create mode 100644 src/vbox/vbox_snapshot_conf.c create mode 100644 src/vbox/vbox_snapshot_conf.h -- 1.7.10.4

From: Manuel VIVES <manuel.vives@diateam.net> It will be needed for the future patches because we will redefine snapshots --- src/vbox/vbox_tmpl.c | 513 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 504 insertions(+), 9 deletions(-) diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c index a305fe2..ac000cf 100644 --- a/src/vbox/vbox_tmpl.c +++ b/src/vbox/vbox_tmpl.c @@ -38,6 +38,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> +#include <libxml/xmlwriter.h> #include "internal.h" #include "datatypes.h" @@ -58,6 +59,8 @@ #include "fdstream.h" #include "viruri.h" #include "virstring.h" +#include "virtime.h" +#include "virutil.h" /* This one changes from version to version. */ #if VBOX_API_VERSION == 2002000 @@ -294,6 +297,13 @@ static void vboxDriverUnlock(vboxGlobalData *data) virMutexUnlock(&data->lock); } +typedef enum { + VBOX_STORAGE_DELETE_FLAG = 0, +#if VBOX_API_VERSION >= 4002000 + VBOX_STORAGE_CLOSE_FLAG = 1, +#endif +} vboxStorageDeleteOrCloseFlags; + #if VBOX_API_VERSION == 2002000 static void nsIDtoChar(unsigned char *uuid, const nsID *iid) @@ -6039,7 +6049,9 @@ vboxDomainSnapshotCreateXML(virDomainPtr dom, virCheckFlags(VIR_DOMAIN_SNAPSHOT_CREATE_NO_METADATA, NULL); if (!(def = virDomainSnapshotDefParseString(xmlDesc, data->caps, - data->xmlopt, 0, 0))) + data->xmlopt, -1, + VIR_DOMAIN_SNAPSHOT_PARSE_DISKS | + VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE))) goto cleanup; if (def->ndisks) { @@ -6130,6 +6142,436 @@ vboxDomainSnapshotCreateXML(virDomainPtr dom, return ret; } +#if VBOX_API_VERSION >=4002000 +static +int vboxSnapshotGetReadWriteDisks(virDomainSnapshotDefPtr def, + virDomainSnapshotPtr snapshot) +{ + virDomainPtr dom = snapshot->domain; + VBOX_OBJECT_CHECK(dom->conn, int, -1); + vboxIID domiid = VBOX_IID_INITIALIZER; + IMachine *machine = NULL; + ISnapshot *snap = NULL; + IMachine *snapMachine = NULL; + vboxArray mediumAttachments = VBOX_ARRAY_INITIALIZER; + PRUint32 maxPortPerInst[StorageBus_Floppy + 1] = {}; + PRUint32 maxSlotPerPort[StorageBus_Floppy + 1] = {}; + int diskCount = 0; + nsresult rc; + vboxIID snapIid = VBOX_IID_INITIALIZER; + char *snapshotUuidStr = NULL; + size_t i = 0; + + vboxIIDFromUUID(&domiid, dom->uuid); + rc = VBOX_OBJECT_GET_MACHINE(domiid.value, &machine); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("no domain with matching UUID")); + goto cleanup; + } + if (!(snap = vboxDomainSnapshotGet(data, dom, machine, snapshot->name))) + goto cleanup; + + rc = snap->vtbl->GetId(snap, &snapIid.value); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Could not get snapshot id")); + goto cleanup; + } + + VBOX_UTF16_TO_UTF8(snapIid.value, &snapshotUuidStr); + rc = snap->vtbl->GetMachine(snap, &snapMachine); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("could not get machine")); + goto cleanup; + } + def->ndisks = 0; + rc = vboxArrayGet(&mediumAttachments, snapMachine, snapMachine->vtbl->GetMediumAttachments); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("no medium attachments")); + goto cleanup; + } + /* get the number of attachments */ + for (i = 0; i < mediumAttachments.count; i++) { + IMediumAttachment *imediumattach = mediumAttachments.items[i]; + if (imediumattach) { + IMedium *medium = NULL; + + rc = imediumattach->vtbl->GetMedium(imediumattach, &medium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get medium")); + goto cleanup; + } + if (medium) { + def->ndisks++; + VBOX_RELEASE(medium); + } + } + } + /* Allocate mem, if fails return error */ + if (VIR_ALLOC_N(def->disks, def->ndisks) < 0) + goto cleanup; + + if (!vboxGetMaxPortSlotValues(data->vboxObj, maxPortPerInst, maxSlotPerPort)) + goto cleanup; + + /* get the attachment details here */ + for (i = 0; i < mediumAttachments.count && diskCount < def->ndisks; i++) { + IStorageController *storageController = NULL; + PRUnichar *storageControllerName = NULL; + PRUint32 deviceType = DeviceType_Null; + PRUint32 storageBus = StorageBus_Null; + IMedium *disk = NULL; + PRUnichar *childLocUtf16 = NULL; + char *childLocUtf8 = NULL; + PRUint32 deviceInst = 0; + PRInt32 devicePort = 0; + PRInt32 deviceSlot = 0; + vboxArray children = VBOX_ARRAY_INITIALIZER; + vboxArray snapshotIids = VBOX_ARRAY_INITIALIZER; + IMediumAttachment *imediumattach = mediumAttachments.items[i]; + size_t j = 0; + size_t k = 0; + if (!imediumattach) + continue; + rc = imediumattach->vtbl->GetMedium(imediumattach, &disk); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get medium")); + goto cleanup; + } + if (!disk) + continue; + rc = imediumattach->vtbl->GetController(imediumattach, &storageControllerName); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get controller")); + goto cleanup; + } + if (!storageControllerName) { + VBOX_RELEASE(disk); + continue; + } + rc = vboxArrayGet(&children, disk, disk->vtbl->GetChildren); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get children disk")); + goto cleanup; + } + rc = vboxArrayGetWithPtrArg(&snapshotIids, disk, disk->vtbl->GetSnapshotIds, domiid.value); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get snapshot ids")); + goto cleanup; + } + for (j = 0; j < children.count; ++j) { + IMedium *child = children.items[j]; + for (k = 0; k < snapshotIids.count; ++k) { + PRUnichar *diskSnapId = snapshotIids.items[k]; + char *diskSnapIdStr = NULL; + VBOX_UTF16_TO_UTF8(diskSnapId, &diskSnapIdStr); + if (STREQ(diskSnapIdStr, snapshotUuidStr)) { + rc = machine->vtbl->GetStorageControllerByName(machine, + storageControllerName, + &storageController); + VBOX_UTF16_FREE(storageControllerName); + if (!storageController) { + VBOX_RELEASE(child); + break; + } + rc = child->vtbl->GetLocation(child, &childLocUtf16); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get disk location")); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(childLocUtf16, &childLocUtf8); + VBOX_UTF16_FREE(childLocUtf16); + if (VIR_STRDUP(def->disks[diskCount].src.path, childLocUtf8) < 0) { + VBOX_RELEASE(child); + VBOX_RELEASE(storageController); + goto cleanup; + } + VBOX_UTF8_FREE(childLocUtf8); + + rc = storageController->vtbl->GetBus(storageController, &storageBus); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get storage controller bus")); + goto cleanup; + } + rc = imediumattach->vtbl->GetType(imediumattach, &deviceType); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get medium attachment type")); + goto cleanup; + } + rc = imediumattach->vtbl->GetPort(imediumattach, &devicePort); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get medium attachchment type")); + goto cleanup; + } + rc = imediumattach->vtbl->GetDevice(imediumattach, &deviceSlot); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get medium attachment device")); + goto cleanup; + } + def->disks[diskCount].src.type = VIR_STORAGE_TYPE_FILE; + def->disks[diskCount].name = vboxGenerateMediumName(storageBus, + deviceInst, + devicePort, + deviceSlot, + maxPortPerInst, + maxSlotPerPort); + } + VBOX_UTF8_FREE(diskSnapIdStr); + } + } + VBOX_RELEASE(storageController); + VBOX_RELEASE(disk); + diskCount++; + } + vboxArrayRelease(&mediumAttachments); + + ret = 0; + cleanup: + if (ret < 0) { + for (i = 0; i < def->dom->ndisks; i++) { + VIR_FREE(def->dom->disks[i]); + } + VIR_FREE(def->dom->disks); + def->dom->ndisks = 0; + ret = -1; + } + VBOX_RELEASE(snap); + return ret; +} + +static +int vboxSnapshotGetReadOnlyDisks(virDomainSnapshotPtr snapshot, + virDomainSnapshotDefPtr def) +{ + virDomainPtr dom = snapshot->domain; + VBOX_OBJECT_CHECK(dom->conn, int, -1); + vboxIID domiid = VBOX_IID_INITIALIZER; + ISnapshot *snap = NULL; + IMachine *machine = NULL; + IMachine *snapMachine = NULL; + IStorageController *storageController = NULL; + IMedium *disk = NULL; + nsresult rc; + vboxIIDFromUUID(&domiid, dom->uuid); + vboxArray mediumAttachments = VBOX_ARRAY_INITIALIZER; + size_t i = 0; + PRUint32 maxPortPerInst[StorageBus_Floppy + 1] = {}; + PRUint32 maxSlotPerPort[StorageBus_Floppy + 1] = {}; + int diskCount = 0; + + rc = VBOX_OBJECT_GET_MACHINE(domiid.value, &machine); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_NO_DOMAIN, "%s", + _("no domain with matching UUID")); + goto cleanup; + } + + if (!(snap = vboxDomainSnapshotGet(data, dom, machine, snapshot->name))) + goto cleanup; + + rc = snap->vtbl->GetMachine(snap, &snapMachine); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get machine")); + goto cleanup; + } + /* + * Get READ ONLY disks + * In the snapshot metadata, these are the disks written inside the <domain> node + */ + rc = vboxArrayGet(&mediumAttachments, snapMachine, snapMachine->vtbl->GetMediumAttachments); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get medium attachments")); + goto cleanup; + } + /* get the number of attachments */ + for (i = 0; i < mediumAttachments.count; i++) { + IMediumAttachment *imediumattach = mediumAttachments.items[i]; + if (imediumattach) { + IMedium *medium = NULL; + + rc = imediumattach->vtbl->GetMedium(imediumattach, &medium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get medium")); + goto cleanup; + } + if (medium) { + def->dom->ndisks++; + VBOX_RELEASE(medium); + } + } + } + + /* Allocate mem, if fails return error */ + if (VIR_ALLOC_N(def->dom->disks, def->dom->ndisks) >= 0) { + for (i = 0; i < def->dom->ndisks; i++) { + if (VIR_ALLOC(def->dom->disks[i]) < 0) + goto cleanup; + } + } else + goto cleanup; + + if (!vboxGetMaxPortSlotValues(data->vboxObj, maxPortPerInst, maxSlotPerPort)) + goto cleanup; + + /* get the attachment details here */ + for (i = 0; i < mediumAttachments.count && diskCount < def->dom->ndisks; i++) { + PRUnichar *storageControllerName = NULL; + PRUint32 deviceType = DeviceType_Null; + PRUint32 storageBus = StorageBus_Null; + PRBool readOnly = PR_FALSE; + PRUnichar *mediumLocUtf16 = NULL; + char *mediumLocUtf8 = NULL; + PRUint32 deviceInst = 0; + PRInt32 devicePort = 0; + PRInt32 deviceSlot = 0; + IMediumAttachment *imediumattach = mediumAttachments.items[i]; + if (!imediumattach) + continue; + rc = imediumattach->vtbl->GetMedium(imediumattach, &disk); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get medium")); + goto cleanup; + } + if (!disk) + continue; + rc = imediumattach->vtbl->GetController(imediumattach, &storageControllerName); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get storage controller name")); + goto cleanup; + } + if (!storageControllerName) { + continue; + } + rc = machine->vtbl->GetStorageControllerByName(machine, + storageControllerName, + &storageController); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get storage controller")); + goto cleanup; + } + VBOX_UTF16_FREE(storageControllerName); + if (!storageController) { + continue; + } + rc = disk->vtbl->GetLocation(disk, &mediumLocUtf16); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get disk location")); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(mediumLocUtf16, &mediumLocUtf8); + VBOX_UTF16_FREE(mediumLocUtf16); + if (VIR_STRDUP(def->dom->disks[diskCount]->src.path, mediumLocUtf8) < 0) + goto cleanup; + + VBOX_UTF8_FREE(mediumLocUtf8); + + rc = storageController->vtbl->GetBus(storageController, &storageBus); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get storage controller bus")); + goto cleanup; + } + if (storageBus == StorageBus_IDE) { + def->dom->disks[diskCount]->bus = VIR_DOMAIN_DISK_BUS_IDE; + } else if (storageBus == StorageBus_SATA) { + def->dom->disks[diskCount]->bus = VIR_DOMAIN_DISK_BUS_SATA; + } else if (storageBus == StorageBus_SCSI) { + def->dom->disks[diskCount]->bus = VIR_DOMAIN_DISK_BUS_SCSI; + } else if (storageBus == StorageBus_Floppy) { + def->dom->disks[diskCount]->bus = VIR_DOMAIN_DISK_BUS_FDC; + } + + rc = imediumattach->vtbl->GetType(imediumattach, &deviceType); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get medium attachment type")); + goto cleanup; + } + if (deviceType == DeviceType_HardDisk) + def->dom->disks[diskCount]->device = VIR_DOMAIN_DISK_DEVICE_DISK; + else if (deviceType == DeviceType_Floppy) + def->dom->disks[diskCount]->device = VIR_DOMAIN_DISK_DEVICE_FLOPPY; + else if (deviceType == DeviceType_DVD) + def->dom->disks[diskCount]->device = VIR_DOMAIN_DISK_DEVICE_CDROM; + + rc = imediumattach->vtbl->GetPort(imediumattach, &devicePort); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get medium attachment port")); + goto cleanup; + } + rc = imediumattach->vtbl->GetDevice(imediumattach, &deviceSlot); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get device")); + goto cleanup; + } + rc = disk->vtbl->GetReadOnly(disk, &readOnly); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get read only attribute")); + goto cleanup; + } + if (readOnly == PR_TRUE) + def->dom->disks[diskCount]->readonly = true; + def->dom->disks[diskCount]->src.type = VIR_STORAGE_TYPE_FILE; + def->dom->disks[diskCount]->dst = vboxGenerateMediumName(storageBus, + deviceInst, + devicePort, + deviceSlot, + maxPortPerInst, + maxSlotPerPort); + if (!def->dom->disks[diskCount]->dst) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Could not generate medium name for the disk " + "at: controller instance:%u, port:%d, slot:%d"), + deviceInst, devicePort, deviceSlot); + ret = -1; + goto cleanup; + } + diskCount ++; + } + /* cleanup on error */ + + ret = 0; + cleanup: + if (ret < 0) { + for (i = 0; i < def->dom->ndisks; i++) { + VIR_FREE(def->dom->disks[i]); + } + VIR_FREE(def->dom->disks); + def->dom->ndisks = 0; + ret = -1; + goto cleanup; + } + VBOX_RELEASE(disk); + VBOX_RELEASE(storageController); + vboxArrayRelease(&mediumAttachments); + VBOX_RELEASE(snap); + return ret; +} +#endif + static char * vboxDomainSnapshotGetXMLDesc(virDomainSnapshotPtr snapshot, unsigned int flags) @@ -6147,6 +6589,10 @@ vboxDomainSnapshotGetXMLDesc(virDomainSnapshotPtr snapshot, PRInt64 timestamp; PRBool online = PR_FALSE; char uuidstr[VIR_UUID_STRING_BUFLEN]; +#if VBOX_API_VERSION >=4002000 + PRUint32 memorySize = 0; + PRUint32 CPUCount = 0; +#endif virCheckFlags(0, NULL); @@ -6161,11 +6607,40 @@ vboxDomainSnapshotGetXMLDesc(virDomainSnapshotPtr snapshot, if (!(snap = vboxDomainSnapshotGet(data, dom, machine, snapshot->name))) goto cleanup; - if (VIR_ALLOC(def) < 0) + if (VIR_ALLOC(def) < 0 + || VIR_ALLOC(def->dom) < 0) goto cleanup; if (VIR_STRDUP(def->name, snapshot->name) < 0) goto cleanup; +#if VBOX_API_VERSION >=4002000 + /* Register def->dom properties for them to be saved inside the snapshot XMl + * Otherwise, there is a problem while parsing the xml + */ + def->dom->virtType = VIR_DOMAIN_VIRT_VBOX; + def->dom->id = dom->id; + memcpy(def->dom->uuid, dom->uuid, VIR_UUID_BUFLEN); + ignore_value(VIR_STRDUP(def->dom->name, dom->name)); + machine->vtbl->GetMemorySize(machine, &memorySize); + def->dom->mem.cur_balloon = memorySize * 1024; + /* Currently setting memory and maxMemory as same, cause + * the notation here seems to be inconsistent while + * reading and while dumping xml + */ + def->dom->mem.max_balloon = memorySize * 1024; + ignore_value(VIR_STRDUP(def->dom->os.type, "hvm")); + def->dom->os.arch = virArchFromHost(); + machine->vtbl->GetCPUCount(machine, &CPUCount); + def->dom->maxvcpus = def->dom->vcpus = CPUCount; + if (vboxSnapshotGetReadWriteDisks(def, snapshot) < 0) { + VIR_DEBUG("Could not get read write disks for snapshot"); + } + + if (vboxSnapshotGetReadOnlyDisks(snapshot, def) <0) { + VIR_DEBUG("Could not get Readonly disks for snapshot"); + } +#endif /* VBOX_API_VERSION >= 4002000 */ + rc = snap->vtbl->GetDescription(snap, &str16); if (NS_FAILED(rc)) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -6230,6 +6705,7 @@ vboxDomainSnapshotGetXMLDesc(virDomainSnapshotPtr snapshot, def->state = VIR_DOMAIN_SHUTOFF; virUUIDFormat(dom->uuid, uuidstr); + memcpy(def->dom->uuid, dom->uuid, VIR_UUID_BUFLEN); ret = virDomainSnapshotDefFormat(uuidstr, def, flags, 0); cleanup: @@ -6936,6 +7412,8 @@ vboxDomainSnapshotDeleteTree(vboxGlobalData *data, return ret; } + + static int vboxDomainSnapshotDelete(virDomainSnapshotPtr snapshot, unsigned int flags) @@ -6971,8 +7449,9 @@ vboxDomainSnapshotDelete(virDomainSnapshotPtr snapshot, goto cleanup; } - /* VBOX snapshots do not require any libvirt metadata, making this - * flag trivial once we know we have a valid snapshot. */ + /* In case we just want to delete the metadata, we will edit the vbox file in order + *to remove the node concerning the snapshot + */ if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY) { ret = 0; goto cleanup; @@ -8838,8 +9317,9 @@ static virStorageVolPtr vboxStorageVolCreateXML(virStoragePoolPtr pool, return ret; } -static int vboxStorageVolDelete(virStorageVolPtr vol, - unsigned int flags) +static int vboxStorageDeleteOrClose(virStorageVolPtr vol, + unsigned int flags, + unsigned int flagDeleteOrClose) { VBOX_OBJECT_CHECK(vol->conn, int, -1); vboxIID hddIID = VBOX_IID_INITIALIZER; @@ -8994,8 +9474,18 @@ static int vboxStorageVolDelete(virStorageVolPtr vol, if (machineIdsSize == 0 || machineIdsSize == deregister) { IProgress *progress = NULL; - +#if VBOX_API_VERSION >= 4002000 + if (flagDeleteOrClose & VBOX_STORAGE_CLOSE_FLAG) { + rc = hardDisk->vtbl->Close(hardDisk); + if (NS_SUCCEEDED(rc)) { + DEBUGIID("HardDisk closed, UUID", hddIID.value); + ret = 0; + } + } +#endif + if (flagDeleteOrClose & VBOX_STORAGE_DELETE_FLAG){ rc = hardDisk->vtbl->DeleteStorage(hardDisk, &progress); + } if (NS_SUCCEEDED(rc) && progress) { progress->vtbl->WaitForCompletion(progress, -1); @@ -9014,8 +9504,13 @@ static int vboxStorageVolDelete(virStorageVolPtr vol, return ret; } -static int -vboxStorageVolGetInfo(virStorageVolPtr vol, virStorageVolInfoPtr info) +static int vboxStorageVolDelete(virStorageVolPtr vol, + unsigned int flags) +{ + return vboxStorageDeleteOrClose(vol, flags, VBOX_STORAGE_DELETE_FLAG); +} + +static int vboxStorageVolGetInfo(virStorageVolPtr vol, virStorageVolInfoPtr info) { VBOX_OBJECT_CHECK(vol->conn, int, -1); IHardDisk *hardDisk = NULL; -- 1.7.10.4

On Fri, Apr 18, 2014 at 11:51:31AM +0200, Yohan BELLEGUIC wrote:
From: Manuel VIVES <manuel.vives@diateam.net>
It will be needed for the future patches because we will redefine snapshots --- src/vbox/vbox_tmpl.c | 513 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 504 insertions(+), 9 deletions(-)
diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c index a305fe2..ac000cf 100644 --- a/src/vbox/vbox_tmpl.c +++ b/src/vbox/vbox_tmpl.c @@ -38,6 +38,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> +#include <libxml/xmlwriter.h>
This can be removed, since the XML creation is now a separate module in your second patch.
@@ -294,6 +297,13 @@ static void vboxDriverUnlock(vboxGlobalData *data) virMutexUnlock(&data->lock); }
+typedef enum { + VBOX_STORAGE_DELETE_FLAG = 0, +#if VBOX_API_VERSION >= 4002000 + VBOX_STORAGE_CLOSE_FLAG = 1, +#endif
IMHO remove this #if and unconditionally define the VBOX_STORAGE_CLOSE_FLAG item. There's no harm having it even if you don't use it, and less #ifdefs makes for clearer code
+#if VBOX_API_VERSION >=4002000 +static +int vboxSnapshotGetReadWriteDisks(virDomainSnapshotDefPtr def, + virDomainSnapshotPtr snapshot)
+ ret = 0; + cleanup: + if (ret < 0) { + for (i = 0; i < def->dom->ndisks; i++) { + VIR_FREE(def->dom->disks[i]); + }
Nitpick, no need for {} on the single line for() statement
+ VIR_FREE(def->dom->disks); + def->dom->ndisks = 0; + ret = -1; + } + VBOX_RELEASE(snap); + return ret; +}
+static +int vboxSnapshotGetReadOnlyDisks(virDomainSnapshotPtr snapshot, + virDomainSnapshotDefPtr def) +{
+ /* Allocate mem, if fails return error */ + if (VIR_ALLOC_N(def->dom->disks, def->dom->ndisks) >= 0) { + for (i = 0; i < def->dom->ndisks; i++) { + if (VIR_ALLOC(def->dom->disks[i]) < 0) + goto cleanup; + } + } else + goto cleanup;
When one part of an if/else has {}, then HACKING rules require that all parts use {}
+ if (!vboxGetMaxPortSlotValues(data->vboxObj, maxPortPerInst, maxSlotPerPort)) + goto cleanup;
This indentation makes it look like you meant this statement to be part of the 'else' clause. If not, reduce the indentation to reflect what you intend
+ + /* get the attachment details here */ + for (i = 0; i < mediumAttachments.count && diskCount < def->dom->ndisks; i++) { + PRUnichar *storageControllerName = NULL; + PRUint32 deviceType = DeviceType_Null; + PRUint32 storageBus = StorageBus_Null; + PRBool readOnly = PR_FALSE; + PRUnichar *mediumLocUtf16 = NULL; + char *mediumLocUtf8 = NULL; + PRUint32 deviceInst = 0; + PRInt32 devicePort = 0; + PRInt32 deviceSlot = 0; + IMediumAttachment *imediumattach = mediumAttachments.items[i]; + if (!imediumattach) + continue; + rc = imediumattach->vtbl->GetMedium(imediumattach, &disk); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get medium")); + goto cleanup; + } + if (!disk) + continue; + rc = imediumattach->vtbl->GetController(imediumattach, &storageControllerName); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get storage controller name")); + goto cleanup; + } + if (!storageControllerName) { + continue; + }
No need for {} on a single line if.
+ rc = machine->vtbl->GetStorageControllerByName(machine, + storageControllerName, + &storageController); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get storage controller")); + goto cleanup; + } + VBOX_UTF16_FREE(storageControllerName); + if (!storageController) { + continue; + }
And again
+ rc = disk->vtbl->GetLocation(disk, &mediumLocUtf16); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get disk location")); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(mediumLocUtf16, &mediumLocUtf8); + VBOX_UTF16_FREE(mediumLocUtf16); + if (VIR_STRDUP(def->dom->disks[diskCount]->src.path, mediumLocUtf8) < 0) + goto cleanup; + + VBOX_UTF8_FREE(mediumLocUtf8); + + rc = storageController->vtbl->GetBus(storageController, &storageBus); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get storage controller bus")); + goto cleanup; + } + if (storageBus == StorageBus_IDE) { + def->dom->disks[diskCount]->bus = VIR_DOMAIN_DISK_BUS_IDE; + } else if (storageBus == StorageBus_SATA) { + def->dom->disks[diskCount]->bus = VIR_DOMAIN_DISK_BUS_SATA; + } else if (storageBus == StorageBus_SCSI) { + def->dom->disks[diskCount]->bus = VIR_DOMAIN_DISK_BUS_SCSI; + } else if (storageBus == StorageBus_Floppy) { + def->dom->disks[diskCount]->bus = VIR_DOMAIN_DISK_BUS_FDC; + } +
+ cleanup: + if (ret < 0) { + for (i = 0; i < def->dom->ndisks; i++) { + VIR_FREE(def->dom->disks[i]); + }
No need for {}
+ VIR_FREE(def->dom->disks); + def->dom->ndisks = 0; + ret = -1; + goto cleanup;
This 'goto cleanup' causes an infinite loop !
+ } + VBOX_RELEASE(disk); + VBOX_RELEASE(storageController); + vboxArrayRelease(&mediumAttachments); + VBOX_RELEASE(snap); + return ret; +} +#endif + static char * vboxDomainSnapshotGetXMLDesc(virDomainSnapshotPtr snapshot, unsigned int flags) @@ -6147,6 +6589,10 @@ vboxDomainSnapshotGetXMLDesc(virDomainSnapshotPtr snapshot, PRInt64 timestamp; PRBool online = PR_FALSE; char uuidstr[VIR_UUID_STRING_BUFLEN]; +#if VBOX_API_VERSION >=4002000 + PRUint32 memorySize = 0; + PRUint32 CPUCount = 0; +#endif
virCheckFlags(0, NULL);
@@ -6161,11 +6607,40 @@ vboxDomainSnapshotGetXMLDesc(virDomainSnapshotPtr snapshot, if (!(snap = vboxDomainSnapshotGet(data, dom, machine, snapshot->name))) goto cleanup;
- if (VIR_ALLOC(def) < 0) + if (VIR_ALLOC(def) < 0 + || VIR_ALLOC(def->dom) < 0) goto cleanup;
Slightly more usual style is to put '||' on the end of the previous line, rather than start of the next line.
if (VIR_STRDUP(def->name, snapshot->name) < 0) goto cleanup;
+#if VBOX_API_VERSION >=4002000 + /* Register def->dom properties for them to be saved inside the snapshot XMl + * Otherwise, there is a problem while parsing the xml + */ + def->dom->virtType = VIR_DOMAIN_VIRT_VBOX; + def->dom->id = dom->id; + memcpy(def->dom->uuid, dom->uuid, VIR_UUID_BUFLEN); + ignore_value(VIR_STRDUP(def->dom->name, dom->name));
Nooo, ignoring OOM like this is not ok.[
+ machine->vtbl->GetMemorySize(machine, &memorySize); + def->dom->mem.cur_balloon = memorySize * 1024; + /* Currently setting memory and maxMemory as same, cause + * the notation here seems to be inconsistent while + * reading and while dumping xml + */ + def->dom->mem.max_balloon = memorySize * 1024; + ignore_value(VIR_STRDUP(def->dom->os.type, "hvm"));
Again this isn't ok.
+ def->dom->os.arch = virArchFromHost(); + machine->vtbl->GetCPUCount(machine, &CPUCount); + def->dom->maxvcpus = def->dom->vcpus = CPUCount; + if (vboxSnapshotGetReadWriteDisks(def, snapshot) < 0) { + VIR_DEBUG("Could not get read write disks for snapshot"); + } + + if (vboxSnapshotGetReadOnlyDisks(snapshot, def) <0) {
Need a space in '<0' to make it '< 0' - i think 'make syntax-check' should complain about this.
+ VIR_DEBUG("Could not get Readonly disks for snapshot"); + } +#endif /* VBOX_API_VERSION >= 4002000 */ + rc = snap->vtbl->GetDescription(snap, &str16); if (NS_FAILED(rc)) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -6230,6 +6705,7 @@ vboxDomainSnapshotGetXMLDesc(virDomainSnapshotPtr snapshot, def->state = VIR_DOMAIN_SHUTOFF;
virUUIDFormat(dom->uuid, uuidstr); + memcpy(def->dom->uuid, dom->uuid, VIR_UUID_BUFLEN); ret = virDomainSnapshotDefFormat(uuidstr, def, flags, 0);
cleanup: @@ -6936,6 +7412,8 @@ vboxDomainSnapshotDeleteTree(vboxGlobalData *data, return ret; }
I've mostly reviewed this for style pointed out a few nitpicks and one serious bug. Superficially it looks ok functionally, but this neither vbox or snapshots are something I'm familiar enough with so will defer to someone else on that. Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

This structure contains the data to be saved in the VirtualBox XML file and can be manipulated with severals exposed functions. The structure is created by vboxSnapshotLoadVboxFile taking the machine XML file. It also can rewrite the XML by using vboxSnapshotSaveVboxFile. --- po/POTFILES.in | 1 + src/Makefile.am | 1 + src/vbox/vbox_snapshot_conf.c | 1383 +++++++++++++++++++++++++++++++++++++++++ src/vbox/vbox_snapshot_conf.h | 100 +++ 4 files changed, 1485 insertions(+) create mode 100644 src/vbox/vbox_snapshot_conf.c create mode 100644 src/vbox/vbox_snapshot_conf.h diff --git a/po/POTFILES.in b/po/POTFILES.in index 122b853..ad56b88 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -208,6 +208,7 @@ src/util/virxml.c src/vbox/vbox_MSCOMGlue.c src/vbox/vbox_XPCOMCGlue.c src/vbox/vbox_driver.c +src/vbox/vbox_snapshot_conf.c src/vbox/vbox_tmpl.c src/vmware/vmware_conf.c src/vmware/vmware_driver.c diff --git a/src/Makefile.am b/src/Makefile.am index 21d56fc..37f4df0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -658,6 +658,7 @@ VMWARE_DRIVER_SOURCES = \ VBOX_DRIVER_SOURCES = \ vbox/vbox_glue.c vbox/vbox_glue.h \ vbox/vbox_driver.c vbox/vbox_driver.h \ + vbox/vbox_snapshot_conf.c vbox/vbox_snapshot_conf.h \ vbox/vbox_V2_2.c vbox/vbox_CAPI_v2_2.h \ vbox/vbox_V3_0.c vbox/vbox_CAPI_v3_0.h \ vbox/vbox_V3_1.c vbox/vbox_CAPI_v3_1.h \ diff --git a/src/vbox/vbox_snapshot_conf.c b/src/vbox/vbox_snapshot_conf.c new file mode 100644 index 0000000..81e780a --- /dev/null +++ b/src/vbox/vbox_snapshot_conf.c @@ -0,0 +1,1383 @@ +/* + * Copyright 2014, diateam (www.diateam.net) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "vbox_snapshot_conf.h" +#include "virerror.h" +#include "viralloc.h" +#include "virlog.h" +#include "virstring.h" +#include "virxml.h" + +#include <libxml/xpathInternals.h> +#include <libxml2/libxml/xmlwriter.h> + +#define VIR_FROM_THIS VIR_FROM_VBOX +VIR_LOG_INIT("vbox.vbox_snapshot_conf"); + +static vboxSnapshotXmlHardDiskPtr createvboxSnapshotXmlHardDiskPtr(xmlNodePtr diskNode, + xmlXPathContextPtr xPathContext, + char *machineLocation) +{ + vboxSnapshotXmlHardDiskPtr hardDisk = NULL; + xmlNodePtr *nodes = NULL; + char *uuid = NULL; + char **searchTabResult = NULL; + int resultSize = 0; + size_t i = 0; + int result = -1; + char *location = NULL; + char *tmp = NULL; + if (VIR_ALLOC(hardDisk) < 0) + goto cleanup; + + xPathContext->node = diskNode; + + hardDisk->nchildren = virXPathNodeSet("./vbox:HardDisk", xPathContext, &nodes); + if (hardDisk->nchildren && VIR_ALLOC_N(hardDisk->children, hardDisk->nchildren) < 0) + goto cleanup; + for (i = 0; i < hardDisk->nchildren; i++) { + hardDisk->children[i] = createvboxSnapshotXmlHardDiskPtr(nodes[i], xPathContext, machineLocation); + if (hardDisk->children[i] == NULL) + goto cleanup; + hardDisk->children[i]->parent = hardDisk; + } + uuid = virXMLPropString(diskNode, "uuid"); + /*we use virStringSearch because the uuid is between brackets*/ + resultSize = virStringSearch(uuid, + VBOX_UUID_REGEX, + 1, + &searchTabResult); + if (resultSize != 1) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <HardDisk> 'uuid' attribute")); + goto cleanup; + } + if (VIR_STRDUP(hardDisk->uuid, searchTabResult[0]) < 0) + goto cleanup; + + location = virXMLPropString(diskNode, "location"); + if (location == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <HardDisk> 'location' attribute")); + goto cleanup; + } + if (location[0] != '/') { + /*The location is a relative path, so we must change it into an absolute one. */ + if (virAsprintf(&tmp, "%s%s", machineLocation, location) < 0) + goto cleanup; + if (VIR_STRDUP(hardDisk->location, tmp) < 0) + goto cleanup; + } else { + if (VIR_STRDUP(hardDisk->location, location) < 0) + goto cleanup; + } + hardDisk->format = virXMLPropString(diskNode, "format"); + if (hardDisk->format == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <HardDisk> 'format' attribute")); + goto cleanup; + } + hardDisk->type = virXMLPropString(diskNode, "type"); + result = 0; + + cleanup: + VIR_FREE(uuid); + VIR_FREE(nodes); + VIR_FREE(location); + VIR_FREE(tmp); + virStringFreeList(searchTabResult); + if (result < 0) + VIR_FREE(hardDisk); + return hardDisk; +} + +static vboxSnapshotXmlMediaRegistryPtr retrieveMediaRegistry(xmlNodePtr mediaRegistryNode, + xmlXPathContextPtr xPathContext, + char *machineLocation) +{ + vboxSnapshotXmlMediaRegistryPtr mediaRegistry = NULL; + xmlNodePtr hardDisksNode = NULL; + xmlNodePtr *nodes = NULL; + size_t i = 0; + int result = -1; + + if (VIR_ALLOC(mediaRegistry) < 0) + goto cleanup; + + xPathContext->node = mediaRegistryNode; + hardDisksNode = virXPathNode("./vbox:HardDisks", xPathContext); + + xPathContext->node = hardDisksNode; + mediaRegistry->ndisks = virXPathNodeSet("./vbox:HardDisk", xPathContext, &nodes); + if (mediaRegistry->ndisks && + VIR_ALLOC_N(mediaRegistry->disks, mediaRegistry->ndisks) < 0) + goto cleanup; + for (i = 0; i < mediaRegistry->ndisks; i++) { + mediaRegistry->disks[i] = createvboxSnapshotXmlHardDiskPtr(nodes[i], + xPathContext, + machineLocation); + if (mediaRegistry->disks[i] == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Cannot create a vboxSnapshotXmlHardDisk")); + goto cleanup; + } + } + VIR_FREE(nodes); + + xPathContext->node = mediaRegistryNode; + mediaRegistry->notherMedia = virXPathNodeSet("./*[not(self::vbox:HardDisks)]", + xPathContext, &nodes); + if (mediaRegistry->notherMedia && + VIR_ALLOC_N(mediaRegistry->otherMedia, mediaRegistry->notherMedia) < 0) + goto cleanup; + for (i = 0; i < mediaRegistry->notherMedia; i++) { + mediaRegistry->otherMedia[i] = virXMLNodeToString(mediaRegistryNode->doc, + nodes[i]); + } + + result = 0; + + cleanup: + if (result < 0) + VIR_FREE(mediaRegistry); + VIR_FREE(nodes); + return mediaRegistry; +} + +static vboxSnapshotXmlSnapshotPtr retrieveSnapshot(xmlNodePtr snapshotNode, + xmlXPathContextPtr xPathContext) +{ + vboxSnapshotXmlSnapshotPtr snapshot = NULL; + xmlNodePtr hardwareNode = NULL; + xmlNodePtr descriptionNode = NULL; + xmlNodePtr storageControllerNode = NULL; + xmlNodePtr snapshotsNode = NULL; + xmlNodePtr *nodes = NULL; + char *uuid = NULL; + char **searchTabResult = NULL; + int resultSize = 0; + size_t i = 0; + int result = -1; + + if (VIR_ALLOC(snapshot) < 0) + goto cleanup; + + uuid = virXMLPropString(snapshotNode, "uuid"); + /*we use virStringSearch because the uuid is between brackets*/ + resultSize = virStringSearch(uuid, + VBOX_UUID_REGEX, + 1, + &searchTabResult); + if (resultSize != 1) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <Snapshot> 'uuid' attribute")); + goto cleanup; + } + if (VIR_STRDUP(snapshot->uuid, searchTabResult[0]) < 0) + goto cleanup; + + snapshot->name = virXMLPropString(snapshotNode, "name"); + if (snapshot->name == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <Snapshot> 'name' attribute")); + goto cleanup; + } + snapshot->timeStamp = virXMLPropString(snapshotNode, "timeStamp"); + if (snapshot->timeStamp == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <Snapshot> 'timeStamp' attribute")); + goto cleanup; + } + + xPathContext->node = snapshotNode; + descriptionNode = virXPathNode("./vbox:Description", xPathContext); + if (descriptionNode != NULL) { + snapshot->description = virXMLNodeToString(descriptionNode->doc, descriptionNode); + } + + hardwareNode = virXPathNode("./vbox:Hardware", xPathContext); + if (hardwareNode == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <Snapshot> <Hardware> node")); + goto cleanup; + } + snapshot->hardware = virXMLNodeToString(snapshotNode->doc, hardwareNode); + + storageControllerNode = virXPathNode("./vbox:StorageControllers", xPathContext); + if (storageControllerNode == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <Snapshot> <StorageControllers> node")); + goto cleanup; + } + snapshot->storageController = virXMLNodeToString(snapshotNode->doc, + storageControllerNode); + + snapshotsNode = virXPathNode("./vbox:Snapshots", xPathContext); + + if (snapshotsNode != NULL) { + xPathContext->node = snapshotsNode; + snapshot->nchildren = virXPathNodeSet("./vbox:Snapshot", xPathContext, &nodes); + if (snapshot->nchildren && VIR_ALLOC_N(snapshot->children, snapshot->nchildren) < 0) + goto cleanup; + for (i = 0; i < snapshot->nchildren; i++) { + snapshot->children[i] = retrieveSnapshot(nodes[i], xPathContext); + if (snapshot->children[i] == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Cannot create a vboxSnapshotXmlSnapshotPtr")); + goto cleanup; + } + snapshot->children[i]->parent = snapshot; + } + } + + result = 0; + + cleanup: + if (result < 0) + VIR_FREE(snapshot); + VIR_FREE(nodes); + VIR_FREE(uuid); + virStringFreeList(searchTabResult); + return snapshot; +} + +vboxSnapshotXmlSnapshotPtr snapshotByName(vboxSnapshotXmlSnapshotPtr snapshot, + char *snapshotName) +{ + size_t i = 0; + vboxSnapshotXmlSnapshotPtr ret = NULL; + if (STREQ(snapshot->name, snapshotName)) + return snapshot; + for (i = 0; i < snapshot->nchildren; i++) { + ret = snapshotByName(snapshot->children[i], snapshotName); + if (ret != NULL) + return ret; + } + return ret; +} + +static vboxSnapshotXmlHardDiskPtr hardDiskById(vboxSnapshotXmlHardDiskPtr disk, + char *parentHardDiskId) +{ + size_t i = 0; + vboxSnapshotXmlHardDiskPtr ret = NULL; + if (STREQ(disk->uuid, parentHardDiskId)) + return disk; + for (i = 0; i < disk->nchildren; i++) { + ret = hardDiskById(disk->children[i], parentHardDiskId); + if (ret != NULL) + return ret; + } + return ret; +} + +static vboxSnapshotXmlHardDiskPtr hardDiskByLocation(vboxSnapshotXmlHardDiskPtr disk, + char *parentLocation) +{ + size_t i = 0; + vboxSnapshotXmlHardDiskPtr ret = NULL; + if (STREQ(disk->location, parentLocation)) + return disk; + for (i = 0; i < disk->nchildren; i++) { + ret = hardDiskByLocation(disk->children[i], parentLocation); + if (ret != NULL) + return ret; + } + return ret; +} + +static xmlNodePtr createHardDiskNode(vboxSnapshotXmlHardDiskPtr hardDisk) +{ + int result = -1; + size_t i = 0; + char *uuid = NULL; + xmlNodePtr ret = xmlNewNode(NULL, BAD_CAST "HardDisk"); + if (virAsprintf(&uuid, "{%s}", hardDisk->uuid) < 0) + goto cleanup; + + if (xmlNewProp(ret, BAD_CAST "uuid", BAD_CAST uuid) == NULL) + goto cleanup; + if (xmlNewProp(ret, BAD_CAST "location", BAD_CAST hardDisk->location) == NULL) + goto cleanup; + if (xmlNewProp(ret, BAD_CAST "format", BAD_CAST hardDisk->format) == NULL) + goto cleanup; + if (hardDisk->type != NULL && xmlNewProp(ret, BAD_CAST "type", BAD_CAST hardDisk->type) == NULL) + goto cleanup; + + for (i = 0; i < hardDisk->nchildren; i++) { + xmlNodePtr child = createHardDiskNode(hardDisk->children[i]); + if (child != NULL) + xmlAddChild(ret, child); + } + + result = 0; + cleanup: + if (result < 0) { + VIR_FREE(ret); + } + VIR_FREE(uuid); + return ret; +} + +static int serializeSnapshot(xmlNodePtr node, vboxSnapshotXmlSnapshotPtr snapshot) +{ + int result = -1; + size_t i = 0; + xmlNodePtr descriptionNode = NULL; + xmlNodePtr snapshotsNode = NULL; + xmlNodePtr hardwareNode = NULL; + xmlNodePtr storageControllerNode = NULL; + xmlParserErrors parseError = XML_ERR_OK; + char *uuid = NULL; + char *timeStamp = NULL; + + char **firstRegex = NULL; + int firstRegexResult = 0; + char **secondRegex = NULL; + int secondRegexResult = 0; + + if (virAsprintf(&uuid, "{%s}", snapshot->uuid) < 0) + goto cleanup; + + if (xmlNewProp(node, BAD_CAST "uuid", BAD_CAST uuid) == NULL) + goto cleanup; + if (xmlNewProp(node, BAD_CAST "name", BAD_CAST snapshot->name) == NULL) + goto cleanup; + + /* We change the date format from "yyyy-MM-dd hh:mm:ss.msec+timeZone" + * to "yyyy-MM-ddThh:mm:ssZ"*/ + firstRegexResult = virStringSearch(snapshot->timeStamp, + "([0-9]{4}-[0-9]{2}-[0-9]{2})", + 1, + &firstRegex); + secondRegexResult = virStringSearch(snapshot->timeStamp, + "([0-9]{2}:[0-9]{2}:[0-9]{2})", + 1, + &secondRegex); + if (firstRegexResult < 1) + goto cleanup; + if (secondRegexResult < 1) + goto cleanup; + if (virAsprintf(&timeStamp, "%sT%sZ", firstRegex[0], secondRegex[0]) < 0) + goto cleanup; + + if (xmlNewProp(node, BAD_CAST "timeStamp", BAD_CAST timeStamp) == NULL) + goto cleanup; + + /*node description*/ + if (snapshot->description != NULL) { + descriptionNode = xmlNewNode(NULL, BAD_CAST "Description"); + xmlNodeSetContent(descriptionNode, BAD_CAST snapshot->description); + xmlAddChild(node, descriptionNode); + } + /*hardware*/ + parseError = xmlParseInNodeContext(node, + snapshot->hardware, + (int)strlen(snapshot->hardware), + 0, + &hardwareNode); + if (parseError != XML_ERR_OK) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Unable to add the snapshot hardware")); + goto cleanup; + } + xmlAddChild(node, hardwareNode); + + /*storageController*/ + if (xmlParseInNodeContext(node, snapshot->storageController, + (int)strlen(snapshot->storageController), + 0, + &storageControllerNode) != XML_ERR_OK) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Unable to add the snapshot storageController")); + goto cleanup; + } + xmlAddChild(node, storageControllerNode); + + if (snapshot->nchildren > 0) { + snapshotsNode = xmlNewNode(NULL, BAD_CAST "Snapshots"); + xmlAddChild(node, snapshotsNode); + for (i = 0; i < snapshot->nchildren; i++) { + xmlNodePtr child = xmlNewNode(NULL, BAD_CAST "Snapshot"); + xmlAddChild(snapshotsNode, child); + if (serializeSnapshot(child, snapshot->children[i]) < 0) { + goto cleanup; + } + } + } + result = 0; + + cleanup: + if (result < 0) { + xmlFreeNode(descriptionNode); + xmlFreeNode(snapshotsNode); + } + virStringFreeList(firstRegex); + virStringFreeList(secondRegex); + VIR_FREE(uuid); + VIR_FREE(timeStamp); + return result; +} + +static size_t allChild(vboxSnapshotXmlHardDiskPtr disk, vboxSnapshotXmlHardDiskPtr **list) +{ + size_t returnSize = 0; + size_t tempSize = 0; + vboxSnapshotXmlHardDiskPtr *ret = NULL; + vboxSnapshotXmlHardDiskPtr *tempList = NULL; + size_t i = 0; + size_t j = 0; + if (VIR_ALLOC_N(ret, 0) < 0) + return 0; + + for (i = 0; i < disk->nchildren; i++) { + tempSize = allChild(disk->children[i], &tempList); + if (VIR_EXPAND_N(ret, returnSize, tempSize) < 0) + return 0; + + for (j = 0; j < tempSize; j++) { + ret[returnSize - tempSize + j] = tempList[j]; + } + } + if (VIR_EXPAND_N(ret, returnSize, 1) < 0) + return 0; + + ret[returnSize - 1] = disk; + *list = ret; + return returnSize; +} + +/* + *vboxSnapshotLoadVboxFile: Create a vboxSnapshotXmlMachinePtr from a VirtualBoxl xml file. + *return an initialized vboxSnapshotXmlMachinePtr on success + *return NULL on failure + *filePath must not be NULL. + */ +vboxSnapshotXmlMachinePtr vboxSnapshotLoadVboxFile(const char *filePath, char *machineLocation) +{ + int ret = -1; + vboxSnapshotXmlMachinePtr machineDescription = NULL; + xmlDocPtr xml = NULL; + xmlNodePtr machineNode = NULL; + xmlNodePtr cur = NULL; + xmlXPathContextPtr xPathContext = NULL; + char *currentStateModifiedString = NULL; + + char **searchResultTab = NULL; + ssize_t searchResultSize = 0; + char *currentSnapshotAttribute = NULL; + + if (filePath == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Filepath is Null")); + goto cleanup; + } + + if (VIR_ALLOC(machineDescription) < 0) + goto cleanup; + + xml = virXMLParse(filePath, NULL, NULL); + if (xml == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Unable to parse the xml")); + goto cleanup; + } + if (!(xPathContext = xmlXPathNewContext(xml))) { + virReportOOMError(); + goto cleanup; + } + if (xmlXPathRegisterNs(xPathContext, + BAD_CAST "vbox", + BAD_CAST "http://www.innotek.de/VirtualBox-settings") < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Failed to register xml namespace " + "'http://www.innotek.de/VirtualBox-settings'")); + goto cleanup; + } + + /*Retrieve MachineNode*/ + cur = xmlDocGetRootElement(xml); + xPathContext->node = cur; + machineNode = virXPathNode("./vbox:Machine", xPathContext); + if (machineNode == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <VirtualBox> <Machine> node")); + goto cleanup; + } + + machineDescription->uuid = virXMLPropString(machineNode, "uuid"); + if (machineDescription->uuid == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <Machine> 'uuid' attribute")); + goto cleanup; + } + machineDescription->name = virXMLPropString(machineNode, "name"); + if (machineDescription->name == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <Machine> 'name' attribute")); + goto cleanup; + } + + currentSnapshotAttribute = virXMLPropString(machineNode, "currentSnapshot"); + if (currentSnapshotAttribute != NULL) { + /*we use virStringSearch because the uuid is between brackets*/ + searchResultSize = virStringSearch(currentSnapshotAttribute, + VBOX_UUID_REGEX, + 1, + &searchResultTab); + if (searchResultSize != 1) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <Machine> 'currentSnapshot' attribute")); + goto cleanup; + } + if (VIR_STRDUP(machineDescription->currentSnapshot, searchResultTab[0]) < 0) + goto cleanup; + } + + + machineDescription->snapshotFolder = virXMLPropString(machineNode, "snapshotFolder"); + if (machineDescription->snapshotFolder == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <Machine> 'snapshotFolder' attribute")); + goto cleanup; + } + + currentStateModifiedString = virXMLPropString(machineNode, "currentStateModified"); + if (currentStateModifiedString != NULL && STREQ(currentStateModifiedString, "true")) { + machineDescription->currentStateModified = 1; + } else { + machineDescription->currentStateModified = 0; + } + machineDescription->lastStateChange = virXMLPropString(machineNode, "lastStateChange"); + if (machineDescription->lastStateChange == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <Machine> 'lastStateChange' attribute")); + goto cleanup; + } + + xPathContext->node = machineNode; + cur = virXPathNode("./vbox:Hardware", xPathContext); + if (cur == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <Machine> <Hardware> node")); + goto cleanup; + } + machineDescription->hardware = virXMLNodeToString(xml, cur); + + cur = virXPathNode("./vbox:ExtraData", xPathContext); + if (cur) + machineDescription->extraData = virXMLNodeToString(xml, cur); + + cur = virXPathNode("./vbox:StorageControllers", xPathContext); + if (cur == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <Machine> <StorageControllers> node")); + goto cleanup; + } + machineDescription->storageController = virXMLNodeToString(xml, cur); + + /*retrieve mediaRegistry*/ + cur = virXPathNode("./vbox:MediaRegistry", xPathContext); + if (cur == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s" + , _("Cannot parse <Machine> <MediaRegistry> node")); + goto cleanup; + } + machineDescription->mediaRegistry = retrieveMediaRegistry(cur, xPathContext, machineLocation); + if (machineDescription->mediaRegistry == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s", + ("Unable to create media registry")); + goto cleanup; + } + + /*retrieve snapshot*/ + xPathContext->node = machineNode; + cur = virXPathNode("./vbox:Snapshot", xPathContext); + if (cur != NULL) { + machineDescription->snapshot = retrieveSnapshot(cur, xPathContext); + } + + ret = 0; + + cleanup: + VIR_FREE(xml); + VIR_FREE(xPathContext); + VIR_FREE(currentStateModifiedString); + virStringFreeList(searchResultTab); + if (ret < 0) + VIR_FREE(machineDescription); + return machineDescription; +} + +/* + *addSnapshotToXmlMachine: Add a vboxSnapshotXmlSnapshotPtr to a vboxSnapshotXmlMachinePtr. + *If 'snapshotParentName' is not NULL, the snapshot whose name is 'snapshotParentName' + *becomes the snapshot parent. + *return 0 on success + *return -1 on failure + */ +int addSnapshotToXmlMachine(vboxSnapshotXmlSnapshotPtr snapshot, + vboxSnapshotXmlMachinePtr machine, + char *snapshotParentName) +{ + int ret = -1; + vboxSnapshotXmlSnapshotPtr parentSnapshot = NULL; + + if (snapshot == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Snapshot is Null")); + goto cleanup; + } + if (machine == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Machine is Null")); + goto cleanup; + } + + /*If parent is NULL and the machine has no snapshot yet, + *it means that the added snapshot is the first snapshot*/ + if (snapshotParentName == NULL) { + if (machine->snapshot != NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to add this snapshot, there is already a snapshot " + "linked to the machine")); + goto cleanup; + } + machine->snapshot = snapshot; + ret = 0; + goto cleanup; + } else { + if (machine->snapshot == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("The machine has no snapshot and it should have it")); + goto cleanup; + } + parentSnapshot = snapshotByName(machine->snapshot, snapshotParentName); + if (parentSnapshot == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to find the snapshot %s"), snapshotParentName); + goto cleanup; + } + if (VIR_EXPAND_N(parentSnapshot->children, parentSnapshot->nchildren, 1) < 0) + goto cleanup; + + parentSnapshot->children[parentSnapshot->nchildren - 1] = snapshot; + ret = 0; + } + + cleanup: + return ret; +} + +/* + *addHardDisksToMediaRegistry: Add a vboxSnapshotXmlHardDiskPtr to the registry as a + *child of the disk whose uuid is 'parentHardDiskId'. + *return 0 on success + *return -1 on failure + */ +int addHardDiskToMediaRegistry(vboxSnapshotXmlHardDiskPtr hardDisk, + vboxSnapshotXmlMediaRegistryPtr mediaRegistry, + char *parentHardDiskId) +{ + int ret = -1; + size_t i = 0; + vboxSnapshotXmlHardDiskPtr parentDisk = NULL; + if (hardDisk == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Hard disk is null")); + goto cleanup; + } + if (mediaRegistry == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Media Registry is null")); + goto cleanup; + } + + for (i = 0; i < mediaRegistry->ndisks; i++) { + parentDisk = hardDiskById(mediaRegistry->disks[i], parentHardDiskId); + if (parentDisk != NULL) + break; + } + if (parentDisk == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to get the parent disk")); + goto cleanup; + } + /*Hard disk found*/ + if (VIR_EXPAND_N(parentDisk->children, parentDisk->nchildren, 1) < 0) + goto cleanup; + + parentDisk->children[parentDisk->nchildren - 1] = hardDisk; + if (hardDisk->parent == NULL) { + hardDisk->parent = parentDisk; + } + ret = 0; + + cleanup: + return ret; +} + +/* + *removeSnapshot: Remove the vboxSnapshotXmlSnapshotPtr whose name is 'snapshotName' + *from a vboxSnapshotXmlMachinePtr. + *return 0 on success + *return -1 on failure + */ +int removeSnapshot(vboxSnapshotXmlMachinePtr machine, char *snapshotName) +{ + int ret = -1; + size_t i = 0; + vboxSnapshotXmlSnapshotPtr snapshot = NULL; + vboxSnapshotXmlSnapshotPtr parentSnapshot = NULL; + if (machine == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("machine is null")); + goto cleanup; + } + if (snapshotName == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("snapshotName is null")); + goto cleanup; + } + if (machine->snapshot == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("the machine has no snapshot")); + goto cleanup; + } + snapshot = snapshotByName(machine->snapshot, snapshotName); + if (snapshot == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to find the snapshot with name %s"), snapshotName); + goto cleanup; + } + if (snapshot->nchildren > 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("This snapshot has children, " + "please delete theses snapshots before")); + goto cleanup; + } + + if (snapshot->parent == NULL) { + if (machine->snapshot != snapshot) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("You are trying to remove a snapshot which does not exists")); + goto cleanup; + } + machine->snapshot = NULL; + VIR_FREE(snapshot); + ret = 0; + goto cleanup; + } + parentSnapshot = snapshot->parent; + + snapshot->parent = NULL; + while (i < parentSnapshot->nchildren && parentSnapshot->children[i] != snapshot) { + ++i; + } + if (VIR_DELETE_ELEMENT(parentSnapshot->children, i, parentSnapshot->nchildren) < 0) + goto cleanup; + + ret = 0; + cleanup: + return ret; +} + +/* + *removeHardDisk: Remove the vboxSnapshotXmlHardDiskPtr whose uuid is 'uuid' from a + *vboxSnapshotXmlMediaRegistryPtr. The hard disk must not have any children. + *return 0 on success + *return -1 on failure + */ +int removeHardDisk(vboxSnapshotXmlMediaRegistryPtr mediaRegistry, char *uuid) +{ + int ret = -1; + size_t i = 0; + vboxSnapshotXmlHardDiskPtr hardDisk = NULL; + vboxSnapshotXmlHardDiskPtr parentHardDisk = NULL; + if (mediaRegistry == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Media registry is null")); + goto cleanup; + } + if (uuid == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Uuid is null")); + goto cleanup; + } + + for (i = 0; i < mediaRegistry->ndisks; i++) { + hardDisk = hardDiskById(mediaRegistry->disks[i], uuid); + if (hardDisk != NULL) + break; + } + if (hardDisk == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to find the hard disk with uuid %s"), uuid); + goto cleanup; + } + if (hardDisk->parent == NULL) { + //it means that the hard disk is in 'root' + for (i = 0; i < mediaRegistry->ndisks; i++) { + if (hardDisk == mediaRegistry->disks[i]) + break; + } + if (VIR_DELETE_ELEMENT(mediaRegistry->disks, i, mediaRegistry->ndisks) < 0) + goto cleanup; + ret = 0; + goto cleanup; + } + + parentHardDisk = hardDisk->parent; + i = 0; + while (i < parentHardDisk->nchildren && parentHardDisk->children[i] != hardDisk) { + ++i; + } + hardDisk->parent = NULL; + if (VIR_DELETE_ELEMENT(parentHardDisk->children, i, parentHardDisk->nchildren) < 0) + goto cleanup; + ret = 0; + + cleanup: + return ret; +} + +/*vboxSnapshotSaveVboxFile: Create a VirtualBox XML file from a vboxSnapshotXmlMachinePtr. + *The file is saved at 'filePath'. + *return 0 on success + *return -1 on failure + */ +int vboxSnapshotSaveVboxFile(vboxSnapshotXmlMachinePtr machine, const char *filePath) +{ + int ret = -1; + size_t i = 0; + xmlDocPtr xml = NULL; + xmlTextWriterPtr writer = NULL; + xmlNodePtr mediaRegistryNode = NULL; + xmlNodePtr snapshotNode = NULL; + xmlNodePtr machineNode = NULL; + xmlNodePtr hardDisksNode = NULL; + xmlXPathContextPtr xPathContext = NULL; + char *currentSnapshot = NULL; + char *timeStamp = NULL; + + char **firstRegex = NULL; + int firstRegexResult = 0; + char **secondRegex = NULL; + int secondRegexResult = 0; + + if (machine == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Machine is null")); + goto cleanup; + } + if (filePath == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Filepath is null")); + goto cleanup; + } + xml = xmlNewDoc(BAD_CAST "1.0"); + writer = xmlNewTextWriterDoc(&xml, 0); + //writting raw data first + if (xmlTextWriterStartDocument(writer, NULL, "ISO-8859-1", NULL) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterStartDocument")); + goto cleanup; + } + /* Write a comment as child of root. + * Please observe, that the input to the xmlTextWriter functions + * HAS to be in UTF-8, even if the output XML is encoded + * in iso-8859-1 */ + if (xmlTextWriterWriteFormatComment(writer, + "WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE \n" + "OVERWRITTEN AND LOST.\n" + "Changes to this xml configuration should be made using Virtualbox \n" + "or other application using the libvirt API") < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterWriteFormatComment")); + goto cleanup; + } + if (xmlTextWriterWriteRaw(writer, BAD_CAST "\n") < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterWriteRaw")); + goto cleanup; + } + /*Start an element named "VirtualBox". Since this is the first element, + *this will we be root element of the document*/ + if (xmlTextWriterStartElement(writer, BAD_CAST "VirtualBox") < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterStartDocument")); + goto cleanup; + } + if (xmlTextWriterWriteAttribute(writer, BAD_CAST "version", BAD_CAST "1.12-linux") < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterWriteAttribute")); + goto cleanup; + } + /*Start an element named "Machine" as child of VirtualBox.*/ + if (xmlTextWriterStartElement(writer, BAD_CAST "Machine") < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterStartElement")); + goto cleanup; + } + //Settings machine Attributes + if (xmlTextWriterWriteAttribute(writer, BAD_CAST "uuid", BAD_CAST machine->uuid) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterWriteAttribute")); + goto cleanup; + } + if (xmlTextWriterWriteAttribute(writer, BAD_CAST "name", BAD_CAST machine->name) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterWriteAttribute")); + goto cleanup; + } + if (machine->currentSnapshot != NULL) { + if (virAsprintf(¤tSnapshot, "{%s}", machine->currentSnapshot) < 0) + goto cleanup; + if (xmlTextWriterWriteAttribute(writer, + BAD_CAST "currentSnapshot", + BAD_CAST currentSnapshot) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterWriteAttribute")); + goto cleanup; + } + } + if (xmlTextWriterWriteAttribute(writer, + BAD_CAST "snapshotFolder", + BAD_CAST machine->snapshotFolder) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterWriteAttribute")); + goto cleanup; + } + if (xmlTextWriterWriteAttribute(writer, + BAD_CAST "currentStateModified", + BAD_CAST(machine->currentStateModified == 0 ? "false" : "true")) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterWriteAttribute")); + goto cleanup; + } + if (xmlTextWriterWriteAttribute(writer, BAD_CAST "OSType", BAD_CAST "Other") < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterWriteAttribute")); + goto cleanup; + } + + firstRegexResult = virStringSearch(machine->lastStateChange, + "([0-9]{4}-[0-9]{2}-[0-9]{2})", + 1, + &firstRegex); + secondRegexResult = virStringSearch(machine->lastStateChange, + "([0-9]{2}:[0-9]{2}:[0-9]{2})", + 1, + &secondRegex); + if (firstRegexResult < 1) + goto cleanup; + if (secondRegexResult < 1) + goto cleanup; + + if (virAsprintf(&timeStamp, "%sT%sZ", firstRegex[0], secondRegex[0]) < 0) + goto cleanup; + + if (xmlTextWriterWriteAttribute(writer, + BAD_CAST "lastStateChange", + BAD_CAST timeStamp) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterWriteAttribute")); + goto cleanup; + } + if (xmlTextWriterStartElement(writer, BAD_CAST "MediaRegistry") < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterStartElement")); + goto cleanup; + } + for (i = 0; i < machine->mediaRegistry->notherMedia; i++) { + if (xmlTextWriterWriteRaw(writer, + BAD_CAST machine->mediaRegistry->otherMedia[i]) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterWriteRaw")); + goto cleanup; + } + } + if (xmlTextWriterEndElement(writer) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterEndElement")); + goto cleanup; + } + if (xmlTextWriterWriteRaw(writer, BAD_CAST machine->hardware) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterWriteRaw")); + goto cleanup; + } + if (machine->extraData != NULL && + xmlTextWriterWriteRaw(writer, BAD_CAST machine->extraData) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterWriteRaw")); + goto cleanup; + } + if (xmlTextWriterWriteRaw(writer, BAD_CAST machine->storageController) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterWriteRaw")); + goto cleanup; + } + if (xmlTextWriterEndDocument(writer) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error at xmlTextWriterEndDocument")); + goto cleanup; + } + + if (!(xPathContext = xmlXPathNewContext(xml))) { + virReportOOMError(); + goto cleanup; + } + + /*Retrieve MediaRegistryNode*/ + xPathContext->node = xmlDocGetRootElement(xml); + machineNode = virXPathNode("./Machine", xPathContext); + if (machineNode == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Cannot parse <VirtualBox> <Machine> node")); + goto cleanup; + } + + xPathContext->node = machineNode; + mediaRegistryNode = virXPathNode("./MediaRegistry", xPathContext); + + hardDisksNode = xmlNewNode(NULL, BAD_CAST "HardDisks"); + for (i = 0; i < machine->mediaRegistry->ndisks; i++) { + xmlNodePtr child = createHardDiskNode(machine->mediaRegistry->disks[i]); + if (child != NULL) + xmlAddChild(hardDisksNode, child); + } + xmlAddChild(mediaRegistryNode, hardDisksNode); + + if (machine->snapshot != NULL) { + snapshotNode = xmlNewNode(NULL, BAD_CAST "Snapshot"); + xmlAddChild(machineNode, snapshotNode); + if (serializeSnapshot(snapshotNode, machine->snapshot) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Failed to serialize snapshot")); + goto cleanup; + } + } + /*Adding namespace*/ + if (xmlNewProp(xmlDocGetRootElement(xml), + BAD_CAST "xmlns", + BAD_CAST "http://www.innotek.de/VirtualBox-settings") == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Error in xmlNewProp")); + goto cleanup; + } + + if (xmlSaveFormatFileEnc(filePath, xml, "ISO-8859-1", -1) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Unable to save the xml")); + goto cleanup; + } + ret = 0; + + cleanup: + VIR_FREE(currentSnapshot); + VIR_FREE(timeStamp); + xmlFreeTextWriter(writer); + xmlFreeDoc(xml); + virStringFreeList(firstRegex); + virStringFreeList(secondRegex); + return ret; +} + +/* + *isCurrentSnapshot: Return 1 if 'snapshotName' corresponds to the + *vboxSnapshotXmlMachinePtr's current snapshot, return 0 otherwise. + */ +int isCurrentSnapshot(vboxSnapshotXmlMachinePtr machine, char *snapshotName) +{ + vboxSnapshotXmlSnapshotPtr snapshot = NULL; + if (machine == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Machine is null")); + goto cleanup; + } + if (snapshotName == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("snapshotName is null")); + goto cleanup; + } + snapshot = snapshotByName(machine->snapshot, snapshotName); + return STREQ(snapshot->uuid, machine->currentSnapshot); + + cleanup: + return 0; +} + +/* + *getRWDisksPathsFromLibvirtXML: Parse a libvirt XML snapshot file, allocates and + *fills a list of read-write disk paths. + *return array length on success, -1 on failure. + */ +int getRWDisksPathsFromLibvirtXML(char *filePath, char ***rwDisksPath) +{ + int result = -1; + size_t i = 0; + char **ret; + xmlDocPtr xml = NULL; + xmlXPathContextPtr xPathContext = NULL; + xmlNodePtr *nodes = NULL; + int nodeSize = 0; + if (filePath == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("filePath is null")); + goto cleanup; + } + xml = virXMLParse(filePath, NULL, NULL); + if (xml == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Unable to parse the xml")); + goto cleanup; + } + if (!(xPathContext = xmlXPathNewContext(xml))) { + virReportOOMError(); + goto cleanup; + } + xPathContext->node = xmlDocGetRootElement(xml); + nodeSize = virXPathNodeSet("/domainsnapshot/disks/disk", xPathContext, &nodes); + + if (VIR_ALLOC_N(ret, nodeSize) < 0) + goto cleanup; + + for (i = 0; i < nodeSize; i++) { + xmlNodePtr node = nodes[i]; + xPathContext->node = node; + xmlNodePtr sourceNode = virXPathNode("./source", xPathContext); + if (sourceNode) { + ret[i] = virXMLPropString(sourceNode, "file"); + } + } + result = 0; + + cleanup: + VIR_FREE(xml); + VIR_FREE(xPathContext); + if (result < 0) { + virStringFreeList(ret); + nodeSize = -1; + } + *rwDisksPath = ret; + return nodeSize; +} + +/* + *getRODisksPathsFromLibvirtXML: *Parse a libvirt XML snapshot file, allocates and fills + *a list of read-only disk paths (the parents of the read-write disks). + *return array length on success, -1 on failure. + */ +int getRODisksPathsFromLibvirtXML(char *filePath, char ***roDisksPath) +{ + int result = -1; + size_t i = 0; + char **ret; + xmlDocPtr xml = NULL; + xmlXPathContextPtr xPathContext = NULL; + xmlNodePtr *nodes = NULL; + int nodeSize = 0; + if (filePath == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("filePath is null")); + goto cleanup; + } + xml = virXMLParse(filePath, NULL, NULL); + if (xml == NULL) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Unable to parse the xml")); + goto cleanup; + } + if (!(xPathContext = xmlXPathNewContext(xml))) { + virReportOOMError(); + goto cleanup; + } + xPathContext->node = xmlDocGetRootElement(xml); + nodeSize = virXPathNodeSet("/domainsnapshot/domain/devices/disk", + xPathContext, + &nodes); + if (VIR_ALLOC_N(ret, nodeSize) < 0) + goto cleanup; + + for (i = 0; i < nodeSize; i++) { + xmlNodePtr node = nodes[i]; + xPathContext->node = node; + xmlNodePtr sourceNode = virXPathNode("./source", xPathContext); + if (sourceNode) { + ret[i] = virXMLPropString(sourceNode, "file"); + } + } + result = 0; + + cleanup: + xmlFreeDoc(xml); + xmlXPathFreeContext(xPathContext); + if (result < 0) { + virStringFreeList(ret); + nodeSize = -1; + } + *roDisksPath = ret; + return nodeSize; +} + +/* + *hardDiskUuidByLocation: Return the uuid of the hard disk whose location is 'location' + *return a valid uuid, or NULL on failure + */ +char *hardDiskUuidByLocation(vboxSnapshotXmlMachinePtr machine, char *location) +{ + size_t i = 0; + vboxSnapshotXmlHardDiskPtr hardDisk = NULL; + for (i = 0; i < machine->mediaRegistry->ndisks; i++) { + hardDisk = hardDiskByLocation(machine->mediaRegistry->disks[i], location); + if (hardDisk != NULL) + break; + } + if (hardDisk == NULL) + return NULL; + return hardDisk->uuid; +} + +/*Retreive the whole ancestry of the vboxSnapshotXmlHardDiskPtr whose location is + *'location', and store them in a newly allocated list of vboxSnapshotXmlHardDiskPtr. + *This list begins with the requested disk, and ends with the farthest ancestor. + *return array length on success, -1 on failure.*/ + +size_t diskListToOpen(vboxSnapshotXmlMachinePtr machine, + vboxSnapshotXmlHardDiskPtr **hardDiskToOpen, + char *location) +{ + size_t i = 0; + size_t returnSize = 0; + vboxSnapshotXmlHardDiskPtr *ret = NULL; + vboxSnapshotXmlHardDiskPtr hardDisk = NULL; + for (i = 0; i < machine->mediaRegistry->ndisks; i++) { + hardDisk = hardDiskByLocation(machine->mediaRegistry->disks[i], location); + if (hardDisk != NULL) + break; + } + if (hardDisk == NULL) + return 0; + if (VIR_ALLOC_N(ret, 1) < 0) + return 0; + + returnSize = 1; + ret[returnSize - 1] = hardDisk; + + while (hardDisk->parent != NULL) { + if (VIR_EXPAND_N(ret, returnSize, 1) < 0) + return 0; + ret[returnSize - 1] = hardDisk->parent; + hardDisk = hardDisk->parent; + } + *hardDiskToOpen = ret; + return returnSize; +} + +/* + *removeFakeDisks: Remove all fake disks from the machine's mediaRegistry + *return 0 on success + *return -1 on failure + */ +int removeFakeDisks(vboxSnapshotXmlMachinePtr machine) +{ + int ret = -1; + size_t i = 0; + size_t j = 0; + size_t tempSize = 0; + size_t diskSize = 0; + vboxSnapshotXmlHardDiskPtr *tempList = NULL; + vboxSnapshotXmlHardDiskPtr *diskList = NULL; + if (VIR_ALLOC_N(diskList, 0) < 0) + return ret; + + for (i = 0; i < machine->mediaRegistry->ndisks; i++) { + tempSize = allChild(machine->mediaRegistry->disks[i], &tempList); + if (VIR_EXPAND_N(diskList, diskSize, tempSize) < 0) + return ret; + for (j = 0; j < tempSize; j++) { + diskList[diskSize - tempSize + j] = tempList[j]; + } + } + for (i = 0; i < diskSize; i++) { + if (strstr(diskList[i]->location, "fake") != NULL) { + if (removeHardDisk(machine->mediaRegistry, diskList[i]->uuid) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to remove hard disk %s from media registry"), + diskList[i]->location); + return ret; + } + } + } + ret = 0; + return ret; +} + +/* + *diskIsInMediaRegistry: Check if the media registry contains the disk whose location is 'location'. + *return 0 if the disk is not in the media registry + *return 1 if the disk is in the media registry + *return -1 on failure + */ +int diskIsInMediaRegistry(vboxSnapshotXmlMachinePtr machine, char *location) +{ + int ret = -1; + size_t i = 0; + size_t j = 0; + size_t tempSize = 0; + size_t diskSize = 0; + vboxSnapshotXmlHardDiskPtr *tempList = NULL; + vboxSnapshotXmlHardDiskPtr *diskList = NULL; + if (VIR_ALLOC_N(diskList, 0) < 0) + return ret; + + for (i = 0; i < machine->mediaRegistry->ndisks; i++) { + tempSize = allChild(machine->mediaRegistry->disks[i], &tempList); + if (VIR_EXPAND_N(diskList, diskSize, tempSize) < 0) + return ret; + for (j = 0; j < tempSize; j++) { + diskList[diskSize - tempSize + j] = tempList[j]; + } + } + for (i = 0; i < diskSize; i++) { + if (STREQ(diskList[i]->location, location)) { + ret = 1; + return ret; + } + } + ret = 0; + return ret; +} + +/* + *hardDisksPtrByLocation: Return a vboxSnapshotXmlHardDiskPtr whose location is 'location' + */ +vboxSnapshotXmlHardDiskPtr hardDiskPtrByLocation(vboxSnapshotXmlMachinePtr machine, char *location) +{ + int it = 0; + vboxSnapshotXmlHardDiskPtr disk = NULL; + for (it = 0; it < machine->mediaRegistry->ndisks; it++) { + disk = hardDiskByLocation(machine->mediaRegistry->disks[it], location); + if (disk != NULL) + break; + } + return disk; +} diff --git a/src/vbox/vbox_snapshot_conf.h b/src/vbox/vbox_snapshot_conf.h new file mode 100644 index 0000000..d2352cb --- /dev/null +++ b/src/vbox/vbox_snapshot_conf.h @@ -0,0 +1,100 @@ +/* + * Copyright 2014, diateam (www.diateam.net) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#ifndef VBOX_SNAPSHOT_CONF_H +# define VBOX_SNAPSHOT_CONF_H + +# include "internal.h" + +# define VBOX_UUID_REGEX "([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})" + +/*Stores VirtualBox xml hard disk information +A hard disk can have a parent and children*/ +typedef struct _vboxSnapshotXmlHardDisk vboxSnapshotXmlHardDisk; +typedef vboxSnapshotXmlHardDisk *vboxSnapshotXmlHardDiskPtr; +struct _vboxSnapshotXmlHardDisk { + vboxSnapshotXmlHardDiskPtr parent; + char *uuid; + char *location; + char *format; + char *type; + size_t nchildren; + vboxSnapshotXmlHardDiskPtr *children; +}; + +/*Stores Virtualbox xml media registry information +We separate disks from other media to manipulate them*/ +typedef struct _vboxSnapshotXmlMediaRegistry vboxSnapshotXmlMediaRegistry; +typedef vboxSnapshotXmlMediaRegistry *vboxSnapshotXmlMediaRegistryPtr; +struct _vboxSnapshotXmlMediaRegistry { + size_t ndisks; + vboxSnapshotXmlHardDiskPtr *disks; + size_t notherMedia; + char **otherMedia; +}; + +/*Stores VirtualBox xml snapshot information +A snapshot can have a parent and children*/ +typedef struct _vboxSnapshotXmlSnapshot vboxSnapshotXmlSnapshot; +typedef vboxSnapshotXmlSnapshot *vboxSnapshotXmlSnapshotPtr; +struct _vboxSnapshotXmlSnapshot { + vboxSnapshotXmlSnapshotPtr parent; + char *uuid; + char *name; + char *timeStamp; + char *description; + char *hardware; + char *storageController; + size_t nchildren; + vboxSnapshotXmlSnapshotPtr *children; +}; + +/*Stores VirtualBox xml Machine information*/ +typedef struct _vboxSnapshotXmlMachine vboxSnapshotXmlMachine; +typedef vboxSnapshotXmlMachine *vboxSnapshotXmlMachinePtr; +struct _vboxSnapshotXmlMachine { + char *uuid; + char *name; + char *currentSnapshot; + char *snapshotFolder; + int currentStateModified; + char *lastStateChange; + vboxSnapshotXmlMediaRegistryPtr mediaRegistry; + char *hardware; + char *extraData; + vboxSnapshotXmlSnapshotPtr snapshot; + char *storageController; +}; + +vboxSnapshotXmlMachinePtr vboxSnapshotLoadVboxFile(const char *filePath, char *machineLocation); +int addSnapshotToXmlMachine(vboxSnapshotXmlSnapshotPtr snapshot, vboxSnapshotXmlMachinePtr machine, char *snapshotParentName); +int addHardDiskToMediaRegistry(vboxSnapshotXmlHardDiskPtr hardDisk, vboxSnapshotXmlMediaRegistryPtr mediaRegistry, char *parentHardDiskId); +int removeSnapshot(vboxSnapshotXmlMachinePtr machine, char *snapshotName); +int removeHardDisk(vboxSnapshotXmlMediaRegistryPtr mediaRegistry,char *uuid); +int vboxSnapshotSaveVboxFile(vboxSnapshotXmlMachinePtr machine, const char *filePath); +int isCurrentSnapshot(vboxSnapshotXmlMachinePtr machine, char *snapshotName); +int getRWDisksPathsFromLibvirtXML(char *filePath, char ***realReadWriteDisksPath); +int getRODisksPathsFromLibvirtXML(char *filePath, char ***realReadOnlyDisksPath); +char *hardDiskUuidByLocation(vboxSnapshotXmlMachinePtr machine, char *location); +size_t diskListToOpen(vboxSnapshotXmlMachinePtr machine, vboxSnapshotXmlHardDiskPtr **hardDiskToOpen, char *location); +int removeFakeDisks(vboxSnapshotXmlMachinePtr machine); +int diskIsInMediaRegistry(vboxSnapshotXmlMachinePtr machine, char *location); +vboxSnapshotXmlHardDiskPtr hardDiskPtrByLocation(vboxSnapshotXmlMachinePtr machine, char *location); +vboxSnapshotXmlSnapshotPtr snapshotByName(vboxSnapshotXmlSnapshotPtr snapshot, char *snapshotName); + +#endif /*VBOX_SNAPSHOT_CONF_H*/ -- 1.7.10.4

On Fri, Apr 18, 2014 at 11:51:32AM +0200, Yohan BELLEGUIC wrote:
This structure contains the data to be saved in the VirtualBox XML file and can be manipulated with severals exposed functions. The structure is created by vboxSnapshotLoadVboxFile taking the machine XML file. It also can rewrite the XML by using vboxSnapshotSaveVboxFile. --- po/POTFILES.in | 1 + src/Makefile.am | 1 + src/vbox/vbox_snapshot_conf.c | 1383 +++++++++++++++++++++++++++++++++++++++++ src/vbox/vbox_snapshot_conf.h | 100 +++ 4 files changed, 1485 insertions(+) create mode 100644 src/vbox/vbox_snapshot_conf.c create mode 100644 src/vbox/vbox_snapshot_conf.h
+/*Stores VirtualBox xml hard disk information +A hard disk can have a parent and children*/ +typedef struct _vboxSnapshotXmlHardDisk vboxSnapshotXmlHardDisk; +typedef vboxSnapshotXmlHardDisk *vboxSnapshotXmlHardDiskPtr; +struct _vboxSnapshotXmlHardDisk {
XML is really only the serialization format - in terms of data we're talking about snapshot configuration. So I'd suggest all these structs should use 'Conf' in their name rather than 'Xml'. Since this is a brand new file, I think it would be nice if the structs and functions in this particular .c/.h file could use our latest preferred naming convention which is to prefix a 'vir' and the driver name on all things. So in this case, I'd suggest that all the structs be called virVBoxSnapshotConfXXXXXX This will make it clearer which functions are libvirt internal code vs part of the vbox API
+ vboxSnapshotXmlHardDiskPtr parent; + char *uuid; + char *location; + char *format; + char *type; + size_t nchildren; + vboxSnapshotXmlHardDiskPtr *children; +}; + +/*Stores Virtualbox xml media registry information +We separate disks from other media to manipulate them*/ +typedef struct _vboxSnapshotXmlMediaRegistry vboxSnapshotXmlMediaRegistry; +typedef vboxSnapshotXmlMediaRegistry *vboxSnapshotXmlMediaRegistryPtr; +struct _vboxSnapshotXmlMediaRegistry { + size_t ndisks; + vboxSnapshotXmlHardDiskPtr *disks; + size_t notherMedia; + char **otherMedia; +}; + +/*Stores VirtualBox xml snapshot information +A snapshot can have a parent and children*/ +typedef struct _vboxSnapshotXmlSnapshot vboxSnapshotXmlSnapshot; +typedef vboxSnapshotXmlSnapshot *vboxSnapshotXmlSnapshotPtr; +struct _vboxSnapshotXmlSnapshot { + vboxSnapshotXmlSnapshotPtr parent; + char *uuid; + char *name; + char *timeStamp; + char *description; + char *hardware; + char *storageController; + size_t nchildren; + vboxSnapshotXmlSnapshotPtr *children; +}; + +/*Stores VirtualBox xml Machine information*/ +typedef struct _vboxSnapshotXmlMachine vboxSnapshotXmlMachine; +typedef vboxSnapshotXmlMachine *vboxSnapshotXmlMachinePtr; +struct _vboxSnapshotXmlMachine { + char *uuid; + char *name; + char *currentSnapshot; + char *snapshotFolder; + int currentStateModified; + char *lastStateChange; + vboxSnapshotXmlMediaRegistryPtr mediaRegistry; + char *hardware; + char *extraData; + vboxSnapshotXmlSnapshotPtr snapshot; + char *storageController; +}; + +vboxSnapshotXmlMachinePtr vboxSnapshotLoadVboxFile(const char *filePath, char *machineLocation); +int addSnapshotToXmlMachine(vboxSnapshotXmlSnapshotPtr snapshot, vboxSnapshotXmlMachinePtr machine, char *snapshotParentName); +int addHardDiskToMediaRegistry(vboxSnapshotXmlHardDiskPtr hardDisk, vboxSnapshotXmlMediaRegistryPtr mediaRegistry, char *parentHardDiskId); +int removeSnapshot(vboxSnapshotXmlMachinePtr machine, char *snapshotName); +int removeHardDisk(vboxSnapshotXmlMediaRegistryPtr mediaRegistry,char *uuid); +int vboxSnapshotSaveVboxFile(vboxSnapshotXmlMachinePtr machine, const char *filePath); +int isCurrentSnapshot(vboxSnapshotXmlMachinePtr machine, char *snapshotName); +int getRWDisksPathsFromLibvirtXML(char *filePath, char ***realReadWriteDisksPath); +int getRODisksPathsFromLibvirtXML(char *filePath, char ***realReadOnlyDisksPath); +char *hardDiskUuidByLocation(vboxSnapshotXmlMachinePtr machine, char *location); +size_t diskListToOpen(vboxSnapshotXmlMachinePtr machine, vboxSnapshotXmlHardDiskPtr **hardDiskToOpen, char *location); +int removeFakeDisks(vboxSnapshotXmlMachinePtr machine); +int diskIsInMediaRegistry(vboxSnapshotXmlMachinePtr machine, char *location); +vboxSnapshotXmlHardDiskPtr hardDiskPtrByLocation(vboxSnapshotXmlMachinePtr machine, char *location); +vboxSnapshotXmlSnapshotPtr snapshotByName(vboxSnapshotXmlSnapshotPtr snapshot, char *snapshotName);
And all of these functions (and any other static functions in the .c file) shoudl also have virVBoxSnapshotConf as the prefix in their names. Since you have now nicely separated the XML parsing and formatting code, it'd be preferrable if you also wrote a test case for this. The general idea would be that you have a bunch of XML files in a directory tests/vboxsnapshotxmldata/*.xml, then create a file tests/vboxsnapshotxmltest.c. In this test, you would first call the function which parses XML, to give you a virVBoxSnapshotConfigMachinePtr instance, and then imediately call the function to turn this struct instance back into an XML document. Then compare the original XML doc with the XML doc you've just created. This lets us validate various important things before we merge it - The code is correctly round-tripping all data through parser & formatter and not loosing or mangling anything - There are no memory leaks in the code eg valgrind --leak-check=full tests/vboxsnapshotxmltest - The code doesn't crash upon OOM errors [1] eg ./configure --enable-test-oom make VIR_TEST_OOM=1 tests/vboxsnapshotxmltest and most of all ensures we don't cause regressions in the future. Regards, Daniel [1] http://libvirt.org/internals/oomtesting.html -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

The machine is unregistered and its vbox XML file is changed in order to add snapshot information. The machine is then registered with the snapshot to redefine. --- src/vbox/vbox_tmpl.c | 949 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 942 insertions(+), 7 deletions(-) diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c index ac000cf..f8667f6 100644 --- a/src/vbox/vbox_tmpl.c +++ b/src/vbox/vbox_tmpl.c @@ -38,12 +38,12 @@ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> -#include <libxml/xmlwriter.h> #include "internal.h" #include "datatypes.h" #include "domain_conf.h" #include "snapshot_conf.h" +#include "vbox_snapshot_conf.h" #include "network_conf.h" #include "virerror.h" #include "domain_event.h" @@ -6023,6 +6023,930 @@ vboxDomainSnapshotGet(vboxGlobalData *data, return snapshot; } +#if VBOX_API_VERSION >= 4002000 +static int vboxCloseDisksRecursively(virDomainPtr dom, char *location) +{ + VBOX_OBJECT_CHECK(dom->conn, int, -1); + nsresult rc; + size_t i = 0; + PRUnichar *locationUtf = NULL; + IMedium *medium = NULL; + IMedium **children = NULL; + PRUint32 childrenSize = 0; + VBOX_UTF8_TO_UTF16(location, &locationUtf); + rc = data->vboxObj->vtbl->OpenMedium(data->vboxObj, + locationUtf, + DeviceType_HardDisk, + AccessMode_ReadWrite, + false, + &medium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to open HardDisk"), rc); + goto cleanup; + } + rc = medium->vtbl->GetChildren(medium, &childrenSize, &children); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s" + , _("Unable to get disk children")); + goto cleanup; + } + for (i = 0; i < childrenSize; i++) { + IMedium *childMedium = children[i]; + if (childMedium) { + PRUnichar *childLocationUtf = NULL; + char *childLocation = NULL; + rc = childMedium->vtbl->GetLocation(childMedium, &childLocationUtf); + VBOX_UTF16_TO_UTF8(childLocationUtf, &childLocation); + VBOX_UTF16_FREE(childLocationUtf); + if (vboxCloseDisksRecursively(dom, childLocation) < 0) { + VIR_DEBUG("Something wrong in the recurse"); + goto cleanup; + } + VIR_FREE(childLocation); + } + } + rc = medium->vtbl->Close(medium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to CLOSE HardDisk"), rc); + goto cleanup; + } + + ret = 0; + cleanup: + VBOX_UTF16_FREE(locationUtf); + return ret; +} + +static int +vboxSnapshotRedefine(virDomainPtr dom, + virDomainSnapshotDefPtr def, + bool isCurrent) +{ + /* + * If your snapshot has a parent, + * it will only be redefined if you have already + * redefined the parent. + * + * The general algorithm of this function is below : + * First of all, we are going to create our vboxSnapshotXmlMachinePtr struct from + * the machine settings path. + * Then, if the machine current snapshot xml file is saved in the machine location, + * it means that this snapshot was previously modified by us and has fake disks. + * Fake disks are added when the flag VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT was not set + * yet, in order to not corrupt read-only disks. The first thing to do is to remove those + * disks and restore the read-write disks, if any, in the vboxSnapshotXmlMachinePtr struct. + * We also delete the current snapshot xml file. + * + * After that, we are going to register the snapshot read-only disks that we want to redefine, + * if they are not in the media registry struct. + * + * The next step is to unregister the machine and close all disks. + * + * Then, we check if the flag VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE has already been set. + * If this flag was set, we just add read-write disks to the media registry + * struct. Otherwise, we save the snapshot xml file into the machine location in order + * to recover the read-write disks during the next redefine and we create differential disks + * from the snapshot read-only disks and add them to the media registry struct. + * + * Finally, we register the machine with the new virtualbox description file. + */ + VBOX_OBJECT_CHECK(dom->conn, int, -1); + vboxIID domiid = VBOX_IID_INITIALIZER; + IMachine *machine = NULL; + nsresult rc; + PRUnichar *settingsFilePath = NULL; + char *settingsFilePath_Utf8 = NULL; + vboxSnapshotXmlMachinePtr snapshotMachineDesc = NULL; + char *currentSnapshotXmlFilePath = NULL; + PRUnichar *machineNameUtf16 = NULL; + char *machineName = NULL; + char **realReadWriteDisksPath = NULL; + int realReadWriteDisksPathSize = 0; + char **realReadOnlyDisksPath = NULL; + int realReadOnlyDisksPathSize = 0; + vboxSnapshotXmlSnapshotPtr newSnapshotPtr = NULL; + unsigned char snapshotUuid[VIR_UUID_BUFLEN]; + int it = 0; + int jt = 0; + PRUint32 aMediaSize = 0; + IMedium **aMedia = NULL; + char *machineLocationPath = NULL; + char *nameTmpUse = NULL; + bool snapshotFileExists = false; + bool needToChangeStorageController = false; + + vboxIIDFromUUID(&domiid, dom->uuid); + rc = VBOX_OBJECT_GET_MACHINE(domiid.value, &machine); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_NO_DOMAIN, "%s", + _("no domain with matching UUID")); + goto cleanup; + } + + rc = machine->vtbl->SaveSettings(machine); + /*It may failed when the machine is not mutable.*/ + rc = machine->vtbl->GetSettingsFilePath(machine, &settingsFilePath); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get settings file path")); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(settingsFilePath, &settingsFilePath_Utf8); + + /*Getting the machine name to retrieve the machine location path.*/ + rc = machine->vtbl->GetName(machine, &machineNameUtf16); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get machine name")); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(machineNameUtf16, &machineName); + + if (virAsprintf(&nameTmpUse, "%s.vbox", machineName) < 0) + goto cleanup; + machineLocationPath = virStringReplace(settingsFilePath_Utf8, nameTmpUse, ""); + if (machineLocationPath == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to get the machine location path")); + goto cleanup; + } + + /*We create the xml struct with the settings file path.*/ + snapshotMachineDesc = vboxSnapshotLoadVboxFile(settingsFilePath_Utf8, machineLocationPath); + if (snapshotMachineDesc == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot create a vboxSnapshotXmlPtr")); + goto cleanup; + } + if (snapshotMachineDesc->currentSnapshot != NULL) { + if (virAsprintf(¤tSnapshotXmlFilePath, "%s%s.xml", machineLocationPath, + snapshotMachineDesc->currentSnapshot) < 0) + goto cleanup; + snapshotFileExists = virFileExists(currentSnapshotXmlFilePath); + } + + if (snapshotFileExists) { + /* + * We have created fake disks, so we have to remove them and replace them with + * the read-write disks if there are any. The fake disks will be closed during + * the machine unregistration. + */ + if (removeFakeDisks(snapshotMachineDesc) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to remove Fake Disks")); + goto cleanup; + } + realReadWriteDisksPathSize = getRWDisksPathsFromLibvirtXML(currentSnapshotXmlFilePath, + &realReadWriteDisksPath); + realReadOnlyDisksPathSize = getRODisksPathsFromLibvirtXML(currentSnapshotXmlFilePath, + &realReadOnlyDisksPath); + /*The read-only disk number is necessarily greater or equal to the + *read-write disk number*/ + if (realReadOnlyDisksPathSize < realReadWriteDisksPathSize) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("The read only disk number must be greater or equal to the " + " read write disk number")); + goto cleanup; + } + for (it = 0; it < realReadWriteDisksPathSize; it++) { + vboxSnapshotXmlHardDiskPtr readWriteDisk = NULL; + PRUnichar *locationUtf = NULL; + IMedium *readWriteMedium = NULL; + PRUnichar *uuidUtf = NULL; + char *uuid = NULL; + PRUnichar *formatUtf = NULL; + char *format = NULL; + char *parentUuid = NULL; + + VBOX_UTF8_TO_UTF16(realReadWriteDisksPath[it], &locationUtf); + rc = data->vboxObj->vtbl->OpenMedium(data->vboxObj, + locationUtf, + DeviceType_HardDisk, + AccessMode_ReadWrite, + false, + &readWriteMedium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to open HardDisk"), rc); + VBOX_UTF16_FREE(locationUtf); + goto cleanup; + } + VBOX_UTF16_FREE(locationUtf); + + rc = readWriteMedium->vtbl->GetId(readWriteMedium, &uuidUtf); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to get the read write medium id")); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(uuidUtf, &uuid); + VBOX_UTF16_FREE(uuidUtf); + + rc = readWriteMedium->vtbl->GetFormat(readWriteMedium, &formatUtf); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to get the read write medium format")); + VIR_FREE(uuid); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(formatUtf, &format); + VBOX_UTF16_FREE(formatUtf); + + if (VIR_ALLOC(readWriteDisk) < 0) { + VIR_FREE(uuid); + VIR_FREE(formatUtf); + goto cleanup; + } + + readWriteDisk->format = format; + readWriteDisk->uuid = uuid; + readWriteDisk->location = realReadWriteDisksPath[it]; + /* + * We get the current snapshot's read-only disk uuid in order to add the + * read-write disk to the media registry as it's child. The read-only disk + * is already in the media registry because it is the fake disk's parent. + */ + parentUuid = hardDiskUuidByLocation(snapshotMachineDesc, + realReadOnlyDisksPath[it]); + if (addHardDiskToMediaRegistry(readWriteDisk, + snapshotMachineDesc->mediaRegistry, + parentUuid) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to add hard disk to media Registry")); + VIR_FREE(readWriteDisk); + goto cleanup; + } + rc = readWriteMedium->vtbl->Close(readWriteMedium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to close HardDisk"), rc); + goto cleanup; + } + } + /* + * Now we have done this swap, we remove the snapshot xml file from the + * current machine location. + */ + if (remove(currentSnapshotXmlFilePath) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to delete file %s"), currentSnapshotXmlFilePath); + goto cleanup; + } + } + /* + * Before unregistering the machine, while all disks are still open, ensure that all + * read-only disks are in the redefined snapshot's media registry (the disks need to + * be open to query their uuid). + */ + for (it = 0; it < def->dom->ndisks; it++) { + int diskInMediaRegistry = 0; + IMedium *readOnlyMedium = NULL; + PRUnichar *locationUtf = NULL; + PRUnichar *uuidUtf = NULL; + char *uuid = NULL; + PRUnichar *formatUtf = NULL; + char *format = NULL; + PRUnichar *parentUuidUtf = NULL; + char *parentUuid = NULL; + vboxSnapshotXmlHardDiskPtr readOnlyDisk = NULL; + + diskInMediaRegistry = diskIsInMediaRegistry(snapshotMachineDesc, + def->dom->disks[it]->src.path); + if (diskInMediaRegistry == -1) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to know if disk is in media registry")); + goto cleanup; + } + if (diskInMediaRegistry == 1) /*Nothing to do.*/ + continue; + /*The read only disk is not in the media registry*/ + + VBOX_UTF8_TO_UTF16(def->dom->disks[it]->src.path, &locationUtf); + rc = data->vboxObj->vtbl->OpenMedium(data->vboxObj, + locationUtf, + DeviceType_HardDisk, + AccessMode_ReadWrite, + false, + &readOnlyMedium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to open HardDisk"), rc); + VBOX_UTF16_FREE(locationUtf); + goto cleanup; + } + VBOX_UTF16_FREE(locationUtf); + + rc = readOnlyMedium->vtbl->GetId(readOnlyMedium, &uuidUtf); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s" + , _("Unable to get hard disk id")); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(uuidUtf, &uuid); + VBOX_UTF16_FREE(uuidUtf); + + rc = readOnlyMedium->vtbl->GetFormat(readOnlyMedium, &formatUtf); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s" + , _("Unable to get hard disk format")); + VIR_FREE(uuid); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(formatUtf, &format); + VBOX_UTF16_FREE(formatUtf); + + /*This disk is already in the media registry*/ + IMedium *parentReadOnlyMedium = NULL; + rc = readOnlyMedium->vtbl->GetParent(readOnlyMedium, &parentReadOnlyMedium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s" + , _("Unable to get parent hard disk")); + VIR_FREE(uuid); + goto cleanup; + } + + rc = parentReadOnlyMedium->vtbl->GetId(parentReadOnlyMedium, &parentUuidUtf); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to get hard disk id"), rc); + VIR_FREE(uuid); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(parentUuidUtf, &parentUuid); + VBOX_UTF16_FREE(parentUuidUtf); + + rc = readOnlyMedium->vtbl->Close(readOnlyMedium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to close HardDisk"), rc); + VIR_FREE(uuid); + VIR_FREE(parentUuid); + goto cleanup; + } + + if (VIR_ALLOC(readOnlyDisk) < 0) { + VIR_FREE(uuid); + VIR_FREE(parentUuid); + goto cleanup; + } + + readOnlyDisk->format = format; + readOnlyDisk->uuid = uuid; + if (VIR_STRDUP(readOnlyDisk->location, def->dom->disks[it]->src.path) < 0) { + VIR_FREE(readOnlyDisk); + goto cleanup; + } + + if (addHardDiskToMediaRegistry(readOnlyDisk, snapshotMachineDesc->mediaRegistry, + parentUuid) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to add hard disk to media registry")); + VIR_FREE(readOnlyDisk); + goto cleanup; + } + } + + /*Now, we can unregister the machine*/ + rc = machine->vtbl->Unregister(machine, + CleanupMode_DetachAllReturnHardDisksOnly, + &aMediaSize, + &aMedia); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to unregister machine"), rc); + goto cleanup; + } + VBOX_RELEASE(machine); + + /* + * Unregister the machine, and then close all disks returned by the unregister method. + * Some close operations will fail because some disks that need to be closed will not + * be returned by virtualbox. We will close them just after. We have to use this + * solution because it is the only way to delete fake disks. + */ + for (it = 0; it < aMediaSize; it++) { + IMedium *medium = aMedia[it]; + if (medium) { + PRUnichar *locationUtf16 = NULL; + char *locationUtf8 = NULL; + rc = medium->vtbl->GetLocation(medium, &locationUtf16); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to get medium location")); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(locationUtf16, &locationUtf8); + VBOX_UTF16_FREE(locationUtf16); + if (strstr(locationUtf8, "fake") != NULL) { + /*we delete the fake disk because we don't need it anymore*/ + IProgress *progress = NULL; + PRInt32 resultCode = -1; + rc = medium->vtbl->DeleteStorage(medium, &progress); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to delete medium"), rc); + VIR_FREE(locationUtf8); + goto cleanup; + } + progress->vtbl->WaitForCompletion(progress, -1); + progress->vtbl->GetResultCode(progress, &resultCode); + if (NS_FAILED(resultCode)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Error while closing medium"), resultCode); + VIR_FREE(locationUtf8); + goto cleanup; + } + VBOX_RELEASE(progress); + } else { + /* + * This a comment from vboxmanage code in the handleUnregisterVM + * function in VBoxManageMisc.cpp : + * Note that the IMachine::Unregister method will return the medium + * reference in a sane order, which means that closing will normally + * succeed, unless there is still another machine which uses the + * medium. No harm done if we ignore the error. + */ + rc = medium->vtbl->Close(medium); + } + VBOX_UTF8_FREE(locationUtf8); + } + } + /*Close all disks that failed to close normally.*/ + for (it = 0; it < snapshotMachineDesc->mediaRegistry->ndisks; it++) { + if (vboxCloseDisksRecursively(dom, snapshotMachineDesc->mediaRegistry->disks[it]->location) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to close recursively all disks")); + goto cleanup; + } + } + /*Here, all disks are closed or deleted*/ + + /*We are now going to create and fill the Snapshot xml struct*/ + if (VIR_ALLOC(newSnapshotPtr) < 0) + goto cleanup; + + if (virUUIDGenerate(snapshotUuid) < 0) + goto cleanup; + + char uuidtmp[VIR_UUID_STRING_BUFLEN]; + virUUIDFormat(snapshotUuid, uuidtmp); + if (VIR_STRDUP(newSnapshotPtr->uuid, uuidtmp) < 0) + goto cleanup; + + VIR_DEBUG("New snapshot UUID: %s", newSnapshotPtr->uuid); + if (VIR_STRDUP(newSnapshotPtr->name, def->name) < 0) + goto cleanup; + + newSnapshotPtr->timeStamp = virTimeStringThen(def->creationTime * 1000); + + if (VIR_STRDUP(newSnapshotPtr->description, def->description) < 0) + goto cleanup; + + if (VIR_STRDUP(newSnapshotPtr->hardware, snapshotMachineDesc->hardware) < 0) + goto cleanup; + + if (VIR_STRDUP(newSnapshotPtr->storageController, snapshotMachineDesc->storageController) < 0) + goto cleanup; + + /*We get the parent disk uuid from the parent disk location to correctly fill the storage controller.*/ + for (it = 0; it < def->dom->ndisks; it++) { + char *location = NULL; + char *uuidReplacing = NULL; + char **searchResultTab = NULL; + ssize_t resultSize = 0; + char *tmp = NULL; + + location = def->dom->disks[it]->src.path; + if (!location) + goto cleanup; + /*Replacing the uuid*/ + uuidReplacing = hardDiskUuidByLocation(snapshotMachineDesc, location); + if (uuidReplacing == NULL) + goto cleanup; + + resultSize = virStringSearch(newSnapshotPtr->storageController, + VBOX_UUID_REGEX, + it + 1, + &searchResultTab); + if (resultSize != it + 1) + goto cleanup; + + tmp = virStringReplace(newSnapshotPtr->storageController, + searchResultTab[it], + uuidReplacing); + virStringFreeList(searchResultTab); + VIR_FREE(newSnapshotPtr->storageController); + if (!tmp) + goto cleanup; + if (VIR_STRDUP(newSnapshotPtr->storageController, tmp) < 0) + goto cleanup; + + VIR_FREE(tmp); + } + if (addSnapshotToXmlMachine(newSnapshotPtr, snapshotMachineDesc, def->parent) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to add the snapshot to the machine description")); + goto cleanup; + } + /* + * We change the current snapshot only if there is no current snapshot or if the + * snapshotFile exists, otherwise, it means that the correct current snapshot is + * already set. + */ + + if (snapshotMachineDesc->currentSnapshot == NULL || snapshotFileExists) { + snapshotMachineDesc->currentSnapshot = newSnapshotPtr->uuid; + needToChangeStorageController = true; + } + + /* + * Open the snapshot's read-write disk's full ancestry to allow opening the + * read-write disk itself. + */ + for (it = 0; it < def->dom->ndisks; it++) { + char *location = NULL; + vboxSnapshotXmlHardDiskPtr *hardDiskToOpen = NULL; + size_t hardDiskToOpenSize = 0; + + location = def->dom->disks[it]->src.path; + if (!location) + goto cleanup; + + hardDiskToOpenSize = diskListToOpen(snapshotMachineDesc, + &hardDiskToOpen, location); + for (jt = hardDiskToOpenSize -1; jt >= 0; jt--) { + IMedium *medium = NULL; + PRUnichar *locationUtf16 = NULL; + VBOX_UTF8_TO_UTF16(hardDiskToOpen[jt]->location, &locationUtf16); + + rc = data->vboxObj->vtbl->OpenMedium(data->vboxObj, + locationUtf16, + DeviceType_HardDisk, + AccessMode_ReadWrite, + false, + &medium); + VBOX_UTF16_FREE(locationUtf16); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to open HardDisk"), rc); + goto cleanup; + } + } + } + if (isCurrent || !needToChangeStorageController) { + /* We don't create a differential hard disk because either the current snapshot + * has already been defined or the snapshot to redefine is the current snapshot. + * If the snapshot to redefine is the current snapshot, we add read-write disks in + * the machine storage controllers. + */ + for (it = 0; it < def->ndisks; it++) { + IMedium *medium = NULL; + PRUnichar *locationUtf16 = NULL; + vboxSnapshotXmlHardDiskPtr disk = NULL; + PRUnichar *formatUtf16 = NULL; + char *format = NULL; + PRUnichar *uuidUtf16 = NULL; + char *uuid = NULL; + IMedium *parentDisk = NULL; + PRUnichar *parentUuidUtf16 = NULL; + char *parentUuid = NULL; + + VBOX_UTF8_TO_UTF16(def->disks[it].src.path, &locationUtf16); + rc = data->vboxObj->vtbl->OpenMedium(data->vboxObj, + locationUtf16, + DeviceType_HardDisk, + AccessMode_ReadWrite, + false, + &medium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to open HardDisk"), rc); + goto cleanup; + } + VBOX_UTF16_FREE(locationUtf16); + + if (VIR_ALLOC(disk) < 0) + goto cleanup; + + rc = medium->vtbl->GetFormat(medium, &formatUtf16); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s" + , _("Unable to get disk format")); + VIR_FREE(disk); + goto cleanup; + } + + VBOX_UTF16_TO_UTF8(formatUtf16, &format); + disk->format = format; + VBOX_UTF16_FREE(formatUtf16); + + if (VIR_STRDUP(disk->location, def->disks[it].src.path) < 0) { + VIR_FREE(disk); + goto cleanup; + } + + rc = medium->vtbl->GetId(medium, &uuidUtf16); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s" + , _("Unable to get disk uuid")); + VIR_FREE(disk); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(uuidUtf16, &uuid); + disk->uuid = uuid; + VBOX_UTF16_FREE(uuidUtf16); + + rc = medium->vtbl->GetParent(medium, &parentDisk); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s" + , _("Unable to get disk parent")); + VIR_FREE(disk); + goto cleanup; + } + + parentDisk->vtbl->GetId(parentDisk, &parentUuidUtf16); + VBOX_UTF16_TO_UTF8(parentUuidUtf16, &parentUuid); + VBOX_UTF16_FREE(parentUuidUtf16); + if (addHardDiskToMediaRegistry(disk, + snapshotMachineDesc->mediaRegistry, + parentUuid) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to add hard disk to the media registry")); + VIR_FREE(disk); + goto cleanup; + } + + if (needToChangeStorageController) { + /*We need to append this disk in the storage controller*/ + char **searchResultTab = NULL; + ssize_t resultSize = 0; + char *tmp = NULL; + resultSize = virStringSearch(snapshotMachineDesc->storageController, + VBOX_UUID_REGEX, + it + 1, + &searchResultTab); + if (resultSize != it + 1) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to find UUID %s"), searchResultTab[it]); + goto cleanup; + } + + tmp = virStringReplace(snapshotMachineDesc->storageController, + searchResultTab[it], + disk->uuid); + virStringFreeList(searchResultTab); + VIR_FREE(snapshotMachineDesc->storageController); + if (!tmp) + goto cleanup; + if (VIR_STRDUP(snapshotMachineDesc->storageController, tmp) < 0) + goto cleanup; + + VIR_FREE(tmp); + } + /*Close disk*/ + rc = medium->vtbl->Close(medium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to close HardDisk"), rc); + goto cleanup; + } + } + } else { + /*Create a "fake" disk to avoid corrupting children snapshot disks.*/ + for (it = 0; it < def->dom->ndisks; it++) { + IMedium *medium = NULL; + PRUnichar *locationUtf16 = NULL; + PRUnichar *parentUuidUtf16 = NULL; + char *parentUuid = NULL; + IMedium *newMedium = NULL; + PRUnichar *formatUtf16 = NULL; + PRUnichar *newLocation = NULL; + char *newLocationUtf8 = NULL; + PRInt32 resultCode = -1; + vboxSnapshotXmlHardDiskPtr disk = NULL; + PRUnichar *uuidUtf16 = NULL; + char *uuid = NULL; + char *format = NULL; + char **searchResultTab = NULL; + ssize_t resultSize = 0; + char *tmp = NULL; + + VBOX_UTF8_TO_UTF16(def->dom->disks[it]->src.path, &locationUtf16); + rc = data->vboxObj->vtbl->OpenMedium(data->vboxObj, + locationUtf16, + DeviceType_HardDisk, + AccessMode_ReadWrite, + false, + &medium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to open HardDisk"), rc); + VBOX_UTF16_FREE(locationUtf16); + goto cleanup; + } + VBOX_UTF16_FREE(locationUtf16); + + rc = medium->vtbl->GetId(medium, &parentUuidUtf16); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to get hardDisk Id"), rc); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(parentUuidUtf16, &parentUuid); + VBOX_UTF16_FREE(parentUuidUtf16); + VBOX_UTF8_TO_UTF16("VDI", &formatUtf16); + + if (virAsprintf(&newLocationUtf8, "%sfakedisk-%d.vdi", machineLocationPath, it) < 0) + goto cleanup; + VBOX_UTF8_TO_UTF16(newLocationUtf8, &newLocation); + rc = data->vboxObj->vtbl->CreateHardDisk(data->vboxObj, + formatUtf16, + newLocation, + &newMedium); + VBOX_UTF16_FREE(newLocation); + VBOX_UTF16_FREE(formatUtf16); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to create HardDisk"), rc); + goto cleanup; + } + + IProgress *progress = NULL; +# if VBOX_API_VERSION < 4003000 + medium->vtbl->CreateDiffStorage(medium, newMedium, MediumVariant_Diff, &progress); +# else + PRUint32 tab[1]; + tab[0] = MediumVariant_Diff; + medium->vtbl->CreateDiffStorage(medium, newMedium, 1, tab, &progress); +# endif + + progress->vtbl->WaitForCompletion(progress, -1); + progress->vtbl->GetResultCode(progress, &resultCode); + if (NS_FAILED(resultCode)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Error while creating diff storage"), resultCode); + goto cleanup; + } + VBOX_RELEASE(progress); + /* + * The differential disk is created, we add it to the media registry and the + * machine storage controllers. + */ + + if (VIR_ALLOC(disk) < 0) + goto cleanup; + + rc = newMedium->vtbl->GetId(newMedium, &uuidUtf16); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to get medium uuid"), rc); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(uuidUtf16, &uuid); + disk->uuid = uuid; + VBOX_UTF16_FREE(uuidUtf16); + + if (VIR_STRDUP(disk->location, newLocationUtf8) < 0) + goto cleanup; + + rc = newMedium->vtbl->GetFormat(newMedium, &formatUtf16); + VBOX_UTF16_TO_UTF8(formatUtf16, &format); + disk->format = format; + VBOX_UTF16_FREE(formatUtf16); + + if (addHardDiskToMediaRegistry(disk, + snapshotMachineDesc->mediaRegistry, + parentUuid) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to add hard disk to the media registry")); + goto cleanup; + } + /*Adding the fake disk to the machine storage controllers*/ + + resultSize = virStringSearch(snapshotMachineDesc->storageController, + VBOX_UUID_REGEX, + it + 1, + &searchResultTab); + if (resultSize != it + 1) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to find UUID %s"), searchResultTab[it]); + goto cleanup; + } + + tmp = virStringReplace(snapshotMachineDesc->storageController, + searchResultTab[it], + disk->uuid); + virStringFreeList(searchResultTab); + VIR_FREE(snapshotMachineDesc->storageController); + if (!tmp) + goto cleanup; + if (VIR_STRDUP(snapshotMachineDesc->storageController, tmp) < 0) + goto cleanup; + + VIR_FREE(tmp); + /*Closing the "fake" disk*/ + rc = newMedium->vtbl->Close(newMedium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to close the new medium"), rc); + goto cleanup; + } + } + /* + * We save the snapshot xml file to retrieve the real read-write disk during the + * next define. This file is saved as "'machineLocation'/snapshot-'uuid'.xml" + */ + VIR_FREE(currentSnapshotXmlFilePath); + if (virAsprintf(¤tSnapshotXmlFilePath, "%s%s.xml", machineLocationPath, snapshotMachineDesc->currentSnapshot) < 0) + goto cleanup; + char *snapshotContent = virDomainSnapshotDefFormat(NULL, def, VIR_DOMAIN_XML_SECURE, 0); + xmlDocPtr newXml = virXMLParse(NULL, snapshotContent, NULL); + VIR_FREE(snapshotContent); + if (newXml && xmlSaveFile(currentSnapshotXmlFilePath, newXml) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Unable to save new snapshot xml file")); + goto cleanup; + } + } + /* + * All the snapshot structure manipulation is done, we close the disks we have + * previously opened. + */ + for (it = 0; it < def->dom->ndisks; it++) { + char *location = def->dom->disks[it]->src.path; + if (!location) + goto cleanup; + + vboxSnapshotXmlHardDiskPtr *hardDiskToOpen = NULL; + size_t hardDiskToOpenSize = diskListToOpen(snapshotMachineDesc, + &hardDiskToOpen, location); + for (jt = 0; jt < hardDiskToOpenSize; jt++) { + IMedium *medium = NULL; + PRUnichar *locationUtf16 = NULL; + VBOX_UTF8_TO_UTF16(hardDiskToOpen[jt]->location, &locationUtf16); + rc = data->vboxObj->vtbl->OpenMedium(data->vboxObj, + locationUtf16, + DeviceType_HardDisk, + AccessMode_ReadWrite, + false, + &medium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to open HardDisk"), rc); + goto cleanup; + } + rc = medium->vtbl->Close(medium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to CLOSE HardDisk"), rc); + goto cleanup; + } + VBOX_UTF16_FREE(locationUtf16); + } + } + + /*Now, we rewrite the 'machineName'.vbox file to redefine the machine.*/ + if (vboxSnapshotSaveVboxFile(snapshotMachineDesc, settingsFilePath_Utf8) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to serialize the machine description")); + goto cleanup; + } + rc = data->vboxObj->vtbl->OpenMachine(data->vboxObj, + settingsFilePath, + &machine); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to open Machine"), rc); + goto cleanup; + } + + rc = data->vboxObj->vtbl->RegisterMachine(data->vboxObj, machine); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to register Machine"), rc); + goto cleanup; + } + + ret = 0; + cleanup: + VBOX_RELEASE(machine); + VBOX_UTF16_FREE(settingsFilePath); + VBOX_UTF8_FREE(settingsFilePath_Utf8); + VIR_FREE(snapshotMachineDesc); + VIR_FREE(currentSnapshotXmlFilePath); + VBOX_UTF16_FREE(machineNameUtf16); + VBOX_UTF8_FREE(machineName); + virStringFreeList(realReadOnlyDisksPath); + virStringFreeList(realReadWriteDisksPath); + VIR_FREE(newSnapshotPtr); + VIR_FREE(machineLocationPath); + VIR_FREE(nameTmpUse); + return ret; +} +#endif + static virDomainSnapshotPtr vboxDomainSnapshotCreateXML(virDomainPtr dom, const char *xmlDesc, @@ -6044,9 +6968,15 @@ vboxDomainSnapshotCreateXML(virDomainPtr dom, #else PRInt32 result; #endif +#if VBOX_API_VERSION >= 4002000 + bool isCurrent = false; +#endif + /* VBox has no snapshot metadata, so this flag is trivial. */ - virCheckFlags(VIR_DOMAIN_SNAPSHOT_CREATE_NO_METADATA, NULL); + virCheckFlags(VIR_DOMAIN_SNAPSHOT_CREATE_NO_METADATA | + VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE | + VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT, NULL); if (!(def = virDomainSnapshotDefParseString(xmlDesc, data->caps, data->xmlopt, -1, @@ -6054,11 +6984,6 @@ vboxDomainSnapshotCreateXML(virDomainPtr dom, VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE))) goto cleanup; - if (def->ndisks) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("disk snapshots not supported yet")); - goto cleanup; - } vboxIIDFromUUID(&domiid, dom->uuid); rc = VBOX_OBJECT_GET_MACHINE(domiid.value, &machine); @@ -6068,6 +6993,16 @@ vboxDomainSnapshotCreateXML(virDomainPtr dom, goto cleanup; } +#if VBOX_API_VERSION >= 4002000 + isCurrent = flags & VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT; + if (flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE) { + if (vboxSnapshotRedefine(dom, def, isCurrent) < 0) + goto cleanup; + ret = virGetDomainSnapshot(dom, def->name); + goto cleanup; + } +#endif + rc = machine->vtbl->GetState(machine, &state); if (NS_FAILED(rc)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", -- 1.7.10.4

On Fri, Apr 18, 2014 at 11:51:33AM +0200, Yohan BELLEGUIC wrote:
The machine is unregistered and its vbox XML file is changed in order to add snapshot information. The machine is then registered with the snapshot to redefine. --- src/vbox/vbox_tmpl.c | 949 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 942 insertions(+), 7 deletions(-)
diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c index ac000cf..f8667f6 100644 --- a/src/vbox/vbox_tmpl.c +++ b/src/vbox/vbox_tmpl.c @@ -38,12 +38,12 @@ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> -#include <libxml/xmlwriter.h>
Ah, removing the bogus include I mentioned in patch 1. Can just squash this change into the first patch instead.
+static int +vboxSnapshotRedefine(virDomainPtr dom, + virDomainSnapshotDefPtr def, + bool isCurrent) +{
+ /* + * Now we have done this swap, we remove the snapshot xml file from the + * current machine location. + */ + if (remove(currentSnapshotXmlFilePath) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to delete file %s"), currentSnapshotXmlFilePath); + goto cleanup; + }
Small preference to use 'unlink' instead of remove, since we know this is a plain file. Also use virReportSystemError(errno, _("....")...) since we have an errno we can usefully provide here.
+ /* + * We save the snapshot xml file to retrieve the real read-write disk during the + * next define. This file is saved as "'machineLocation'/snapshot-'uuid'.xml" + */ + VIR_FREE(currentSnapshotXmlFilePath); + if (virAsprintf(¤tSnapshotXmlFilePath, "%s%s.xml", machineLocationPath, snapshotMachineDesc->currentSnapshot) < 0) + goto cleanup; + char *snapshotContent = virDomainSnapshotDefFormat(NULL, def, VIR_DOMAIN_XML_SECURE, 0);
Need to check for snapshotContent == NULL here.
+ xmlDocPtr newXml = virXMLParse(NULL, snapshotContent, NULL);
Or this will cause a crash on OOM
+ VIR_FREE(snapshotContent); + if (newXml && xmlSaveFile(currentSnapshotXmlFilePath, newXml) < 0) {
This whole sequence of functions is wierd. You have 'snapshotContent' which is a string containing an XML document. You then parse this to get an xmlDocPtr. You then call xmlSaveFile which turns xmlDocPtr back into a string and writes it to a file. IMHO you should just use virFileWriteStr(snapshotContent) and remove this parsing
+ virReportError(VIR_ERR_XML_ERROR, "%s", + _("Unable to save new snapshot xml file")); + goto cleanup; + } + }
Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

All snapshots information will be deleted from the vbox XML, but differencing disks will be kept so the user will be able to redefine the snapshot. --- src/vbox/vbox_tmpl.c | 453 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 452 insertions(+), 1 deletion(-) diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c index f8667f6..c5b5074 100644 --- a/src/vbox/vbox_tmpl.c +++ b/src/vbox/vbox_tmpl.c @@ -8347,7 +8347,443 @@ vboxDomainSnapshotDeleteTree(vboxGlobalData *data, return ret; } +#if VBOX_API_VERSION >= 4002000 +static int +vboxDomainSnapshotDeleteMetadataOnly(virDomainSnapshotPtr snapshot) +{ + /* + * This function will remove the node in the vbox xml corresponding to the snapshot. + * It is usually called by vboxDomainSnapshotDelete() with the flag + * VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY. + * If you want to use it anywhere else, be careful, if the snapshot you want to delete + * has children, the result is not granted, they will probably will be deleted in the + * xml, but you may have a problem with hard drives. + * + * If the snapshot which is being deleted is the current one, we will set the current + * snapshot of the machine to the parent of this snapshot. Before writing the modified + * xml file, we undefine the machine from vbox. After writing the file, we redefine + * the machine with the new file. + */ + + virDomainPtr dom = snapshot->domain; + VBOX_OBJECT_CHECK(dom->conn, int, -1); + virDomainSnapshotDefPtr def= NULL; + char *defXml = NULL; + vboxIID domiid = VBOX_IID_INITIALIZER; + nsresult rc; + IMachine *machine = NULL; + PRUnichar *settingsFilePathUtf16 = NULL; + char *settingsFilepath = NULL; + vboxSnapshotXmlMachinePtr snapshotMachineDesc = NULL; + int isCurrent = -1; + int it = 0; + PRUnichar *machineNameUtf16 = NULL; + char *machineName = NULL; + char *nameTmpUse = NULL; + char *machineLocationPath = NULL; + PRUint32 aMediaSize = 0; + IMedium **aMedia = NULL; + + defXml = vboxDomainSnapshotGetXMLDesc(snapshot, 0); + if (!defXml) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to get XML Desc of snapshot")); + goto cleanup; + } + def = virDomainSnapshotDefParseString(defXml, + data->caps, + data->xmlopt, + -1, + VIR_DOMAIN_SNAPSHOT_PARSE_DISKS | + VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE); + if (!def) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to get a virDomainSnapshotDefPtr")); + goto cleanup; + } + + vboxIIDFromUUID(&domiid, dom->uuid); + rc = VBOX_OBJECT_GET_MACHINE(domiid.value, &machine); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_NO_DOMAIN, "%s", + _("no domain with matching UUID")); + goto cleanup; + } + rc = machine->vtbl->GetSettingsFilePath(machine, &settingsFilePathUtf16); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get settings file path")); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(settingsFilePathUtf16, &settingsFilepath); + /*Getting the machine name to retrieve the machine location path.*/ + rc = machine->vtbl->GetName(machine, &machineNameUtf16); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot get machine name")); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(machineNameUtf16, &machineName); + if (virAsprintf(&nameTmpUse, "%s.vbox", machineName) < 0) + goto cleanup; + machineLocationPath = virStringReplace(settingsFilepath, nameTmpUse, ""); + if (machineLocationPath == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to get the machine location path")); + goto cleanup; + } + snapshotMachineDesc = vboxSnapshotLoadVboxFile(settingsFilepath, machineLocationPath); + if (!snapshotMachineDesc) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot create a vboxSnapshotXmlPtr")); + goto cleanup; + } + + isCurrent = isCurrentSnapshot(snapshotMachineDesc, def->name); + if (isCurrent < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to know if the snapshot is the current snapshot")); + goto cleanup; + } + if (isCurrent) { + /* + * If the snapshot is the current snapshot, it means that the machine has read-write + * disks. The first thing to do is to manipulate VirtualBox API to create + * differential read-write disks if the parent snapshot is not null. + */ + if (def->parent != NULL) { + for (it = 0; it < def->dom->ndisks; it++) { + vboxSnapshotXmlHardDiskPtr readOnly = NULL; + IMedium *medium = NULL; + PRUnichar *locationUtf16 = NULL; + PRUnichar *parentUuidUtf16 = NULL; + char *parentUuid = NULL; + IMedium *newMedium = NULL; + PRUnichar *formatUtf16 = NULL; + PRUnichar *newLocation = NULL; + char *newLocationUtf8 = NULL; + IProgress *progress = NULL; + PRInt32 resultCode = -1; + vboxSnapshotXmlHardDiskPtr disk = NULL; + PRUnichar *uuidUtf16 = NULL; + char *uuid = NULL; + char *format = NULL; + char **searchResultTab = NULL; + ssize_t resultSize = 0; + char *tmp = NULL; + + readOnly = hardDiskPtrByLocation(snapshotMachineDesc, + def->dom->disks[it]->src.path); + if (!readOnly) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Cannot get hard disk by location")); + goto cleanup; + } + if (readOnly->parent == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("The read only disk has no parent")); + goto cleanup; + } + + VBOX_UTF8_TO_UTF16(readOnly->parent->location, &locationUtf16); + rc = data->vboxObj->vtbl->OpenMedium(data->vboxObj, + locationUtf16, + DeviceType_HardDisk, + AccessMode_ReadWrite, + false, + &medium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to open HardDisk"), rc); + goto cleanup; + } + + rc = medium->vtbl->GetId(medium, &parentUuidUtf16); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to get hardDisk Id"), rc); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(parentUuidUtf16, &parentUuid); + VBOX_UTF16_FREE(parentUuidUtf16); + VBOX_UTF16_FREE(locationUtf16); + VBOX_UTF8_TO_UTF16("VDI", &formatUtf16); + + if (virAsprintf(&newLocationUtf8, "%sfakedisk-%s-%d.vdi", + machineLocationPath, def->parent, it) < 0) + goto cleanup; + VBOX_UTF8_TO_UTF16(newLocationUtf8, &newLocation); + rc = data->vboxObj->vtbl->CreateHardDisk(data->vboxObj, + formatUtf16, + newLocation, + &newMedium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to create HardDisk"), rc); + goto cleanup; + } + VBOX_UTF16_FREE(formatUtf16); + VBOX_UTF16_FREE(newLocation); + +# if VBOX_API_VERSION < 4003000 + medium->vtbl->CreateDiffStorage(medium, newMedium, MediumVariant_Diff, &progress); +# else + PRUint32 tab[1]; + tab[0] = MediumVariant_Diff; + medium->vtbl->CreateDiffStorage(medium, newMedium, 1, tab, &progress); +# endif + + progress->vtbl->WaitForCompletion(progress, -1); + progress->vtbl->GetResultCode(progress, &resultCode); + if (NS_FAILED(resultCode)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Error while creating diff storage"), resultCode); + goto cleanup; + } + VBOX_RELEASE(progress); + /* + * The differential disk is created, we add it to the media registry and + * the machine storage controller. + */ + + if (VIR_ALLOC(disk) < 0) + goto cleanup; + + rc = newMedium->vtbl->GetId(newMedium, &uuidUtf16); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to get medium uuid"), rc); + goto cleanup; + } + VBOX_UTF16_TO_UTF8(uuidUtf16, &uuid); + disk->uuid = uuid; + VBOX_UTF16_FREE(uuidUtf16); + + if (VIR_STRDUP(disk->location, newLocationUtf8) < 0) + goto cleanup; + + rc = newMedium->vtbl->GetFormat(newMedium, &formatUtf16); + VBOX_UTF16_TO_UTF8(formatUtf16, &format); + disk->format = format; + VBOX_UTF16_FREE(formatUtf16); + + if (addHardDiskToMediaRegistry(disk, + snapshotMachineDesc->mediaRegistry, + parentUuid) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to add hard disk to the media registry")); + goto cleanup; + } + /*Adding fake disks to the machine storage controllers*/ + + resultSize = virStringSearch(snapshotMachineDesc->storageController, + VBOX_UUID_REGEX, + it + 1, + &searchResultTab); + if (resultSize != it + 1) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to find UUID %s"), searchResultTab[it]); + goto cleanup; + } + + tmp = virStringReplace(snapshotMachineDesc->storageController, + searchResultTab[it], + disk->uuid); + virStringFreeList(searchResultTab); + VIR_FREE(snapshotMachineDesc->storageController); + if (!tmp) + goto cleanup; + if (VIR_STRDUP(snapshotMachineDesc->storageController, tmp) < 0) + goto cleanup; + + VIR_FREE(tmp); + /*Closing the "fake" disk*/ + rc = newMedium->vtbl->Close(newMedium); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to close the new medium"), rc); + goto cleanup; + } + } + } else { + for (it = 0; it < def->dom->ndisks; it++) { + char *uuidRO = NULL; + char **searchResultTab = NULL; + ssize_t resultSize = 0; + char *tmp = NULL; + uuidRO = hardDiskUuidByLocation(snapshotMachineDesc, + def->dom->disks[it]->src.path); + if (!uuidRO) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("No such disk in media registry %s"), + def->dom->disks[it]->src.path); + goto cleanup; + } + + resultSize = virStringSearch(snapshotMachineDesc->storageController, + VBOX_UUID_REGEX, + it + 1, + &searchResultTab); + if (resultSize != it + 1) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to find UUID %s"), + searchResultTab[it]); + goto cleanup; + } + + tmp = virStringReplace(snapshotMachineDesc->storageController, + searchResultTab[it], + uuidRO); + virStringFreeList(searchResultTab); + VIR_FREE(snapshotMachineDesc->storageController); + if (!tmp) + goto cleanup; + if (VIR_STRDUP(snapshotMachineDesc->storageController, tmp) < 0) + goto cleanup; + + VIR_FREE(tmp); + } + } + } + /*We remove the read write disks from the media registry*/ + for (it = 0; it < def->ndisks; it++) { + char *uuidRW = hardDiskUuidByLocation(snapshotMachineDesc, def->disks[it].src.path); + if (!uuidRW) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to find UUID for location %s"), def->disks[it].src.path); + goto cleanup; + } + if (removeHardDisk(snapshotMachineDesc->mediaRegistry, uuidRW) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to remove disk from media registry. uuid = %s"), uuidRW); + goto cleanup; + } + } + /*If the parent snapshot is not NULL, we remove the-read only disks from the media registry*/ + if (def->parent != NULL) { + for (it = 0; it < def->dom->ndisks; it++) { + char *uuidRO = hardDiskUuidByLocation(snapshotMachineDesc, def->dom->disks[it]->src.path); + if (!uuidRO) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to find UUID for location %s"), def->dom->disks[it]->src.path); + goto cleanup; + } + if (removeHardDisk(snapshotMachineDesc->mediaRegistry, uuidRO) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to remove disk from media registry. uuid = %s"), uuidRO); + goto cleanup; + } + } + } + rc = machine->vtbl->Unregister(machine, + CleanupMode_DetachAllReturnHardDisksOnly, + &aMediaSize, + &aMedia); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to unregiter machine"), rc); + goto cleanup; + } + VBOX_RELEASE(machine); + for (it = 0; it < aMediaSize; it++) { + IMedium *medium = aMedia[it]; + if (medium) { + PRUnichar *locationUtf16 = NULL; + char *locationUtf8 = NULL; + rc = medium->vtbl->GetLocation(medium, &locationUtf16); + VBOX_UTF16_TO_UTF8(locationUtf16, &locationUtf8); + if (isCurrent && strstr(locationUtf8, "fake") != NULL) { + /*we delete the fake disk because we don't need it anymore*/ + IProgress *progress = NULL; + PRInt32 resultCode = -1; + rc = medium->vtbl->DeleteStorage(medium, &progress); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to delete medium"), rc); + goto cleanup; + } + progress->vtbl->WaitForCompletion(progress, -1); + progress->vtbl->GetResultCode(progress, &resultCode); + if (NS_FAILED(resultCode)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Error while closing medium"), resultCode); + goto cleanup; + } + VBOX_RELEASE(progress); + } else { + /* This a comment from vboxmanage code in the handleUnregisterVM + * function in VBoxManageMisc.cpp : + * Note that the IMachine::Unregister method will return the medium + * reference in a sane order, which means that closing will normally + * succeed, unless there is still another machine which uses the + * medium. No harm done if we ignore the error. */ + rc = medium->vtbl->Close(medium); + } + VBOX_UTF16_FREE(locationUtf16); + VBOX_UTF8_FREE(locationUtf8); + } + } + + /*removing the snapshot*/ + if (removeSnapshot(snapshotMachineDesc, def->name) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to remove snapshot %s"), def->name); + goto cleanup; + } + + if (isCurrent) { + VIR_FREE(snapshotMachineDesc->currentSnapshot); + if (def->parent != NULL) { + vboxSnapshotXmlSnapshotPtr snap = snapshotByName(snapshotMachineDesc->snapshot, def->parent); + if (!snap) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to get the snapshot to remove")); + goto cleanup; + } + if (VIR_STRDUP(snapshotMachineDesc->currentSnapshot, snap->uuid) < 0) + goto cleanup; + } + } + + /*Registering the machine*/ + if (vboxSnapshotSaveVboxFile(snapshotMachineDesc, settingsFilepath) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to serialize the machine description")); + goto cleanup; + } + rc = data->vboxObj->vtbl->OpenMachine(data->vboxObj, + settingsFilePathUtf16, + &machine); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to open Machine"), rc); + goto cleanup; + } + + rc = data->vboxObj->vtbl->RegisterMachine(data->vboxObj, machine); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to register Machine"), rc); + goto cleanup; + } + + ret = 0; + cleanup: + VIR_FREE(def); + VIR_FREE(defXml); + VBOX_RELEASE(machine); + VBOX_UTF16_FREE(settingsFilePathUtf16); + VBOX_UTF8_FREE(settingsFilepath); + VIR_FREE(snapshotMachineDesc); + VBOX_UTF16_FREE(machineNameUtf16); + VBOX_UTF8_FREE(machineName); + VIR_FREE(machineLocationPath); + VIR_FREE(nameTmpUse); + + return ret; +} +#endif static int vboxDomainSnapshotDelete(virDomainSnapshotPtr snapshot, @@ -8361,6 +8797,7 @@ vboxDomainSnapshotDelete(virDomainSnapshotPtr snapshot, IConsole *console = NULL; PRUint32 state; nsresult rc; + vboxArray snapChildren = VBOX_ARRAY_INITIALIZER; virCheckFlags(VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN | VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY, -1); @@ -8388,7 +8825,21 @@ vboxDomainSnapshotDelete(virDomainSnapshotPtr snapshot, *to remove the node concerning the snapshot */ if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY) { - ret = 0; + rc = vboxArrayGet(&snapChildren, snap, snap->vtbl->GetChildren); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("could not get snapshot children")); + goto cleanup; + } + if (snapChildren.count != 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot delete metadata of a snapshot with children")); + goto cleanup; + } else { +#if VBOX_API_VERSION >= 4002000 + ret = vboxDomainSnapshotDeleteMetadataOnly(snapshot); +#endif + } goto cleanup; } -- 1.7.10.4

On Fri, Apr 18, 2014 at 11:51:34AM +0200, Yohan BELLEGUIC wrote:
All snapshots information will be deleted from the vbox XML, but differencing disks will be kept so the user will be able to redefine the snapshot. --- src/vbox/vbox_tmpl.c | 453 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 452 insertions(+), 1 deletion(-)
diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c index f8667f6..c5b5074 100644 --- a/src/vbox/vbox_tmpl.c +++ b/src/vbox/vbox_tmpl.c @@ -8347,7 +8347,443 @@ vboxDomainSnapshotDeleteTree(vboxGlobalData *data, return ret; }
+ + /*Registering the machine*/ + if (vboxSnapshotSaveVboxFile(snapshotMachineDesc, settingsFilepath) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to serialize the machine description")); + goto cleanup; + } + rc = data->vboxObj->vtbl->OpenMachine(data->vboxObj, + settingsFilePathUtf16, + &machine); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to open Machine"), rc);
The ',' should be immediately following the arg, not on a new line
+ goto cleanup; + } + + rc = data->vboxObj->vtbl->RegisterMachine(data->vboxObj, machine); + if (NS_FAILED(rc)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s %x" + , _("Unable to register Machine"), rc);e
Same point. Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

On Fri, Apr 18, 2014 at 11:51:30AM +0200, Yohan BELLEGUIC wrote:
Hello,
This is a new series of patches in order to support undefining and redefining snapshots with VirtualBox 4.2 or higher. These patches are based on Manuel Vives' patches, taking into account Daniel P. Berrange's remarks.
The VirtualBox API provides only high level operations to manipulate snapshots, so it not possible to support flags like VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE and VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY with only API calls. Following an IRC talk with Eric Blake, the decision was made to emulate these behaviours by manipulating directly the .vbox XML files.
The first patch adds extra details in the snapshot XML returned by libvirt. We will need those details in order to redefine the snapshots.
The second patch adds a new API to manipulate the VirtualBox XML file. It provides several structs describing the VirtualBox XML file nodes and functions which can manipulate these structs.
Thanks for doing this, it has made the code alot clearer to follow IMHO and makes it possible todo testing of it too. Overall this series looks like it is alot closer to something that is ready to be merged. From a functional POV, I'd mostly just like to see a test added for the XML parser/formatter as explained in my comments on patch 2. If you see inspiration take a look at any of the existing tests which have 'xml2xml' in their name - they demonstrate the basic concept we use for testing XML parsers. Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
participants (2)
-
Daniel P. Berrange
-
Yohan BELLEGUIC