[libvirt] [PATCH 05/16] LXC from native: migrate veth network configuration

Cédric Bosdonnat cbosdonnat at suse.com
Tue Jan 14 13:49:54 UTC 2014


Some of the LXC configuration properties aren't migrated since they
would only cause problems in libvirt-lxc:
  lxc.network.ipv[46]: LXC driver doesn't setup IP address of guests
  lxc.network.name
---
 src/lxc/lxc_native.c                         | 98 ++++++++++++++++++++++++++--
 tests/lxcconf2xmldata/lxcconf2xml-simple.xml |  5 ++
 2 files changed, 96 insertions(+), 7 deletions(-)

diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c
index 6b62a5b..a9ef453 100644
--- a/src/lxc/lxc_native.c
+++ b/src/lxc/lxc_native.c
@@ -517,35 +517,119 @@ error:
     return -1;
 }
 
+static virDomainNetDefPtr
+lxcCreateNetDef(const char *type,
+                const char *link,
+                const char *mac,
+                const char *flag)
+{
+    virDomainNetDefPtr net = NULL;
+
+    if (VIR_ALLOC(net) < 0)
+        goto error;
+
+    if (flag) {
+        if (STREQ(flag, "up"))
+            net->linkstate = VIR_DOMAIN_NET_INTERFACE_LINK_STATE_UP;
+        else
+            net->linkstate = VIR_DOMAIN_NET_INTERFACE_LINK_STATE_DOWN;
+    }
+
+    if (STREQ(type, "veth")) {
+        virMacAddr macAddr;
+
+        if (!link)
+            goto error;
+
+        net->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
+
+        if (VIR_STRDUP(net->data.bridge.brname, link) < 0)
+            goto error;
+
+        if (mac && virMacAddrParse(mac, &macAddr) == 0)
+            net->mac = macAddr;
+
+    }
+
+    return net;
+
+error:
+    virDomainNetDefFree(net);
+    return NULL;
+}
+
+static int
+lxcAddNetworkDefinition(virDomainDefPtr def,
+                        const char *type,
+                        const char *link,
+                        const char *mac,
+                        const char *flag)
+{
+    virDomainNetDefPtr net = NULL;
+
+    if ((type == NULL) || STREQ(type, "empty") || STREQ(type, ""))
+        return 0;
+
+    if (!(net = lxcCreateNetDef(type, link, mac, flag)))
+        goto error;
+
+    if (VIR_EXPAND_N(def->nets, def->nnets, 1) < 0)
+        goto error;
+    def->nets[def->nnets - 1] = net;
+
+    return 1;
+
+error:
+    virDomainNetDefFree(net);
+    return -1;
+}
+
 static int
 lxcConvertNetworkSettings(virDomainDefPtr def, virPropertiesPtr properties)
 {
     virPropertyEntryPtr property = NULL;
     char *type = NULL;
+    char *link = NULL;
+    char *mac = NULL;
+    char *flag = NULL;
     bool nonetwork = true;
+    int status;
 
     if (properties) {
         for (property = properties->elements;
                 property;
                 property = property->next) {
             if (STREQ(property->key, "lxc.network.type")) {
-                if ((type != NULL) && STRNEQ(type, "empty") &&
-                                STRNEQ(type, "")) {
+                /* Store the previous NIC */
+                status = lxcAddNetworkDefinition(def, type, link, mac, flag);
+                if (status < 0)
+                    return -1;
+                else if (status > 0)
                     nonetwork = false;
-                }
 
                 /* Start a new network interface config */
                 type = NULL;
+                link = NULL;
+                mac = NULL;
+                flag = NULL;
 
                 /* Keep the new value */
                 type = property->value;
             }
+            else if (STREQ(property->key, "lxc.network.link"))
+                link = property->value;
+            else if (STREQ(property->key, "lxc.network.hwaddr"))
+                mac = property->value;
+            else if (STREQ(property->key, "lxc.network.flags"))
+                flag = property->value;
         }
-    }
 
-    if ((type != NULL) && STRNEQ(type, "empty") &&
-                    STRNEQ(type, "")) {
-        nonetwork = false;
+        /* Add the last network definition found */
+        status = lxcAddNetworkDefinition(def, type, link, mac, flag);
+        if (status < 0)
+            return -1;
+        else if (status > 0)
+            nonetwork = false;
     }
 
     if (nonetwork) {
diff --git a/tests/lxcconf2xmldata/lxcconf2xml-simple.xml b/tests/lxcconf2xmldata/lxcconf2xml-simple.xml
index 8f2d442..de249ea 100644
--- a/tests/lxcconf2xmldata/lxcconf2xml-simple.xml
+++ b/tests/lxcconf2xmldata/lxcconf2xml-simple.xml
@@ -26,5 +26,10 @@
       <source usage='2017885' units='KiB'/>
       <target dir='/run'/>
     </filesystem>
+    <interface type='bridge'>
+      <mac address='02:00:15:8f:05:c1'/>
+      <source bridge='virbr0'/>
+      <link state='up'/>
+    </interface>
   </devices>
 </domain>
-- 
1.8.5.2




More information about the libvir-list mailing list