Re: [libvirt] [PATCH v8 3/4] vbox_tmpl.c: Patch for redefining snapshots

2014-06-11 Thread John Ferlan

Another coverity error as listed in-line below in vboxSnapshotRedefine()


John

On 05/19/2014 08:47 AM, 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 |  976 
 +-
  1 file changed, 970 insertions(+), 6 deletions(-)
 
 diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c
 index 5d4a7ba..a7f15d4 100644
 --- a/src/vbox/vbox_tmpl.c
 +++ b/src/vbox/vbox_tmpl.c
 @@ -43,6 +43,7 @@
  #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
 @@ -6015,6 +6016,958 @@ 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,
 +   _(Unable to open HardDisk, rc=%08x),
 +   (unsigned)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) {
 +virReportError(VIR_ERR_INTERNAL_ERROR, %s
 +   , _(Unable to close disk children));
 +goto cleanup;
 +}
 +VIR_FREE(childLocation);
 +}
 +}
 +rc = medium-vtbl-Close(medium);
 +if (NS_FAILED(rc)) {
 +virReportError(VIR_ERR_INTERNAL_ERROR,
 +   _(Unable to close HardDisk, rc=%08x),
 +   (unsigned)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 = 

Re: [libvirt] [PATCH v8 3/4] vbox_tmpl.c: Patch for redefining snapshots

2014-06-10 Thread Daniel P. Berrange
On Mon, May 19, 2014 at 02:47:32PM +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 |  976 
 +-
  1 file changed, 970 insertions(+), 6 deletions(-)

ACK


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 :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v8 3/4] vbox_tmpl.c: Patch for redefining snapshots

2014-05-19 Thread Yohan BELLEGUIC
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 |  976 +-
 1 file changed, 970 insertions(+), 6 deletions(-)

diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c
index 5d4a7ba..a7f15d4 100644
--- a/src/vbox/vbox_tmpl.c
+++ b/src/vbox/vbox_tmpl.c
@@ -43,6 +43,7 @@
 #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
@@ -6015,6 +6016,958 @@ 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,
+   _(Unable to open HardDisk, rc=%08x),
+   (unsigned)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) {
+virReportError(VIR_ERR_INTERNAL_ERROR, %s
+   , _(Unable to close disk children));
+goto cleanup;
+}
+VIR_FREE(childLocation);
+}
+}
+rc = medium-vtbl-Close(medium);
+if (NS_FAILED(rc)) {
+virReportError(VIR_ERR_INTERNAL_ERROR,
+   _(Unable to close HardDisk, rc=%08x),
+   (unsigned)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;
+virVBoxSnapshotConfMachinePtr snapshotMachineDesc = NULL;
+char *currentSnapshotXmlFilePath = NULL;
+