[libvirt] [PATCH v3 05/21] LXC from native: implement no network conversion

Cédric Bosdonnat cbosdonnat at suse.com
Wed Feb 5 14:10:03 UTC 2014


If no network configuration is provided, LXC only provides the loopback
interface. To match this, we need to use the privnet feature. LXC will
also define a 'none' network type in its 1.0.0 version that fits
libvirt LXC driver's default.
---
 src/lxc/lxc_native.c                               | 55 ++++++++++++++++++++++
 .../lxcconf2xmldata/lxcconf2xml-nonenetwork.config |  4 ++
 tests/lxcconf2xmldata/lxcconf2xml-nonenetwork.xml  | 21 +++++++++
 tests/lxcconf2xmldata/lxcconf2xml-nonetwork.config |  3 ++
 tests/lxcconf2xmldata/lxcconf2xml-nonetwork.xml    | 24 ++++++++++
 tests/lxcconf2xmltest.c                            |  2 +
 6 files changed, 109 insertions(+)
 create mode 100644 tests/lxcconf2xmldata/lxcconf2xml-nonenetwork.config
 create mode 100644 tests/lxcconf2xmldata/lxcconf2xml-nonenetwork.xml
 create mode 100644 tests/lxcconf2xmldata/lxcconf2xml-nonetwork.config
 create mode 100644 tests/lxcconf2xmldata/lxcconf2xml-nonetwork.xml

diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c
index acd68db..bc14d69 100644
--- a/src/lxc/lxc_native.c
+++ b/src/lxc/lxc_native.c
@@ -325,6 +325,57 @@ lxcFstabWalkCallback(const char* name, virConfValuePtr value, void * data)
     return ret;
 }
 
+typedef struct {
+    char *type;
+    bool privnet;
+    size_t networks;
+} lxcNetworkParseData;
+
+static int
+lxcNetworkWalkCallback(const char *name, virConfValuePtr value, void *data)
+{
+    lxcNetworkParseData *parseData = data;
+
+    if (STREQ(name, "lxc.network.type")) {
+        if (parseData->type != NULL && STREQ(parseData->type, "none"))
+            parseData->privnet = false;
+        else if ((parseData->type != NULL) &&
+                        STRNEQ(parseData->type, "empty") &&
+                        STRNEQ(parseData->type, "")) {
+            parseData->networks++;
+        }
+
+        /* Start a new network interface config */
+        parseData->type = NULL;
+
+        /* Keep the new value */
+        parseData->type = value->str;
+    }
+    return 0;
+}
+
+static int
+lxcConvertNetworkSettings(virDomainDefPtr def, virConfPtr properties)
+{
+    lxcNetworkParseData data = {NULL, true, 0};
+
+    virConfWalk(properties, lxcNetworkWalkCallback, &data);
+
+    if ((data.type != NULL) && STREQ(data.type, "none"))
+        data.privnet = false;
+    else if ((data.type != NULL) && STRNEQ(data.type, "empty") &&
+                    STRNEQ(data.type, "")) {
+        data.networks++;
+    }
+
+    if (data.networks == 0 && data.privnet) {
+        /* When no network type is provided LXC only adds loopback */
+        def->features[VIR_DOMAIN_FEATURE_PRIVNET] = VIR_DOMAIN_FEATURE_STATE_ON;
+    }
+
+    return 0;
+}
+
 virDomainDefPtr
 lxcParseConfigString(const char *config)
 {
@@ -385,6 +436,10 @@ lxcParseConfigString(const char *config)
     if (virConfWalk(properties, lxcFstabWalkCallback, vmdef) < 0)
         goto error;
 
+    /* Network configuration */
+    if (lxcConvertNetworkSettings(vmdef, properties) < 0)
+        goto error;
+
     goto cleanup;
 
 error:
diff --git a/tests/lxcconf2xmldata/lxcconf2xml-nonenetwork.config b/tests/lxcconf2xmldata/lxcconf2xml-nonenetwork.config
new file mode 100644
index 0000000..c1bb9a6
--- /dev/null
+++ b/tests/lxcconf2xmldata/lxcconf2xml-nonenetwork.config
@@ -0,0 +1,4 @@
+lxc.rootfs = /var/lib/lxc/migrate_test/rootfs
+lxc.utsname = migrate_test
+lxc.autodev=1
+lxc.network.type = none
diff --git a/tests/lxcconf2xmldata/lxcconf2xml-nonenetwork.xml b/tests/lxcconf2xmldata/lxcconf2xml-nonenetwork.xml
new file mode 100644
index 0000000..eebcb4e
--- /dev/null
+++ b/tests/lxcconf2xmldata/lxcconf2xml-nonenetwork.xml
@@ -0,0 +1,21 @@
+<domain type='lxc'>
+  <name>migrate_test</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory unit='KiB'>65536</memory>
+  <currentMemory unit='KiB'>0</currentMemory>
+  <vcpu placement='static' current='0'>1</vcpu>
+  <os>
+    <type>exe</type>
+    <init>/sbin/init</init>
+  </os>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <filesystem type='mount' accessmode='passthrough'>
+      <source dir='/var/lib/lxc/migrate_test/rootfs'/>
+      <target dir='/'/>
+    </filesystem>
+  </devices>
+</domain>
diff --git a/tests/lxcconf2xmldata/lxcconf2xml-nonetwork.config b/tests/lxcconf2xmldata/lxcconf2xml-nonetwork.config
new file mode 100644
index 0000000..b6fbe1d
--- /dev/null
+++ b/tests/lxcconf2xmldata/lxcconf2xml-nonetwork.config
@@ -0,0 +1,3 @@
+lxc.rootfs = /var/lib/lxc/migrate_test/rootfs
+lxc.utsname = migrate_test
+lxc.autodev=1
diff --git a/tests/lxcconf2xmldata/lxcconf2xml-nonetwork.xml b/tests/lxcconf2xmldata/lxcconf2xml-nonetwork.xml
new file mode 100644
index 0000000..511e3dd
--- /dev/null
+++ b/tests/lxcconf2xmldata/lxcconf2xml-nonetwork.xml
@@ -0,0 +1,24 @@
+<domain type='lxc'>
+  <name>migrate_test</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory unit='KiB'>65536</memory>
+  <currentMemory unit='KiB'>0</currentMemory>
+  <vcpu placement='static' current='0'>1</vcpu>
+  <os>
+    <type>exe</type>
+    <init>/sbin/init</init>
+  </os>
+  <features>
+    <privnet/>
+  </features>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <filesystem type='mount' accessmode='passthrough'>
+      <source dir='/var/lib/lxc/migrate_test/rootfs'/>
+      <target dir='/'/>
+    </filesystem>
+  </devices>
+</domain>
diff --git a/tests/lxcconf2xmltest.c b/tests/lxcconf2xmltest.c
index c4b21ec..50041a5 100644
--- a/tests/lxcconf2xmltest.c
+++ b/tests/lxcconf2xmltest.c
@@ -104,6 +104,8 @@ mymain(void)
 
     DO_TEST("simple", false);
     DO_TEST("fstab", true);
+    DO_TEST("nonetwork", false);
+    DO_TEST("nonenetwork", false);
 
     return ret;
 }
-- 
1.8.5.2




More information about the libvir-list mailing list