]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
introduce lxc_config
authorSerge Hallyn <serge.hallyn@ubuntu.com>
Mon, 29 Apr 2013 20:09:06 +0000 (22:09 +0200)
committerSerge Hallyn <serge.hallyn@ubuntu.com>
Tue, 30 Apr 2013 14:15:44 +0000 (09:15 -0500)
It's a tiny program (exported through the api) wrapping the util.c
helpers for reading /etc/lxc/lxc.conf variables, and replaces
the kludgy shell duplication in lxc.functions.in

Changelog: Apr 30: address feedback from Dwight
(exit error on failure, and use 'lxcpath' as name, not
'default_path').

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Dwight Engen <dwight.engen@oracle.com>
src/lxc/Makefile.am
src/lxc/lxc.functions.in
src/lxc/lxc_config.c [new file with mode: 0644]
src/lxc/lxccontainer.c
src/lxc/lxccontainer.h

index 580b41d88797a5a6c7b9d5896177208780b6d5d4..f392bf5497a8c153f52316bb49b0a16cf837e778 100644 (file)
@@ -162,7 +162,8 @@ bin_PROGRAMS = \
        lxc-unfreeze \
        lxc-checkpoint \
        lxc-restart \
-       lxc-kill
+       lxc-kill \
+       lxc-config
 
 pkglibexec_PROGRAMS = \
        lxc-init
@@ -179,6 +180,7 @@ LDADD=liblxc.so @CAP_LIBS@ @APPARMOR_LIBS@ @SECCOMP_LIBS@
 lxc_attach_SOURCES = lxc_attach.c
 lxc_cgroup_SOURCES = lxc_cgroup.c
 lxc_checkpoint_SOURCES = lxc_checkpoint.c
+lxc_config_SOURCES = lxc_config.c
 lxc_console_SOURCES = lxc_console.c
 lxc_execute_SOURCES = lxc_execute.c
 lxc_freeze_SOURCES = lxc_freeze.c
index 416267f74f1403e95f1927e79f3e164c8a830097..ad3d42f1e302cc4dceb05e79f30cacef3938fea8 100644 (file)
@@ -25,33 +25,6 @@ bindir=@BINDIR@
 templatedir=@LXCTEMPLATEDIR@
 lxcinitdir=@LXCINITDIR@
 
-get_default_lxcpath() {
-       LXC_PATH=$(grep -v "^#" "$globalconf" 2>/dev/null | grep "[ \t]*lxcpath[ \t]*=") || true
-       if [ -n "$LXC_PATH" ]; then
-               echo $LXC_PATH | awk -F= '{ print $2 }'
-       else
-               echo @LXCPATH@
-       fi
-}
-
-get_default_vg() {
-       LXC_VG=$(grep -v "^#" "$globalconf" 2>/dev/null | grep "[ \t]*lvm_vg[ \t]*=") || true
-       if [ -n "$LXC_VG" ]; then
-               echo $LXC_VG | awk -F= '{ print $2 }'
-       else
-               echo "lxc"
-       fi
-}
-
-get_default_zfsroot() {
-       LXC_ZFSROOT=$(grep -v "^#" "$globalconf" 2>/dev/null | grep "[ \t]*zfsroot[ \t]*=") || true
-       if [ -n "$LXC_ZFSROOT" ]; then
-               echo $LXC_ZFSROOT | awk -F= '{ print $2 }'
-       else
-               echo "tank/lxc"
-       fi
-}
-
-lxc_path=`get_default_lxcpath`
-lxc_vg=`get_default_vg`
-lxc_zfsroot=`get_default_zfsroot`
+lxc_path=`lxc-config default_path`
+lxc_vg=`lxc-config lvm_vg`
+lxc_zfsroot=`lxc-config zfsroot`
diff --git a/src/lxc/lxc_config.c b/src/lxc/lxc_config.c
new file mode 100644 (file)
index 0000000..a454e07
--- /dev/null
@@ -0,0 +1,50 @@
+#include <stdio.h>
+#include "config.h"
+#include "lxccontainer.h"
+
+struct lxc_config_items {
+       char *name;
+       const char *(*fn)(void);
+};
+
+struct lxc_config_items items[] =
+{
+       { .name = "lxcpath", .fn = &lxc_get_default_config_path, },
+       { .name = "lvm_vg", .fn = &lxc_get_default_lvm_vg, },
+       { .name = "zfsroot", .fn = &lxc_get_default_zfs_root, },
+       { .name = NULL, },
+};
+
+void usage(char *me)
+{
+       printf("Usage: %s -l: list all available configuration items\n", me);
+       printf("       %s item: print configuration item\n", me);
+       exit(1);
+}
+
+void list_config_items(void)
+{
+       struct lxc_config_items *i;
+
+       for (i = &items[0]; i->name; i++)
+               printf("%s\n", i->name);
+       exit(0);
+}
+
+int main(int argc, char *argv[])
+{
+       struct lxc_config_items *i;
+
+       if (argc < 2)
+               usage(argv[0]);
+       if (strcmp(argv[1], "-l") == 0)
+               list_config_items();
+       for (i = &items[0]; i->name; i++) {
+               if (strcmp(argv[1], i->name) == 0) {
+                       printf("%s\n", i->fn());
+                       exit(0);
+               }
+       }
+       printf("Unknown configuration item: %s\n", argv[1]);
+       exit(-1);
+}
index 312cc99e48b85ce675d3bedbee508117017db764..10f188ea1621e4f95f08390a7b96f3fb3fc46c86 100644 (file)
@@ -1009,6 +1009,16 @@ const char *lxc_get_default_config_path(void)
        return default_lxc_path();
 }
 
+const char *lxc_get_default_lvm_vg(void)
+{
+       return default_lvm_vg();
+}
+
+const char *lxc_get_default_zfs_root(void)
+{
+       return default_zfs_root();
+}
+
 const char *lxc_get_version(void)
 {
        return lxc_version();
index a4be7537c242ab81987593c6119dd11a7fc60513..b6bd97c2e3f40b4e6255eab0d769d5a40d5c21a8 100644 (file)
@@ -122,6 +122,8 @@ int lxc_container_get(struct lxc_container *c);
 int lxc_container_put(struct lxc_container *c);
 int lxc_get_wait_states(const char **states);
 const char *lxc_get_default_config_path(void);
+const char *lxc_get_default_lvm_vg(void);
+const char *lxc_get_default_zfs_root(void);
 const char *lxc_get_version(void);
 
 #if 0