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

[libvirt] [PATCH 09/16] LXC from native: convert lxc.id_map into <idmap>



---
 src/lxc/lxc_native.c                           | 41 ++++++++++++++++++++++++++
 tests/lxcconf2xmldata/lxcconf2xml-idmap.config |  6 ++++
 tests/lxcconf2xmldata/lxcconf2xml-idmap.xml    | 32 ++++++++++++++++++++
 tests/lxcconf2xmltest.c                        |  1 +
 4 files changed, 80 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 a99bc1c..7cb5125 100644
--- a/src/lxc/lxc_native.c
+++ b/src/lxc/lxc_native.c
@@ -730,6 +730,43 @@ error:
     return -1;
 }
 
+static int
+lxcCreateIdmap(virDomainDefPtr def, virPropertiesPtr properties)
+{
+    char *value = NULL;
+    virDomainIdMapEntryPtr idmap = NULL;
+    char type;
+    unsigned long start, target, count;
+
+    value = virPropertiesLookup(properties, "lxc.id_map");
+    while (value) {
+        if (sscanf(value, "%c %lu %lu %lu", &type, &target, &start, &count) != 4)
+            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;
+
+        value = virPropertiesLookup(properties, NULL);
+    }
+
+    return 0;
+}
+
 virDomainDefPtr
 lxcParseConfigString(const char *config,
                      const char *fstab,
@@ -821,6 +858,10 @@ lxcParseConfigString(const char *config,
     if (lxcCreateConsoles(vmdef, properties) < 0)
         goto error;
 
+    /* lxc.id_map */
+    if (lxcCreateIdmap(vmdef, properties) < 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..1eedbe8
--- /dev/null
+++ b/tests/lxcconf2xmldata/lxcconf2xml-idmap.config
@@ -0,0 +1,6 @@
+lxc.rootfs = /var/lib/lxc/migrate_test/rootfs
+lxc.utsname = migrate_test
+lxc.mount = /var/lib/lxc/migrate_test/fstab
+
+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..6de99af
--- /dev/null
+++ b/tests/lxcconf2xmldata/lxcconf2xml-idmap.xml
@@ -0,0 +1,32 @@
+<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>
+  <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>
+    <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 8d23a11..6793c29 100644
--- a/tests/lxcconf2xmltest.c
+++ b/tests/lxcconf2xmltest.c
@@ -110,6 +110,7 @@ mymain(void)
     DO_TEST("nonetwork");
     DO_TEST("physnetwork");
     DO_TEST("macvlannetwork");
+    DO_TEST("idmap");
 
     return ret;
 }
-- 
1.8.5.2


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