From: Liza Tretyakova Date: Wed, 2 May 2018 07:28:39 +0000 (+0300) Subject: conf, confile: add parsing of a shmounts config parameter X-Git-Tag: lxc-3.1.0~195^2~17 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0d190408c24e2ac78ddb449b1f1299f1afa6722d;p=thirdparty%2Flxc.git conf, confile: add parsing of a shmounts config parameter Signed-off-by: Liza Tretyakova --- diff --git a/src/lxc/conf.c b/src/lxc/conf.c index 723910cb2..fa3286a90 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -23,6 +23,7 @@ #define _GNU_SOURCE #include "config.h" +#include "confile.h" #include #include @@ -647,6 +648,30 @@ unsigned long add_required_remount_flags(const char *s, const char *d, #endif } +static int add_shmount_to_list(struct lxc_conf *conf) { + char new_mount[MAXPATHLEN]; + size_t len_mount; + /* Offset for the leading '/' since the path_cont + * is absolute inside the container */ + int ret = -1, offset = 1; + + /* +1 for the separating whitespace */ + len_mount = strlen(conf->lxc_shmount.path_host) + 1 + + strlen(conf->lxc_shmount.path_cont) - offset + + sizeof(" none bind,create=dir 0 0") - 1; + + ret = snprintf(new_mount, len_mount + 1, "%s %s none bind,create=dir 0 0", + conf->lxc_shmount.path_host, conf->lxc_shmount.path_cont + offset); + if (ret < 0 || (size_t)ret >= len_mount + 1) + return -1; + + ret = add_elem_to_mount_list(new_mount, conf); + if (ret < 0) + ERROR("Failed to add new mount \"%s\" to the config", new_mount); + + return ret; +} + static int lxc_mount_auto_mounts(struct lxc_conf *conf, int flags, struct lxc_handler *handler) { int i, r; @@ -783,6 +808,14 @@ static int lxc_mount_auto_mounts(struct lxc_conf *conf, int flags, struct lxc_ha } } + if (flags & LXC_AUTO_SHMOUNTS_MASK) { + int ret = add_shmount_to_list(conf); + if (ret < 0) { + ERROR("Failed to add shmount entry to container config"); + return ret; + } + } + return 0; } diff --git a/src/lxc/confile.c b/src/lxc/confile.c index 86dcdb90b..84a7ab30f 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -1677,6 +1677,8 @@ static int set_config_mount_auto(const char *key, const char *value, return -1; for (autoptr = autos;; autoptr = NULL) { + bool is_shmounts = false; + token = strtok_r(autoptr, " \t", &sptr); if (!token) { ret = 0; @@ -1686,6 +1688,12 @@ static int set_config_mount_auto(const char *key, const char *value, for (i = 0; allowed_auto_mounts[i].token; i++) { if (!strcmp(allowed_auto_mounts[i].token, token)) break; + + if (strcmp("shmounts:", allowed_auto_mounts[i].token) == 0 + && strncmp("shmounts:", token, sizeof("shmounts:") - 1) == 0) { + is_shmounts = true; + break; + } } if (!allowed_auto_mounts[i].token) { @@ -1695,6 +1703,14 @@ static int set_config_mount_auto(const char *key, const char *value, lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask; lxc_conf->auto_mounts |= allowed_auto_mounts[i].flag; + if (is_shmounts) { + lxc_conf->lxc_shmount.path_host = strdup(token + (sizeof("shmounts:") - 1)); + if (strcmp(lxc_conf->lxc_shmount.path_host, "") == 0) { + ERROR("Invalid shmounts path: empty"); + break; + } + lxc_conf->lxc_shmount.path_cont = strdup("/dev/.lxc-mounts"); + } } free(autos); @@ -1726,6 +1742,10 @@ static int set_config_mount(const char *key, const char *value, return 0; } +int add_elem_to_mount_list(const char *value, struct lxc_conf *lxc_conf) { + return set_config_mount(NULL, value, lxc_conf, NULL); +} + static int set_config_cap_keep(const char *key, const char *value, struct lxc_conf *lxc_conf, void *data) { diff --git a/src/lxc/confile.h b/src/lxc/confile.h index 4e05db16e..9a245e890 100644 --- a/src/lxc/confile.h +++ b/src/lxc/confile.h @@ -121,4 +121,6 @@ bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath, extern bool network_new_hwaddrs(struct lxc_conf *conf); +extern int add_elem_to_mount_list(const char *value, struct lxc_conf *lxc_conf); + #endif /* __LXC_CONFILE_H */