Re: Extra options to dnsmasq

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

 



tir, 22 04 2008 kl. 17:50 +0200, skrev PATTE, Mathieu:
> Is there any way I can pass extra options to dnsmasq when starting a
> virtual network? I need that in order to specify static dhcp leases.
> (is there another way to do that?)

Actually, I wrote a patch for doing static hosts some time ago, but I
haven't had the time to clean it up properly.
It should apply cleanly to 0.4.0, but it shouldn't be hard to adopt to
CVS head.

In XML it works by specifying a stanza such as:
  <ip address="192.168.122.1" netmask="255.255.255.0">
    <dhcp>
      <range start="192.168.122.4" end="192.168.122.254" />
      <statichost mac="00:16:3E:XX:XX:XX" host="XXX"
ip="192.168.122.2" />
      <statichost host="YYY" ip="192.168.122.3" />
    </dhcp>
  </ip>

The patch is attached, please let me know if you have any questions :-)

-- 
Mads Chr. Olesen <shiyee@xxxxxxxxx>
shiyee.dk
Index: libvirt-0.4.0/src/qemu_conf.c
===================================================================
--- libvirt-0.4.0.orig/src/qemu_conf.c	2008-02-14 19:36:51.000000000 +0100
+++ libvirt-0.4.0/src/qemu_conf.c	2008-02-14 21:40:37.000000000 +0100
@@ -2120,6 +2120,12 @@
         free(range);
         range = next;
     }
+    struct qemud_dhcp_statichost_def *statichost = def->statichosts;
+    while (statichost) {
+        struct qemud_dhcp_statichost_def *next = statichost->next;
+        free(statichost);
+        statichost = next;
+    }
     free(def);
 }
 
@@ -2172,40 +2178,83 @@
     cur = node->children;
     while (cur != NULL) {
         struct qemud_dhcp_range_def *range;
-        xmlChar *start, *end;
+        struct qemud_dhcp_statichost_def *statichost;
+        xmlChar *start, *end, *mac, *host, *staticip;
 
-        if (cur->type != XML_ELEMENT_NODE ||
-            !xmlStrEqual(cur->name, BAD_CAST "range")) {
+        if (cur->type != XML_ELEMENT_NODE) {
             cur = cur->next;
             continue;
         }
 
-        if (!(range = calloc(1, sizeof(*range)))) {
-            qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "range");
-            return 0;
-        }
+        if (xmlStrEqual(cur->name, BAD_CAST "range")) {
+            if (!(range = calloc(1, sizeof(*range)))) {
+                qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "range");
+                return 0;
+            }
 
-        start = xmlGetProp(cur, BAD_CAST "start");
-        end = xmlGetProp(cur, BAD_CAST "end");
+            start = xmlGetProp(cur, BAD_CAST "start");
+            end = xmlGetProp(cur, BAD_CAST "end");
 
-        if (start && start[0] && end && end[0]) {
-            strncpy(range->start, (const char *)start, BR_INET_ADDR_MAXLEN-1);
-            range->start[BR_INET_ADDR_MAXLEN-1] = '\0';
-
-            strncpy(range->end, (const char *)end, BR_INET_ADDR_MAXLEN-1);
-            range->end[BR_INET_ADDR_MAXLEN-1] = '\0';
-
-            range->next = def->ranges;
-            def->ranges = range;
-            def->nranges++;
-        } else {
-            free(range);
-        }
+            if (start && start[0] && end && end[0]) {
+                strncpy(range->start, (const char *)start, BR_INET_ADDR_MAXLEN-1);
+                range->start[BR_INET_ADDR_MAXLEN-1] = '\0';
+
+                strncpy(range->end, (const char *)end, BR_INET_ADDR_MAXLEN-1);
+                range->end[BR_INET_ADDR_MAXLEN-1] = '\0';
+
+                range->next = def->ranges;
+                def->ranges = range;
+                def->nranges++;
+            } else {
+                free(range);
+            }
+
+            if (start)
+                xmlFree(start);
+            if (end)
+                xmlFree(end);
+        }
+        else if (xmlStrEqual(cur->name, BAD_CAST "statichost")) {
+            if (!(statichost = calloc(1, sizeof(*statichost)))) {
+                qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "statichost");
+                return 0;
+            }
+
+            mac = xmlGetProp(cur, BAD_CAST "mac");
+            host = xmlGetProp(cur, BAD_CAST "host");
+            staticip = xmlGetProp(cur, BAD_CAST "ip");
+
+            if (((mac && mac[0]) || (host && host[0])) && staticip && staticip[0]) {
+                if (mac && mac[0]) {
+                    strncpy(statichost->mac, (const char *)mac, 18-1);
+                    statichost->mac[18-1] = '\0';
+                } else {
+                    statichost->mac[0] = '\0';
+                }
+                if (host && host[0]) {
+                    strncpy(statichost->host, (const char *)host, QEMUD_MAX_NAME_LEN-1);
+                    statichost->host[QEMUD_MAX_NAME_LEN-1] = '\0';
+                } else {
+                    statichost->host[0] = '\0';
+                }
+
+                strncpy(statichost->staticip, (const char *)staticip, BR_INET_ADDR_MAXLEN-1);
+                statichost->staticip[BR_INET_ADDR_MAXLEN-1] = '\0';
+
+                statichost->next = def->statichosts;
+                def->statichosts = statichost;
+                def->nstatichosts++;
+            } else {
+                free(statichost);
+            }
 
-        if (start)
-            xmlFree(start);
-        if (end)
-            xmlFree(end);
+            if (mac)
+                xmlFree(mac);
+            if (host)
+                xmlFree(host);
+            if (staticip)
+                xmlFree(staticip);
+        }
 
         cur = cur->next;
     }
@@ -3012,6 +3061,7 @@
 
         if (def->ranges) {
             struct qemud_dhcp_range_def *range = def->ranges;
+            struct qemud_dhcp_statichost_def *statichost = def->statichosts;
             if (virBufferAdd(buf, "    <dhcp>\n", -1) < 0)
                 goto no_memory;
             while (range) {
@@ -3020,6 +3070,25 @@
                     goto no_memory;
                 range = range->next;
             }
+            while (statichost) {
+                if (virBufferAdd(buf, "      <statichost", -1) < 0)
+                    goto no_memory;
+
+                if (statichost->mac && virBufferVSprintf(buf, " mac='%s'", statichost->mac) < 0)
+                    goto no_memory;
+
+                if (statichost->host && virBufferVSprintf(buf, " host='%s'", statichost->host) < 0)
+                    goto no_memory;
+
+                if (virBufferVSprintf(buf, " ip='%s'", statichost->staticip) < 0)
+                    goto no_memory;
+
+                if (virBufferAdd(buf, " />\n", -1) < 0)
+                    goto no_memory;
+
+                statichost = statichost->next;
+            }
+
             if (virBufferAdd(buf, "    </dhcp>\n", -1) < 0)
                 goto no_memory;
         }
Index: libvirt-0.4.0/src/qemu_conf.h
===================================================================
--- libvirt-0.4.0.orig/src/qemu_conf.h	2008-02-14 20:09:25.000000000 +0100
+++ libvirt-0.4.0/src/qemu_conf.h	2008-02-14 20:26:25.000000000 +0100
@@ -254,6 +254,15 @@
     struct qemud_dhcp_range_def *next;
 };
 
+/* Store mac address, hostname and ip address of a static dhcp host mapping */
+struct qemud_dhcp_statichost_def {
+    char mac[18]; /* "01:23:45:67:89:0a" */
+    char host[QEMUD_MAX_NAME_LEN];
+    char staticip[BR_INET_ADDR_MAXLEN];
+
+    struct qemud_dhcp_statichost_def *next;
+};
+
 /* Virtual Network main configuration */
 struct qemud_network_def {
     unsigned char uuid[VIR_UUID_BUFLEN];
@@ -272,6 +281,8 @@
 
     int nranges;
     struct qemud_dhcp_range_def *ranges;
+    int nstatichosts;
+    struct qemud_dhcp_statichost_def *statichosts;
 };
 
 /* Virtual Network runtime state */
Index: libvirt-0.4.0/src/qemu_driver.c
===================================================================
--- libvirt-0.4.0.orig/src/qemu_driver.c	2008-02-14 19:36:24.000000000 +0100
+++ libvirt-0.4.0/src/qemu_driver.c	2008-02-14 21:35:32.000000000 +0100
@@ -823,6 +823,7 @@
     int i, len;
     char buf[PATH_MAX];
     struct qemud_dhcp_range_def *range;
+    struct qemud_dhcp_statichost_def *statichost;
 
     len =
         1 + /* dnsmasq */
@@ -836,6 +837,7 @@
         2 + /* --listen-address 10.0.0.1 */
         1 + /* --dhcp-leasefile=path */
         (2 * network->def->nranges) + /* --dhcp-range 10.0.0.2,10.0.0.254 */
+        (2 * network->def->nstatichosts) + /* --dhcp-host 01:23:45:67:89:0a,hostname,10.0.0.3 */
         1;  /* NULL */
 
     if (!(*argv = calloc(len, sizeof(**argv))))
@@ -899,6 +901,25 @@
         range = range->next;
     }
 
+    statichost = network->def->statichosts;
+    while (statichost) {
+        buf[0] = '\0';
+        if (statichost->mac && statichost->mac[0] && statichost->host && statichost->host[0])
+            snprintf(buf, sizeof(buf), "%s,%s,%s",
+                     statichost->mac, statichost->host, statichost->staticip);
+        else if (statichost->mac && statichost->mac[0])
+            snprintf(buf, sizeof(buf), "%s,%s",
+                     statichost->mac, statichost->staticip);
+        else if (statichost->host && statichost->host[0])
+            snprintf(buf, sizeof(buf), "%s,%s",
+                     statichost->host, statichost->staticip);
+
+        APPEND_ARG(*argv, i++, "--dhcp-host");
+        APPEND_ARG(*argv, i++, buf);
+
+        statichost = statichost->next;
+    }
+
 #undef APPEND_ARG
 
     return 0;
--
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]