conf, confile: add parsing of a shmounts config parameter

Signed-off-by: Liza Tretyakova <elizabet.tretyakova@gmail.com>
This commit is contained in:
Liza Tretyakova 2018-05-02 10:28:39 +03:00 committed by Christian Brauner
parent adf0ba1fc7
commit 0d190408c2
No known key found for this signature in database
GPG Key ID: 8EB056D53EECB12D
3 changed files with 55 additions and 0 deletions

View File

@ -23,6 +23,7 @@
#define _GNU_SOURCE
#include "config.h"
#include "confile.h"
#include <arpa/inet.h>
#include <dirent.h>
@ -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;
}

View File

@ -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)
{

View File

@ -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 */