From: Christian Brauner Date: Fri, 12 Jan 2018 13:33:06 +0000 (+0100) Subject: tools: move lxc-create to API symbols only X-Git-Tag: lxc-3.0.0.beta1~36^2~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e6294545d11d237d17aca02cdc0778d8b078cc05;p=thirdparty%2Flxc.git tools: move lxc-create to API symbols only Closes #2073. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/tools/lxc_create.c b/src/lxc/tools/lxc_create.c index f702c2ba7..9a4640316 100644 --- a/src/lxc/tools/lxc_create.c +++ b/src/lxc/tools/lxc_create.c @@ -22,16 +22,14 @@ #include #include #include +#include #include -#include #include +#include + #include "arguments.h" -#include "log.h" -#include "lxc.h" -#include "storage.h" -#include "storage_utils.h" -#include "utils.h" +#include "tool_utils.h" static uint64_t get_fssize(char *s) { @@ -203,6 +201,23 @@ static bool validate_bdev_args(struct lxc_arguments *a) return true; } +static bool is_valid_storage_type(const char *type) +{ + if (strcmp(type, "dir") == 0 || + strcmp(type, "btrfs") == 0 || + strcmp(type, "aufs") == 0 || + strcmp(type, "loop") == 0 || + strcmp(type, "lvm") == 0 || + strcmp(type, "nbd") == 0 || + strcmp(type, "overlay") == 0 || + strcmp(type, "overlayfs") == 0 || + strcmp(type, "rbd") == 0 || + strcmp(type, "zfs") == 0) + return true; + + return false; +} + int main(int argc, char *argv[]) { struct lxc_container *c; @@ -225,7 +240,6 @@ int main(int argc, char *argv[]) if (lxc_log_init(&log)) exit(EXIT_FAILURE); - lxc_log_options_no_override(); /* REMOVE IN LXC 3.0 */ setenv("LXC_UPDATE_CONFIG_FORMAT", "1", 0); @@ -286,7 +300,7 @@ int main(int argc, char *argv[]) if (my_args.configfile) c->load_config(c, my_args.configfile); else - c->load_config(c, lxc_global_config_value("lxc.default_config")); + c->load_config(c, lxc_get_global_config_item("lxc.default_config")); if (my_args.fstype) spec.fstype = my_args.fstype; diff --git a/src/lxc/tools/tool_utils.c b/src/lxc/tools/tool_utils.c index 29adc6776..94f118e63 100644 --- a/src/lxc/tools/tool_utils.c +++ b/src/lxc/tools/tool_utils.c @@ -539,3 +539,60 @@ size_t lxc_array_len(void **array) return result; } + +/* + * Given the '-t' template option to lxc-create, figure out what to + * do. If the template is a full executable path, use that. If it + * is something like 'sshd', then return $templatepath/lxc-sshd. + * On success return the template, on error return NULL. + */ +char *get_template_path(const char *t) +{ + int ret, len; + char *tpath; + + if (t[0] == '/' && access(t, X_OK) == 0) { + tpath = strdup(t); + return tpath; + } + + len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1; + tpath = malloc(len); + if (!tpath) + return NULL; + ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t); + if (ret < 0 || ret >= len) { + free(tpath); + return NULL; + } + if (access(tpath, X_OK) < 0) { + fprintf(stderr, "Bad template: %s\n", t); + free(tpath); + return NULL; + } + + return tpath; +} + +int mkdir_p(const char *dir, mode_t mode) +{ + const char *tmp = dir; + const char *orig = dir; + char *makeme; + + do { + dir = tmp + strspn(tmp, "/"); + tmp = dir + strcspn(dir, "/"); + makeme = strndup(orig, dir - orig); + if (*makeme) { + if (mkdir(makeme, mode) && errno != EEXIST) { + fprintf(stderr, "Failed to create directory \"%s\"\n", makeme); + free(makeme); + return -1; + } + } + free(makeme); + } while(tmp != dir); + + return 0; +} diff --git a/src/lxc/tools/tool_utils.h b/src/lxc/tools/tool_utils.h index 2453e3d61..02b2eedd0 100644 --- a/src/lxc/tools/tool_utils.h +++ b/src/lxc/tools/tool_utils.h @@ -139,6 +139,9 @@ extern char **lxc_normalize_path(const char *path); extern char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix); +extern int mkdir_p(const char *dir, mode_t mode); extern int is_dir(const char *path); +extern char *get_template_path(const char *t); + #endif /* __LXC_UTILS_H */