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

[libvirt] [PATCH v2 10/18] LXC from native: convert lxc.id_map into <idmap>



---
 src/lxc/lxc_native.c                           | 43 ++++++++++++++++++++++++++
 tests/lxcconf2xmldata/lxcconf2xml-idmap.config |  5 +++
 tests/lxcconf2xmldata/lxcconf2xml-idmap.xml    | 28 +++++++++++++++++
 tests/lxcconf2xmltest.c                        |  1 +
 4 files changed, 77 insertions(+)
 create mode 100644 tests/lxcconf2xmldata/lxcconf2xml-idmap.config
 create mode 100644 tests/lxcconf2xmldata/lxcconf2xml-idmap.xml

diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c
index 9ddbcc7..4cbc0e6 100644
--- a/src/lxc/lxc_native.c
+++ b/src/lxc/lxc_native.c
@@ -561,6 +561,45 @@ error:
     return -1;
 }
 
+static int
+lxcIdmapWalkCallback(const char *name, virConfValuePtr value, void *data)
+{
+    virDomainDefPtr def = data;
+    virDomainIdMapEntryPtr idmap = NULL;
+    char type;
+    unsigned long start, target, count;
+
+    if (STRNEQ(name, "lxc.id_map") || !value->str)
+        return 0;
+
+    if (sscanf(value->str, "%c %lu %lu %lu", &type,
+               &target, &start, &count) != 4) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, _("invalid lxc.id_map: '%s'"),
+                       value->str);
+        return -1;
+    }
+
+    if (VIR_ALLOC(idmap) < 0)
+        return -1;
+
+    if (type == 'u') {
+        if (VIR_EXPAND_N(def->idmap.uidmap, def->idmap.nuidmap, 1) < 0)
+            return -1;
+        idmap = &def->idmap.uidmap[def->idmap.nuidmap - 1];
+    } else if (type == 'g') {
+        if (VIR_EXPAND_N(def->idmap.gidmap, def->idmap.ngidmap, 1) < 0)
+            return -1;
+        idmap = &def->idmap.gidmap[def->idmap.ngidmap - 1];
+    } else
+        return -1;
+
+    idmap->start = start;
+    idmap->target = target;
+    idmap->count = count;
+
+    return 0;
+}
+
 virDomainDefPtr
 lxcParseConfigString(const char *config)
 {
@@ -629,6 +668,10 @@ lxcParseConfigString(const char *config)
     if (lxcCreateConsoles(vmdef, properties) < 0)
         goto error;
 
+    /* lxc.id_map */
+    if (virConfWalk(properties, lxcIdmapWalkCallback, vmdef) < 0)
+        goto error;
+
     goto cleanup;
 
 error:
diff --git a/tests/lxcconf2xmldata/lxcconf2xml-idmap.config b/tests/lxcconf2xmldata/lxcconf2xml-idmap.config
new file mode 100644
index 0000000..4c3657a
--- /dev/null
+++ b/tests/lxcconf2xmldata/lxcconf2xml-idmap.config
@@ -0,0 +1,5 @@
+lxc.rootfs = /var/lib/lxc/migrate_test/rootfs
+lxc.utsname = migrate_test
+
+lxc.id_map = u 10000 0 2000
+lxc.id_map = g 10000 0 1000
diff --git a/tests/lxcconf2xmldata/lxcconf2xml-idmap.xml b/tests/lxcconf2xmldata/lxcconf2xml-idmap.xml
new file mode 100644
index 0000000..576c903
--- /dev/null
+++ b/tests/lxcconf2xmldata/lxcconf2xml-idmap.xml
@@ -0,0 +1,28 @@
+<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>
+  <idmap>
+    <uid start='0' target='10000' count='2000'/>
+    <gid start='0' target='10000' count='1000'/>
+  </idmap>
+  <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 4e7dd58..7042bdf 100644
--- a/tests/lxcconf2xmltest.c
+++ b/tests/lxcconf2xmltest.c
@@ -108,6 +108,7 @@ mymain(void)
     DO_TEST("nonenetwork", false);
     DO_TEST("physnetwork", false);
     DO_TEST("macvlannetwork", false);
+    DO_TEST("idmap", false);
 
     return ret;
 }
-- 
1.8.5.2


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