[PATCH v3] driver: Include source as a flag to virDomainGetHostname

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



There is a lots of possibilities to retrieve hostname information from
domain. Libvirt could use lease information from dnsmasq to get current
hostname too. QEMU supports QEMU-agent but it can use lease source. See
'domifaddr' as an example.

This commit still adds lease options for QEMU. It will get the first
hostname available from domain networks.

This case, QEMU driver has a default section inside switch to keep
compatibility. So, if someone call 'domhostname' without specifying
source, it will get the default option. Some other drivers like LXC and
OpenVZ supports only one type, virCheckFlags will handle this case.

Signed-off-by: Julio Faracco <jcfaracco@xxxxxxxxx>
---
v1-v2: Moving sources into flags.
v2-v3: Applying Michal's suggestions.
---
 docs/manpages/virsh.rst          |  7 ++-
 include/libvirt/libvirt-domain.h |  5 ++
 src/lxc/lxc_driver.c             | 78 ++++++++++++++++++++++++++++++++
 src/qemu/qemu_driver.c           | 76 +++++++++++++++++++++++++++----
 tools/virsh-completer-domain.c   | 17 +++++++
 tools/virsh-completer-domain.h   |  4 ++
 tools/virsh-domain.c             | 26 ++++++++++-
 7 files changed, 201 insertions(+), 12 deletions(-)

diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst
index fea0527caf..ba483d4d00 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/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index e60003978a..666c1875cc 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -4833,6 +4833,11 @@ typedef struct _virTypedParameter virMemoryParameter;
  */
 typedef virMemoryParameter *virMemoryParameterPtr;
 
+typedef enum {
+    VIR_DOMAIN_GET_HOSTNAME_LEASE = (1 << 0), /* Parse DHCP lease file */
+    VIR_DOMAIN_GET_HOSTNAME_AGENT = (1 << 1), /* Query qemu guest agent */
+} virDomainGetHostnameFlags;
+
 typedef enum {
     VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE = 0, /* Parse DHCP lease file */
     VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT = 1, /* Query qemu guest agent */
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 780c6ed4a2..2dac730e70 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -5291,6 +5291,83 @@ lxcDomainGetCPUStats(virDomainPtr dom,
 }
 
 
+static char *
+lxcDomainGetHostname(virDomainPtr dom,
+                     unsigned int flags)
+{
+    virLXCDriverPtr driver = dom->conn->privateData;
+    virDomainObjPtr vm = NULL;
+    char macaddr[VIR_MAC_STRING_BUFLEN];
+    g_autoptr(virConnect) conn = NULL;
+    virNetworkDHCPLeasePtr *leases = NULL;
+    int n_leases;
+    size_t i, j;
+    char *hostname = NULL;
+
+    virCheckFlags(VIR_DOMAIN_GET_HOSTNAME_LEASE, NULL);
+
+    if (!(vm = lxcDomObjFromDomain(dom)))
+        return NULL;
+
+    if (virDomainGetHostnameEnsureACL(dom->conn, vm->def) < 0)
+        goto cleanup;
+
+    if (virLXCDomainObjBeginJob(driver, vm, LXC_JOB_QUERY) < 0)
+        goto cleanup;
+
+    if (virDomainObjCheckActive(vm) < 0)
+        goto endjob;
+
+    if (!(conn = virGetConnectNetwork()))
+        goto endjob;
+
+    for (i = 0; i < vm->def->nnets; i++) {
+        g_autoptr(virNetwork) network = NULL;
+
+        if (vm->def->nets[i]->type != VIR_DOMAIN_NET_TYPE_NETWORK)
+            continue;
+
+        virMacAddrFormat(&(vm->def->nets[i]->mac), macaddr);
+        network = virNetworkLookupByName(conn,
+                                         vm->def->nets[i]->data.network.name);
+
+        if (!network)
+            goto endjob;
+
+        if ((n_leases = virNetworkGetDHCPLeases(network, macaddr,
+                                                &leases, 0)) < 0)
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                           _("There is no available hostname %d"),
+                           flags);
+
+        for (j = 0; j < n_leases; j++) {
+            virNetworkDHCPLeasePtr lease = leases[j];
+            if (lease->hostname) {
+                hostname = g_strdup(lease->hostname);
+
+                for (j = 0; j < n_leases; j++)
+                    virNetworkDHCPLeaseFree(leases[j]);
+
+                VIR_FREE(leases);
+
+                goto endjob;
+            }
+
+            virNetworkDHCPLeaseFree(lease);
+        }
+
+        VIR_FREE(leases);
+    }
+
+ endjob:
+    virLXCDomainObjEndJob(driver, vm);
+
+ cleanup:
+    virDomainObjEndAPI(&vm);
+    return hostname;
+}
+
+
 static int
 lxcNodeGetFreePages(virConnectPtr conn,
                     unsigned int npages,
@@ -5436,6 +5513,7 @@ static virHypervisorDriver lxcHypervisorDriver = {
     .domainSetMetadata = lxcDomainSetMetadata, /* 1.1.3 */
     .domainGetMetadata = lxcDomainGetMetadata, /* 1.1.3 */
     .domainGetCPUStats = lxcDomainGetCPUStats, /* 1.2.2 */
+    .domainGetHostname = lxcDomainGetHostname, /* 6.0.0 */
     .nodeGetMemoryParameters = lxcNodeGetMemoryParameters, /* 0.10.2 */
     .nodeSetMemoryParameters = lxcNodeSetMemoryParameters, /* 0.10.2 */
     .domainSendProcessSignal = lxcDomainSendProcessSignal, /* 1.0.1 */
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index ec8faf384c..d8ed859f70 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -20280,9 +20280,19 @@ qemuDomainGetHostname(virDomainPtr dom,
     virQEMUDriverPtr driver = dom->conn->privateData;
     virDomainObjPtr vm = NULL;
     qemuAgentPtr agent;
+    char macaddr[VIR_MAC_STRING_BUFLEN];
+    g_autoptr(virConnect) conn = NULL;
+    virNetworkDHCPLeasePtr *leases = NULL;
+    int n_leases;
+    size_t i, j;
     char *hostname = NULL;
 
-    virCheckFlags(0, NULL);
+    virCheckFlags(VIR_DOMAIN_GET_HOSTNAME_LEASE |
+                  VIR_DOMAIN_GET_HOSTNAME_AGENT, NULL);
+
+    VIR_EXCLUSIVE_FLAGS_RET(VIR_DOMAIN_GET_HOSTNAME_LEASE,
+                            VIR_DOMAIN_GET_HOSTNAME_AGENT,
+                            NULL);
 
     if (!(vm = qemuDomainObjFromDomain(dom)))
         return NULL;
@@ -20290,18 +20300,66 @@ qemuDomainGetHostname(virDomainPtr dom,
     if (virDomainGetHostnameEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
 
-    if (qemuDomainObjBeginAgentJob(driver, vm, QEMU_AGENT_JOB_QUERY) < 0)
-        goto cleanup;
-
     if (virDomainObjCheckActive(vm) < 0)
         goto endjob;
 
-    if (!qemuDomainAgentAvailable(vm, true))
-        goto endjob;
+    switch (flags) {
+    default:
+    case VIR_DOMAIN_GET_HOSTNAME_AGENT:
+        if (qemuDomainObjBeginAgentJob(driver, vm, QEMU_AGENT_JOB_QUERY) < 0)
+            goto cleanup;
 
-    agent = qemuDomainObjEnterAgent(vm);
-    ignore_value(qemuAgentGetHostname(agent, &hostname));
-    qemuDomainObjExitAgent(vm, agent);
+        if (!qemuDomainAgentAvailable(vm, true))
+            goto endjob;
+
+        agent = qemuDomainObjEnterAgent(vm);
+        ignore_value(qemuAgentGetHostname(agent, &hostname));
+        qemuDomainObjExitAgent(vm, agent);
+
+        break;
+    case VIR_DOMAIN_GET_HOSTNAME_LEASE:
+        if (!(conn = virGetConnectNetwork()))
+            goto endjob;
+
+        for (i = 0; i < vm->def->nnets; i++) {
+            g_autoptr(virNetwork) network = NULL;
+
+            if (vm->def->nets[i]->type != VIR_DOMAIN_NET_TYPE_NETWORK)
+                continue;
+
+            virMacAddrFormat(&(vm->def->nets[i]->mac), macaddr);
+            network = virNetworkLookupByName(conn,
+                                             vm->def->nets[i]->data.network.name);
+
+            if (!network)
+                goto endjob;
+
+            if ((n_leases = virNetworkGetDHCPLeases(network, macaddr,
+                                                    &leases, 0)) < 0)
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                               _("There is no available hostname %d"),
+                               flags);
+
+            for (j = 0; j < n_leases; j++) {
+                virNetworkDHCPLeasePtr lease = leases[j];
+                if (lease->hostname) {
+                    hostname = g_strdup(lease->hostname);
+
+                    for (j = 0; j < n_leases; j++)
+                        virNetworkDHCPLeaseFree(leases[j]);
+
+                    VIR_FREE(leases);
+
+                    goto endjob;
+                }
+
+                virNetworkDHCPLeaseFree(lease);
+            }
+
+            VIR_FREE(leases);
+        }
+        break;
+    }
 
  endjob:
     qemuDomainObjEndAgentJob(vm);
diff --git a/tools/virsh-completer-domain.c b/tools/virsh-completer-domain.c
index 0311ee50d0..67e67d485a 100644
--- a/tools/virsh-completer-domain.c
+++ b/tools/virsh-completer-domain.c
@@ -296,3 +296,20 @@ virshDomainShutdownModeCompleter(vshControl *ctl,
 
     return virshCommaStringListComplete(mode, modes);
 }
+
+
+char **
+virshDomainGetHosnameSourcesCompleter(vshControl *ctl,
+                                      const vshCmd *cmd,
+                                      unsigned int flags)
+{
+    const char *sources[] = {"lease", "agent", NULL};
+    const char *source = NULL;
+
+    virCheckFlags(0, NULL);
+
+    if (vshCommandOptStringQuiet(ctl, cmd, "source", &source) < 0)
+        return NULL;
+
+    return virshCommaStringListComplete(source, sources);
+}
diff --git a/tools/virsh-completer-domain.h b/tools/virsh-completer-domain.h
index 083ab327cc..ad1b37a255 100644
--- a/tools/virsh-completer-domain.h
+++ b/tools/virsh-completer-domain.h
@@ -53,3 +53,7 @@ char ** virshDomainDeviceAliasCompleter(vshControl *ctl,
 char ** virshDomainShutdownModeCompleter(vshControl *ctl,
                                          const vshCmd *cmd,
                                          unsigned int flags);
+
+char ** virshDomainGetHosnameSourcesCompleter(vshControl *ctl,
+                                              const vshCmd *cmd,
+                                              unsigned int flags);
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 9d4cdd26dd..73e6780116 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -11743,6 +11743,11 @@ 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 = virshDomainGetHosnameSourcesCompleter,
+     .help = N_("address source: 'lease' or 'agent'")},
     {.name = NULL}
 };
 
@@ -11752,21 +11757,38 @@ 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) {
+        if (STREQ(sourcestr, "lease")) {
+            flags |= VIR_DOMAIN_GET_HOSTNAME_LEASE;
+        } else if (STREQ(sourcestr, "agent")) {
+            flags |= VIR_DOMAIN_GET_HOSTNAME_AGENT;
+        } else {
+            vshError(ctl, _("Unknown data source '%s'"), sourcestr);
+            goto error;
+        }
+    }
+
+    hostname = virDomainGetHostname(dom, flags);
     if (hostname == NULL) {
         vshError(ctl, "%s", _("failed to get hostname"));
         goto error;
     }
 
     vshPrint(ctl, "%s\n", hostname);
+
+    VIR_FREE(hostname);
     ret = true;
 
  error:
-    VIR_FREE(hostname);
     virshDomainFree(dom);
     return ret;
 }
-- 
2.20.1


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




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux