]> xenbits.xensource.com Git - libvirt.git/commitdiff
network: Generate TFTP config regardless of DHCP
authorMichal Privoznik <mprivozn@redhat.com>
Thu, 9 Dec 2021 15:47:04 +0000 (16:47 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 1 Jun 2022 07:40:40 +0000 (09:40 +0200)
We already allow users to provide TFTP root path in network XML
and not specify any DHCP. This makes sense, because dnsmasq is
not only DHCP server but also TFTP server and users might have
a DHCP server configured on their own, outside of libvirt's
control and want just the TFTP part.

By moving TFTP config generator out of DHCP generator and calling
it for every IPv4 range, users can finally enable just TFTP.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2026765
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/network/bridge_driver.c
tests/networkxml2confdata/netboot-network.conf
tests/networkxml2confdata/netboot-tftp.conf [new file with mode: 0644]
tests/networkxml2confdata/netboot-tftp.xml [new file with mode: 0644]
tests/networkxml2conftest.c
tests/networkxml2xmlin/netboot-tftp.xml [new symlink]
tests/networkxml2xmlout/netboot-tftp.xml [new symlink]
tests/networkxml2xmltest.c

index f087e07c52af69d166bde9851803a2686f058c57..7449c7e02a3a344a382a01ba0040441d2f9e6fd5 100644 (file)
@@ -1088,11 +1088,6 @@ networkDnsmasqConfDHCP(virBuffer *buf,
             virBufferAddLit(buf, "dhcp-authoritative\n");
         }
 
-        if (ipdef->tftproot) {
-            virBufferAddLit(buf, "enable-tftp\n");
-            virBufferAsprintf(buf, "tftp-root=%s\n", ipdef->tftproot);
-        }
-
         if (ipdef->bootfile) {
             if (VIR_SOCKET_ADDR_VALID(&ipdef->bootserver)) {
                 g_autofree char *bootserver = virSocketAddrFormat(&ipdef->bootserver);
@@ -1111,6 +1106,22 @@ networkDnsmasqConfDHCP(virBuffer *buf,
 }
 
 
+static void
+networkDnsmasqConfTFTP(virBuffer *buf,
+                       virNetworkIPDef *ipdef,
+                       bool *enableTFTP)
+{
+    if (!ipdef->tftproot)
+        return;
+
+    if (!*enableTFTP) {
+        virBufferAddLit(buf, "enable-tftp\n");
+        *enableTFTP = true;
+    }
+    virBufferAsprintf(buf, "tftp-root=%s\n", ipdef->tftproot);
+}
+
+
 int
 networkDnsmasqConfContents(virNetworkObj *obj,
                            const char *pidfile,
@@ -1129,6 +1140,7 @@ networkDnsmasqConfContents(virNetworkObj *obj,
     virNetworkIPDef *ipv4def = NULL;
     virNetworkIPDef *ipv6def = NULL;
     bool ipv6SLAAC = false;
+    bool enableTFTP = false;
 
     *configstr = NULL;
 
@@ -1339,6 +1351,8 @@ networkDnsmasqConfContents(virNetworkObj *obj,
                     ipv4def = ipdef;
                 }
             }
+
+            networkDnsmasqConfTFTP(&configbuf, ipdef, &enableTFTP);
         }
         if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET6)) {
             if (ipdef->nranges || ipdef->nhosts) {
@@ -1500,7 +1514,7 @@ networkStartDhcpDaemon(virNetworkDriverState *driver,
     i = 0;
     while ((ipdef = virNetworkDefGetIPByIndex(def, AF_UNSPEC, i))) {
         i++;
-        if (ipdef->nranges || ipdef->nhosts)
+        if (ipdef->nranges || ipdef->nhosts || ipdef->tftproot)
             needDnsmasq = true;
     }
 
@@ -3255,7 +3269,7 @@ networkUpdate(virNetworkPtr net,
     for (i = 0;
          (ipdef = virNetworkDefGetIPByIndex(def, AF_INET, i));
          i++) {
-        if (ipdef->nranges || ipdef->nhosts) {
+        if (ipdef->nranges || ipdef->nhosts || ipdef->tftproot) {
             oldDhcpActive = true;
             break;
         }
@@ -3370,7 +3384,7 @@ networkUpdate(virNetworkPtr net,
 
             for (i = 0; (ipdef = virNetworkDefGetIPByIndex(def, AF_INET, i));
                  i++) {
-                if (ipdef->nranges || ipdef->nhosts) {
+                if (ipdef->nranges || ipdef->nhosts || ipdef->tftproot) {
                     newDhcpActive = true;
                     break;
                 }
index a13239a54f579077f34d9b17bd5cfe362b52907c..32ef25b05f458c0499ecd75503c4b8954378fcfd 100644 (file)
@@ -10,11 +10,11 @@ expand-hosts
 except-interface=lo
 bind-dynamic
 interface=virbr1
+enable-tftp
+tftp-root=/var/lib/tftproot
 dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0
 dhcp-no-override
 dhcp-authoritative
-enable-tftp
-tftp-root=/var/lib/tftproot
 dhcp-boot=pxeboot.img
 dhcp-lease-max=253
 dhcp-hostsfile=/var/lib/libvirt/dnsmasq/netboot.hostsfile
diff --git a/tests/networkxml2confdata/netboot-tftp.conf b/tests/networkxml2confdata/netboot-tftp.conf
new file mode 100644 (file)
index 0000000..45615f3
--- /dev/null
@@ -0,0 +1,13 @@
+##WARNING:  THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
+##OVERWRITTEN AND LOST.  Changes to this configuration should be made using:
+##    virsh net-edit tftp-only
+## or other application using the libvirt API.
+##
+## dnsmasq conf file created by libvirt
+strict-order
+except-interface=lo
+bind-dynamic
+interface=virbr0
+enable-tftp
+tftp-root=/var/lib/tftproot
+addn-hosts=/var/lib/libvirt/dnsmasq/tftp-only.addnhosts
diff --git a/tests/networkxml2confdata/netboot-tftp.xml b/tests/networkxml2confdata/netboot-tftp.xml
new file mode 100644 (file)
index 0000000..297f5a7
--- /dev/null
@@ -0,0 +1,9 @@
+<network>
+  <name>tftp-only</name>
+  <uuid>eb486e5c-4df5-42ee-ae4a-ad8557998d00</uuid>
+  <forward mode='nat'/>
+  <bridge name='virbr0' stp='off' delay='1'/>
+  <ip address='192.168.122.1' netmask='255.255.255.0'>
+    <tftp root='/var/lib/tftproot'/>
+  </ip>
+</network>
index 0bc9e128e3136badb0749bdcdffea8fbed342984..a062ff9c3c6244738a03aa023a6be2e689e4e278 100644 (file)
@@ -168,6 +168,7 @@ mymain(void)
     DO_TEST("isolated-network", full);
     DO_TEST("netboot-network", full);
     DO_TEST("netboot-proxy-network", full);
+    DO_TEST("netboot-tftp", full);
     DO_TEST("nat-network-dns-srv-record-minimal", full);
     DO_TEST("nat-network-name-with-quotes", full);
     DO_TEST("routed-network", full);
diff --git a/tests/networkxml2xmlin/netboot-tftp.xml b/tests/networkxml2xmlin/netboot-tftp.xml
new file mode 120000 (symlink)
index 0000000..1487de5
--- /dev/null
@@ -0,0 +1 @@
+../networkxml2confdata/netboot-tftp.xml
\ No newline at end of file
diff --git a/tests/networkxml2xmlout/netboot-tftp.xml b/tests/networkxml2xmlout/netboot-tftp.xml
new file mode 120000 (symlink)
index 0000000..1487de5
--- /dev/null
@@ -0,0 +1 @@
+../networkxml2confdata/netboot-tftp.xml
\ No newline at end of file
index ca24305ace1ec22e9f7f97124aa7cb937dfceb9c..9e8d675a10bf5e691700f30dbbfe36c999e1eae9 100644 (file)
@@ -127,6 +127,7 @@ mymain(void)
     DO_TEST("nat-network");
     DO_TEST("netboot-network");
     DO_TEST("netboot-proxy-network");
+    DO_TEST("netboot-tftp");
     DO_TEST("nat-network-dns-txt-record");
     DO_TEST("nat-network-dns-srv-record");
     DO_TEST("nat-network-dns-srv-records");