[libvirt] [PATCHv2 0/5] Allow to query a guest's hostname

The following patches allow to query a guest's hostname. Once we also have support to set the hostname via the domain's xml we can also print it in "virsh dominfo" by default so I left it out for now. I left the version at 0.9.14. It'd be happy to change this ot 0.10 should we settle for that one. Changes since last time: * drop patch to print hostname in "virsh list" * add remote protocoll support * Addressed Eric's style comments * openvzVEGetStringParam: Let virCommandRun do the error printing and missing error handling * Don't return empty hostnames but raise VIR_ERR_OPERATION_FAILED instead Cheers, -- Guido Guido Günther (5): Add virDomainGetHostname virsh: Add domhostname remote: Provide RPC call for domainGetHostname openvz: Add openvzVEGetStringParam openvz: Implement domainGetHostname include/libvirt/libvirt.h.in | 2 ++ src/driver.h | 6 ++++++ src/libvirt.c | 45 ++++++++++++++++++++++++++++++++++++++++++ src/libvirt_openvz.syms | 2 +- src/libvirt_public.syms | 5 +++++ src/openvz/openvz_driver.c | 42 +++++++++++++++++++++++++++++++++++++++ src/openvz/openvz_util.c | 32 ++++++++++++++++++++++++++++++ src/openvz/openvz_util.h | 1 + src/remote/remote_driver.c | 1 + src/remote/remote_protocol.x | 12 ++++++++++- src/remote_protocol-structs | 8 ++++++++ tools/virsh.c | 44 +++++++++++++++++++++++++++++++++++++++++ tools/virsh.pod | 4 ++++ 13 files changed, 202 insertions(+), 2 deletions(-) -- 1.7.10.4

to query a guests's hostname. Containers like LXC and OpenVZ allow to set a hostname different from the hosts name and QEMU's guest agent could provide similar functionality. --- include/libvirt/libvirt.h.in | 2 ++ src/driver.h | 6 ++++++ src/libvirt.c | 45 ++++++++++++++++++++++++++++++++++++++++++ src/libvirt_public.syms | 5 +++++ 4 files changed, 58 insertions(+) diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in index e34438c..fcef461 100644 --- a/include/libvirt/libvirt.h.in +++ b/include/libvirt/libvirt.h.in @@ -1540,6 +1540,8 @@ int virDomainSetMemoryFlags (virDomainPtr domain, int virDomainGetMaxVcpus (virDomainPtr domain); int virDomainGetSecurityLabel (virDomainPtr domain, virSecurityLabelPtr seclabel); +char * virDomainGetHostname (virDomainPtr domain, + unsigned int flags); typedef enum { VIR_DOMAIN_METADATA_DESCRIPTION = 0, /* Operate on <description> */ diff --git a/src/driver.h b/src/driver.h index b3c1740..46d9846 100644 --- a/src/driver.h +++ b/src/driver.h @@ -142,6 +142,11 @@ typedef int unsigned int flags); typedef char * (*virDrvDomainGetOSType) (virDomainPtr domain); + +typedef char * + (*virDrvDomainGetHostname) (virDomainPtr domain, + unsigned int flags); + typedef unsigned long long (*virDrvDomainGetMaxMemory) (virDomainPtr domain); typedef int @@ -904,6 +909,7 @@ struct _virDriver { virDrvDomainDestroy domainDestroy; virDrvDomainDestroyFlags domainDestroyFlags; virDrvDomainGetOSType domainGetOSType; + virDrvDomainGetHostname domainGetHostname; virDrvDomainGetMaxMemory domainGetMaxMemory; virDrvDomainSetMaxMemory domainSetMaxMemory; virDrvDomainSetMemory domainSetMemory; diff --git a/src/libvirt.c b/src/libvirt.c index df78e8a..8315b4f 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -18977,3 +18977,48 @@ error: virDispatchError(dom->conn); return -1; } + +/** + * virDomainGetHostname: + * @domain: a domain object + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * Get the hostname for that domain. + * + * Dependent on hypervisor used, this may require a guest agent to be + * available. + * + * Returns the hostname which must be freed by the caller, or + * NULL if there was an error. + */ +char * +virDomainGetHostname(virDomainPtr domain, unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain); + + virResetLastError(); + + if (!VIR_IS_CONNECTED_DOMAIN(domain)) { + virLibDomainError(VIR_ERR_INVALID_DOMAIN, __FUNCTION__); + virDispatchError(NULL); + return NULL; + } + + conn = domain->conn; + + if (conn->driver->domainGetHostname) { + char *ret; + ret = conn->driver->domainGetHostname (domain, flags); + if (!ret) + goto error; + return ret; + } + + virLibConnError(VIR_ERR_NO_SUPPORT, __FUNCTION__); + +error: + virDispatchError(domain->conn); + return NULL; +} diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms index 2913a81..1a8e58a 100644 --- a/src/libvirt_public.syms +++ b/src/libvirt_public.syms @@ -544,4 +544,9 @@ LIBVIRT_0.9.13 { virDomainSnapshotRef; } LIBVIRT_0.9.11; +LIBVIRT_0.9.14 { + global: + virDomainGetHostname; +} LIBVIRT_0.9.13; + # .... define new API here using predicted next version number .... -- 1.7.10.4

On Sun, Jul 15, 2012 at 11:45:05PM +0200, Guido Günther wrote:
to query a guests's hostname. Containers like LXC and OpenVZ allow to set a hostname different from the hosts name and QEMU's guest agent could provide similar functionality. --- include/libvirt/libvirt.h.in | 2 ++ src/driver.h | 6 ++++++ src/libvirt.c | 45 ++++++++++++++++++++++++++++++++++++++++++ src/libvirt_public.syms | 5 +++++ 4 files changed, 58 insertions(+)
ACK 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 Wed, Jul 18, 2012 at 09:19:05PM +0100, Daniel P. Berrange wrote:
On Sun, Jul 15, 2012 at 11:45:05PM +0200, Guido Günther wrote:
to query a guests's hostname. Containers like LXC and OpenVZ allow to set a hostname different from the hosts name and QEMU's guest agent could provide similar functionality. --- include/libvirt/libvirt.h.in | 2 ++ src/driver.h | 6 ++++++ src/libvirt.c | 45 ++++++++++++++++++++++++++++++++++++++++++ src/libvirt_public.syms | 5 +++++ 4 files changed, 58 insertions(+)
ACK I've applied the whole series with your suggestions. Thanks, -- Guido

to query the guest's hostname. --- tools/virsh.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ tools/virsh.pod | 4 ++++ 2 files changed, 48 insertions(+) diff --git a/tools/virsh.c b/tools/virsh.c index f9fe4b1..7c20202 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -14133,6 +14133,49 @@ cmdTTYConsole(vshControl *ctl, const vshCmd *cmd) } /* + * "domhostname" command + */ +static const vshCmdInfo info_domhostname[] = { + {"help", N_("print the domain's hostname")}, + {"desc", ""}, + {NULL, NULL} +}; + +static const vshCmdOptDef opts_domhostname[] = { + {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")}, + {NULL, 0, 0, NULL} +}; + +static bool +cmdDomHostname(vshControl *ctl, const vshCmd *cmd) +{ + char *hostname; + virDomainPtr dom; + bool ret = false; + + if (!vshConnectionUsability(ctl, ctl->conn)) + return false; + + if (!(dom = vshCommandOptDomain(ctl, cmd, NULL))) + return false; + + hostname = virDomainGetHostname(dom, 0); + if (hostname == NULL) { + vshError(ctl, "%s", _("failed to get hostname")); + goto error; + } + + vshPrint(ctl, "%s\n", hostname); + ret = true; + +error: + VIR_FREE(hostname); + virDomainFree(dom); + return ret; +} + + +/* * "attach-device" command */ static const vshCmdInfo info_attach_device[] = { @@ -18171,6 +18214,7 @@ static const vshCmdDef domManagementCmds[] = { {"detach-interface", cmdDetachInterface, opts_detach_interface, info_detach_interface, 0}, {"domdisplay", cmdDomDisplay, opts_domdisplay, info_domdisplay, 0}, + {"domhostname", cmdDomHostname, opts_domhostname, info_domhostname, 0}, {"domid", cmdDomid, opts_domid, info_domid, 0}, {"domif-setlink", cmdDomIfSetLink, opts_domif_setlink, info_domif_setlink, 0}, {"domiftune", cmdDomIftune, opts_domiftune, info_domiftune, 0}, diff --git a/tools/virsh.pod b/tools/virsh.pod index 4bddf15..dc318ef 100644 --- a/tools/virsh.pod +++ b/tools/virsh.pod @@ -826,6 +826,10 @@ Output a URI which can be used to connect to the graphical display of the domain via VNC, SPICE or RDP. If I<--include-password> is specified, the SPICE channel password will be included in the URI. +=item B<domhostname> I<domain-id> + +Returns the hostname of a domain. + =item B<dominfo> I<domain-id> Returns basic information about the domain. -- 1.7.10.4

On Sun, Jul 15, 2012 at 11:45:06PM +0200, Guido Günther wrote:
to query the guest's hostname. --- tools/virsh.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ tools/virsh.pod | 4 ++++ 2 files changed, 48 insertions(+)
diff --git a/tools/virsh.c b/tools/virsh.c index f9fe4b1..7c20202 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -14133,6 +14133,49 @@ cmdTTYConsole(vshControl *ctl, const vshCmd *cmd) }
/* + * "domhostname" command + */ +static const vshCmdInfo info_domhostname[] = { + {"help", N_("print the domain's hostname")}, + {"desc", ""}, + {NULL, NULL} +}; + +static const vshCmdOptDef opts_domhostname[] = { + {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")}, + {NULL, 0, 0, NULL} +}; + +static bool +cmdDomHostname(vshControl *ctl, const vshCmd *cmd) +{ + char *hostname; + virDomainPtr dom; + bool ret = false; + + if (!vshConnectionUsability(ctl, ctl->conn)) + return false; + + if (!(dom = vshCommandOptDomain(ctl, cmd, NULL))) + return false; + + hostname = virDomainGetHostname(dom, 0); + if (hostname == NULL) { + vshError(ctl, "%s", _("failed to get hostname")); + goto error; + } + + vshPrint(ctl, "%s\n", hostname); + ret = true; + +error: + VIR_FREE(hostname); + virDomainFree(dom); + return ret; +} + + +/* * "attach-device" command */ static const vshCmdInfo info_attach_device[] = { @@ -18171,6 +18214,7 @@ static const vshCmdDef domManagementCmds[] = { {"detach-interface", cmdDetachInterface, opts_detach_interface, info_detach_interface, 0}, {"domdisplay", cmdDomDisplay, opts_domdisplay, info_domdisplay, 0}, + {"domhostname", cmdDomHostname, opts_domhostname, info_domhostname, 0}, {"domid", cmdDomid, opts_domid, info_domid, 0}, {"domif-setlink", cmdDomIfSetLink, opts_domif_setlink, info_domif_setlink, 0}, {"domiftune", cmdDomIftune, opts_domiftune, info_domiftune, 0}, diff --git a/tools/virsh.pod b/tools/virsh.pod index 4bddf15..dc318ef 100644 --- a/tools/virsh.pod +++ b/tools/virsh.pod @@ -826,6 +826,10 @@ Output a URI which can be used to connect to the graphical display of the domain via VNC, SPICE or RDP. If I<--include-password> is specified, the SPICE channel password will be included in the URI.
+=item B<domhostname> I<domain-id> + +Returns the hostname of a domain. + =item B<dominfo> I<domain-id>
Returns basic information about the domain.
ACK 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 07/18/2012 02:19 PM, Daniel P. Berrange wrote:
On Sun, Jul 15, 2012 at 11:45:06PM +0200, Guido Günther wrote:
to query the guest's hostname. ---
+++ b/tools/virsh.pod @@ -826,6 +826,10 @@ Output a URI which can be used to connect to the graphical display of the domain via VNC, SPICE or RDP. If I<--include-password> is specified, the SPICE channel password will be included in the URI.
+=item B<domhostname> I<domain-id> + +Returns the hostname of a domain. +
Might be worth mentioning that not all hypervisors support this information. -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On Wed, Jul 18, 2012 at 02:24:17PM -0600, Eric Blake wrote:
On 07/18/2012 02:19 PM, Daniel P. Berrange wrote:
On Sun, Jul 15, 2012 at 11:45:06PM +0200, Guido Günther wrote:
to query the guest's hostname. ---
+++ b/tools/virsh.pod @@ -826,6 +826,10 @@ Output a URI which can be used to connect to the graphical display of the domain via VNC, SPICE or RDP. If I<--include-password> is specified, the SPICE channel password will be included in the URI.
+=item B<domhostname> I<domain-id> + +Returns the hostname of a domain. +
Might be worth mentioning that not all hypervisors support this information.
Yep, how about just changing that to "Returns the hostname of a domain, if the hypervisor makes it available" 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 :|

--- src/remote/remote_driver.c | 1 + src/remote/remote_protocol.x | 12 +++++++++++- src/remote_protocol-structs | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 3314f80..5d1ce4d 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -5335,6 +5335,7 @@ static virDriver remote_driver = { .domainGetDiskErrors = remoteDomainGetDiskErrors, /* 0.9.10 */ .domainSetMetadata = remoteDomainSetMetadata, /* 0.9.10 */ .domainGetMetadata = remoteDomainGetMetadata, /* 0.9.10 */ + .domainGetHostname = remoteDomainGetHostname, /* 0.9.14 */ }; static virNetworkDriver network_driver = { diff --git a/src/remote/remote_protocol.x b/src/remote/remote_protocol.x index 8f1d9b5..dd460d4 100644 --- a/src/remote/remote_protocol.x +++ b/src/remote/remote_protocol.x @@ -1221,6 +1221,15 @@ struct remote_domain_get_cpu_stats_ret { int nparams; }; +struct remote_domain_get_hostname_args { + remote_nonnull_domain dom; + unsigned int flags; +}; + +struct remote_domain_get_hostname_ret { + remote_nonnull_string hostname; +}; + /* Network calls: */ struct remote_num_of_networks_ret { @@ -2844,7 +2853,8 @@ enum remote_procedure { REMOTE_PROC_CONNECT_LIST_ALL_DOMAINS = 273, /* skipgen skipgen priority:high */ REMOTE_PROC_DOMAIN_LIST_ALL_SNAPSHOTS = 274, /* skipgen skipgen priority:high */ REMOTE_PROC_DOMAIN_SNAPSHOT_LIST_ALL_CHILDREN = 275, /* skipgen skipgen priority:high */ - REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE = 276 /* autogen autogen */ + REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE = 276, /* autogen autogen */ + REMOTE_PROC_DOMAIN_GET_HOSTNAME = 277 /* autogen autogen */ /* * Notice how the entries are grouped in sets of 10 ? diff --git a/src/remote_protocol-structs b/src/remote_protocol-structs index 511284c..8d09138 100644 --- a/src/remote_protocol-structs +++ b/src/remote_protocol-structs @@ -881,6 +881,13 @@ struct remote_domain_get_cpu_stats_ret { } params; int nparams; }; +struct remote_domain_get_hostname_args { + remote_nonnull_domain dom; + u_int flags; +}; +struct remote_domain_get_hostname_ret { + remote_nonnull_string hostname; +}; struct remote_num_of_networks_ret { int num; }; @@ -2251,4 +2258,5 @@ enum remote_procedure { REMOTE_PROC_DOMAIN_LIST_ALL_SNAPSHOTS = 274, REMOTE_PROC_DOMAIN_SNAPSHOT_LIST_ALL_CHILDREN = 275, REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE = 276, + REMOTE_PROC_DOMAIN_GET_HOSTNAME = 277, }; -- 1.7.10.4

On Sun, Jul 15, 2012 at 11:45:07PM +0200, Guido Günther wrote:
--- src/remote/remote_driver.c | 1 + src/remote/remote_protocol.x | 12 +++++++++++- src/remote_protocol-structs | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 3314f80..5d1ce4d 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -5335,6 +5335,7 @@ static virDriver remote_driver = { .domainGetDiskErrors = remoteDomainGetDiskErrors, /* 0.9.10 */ .domainSetMetadata = remoteDomainSetMetadata, /* 0.9.10 */ .domainGetMetadata = remoteDomainGetMetadata, /* 0.9.10 */ + .domainGetHostname = remoteDomainGetHostname, /* 0.9.14 */ };
static virNetworkDriver network_driver = { diff --git a/src/remote/remote_protocol.x b/src/remote/remote_protocol.x index 8f1d9b5..dd460d4 100644 --- a/src/remote/remote_protocol.x +++ b/src/remote/remote_protocol.x @@ -1221,6 +1221,15 @@ struct remote_domain_get_cpu_stats_ret { int nparams; };
+struct remote_domain_get_hostname_args { + remote_nonnull_domain dom; + unsigned int flags; +}; + +struct remote_domain_get_hostname_ret { + remote_nonnull_string hostname; +}; + /* Network calls: */
struct remote_num_of_networks_ret { @@ -2844,7 +2853,8 @@ enum remote_procedure { REMOTE_PROC_CONNECT_LIST_ALL_DOMAINS = 273, /* skipgen skipgen priority:high */ REMOTE_PROC_DOMAIN_LIST_ALL_SNAPSHOTS = 274, /* skipgen skipgen priority:high */ REMOTE_PROC_DOMAIN_SNAPSHOT_LIST_ALL_CHILDREN = 275, /* skipgen skipgen priority:high */ - REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE = 276 /* autogen autogen */ + REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE = 276, /* autogen autogen */ + REMOTE_PROC_DOMAIN_GET_HOSTNAME = 277 /* autogen autogen */
/* * Notice how the entries are grouped in sets of 10 ? diff --git a/src/remote_protocol-structs b/src/remote_protocol-structs index 511284c..8d09138 100644 --- a/src/remote_protocol-structs +++ b/src/remote_protocol-structs @@ -881,6 +881,13 @@ struct remote_domain_get_cpu_stats_ret { } params; int nparams; }; +struct remote_domain_get_hostname_args { + remote_nonnull_domain dom; + u_int flags; +}; +struct remote_domain_get_hostname_ret { + remote_nonnull_string hostname; +}; struct remote_num_of_networks_ret { int num; }; @@ -2251,4 +2258,5 @@ enum remote_procedure { REMOTE_PROC_DOMAIN_LIST_ALL_SNAPSHOTS = 274, REMOTE_PROC_DOMAIN_SNAPSHOT_LIST_ALL_CHILDREN = 275, REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE = 276, + REMOTE_PROC_DOMAIN_GET_HOSTNAME = 277, };
ACK 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 :|

to retrieve a VEs config parameters as a single string. This will be used by the upcoming domainGetHostname implementation. --- src/libvirt_openvz.syms | 2 +- src/openvz/openvz_util.c | 32 ++++++++++++++++++++++++++++++++ src/openvz/openvz_util.h | 1 + 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/libvirt_openvz.syms b/src/libvirt_openvz.syms index 11c5587..1993eb5 100644 --- a/src/libvirt_openvz.syms +++ b/src/libvirt_openvz.syms @@ -1,7 +1,7 @@ # # These symbols are dependent upon --with-openvz via WITH_OPENVZ # - openvzReadConfigParam; openvzReadNetworkConf; openvzLocateConfFile; +openvzVEGetStringParam; diff --git a/src/openvz/openvz_util.c b/src/openvz/openvz_util.c index 61b55de..2091b8e 100644 --- a/src/openvz/openvz_util.c +++ b/src/openvz/openvz_util.c @@ -26,6 +26,9 @@ #include "internal.h" #include "virterror_internal.h" +#include "command.h" +#include "datatypes.h" +#include "memory.h" #include "openvz_conf.h" #include "openvz_util.h" @@ -49,3 +52,32 @@ openvzKBPerPages(void) } return kb_per_pages; } + +char* +openvzVEGetStringParam(virDomainPtr domain, const char* param) +{ + int len; + char *output = NULL; + + virCommandPtr cmd = virCommandNewArgList(VZLIST, + "-o", + param, + domain->name, + "-H" , NULL); + + virCommandSetOutputBuffer(cmd, &output); + if (virCommandRun(cmd, NULL) < 0) { + VIR_FREE(output); + /* virCommandRun sets the virError */ + goto out; + } + + /* delete trailing newline */ + len = strlen(output); + if (len && output[len - 1] == '\n') + output[len - 1] = '\0'; + +out: + virCommandFree(cmd); + return output; +} diff --git a/src/openvz/openvz_util.h b/src/openvz/openvz_util.h index a0d9bbb..6a991f3 100644 --- a/src/openvz/openvz_util.h +++ b/src/openvz/openvz_util.h @@ -24,5 +24,6 @@ # define OPENVZ_UTIL_H long openvzKBPerPages(void); +char* openvzVEGetStringParam(virDomainPtr dom, const char *param); #endif -- 1.7.10.4

On Sun, Jul 15, 2012 at 11:45:08PM +0200, Guido Günther wrote:
to retrieve a VEs config parameters as a single string. This will be used by the upcoming domainGetHostname implementation. --- src/libvirt_openvz.syms | 2 +- src/openvz/openvz_util.c | 32 ++++++++++++++++++++++++++++++++ src/openvz/openvz_util.h | 1 + 3 files changed, 34 insertions(+), 1 deletion(-)
diff --git a/src/libvirt_openvz.syms b/src/libvirt_openvz.syms index 11c5587..1993eb5 100644 --- a/src/libvirt_openvz.syms +++ b/src/libvirt_openvz.syms @@ -1,7 +1,7 @@ # # These symbols are dependent upon --with-openvz via WITH_OPENVZ # - openvzReadConfigParam; openvzReadNetworkConf; openvzLocateConfFile; +openvzVEGetStringParam; diff --git a/src/openvz/openvz_util.c b/src/openvz/openvz_util.c index 61b55de..2091b8e 100644 --- a/src/openvz/openvz_util.c +++ b/src/openvz/openvz_util.c @@ -26,6 +26,9 @@ #include "internal.h"
#include "virterror_internal.h" +#include "command.h" +#include "datatypes.h" +#include "memory.h"
#include "openvz_conf.h" #include "openvz_util.h" @@ -49,3 +52,32 @@ openvzKBPerPages(void) } return kb_per_pages; } + +char* +openvzVEGetStringParam(virDomainPtr domain, const char* param) +{ + int len; + char *output = NULL; + + virCommandPtr cmd = virCommandNewArgList(VZLIST, + "-o", + param, + domain->name, + "-H" , NULL); + + virCommandSetOutputBuffer(cmd, &output); + if (virCommandRun(cmd, NULL) < 0) { + VIR_FREE(output); + /* virCommandRun sets the virError */ + goto out; + } + + /* delete trailing newline */ + len = strlen(output); + if (len && output[len - 1] == '\n') + output[len - 1] = '\0'; + +out:
s/out/cleanup/ in this function to follow the standard naming convention we use.
+ virCommandFree(cmd); + return output; +} diff --git a/src/openvz/openvz_util.h b/src/openvz/openvz_util.h index a0d9bbb..6a991f3 100644 --- a/src/openvz/openvz_util.h +++ b/src/openvz/openvz_util.h @@ -24,5 +24,6 @@ # define OPENVZ_UTIL_H
long openvzKBPerPages(void); +char* openvzVEGetStringParam(virDomainPtr dom, const char *param);
s/char* /char */
#endif
ACK if you fix the issues above when you commit 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 :|

--- src/openvz/openvz_driver.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c index c9150e0..6046a60 100644 --- a/src/openvz/openvz_driver.c +++ b/src/openvz/openvz_driver.c @@ -234,6 +234,47 @@ cleanup: } +static char * +openvzDomainGetHostname(virDomainPtr dom, unsigned int flags) +{ + char *hostname = NULL; + struct openvz_driver *driver = dom->conn->privateData; + virDomainObjPtr vm; + + virCheckFlags(0, NULL); + + openvzDriverLock(driver); + vm = virDomainFindByUUID(&driver->domains, dom->uuid); + openvzDriverUnlock(driver); + + if (!vm) { + openvzError(VIR_ERR_NO_DOMAIN, "%s", + _("no domain with matching uuid")); + goto cleanup; + } + + hostname = openvzVEGetStringParam(dom, "hostname"); + if (hostname == NULL) + goto error; + + /* vzlist prints an unset hostname as '-' */ + if (STREQ(hostname, "-")) { + openvzError(VIR_ERR_OPERATION_FAILED, + _("Hostname of '%s' is unset"), vm->def->name); + goto error; + } + +cleanup: + if (vm) + virDomainObjUnlock(vm); + return hostname; + +error: + VIR_FREE(hostname); + goto cleanup; +} + + static virDomainPtr openvzDomainLookupByID(virConnectPtr conn, int id) { struct openvz_driver *driver = conn->privateData; @@ -2127,6 +2168,7 @@ static virDriver openvzDriver = { .domainIsUpdated = openvzDomainIsUpdated, /* 0.8.6 */ .isAlive = openvzIsAlive, /* 0.9.8 */ .domainUpdateDeviceFlags = openvzDomainUpdateDeviceFlags, /* 0.9.13 */ + .domainGetHostname = openvzDomainGetHostname, /* 0.9.14 */ }; int openvzRegister(void) { -- 1.7.10.4

On Sun, Jul 15, 2012 at 11:45:09PM +0200, Guido Günther wrote:
--- src/openvz/openvz_driver.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)
diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c index c9150e0..6046a60 100644 --- a/src/openvz/openvz_driver.c +++ b/src/openvz/openvz_driver.c @@ -234,6 +234,47 @@ cleanup: }
+static char * +openvzDomainGetHostname(virDomainPtr dom, unsigned int flags) +{ + char *hostname = NULL; + struct openvz_driver *driver = dom->conn->privateData; + virDomainObjPtr vm; + + virCheckFlags(0, NULL); + + openvzDriverLock(driver); + vm = virDomainFindByUUID(&driver->domains, dom->uuid); + openvzDriverUnlock(driver); + + if (!vm) { + openvzError(VIR_ERR_NO_DOMAIN, "%s", + _("no domain with matching uuid")); + goto cleanup; + } + + hostname = openvzVEGetStringParam(dom, "hostname"); + if (hostname == NULL) + goto error; + + /* vzlist prints an unset hostname as '-' */ + if (STREQ(hostname, "-")) { + openvzError(VIR_ERR_OPERATION_FAILED, + _("Hostname of '%s' is unset"), vm->def->name); + goto error; + } + +cleanup: + if (vm) + virDomainObjUnlock(vm); + return hostname; + +error: + VIR_FREE(hostname); + goto cleanup; +} + + static virDomainPtr openvzDomainLookupByID(virConnectPtr conn, int id) { struct openvz_driver *driver = conn->privateData; @@ -2127,6 +2168,7 @@ static virDriver openvzDriver = { .domainIsUpdated = openvzDomainIsUpdated, /* 0.8.6 */ .isAlive = openvzIsAlive, /* 0.9.8 */ .domainUpdateDeviceFlags = openvzDomainUpdateDeviceFlags, /* 0.9.13 */ + .domainGetHostname = openvzDomainGetHostname, /* 0.9.14 */ };
int openvzRegister(void) {
ACK 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 (3)
-
Daniel P. Berrange
-
Eric Blake
-
Guido Günther