From: Serge Hallyn Date: Mon, 29 Apr 2013 20:09:06 +0000 (+0200) Subject: introduce lxc_config X-Git-Tag: lxc-1.0.0.alpha1~1^2~249 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a8428dfa2c6a43ee195f4be3e04a519ca1fc6ec0;p=thirdparty%2Flxc.git introduce lxc_config 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 Acked-by: Dwight Engen --- diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am index 580b41d88..f392bf549 100644 --- a/src/lxc/Makefile.am +++ b/src/lxc/Makefile.am @@ -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 diff --git a/src/lxc/lxc.functions.in b/src/lxc/lxc.functions.in index 416267f74..ad3d42f1e 100644 --- a/src/lxc/lxc.functions.in +++ b/src/lxc/lxc.functions.in @@ -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 index 000000000..a454e07bd --- /dev/null +++ b/src/lxc/lxc_config.c @@ -0,0 +1,50 @@ +#include +#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); +} diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index 312cc99e4..10f188ea1 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -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(); diff --git a/src/lxc/lxccontainer.h b/src/lxc/lxccontainer.h index a4be7537c..b6bd97c2e 100644 --- a/src/lxc/lxccontainer.h +++ b/src/lxc/lxccontainer.h @@ -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