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

[libvirt] [PATCH 04/16] LXC from native: implement no network conversion



If no network configuration is provided, LXC only provides the loopback
interface. To match this, we need to use the privnet feature.
---
 src/lxc/lxc_native.c                               | 43 ++++++++++++++++++++++
 tests/lxcconf2xmldata/lxcconf2xml-nonetwork.config |  5 +++
 tests/lxcconf2xmldata/lxcconf2xml-nonetwork.xml    | 33 +++++++++++++++++
 tests/lxcconf2xmltest.c                            |  1 +
 4 files changed, 82 insertions(+)
 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 9e2e870..6b62a5b 100644
--- a/src/lxc/lxc_native.c
+++ b/src/lxc/lxc_native.c
@@ -517,6 +517,45 @@ error:
     return -1;
 }
 
+static int
+lxcConvertNetworkSettings(virDomainDefPtr def, virPropertiesPtr properties)
+{
+    virPropertyEntryPtr property = NULL;
+    char *type = NULL;
+    bool nonetwork = true;
+
+    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, "")) {
+                    nonetwork = false;
+                }
+
+                /* Start a new network interface config */
+                type = NULL;
+
+                /* Keep the new value */
+                type = property->value;
+            }
+        }
+    }
+
+    if ((type != NULL) && STRNEQ(type, "empty") &&
+                    STRNEQ(type, "")) {
+        nonetwork = false;
+    }
+
+    if (nonetwork) {
+        /* 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,
                      const char *fstab,
@@ -600,6 +639,10 @@ lxcParseConfigString(const char *config,
         fstabIter = fstabIter->next;
     }
 
+    /* Network configuration */
+    if (lxcConvertNetworkSettings(vmdef, properties) < 0)
+        goto error;
+
     goto cleanup;
 
 error:
diff --git a/tests/lxcconf2xmldata/lxcconf2xml-nonetwork.config b/tests/lxcconf2xmldata/lxcconf2xml-nonetwork.config
new file mode 100644
index 0000000..2da2a8f
--- /dev/null
+++ b/tests/lxcconf2xmldata/lxcconf2xml-nonetwork.config
@@ -0,0 +1,5 @@
+lxc.mount.entry = /etc/resolv.conf etc/resolv.conf none bind,ro 0 0
+lxc.rootfs = /var/lib/lxc/migrate_test/rootfs
+lxc.utsname = migrate_test
+lxc.autodev=1
+lxc.mount = /var/lib/lxc/migrate_test/fstab
diff --git a/tests/lxcconf2xmldata/lxcconf2xml-nonetwork.xml b/tests/lxcconf2xmldata/lxcconf2xml-nonetwork.xml
new file mode 100644
index 0000000..d8bc318
--- /dev/null
+++ b/tests/lxcconf2xmldata/lxcconf2xml-nonetwork.xml
@@ -0,0 +1,33 @@
+<domain type='lxc'>
+  <name>migrate_test</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory unit='KiB'>4035770</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>
+    <filesystem type='mount' accessmode='passthrough'>
+      <source dir='/etc/resolv.conf'/>
+      <target dir='/etc/resolv.conf'/>
+      <readonly/>
+    </filesystem>
+    <filesystem type='ram' accessmode='passthrough'>
+      <source usage='2017885' units='KiB'/>
+      <target dir='/run'/>
+    </filesystem>
+  </devices>
+</domain>
diff --git a/tests/lxcconf2xmltest.c b/tests/lxcconf2xmltest.c
index 6f0f97e..34ddb67 100644
--- a/tests/lxcconf2xmltest.c
+++ b/tests/lxcconf2xmltest.c
@@ -107,6 +107,7 @@ mymain(void)
         ret = EXIT_FAILURE
 
     DO_TEST("simple");
+    DO_TEST("nonetwork");
 
     return ret;
 }
-- 
1.8.5.2


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