[libvirt] [PATCH 2/2] lxc: Introduce lxcDomainGetHostname

jcfaracco@gmail.com posted 2 patches 7 weeks ago

[libvirt] [PATCH 2/2] lxc: Introduce lxcDomainGetHostname

Posted by jcfaracco@gmail.com 7 weeks ago
From: Julio Faracco <jcfaracco@gmail.com>

Using lease information from dnsmasq, libvirt can retrieve hostname
information. LXC documentation says that hostname can be 'lxc.uts.name',
but if an user runs 'hostnamectl' and change hostname during container
execution, it will reflect the command output. This option could fix
this.

Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
---
 src/lxc/lxc_driver.c | 71 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 71 insertions(+)

diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index ea86451b09..999e74fcc8 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -5317,6 +5317,76 @@ lxcDomainGetCPUStats(virDomainPtr dom,
     return ret;
 }
 
+static char *
+lxcDomainGetHostname(virDomainPtr dom,
+                     unsigned int source,
+                     unsigned int flags)
+{
+    virDomainObjPtr vm = NULL;
+    char macaddr[VIR_MAC_STRING_BUFLEN];
+    g_autoptr(virNetwork) network = NULL;
+    virNetworkDHCPLeasePtr *leases = NULL;
+    int n_leases;
+    size_t i, j;
+    char *hostname = NULL;
+
+    virCheckFlags(0, NULL);
+
+    if (!(vm = lxcDomObjFromDomain(dom)))
+        return NULL;
+
+    if (virDomainGetHostnameEnsureACL(dom->conn, vm->def) < 0)
+        goto cleanup;
+
+    switch (source) {
+    default:
+    case VIR_DOMAIN_HOSTNAME_SRC_LEASE:
+        for (i = 0; i < vm->def->nnets; i++) {
+            if (vm->def->nets[i]->type != VIR_DOMAIN_NET_TYPE_NETWORK)
+                continue;
+
+            virMacAddrFormat(&(vm->def->nets[i]->mac), macaddr);
+            virObjectUnref(network);
+            network = virNetworkLookupByName(dom->conn,
+                                             vm->def->nets[i]->data.network.name);
+
+            if ((n_leases = virNetworkGetDHCPLeases(network, macaddr,
+                                                    &leases, 0)) < 0)
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                               _("There is no available hostname %d"),
+                               source);
+
+            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 cleanup;
+                }
+            }
+
+            for (j = 0; j < n_leases; j++)
+                virNetworkDHCPLeaseFree(leases[j]);
+
+            VIR_FREE(leases);
+        }
+        break;
+    case VIR_DOMAIN_HOSTNAME_SRC_AGENT:
+        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
+                       _("Unknown hostname data source %d"),
+                       source);
+        break;
+    }
+
+ cleanup:
+    virDomainObjEndAPI(&vm);
+    return hostname;
+}
 
 static int
 lxcNodeGetFreePages(virConnectPtr conn,
@@ -5463,6 +5533,7 @@ static virHypervisorDriver lxcHypervisorDriver = {
     .domainSetMetadata = lxcDomainSetMetadata, /* 1.1.3 */
     .domainGetMetadata = lxcDomainGetMetadata, /* 1.1.3 */
     .domainGetCPUStats = lxcDomainGetCPUStats, /* 1.2.2 */
+    .domainGetHostname = lxcDomainGetHostname, /* 5.9.0 */
     .nodeGetMemoryParameters = lxcNodeGetMemoryParameters, /* 0.10.2 */
     .nodeSetMemoryParameters = lxcNodeSetMemoryParameters, /* 0.10.2 */
     .domainSendProcessSignal = lxcDomainSendProcessSignal, /* 1.0.1 */
-- 
2.20.1


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