From 0f814c0fed420209ccb881325b854beaa7c70fcf Mon Sep 17 00:00:00 2001 From: Julio Faracco Date: Fri, 27 Dec 2019 17:36:25 -0300 Subject: [PATCH] virsh: Expose virDomainGetHostnameFlags Our virsh already has 'domhostname' command. Add '--source' argument to it so that users can chose between 'lease' and 'agent' sources. Also, implement completer for the argument. Signed-off-by: Julio Faracco Signed-off-by: Michal Privoznik Reviewed-by: Erik Skultety --- docs/manpages/virsh.rst | 7 ++++++- tools/virsh-completer-domain.c | 19 +++++++++++++++++ tools/virsh-completer-domain.h | 4 ++++ tools/virsh-domain.c | 37 +++++++++++++++++++++++++++++++++- tools/virsh-domain.h | 8 ++++++++ 5 files changed, 73 insertions(+), 2 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index c637caa583..ef15c10e02 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -1797,10 +1797,15 @@ domhostname .. code-block:: - domhostname domain + domhostname domain [--source lease|agent] Returns the hostname of a domain, if the hypervisor makes it available. +The *--source* argument specifies what data source to use for the +hostnames, currently 'lease' to read DHCP leases or 'agent' to query +the guest OS via an agent. If unspecified, driver returns the default +method available (some drivers support only one type of source). + domid ----- diff --git a/tools/virsh-completer-domain.c b/tools/virsh-completer-domain.c index 6da603048e..4472ee08f2 100644 --- a/tools/virsh-completer-domain.c +++ b/tools/virsh-completer-domain.c @@ -316,3 +316,22 @@ virshDomainInterfaceAddrSourceCompleter(vshControl *ctl G_GNUC_UNUSED, return ret; } + + +char ** +virshDomainHostnameSourceCompleter(vshControl *ctl G_GNUC_UNUSED, + const vshCmd *cmd G_GNUC_UNUSED, + unsigned int flags) +{ + char **ret = NULL; + size_t i; + + virCheckFlags(0, NULL); + + ret = g_new0(typeof(*ret), VIRSH_DOMAIN_HOSTNAME_SOURCE_LAST + 1); + + for (i = 0; i < VIRSH_DOMAIN_HOSTNAME_SOURCE_LAST; i++) + ret[i] = g_strdup(virshDomainHostnameSourceTypeToString(i)); + + return ret; +} diff --git a/tools/virsh-completer-domain.h b/tools/virsh-completer-domain.h index 79beec2cfe..b00b05e3bd 100644 --- a/tools/virsh-completer-domain.h +++ b/tools/virsh-completer-domain.h @@ -58,3 +58,7 @@ char ** virshDomainInterfaceAddrSourceCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); + +char ** virshDomainHostnameSourceCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 358067dce4..ac8ac33582 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -11742,20 +11742,55 @@ static const vshCmdInfo info_domhostname[] = { static const vshCmdOptDef opts_domhostname[] = { VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE), + {.name = "source", + .type = VSH_OT_STRING, + .flags = VSH_OFLAG_NONE, + .completer = virshDomainHostnameSourceCompleter, + .help = N_("address source: 'lease' or 'agent'")}, {.name = NULL} }; +VIR_ENUM_IMPL(virshDomainHostnameSource, + VIRSH_DOMAIN_HOSTNAME_SOURCE_LAST, + "agent", + "lease"); + static bool cmdDomHostname(vshControl *ctl, const vshCmd *cmd) { char *hostname; virDomainPtr dom; bool ret = false; + const char *sourcestr = NULL; + int flags = 0; /* Use default value. Drivers can have its own default. */ if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) return false; - hostname = virDomainGetHostname(dom, 0); + if (vshCommandOptStringReq(ctl, cmd, "source", &sourcestr) < 0) + goto error; + + if (sourcestr) { + int source = virshDomainHostnameSourceTypeFromString(sourcestr); + + if (source < 0) { + vshError(ctl, _("Unknown data source '%s'"), sourcestr); + goto error; + } + + switch ((virshDomainHostnameSource) source) { + case VIRSH_DOMAIN_HOSTNAME_SOURCE_AGENT: + flags |= VIR_DOMAIN_GET_HOSTNAME_AGENT; + break; + case VIRSH_DOMAIN_HOSTNAME_SOURCE_LEASE: + flags |= VIR_DOMAIN_GET_HOSTNAME_LEASE; + break; + case VIRSH_DOMAIN_HOSTNAME_SOURCE_LAST: + break; + } + } + + hostname = virDomainGetHostname(dom, flags); if (hostname == NULL) { vshError(ctl, "%s", _("failed to get hostname")); goto error; diff --git a/tools/virsh-domain.h b/tools/virsh-domain.h index 02996d51b1..0d59c579d4 100644 --- a/tools/virsh-domain.h +++ b/tools/virsh-domain.h @@ -30,4 +30,12 @@ typedef struct virshDomainEventCallback virshDomainEventCallback; extern virshDomainEventCallback virshDomainEventCallbacks[]; +typedef enum { + VIRSH_DOMAIN_HOSTNAME_SOURCE_AGENT, + VIRSH_DOMAIN_HOSTNAME_SOURCE_LEASE, + VIRSH_DOMAIN_HOSTNAME_SOURCE_LAST +} virshDomainHostnameSource; + +VIR_ENUM_DECL(virshDomainHostnameSource); + extern const vshCmdDef domManagementCmds[];