[libvirt] [libvirt-glib] Ability to create new domains

From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org> --- libvirt-gobject/libvirt-gobject-connection.c | 38 ++++++++++++++++++++++++++ libvirt-gobject/libvirt-gobject-connection.h | 3 ++ libvirt-gobject/libvirt-gobject.sym | 1 + 3 files changed, 42 insertions(+), 0 deletions(-) diff --git a/libvirt-gobject/libvirt-gobject-connection.c b/libvirt-gobject/libvirt-gobject-connection.c index 32f749c..072484e 100644 --- a/libvirt-gobject/libvirt-gobject-connection.c +++ b/libvirt-gobject/libvirt-gobject-connection.c @@ -720,3 +720,41 @@ GVirStream *gvir_connection_get_stream(GVirConnection *self, return klass->stream_new(self, st); } + +/** + * gvir_connection_create_domain: + * @conn: the connection on which to create the dmain + * @conf: the configuration for the new domain + * Returns: (transfer full): the newly created domain + */ +GVirDomain *gvir_connection_create_domain(GVirConnection *conn, + GVirConfigDomain *conf, + GError **err) +{ + const gchar *xml; + virDomainPtr handle; + GVirConnectionPrivate *priv = conn->priv; + + xml = gvir_config_object_get_doc(GVIR_CONFIG_OBJECT(conf)); + + g_return_val_if_fail(xml != NULL, NULL); + + if (!(handle = virDomainDefineXML(priv->conn, xml))) { + *err = gvir_error_new_literal(GVIR_CONNECTION_ERROR, + 0, + "Failed to create domain"); + return NULL; + } + + GVirDomain *domain; + + domain = GVIR_DOMAIN(g_object_new(GVIR_TYPE_DOMAIN, + "handle", handle, + NULL)); + + g_hash_table_insert(priv->domains, + g_strdup(gvir_domain_get_uuid(domain)), + domain); + + return domain; +} diff --git a/libvirt-gobject/libvirt-gobject-connection.h b/libvirt-gobject/libvirt-gobject-connection.h index 0172d02..c453bed 100644 --- a/libvirt-gobject/libvirt-gobject-connection.h +++ b/libvirt-gobject/libvirt-gobject-connection.h @@ -107,6 +107,9 @@ GVirDomain *gvir_connection_find_domain_by_id(GVirConnection *conn, GVirDomain *gvir_connection_find_domain_by_name(GVirConnection *conn, const gchar *name); +GVirDomain *gvir_connection_create_domain(GVirConnection *conn, + GVirConfigDomain *conf, + GError **err); #if 0 GList *gvir_connection_get_interfaces(GVirConnection *conn); diff --git a/libvirt-gobject/libvirt-gobject.sym b/libvirt-gobject/libvirt-gobject.sym index a68db4b..eae40a2 100644 --- a/libvirt-gobject/libvirt-gobject.sym +++ b/libvirt-gobject/libvirt-gobject.sym @@ -18,6 +18,7 @@ LIBVIRT_GOBJECT_0.0.1 { gvir_connection_get_domain; gvir_connection_find_domain_by_id; gvir_connection_find_domain_by_name; + gvir_connection_create_domain; gvir_domain_get_type; gvir_domain_handle_get_type; -- 1.7.6.2

On Mon, Sep 26, 2011 at 09:29:33PM +0300, Zeeshan Ali (Khattak) wrote:
From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org>
--- libvirt-gobject/libvirt-gobject-connection.c | 38 ++++++++++++++++++++++++++ libvirt-gobject/libvirt-gobject-connection.h | 3 ++ libvirt-gobject/libvirt-gobject.sym | 1 + 3 files changed, 42 insertions(+), 0 deletions(-)
diff --git a/libvirt-gobject/libvirt-gobject-connection.c b/libvirt-gobject/libvirt-gobject-connection.c index 32f749c..072484e 100644 --- a/libvirt-gobject/libvirt-gobject-connection.c +++ b/libvirt-gobject/libvirt-gobject-connection.c @@ -720,3 +720,41 @@ GVirStream *gvir_connection_get_stream(GVirConnection *self,
return klass->stream_new(self, st); } + +/** + * gvir_connection_create_domain: + * @conn: the connection on which to create the dmain + * @conf: the configuration for the new domain + * Returns: (transfer full): the newly created domain + */ +GVirDomain *gvir_connection_create_domain(GVirConnection *conn, + GVirConfigDomain *conf, + GError **err) +{ + const gchar *xml; + virDomainPtr handle; + GVirConnectionPrivate *priv = conn->priv; + + xml = gvir_config_object_get_doc(GVIR_CONFIG_OBJECT(conf)); + + g_return_val_if_fail(xml != NULL, NULL); + + if (!(handle = virDomainDefineXML(priv->conn, xml))) { + *err = gvir_error_new_literal(GVIR_CONNECTION_ERROR, + 0, + "Failed to create domain"); + return NULL; + } + + GVirDomain *domain; + + domain = GVIR_DOMAIN(g_object_new(GVIR_TYPE_DOMAIN, + "handle", handle, + NULL)); + + g_hash_table_insert(priv->domains, + g_strdup(gvir_domain_get_uuid(domain)), + domain);
Since the return value is annotated '(transfer full)' you need todo 'g_object_ref(domain)' before putting it into the hash table, since the hash needs to keep hold of a reference. Also you want to have a g_mutex_lock/unlock either side of use of the hash table. 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 Tue, Sep 27, 2011 at 11:56 AM, Daniel P. Berrange <berrange@redhat.com> wrote:
Since the return value is annotated '(transfer full)' you need todo 'g_object_ref(domain)' before putting it into the hash table, since the hash needs to keep hold of a reference. Also you want to have a g_mutex_lock/unlock either side of use of the hash table.
K, ACK otherwise? -- Regards, Zeeshan Ali (Khattak) FSF member#5124

On Tue, Sep 27, 2011 at 02:59:11PM +0300, Zeeshan Ali (Khattak) wrote:
On Tue, Sep 27, 2011 at 11:56 AM, Daniel P. Berrange <berrange@redhat.com> wrote:
Since the return value is annotated '(transfer full)' you need todo 'g_object_ref(domain)' before putting it into the hash table, since the hash needs to keep hold of a reference. Also you want to have a g_mutex_lock/unlock either side of use of the hash table.
K, ACK otherwise?
Yep. 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
-
Zeeshan Ali (Khattak)