[libvirt PATCH 6/7] network: Make virtual domains resolvable from the host

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

 



This patch adds a new attribute "register" to the <domain> element. If
set to "yes", the DNS server created for the virtual network is
registered with systemd-resolved as a name server for the associated
domain. The names known to the dnsmasq process serving DNS and DHCP
requests for the virtual network will then be resolvable from the host
by appending the domain name to them.

Signed-off-by: Jiri Denemark <jdenemar@xxxxxxxxxx>
---
 docs/formatnetwork.rst       |  9 ++++++++-
 src/conf/network_conf.c      | 18 ++++++++++++++++++
 src/conf/network_conf.h      |  1 +
 src/conf/schemas/network.rng |  3 +++
 src/network/bridge_driver.c  | 32 +++++++++++++++++++++++++++++++-
 5 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/docs/formatnetwork.rst b/docs/formatnetwork.rst
index 16e81246fa..dcdaf1e5a5 100644
--- a/docs/formatnetwork.rst
+++ b/docs/formatnetwork.rst
@@ -88,7 +88,7 @@ to the physical LAN (if at all).
    ...
    <bridge name="virbr0" stp="on" delay="5" macTableManager="libvirt"/>
    <mtu size="9000"/>
-   <domain name="example.com" localOnly="no"/>
+   <domain name="example.com" localOnly="no" register="no"/>
    <forward mode="nat" dev="eth0"/>
    ...
 
@@ -162,6 +162,13 @@ to the physical LAN (if at all).
    DNS server. If ``localOnly`` is "no", and by default, unresolved requests
    **will** be forwarded. :since:`Since 1.2.12`
 
+   :since:`Since 10.1.0` the optional ``register`` attribute can be used to
+   request registering the DNS server for resolving this domain with the host's
+   DNS resolver. When set to "yes", the host resolver will forward all requests
+   for domain names from this domain to the DNS server created for this virtual
+   network. To avoid DNS loops ``localOnly`` has to be set to "yes" as well.
+   This feature requires ``systemd-resolved`` to be running on the host.
+
 ``forward``
    Inclusion of the ``forward`` element indicates that the virtual network is to
    be connected to the physical LAN. :since:`Since 0.3.0.` The ``mode``
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index ef3415cd89..cc92ed0b03 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -1582,6 +1582,19 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
                                &def->domainLocalOnly) < 0)
         return NULL;
 
+    if (virXMLPropTristateBool(domain_node, "register",
+                               VIR_XML_PROP_NONE,
+                               &def->domainRegister) < 0)
+        return NULL;
+
+    if (def->domainRegister == VIR_TRISTATE_BOOL_YES &&
+        def->domainLocalOnly != VIR_TRISTATE_BOOL_YES) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("attribute 'register=yes' in <domain> element requires 'localOnly=yes' in network %1$s"),
+                       def->name);
+        return NULL;
+    }
+
     if ((bandwidthNode = virXPathNode("./bandwidth", ctxt)) &&
         virNetDevBandwidthParse(&def->bandwidth, NULL, bandwidthNode, false) < 0)
         return NULL;
@@ -2405,6 +2418,11 @@ virNetworkDefFormatBuf(virBuffer *buf,
             virBufferAsprintf(buf, " localOnly='%s'", local);
         }
 
+        if (def->domainRegister) {
+            virBufferAsprintf(buf, " register='%s'",
+                              virTristateBoolTypeToString(def->domainRegister));
+        }
+
         virBufferAddLit(buf, "/>\n");
     }
 
diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h
index 1d7fd3ab6a..c2a4198abc 100644
--- a/src/conf/network_conf.h
+++ b/src/conf/network_conf.h
@@ -245,6 +245,7 @@ struct _virNetworkDef {
     int  macTableManager; /* enum virNetworkBridgeMACTableManager */
     char *domain;
     virTristateBool domainLocalOnly; /* yes disables dns forwarding */
+    virTristateBool domainRegister;
     unsigned long delay;   /* Bridge forward delay (ms) */
     bool stp; /* Spanning tree protocol */
     unsigned int mtu; /* MTU for bridge, 0 means "default" i.e. unset in config */
diff --git a/src/conf/schemas/network.rng b/src/conf/schemas/network.rng
index e56e07d130..b7c8551fad 100644
--- a/src/conf/schemas/network.rng
+++ b/src/conf/schemas/network.rng
@@ -258,6 +258,9 @@
             <optional>
               <attribute name="localOnly"><ref name="virYesNo"/></attribute>
             </optional>
+            <optional>
+              <attribute name="register"><ref name="virYesNo"/></attribute>
+            </optional>
           </element>
         </optional>
 
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index 9921c7cd14..d89700c6ee 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -63,7 +63,7 @@
 #include "virjson.h"
 #include "virnetworkportdef.h"
 #include "virutil.h"
-
+#include "virsystemd.h"
 #include "netdev_bandwidth_conf.h"
 
 #define VIR_FROM_THIS VIR_FROM_NETWORK
@@ -1902,6 +1902,7 @@ networkStartNetworkVirtual(virNetworkDriverState *driver,
     bool dnsmasqStarted = false;
     bool devOnline = false;
     bool firewalRulesAdded = false;
+    virSocketAddr *dnsServer = NULL;
 
     /* Check to see if any network IP collides with an existing route */
     if (networkCheckRouteCollision(def) < 0)
@@ -1958,6 +1959,9 @@ networkStartNetworkVirtual(virNetworkDriverState *driver,
         if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET6))
             v6present = true;
 
+        if (!dnsServer)
+            dnsServer = &ipdef->address;
+
         /* Add the IP address/netmask to the bridge */
         if (networkAddAddrToBridge(obj, ipdef) < 0)
             goto error;
@@ -2011,6 +2015,32 @@ networkStartNetworkVirtual(virNetworkDriverState *driver,
             goto error;
 
         dnsmasqStarted = true;
+
+        if (def->domain && def->domainRegister && dnsServer) {
+            unsigned int link;
+            int rc;
+
+            if ((link = if_nametoindex(def->bridge)) == 0) {
+                virReportSystemError(ENODEV,
+                                     _("unable to get interface index for %1$s"),
+                                     def->bridge);
+                goto error;
+            }
+
+            rc = virSystemdResolvedRegisterNameServer(link, def->domain,
+                                                      dnsServer);
+            if (rc == -2) {
+                virReportError(VIR_ERR_OPERATION_INVALID, "%s",
+                               _("failed to register name server: systemd-resolved is not available"));
+                goto error;
+            }
+
+            if (rc < 0) {
+                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                               _("failed to register name server"));
+                goto error;
+            }
+        }
     }
 
     if (virNetDevBandwidthSet(def->bridge, def->bandwidth, true, true) < 0)
-- 
2.43.0
_______________________________________________
Devel mailing list -- devel@xxxxxxxxxxxxxxxxx
To unsubscribe send an email to devel-leave@xxxxxxxxxxxxxxxxx




[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