support configure sysctl for container

{
	"id": "test-container-sysctl",
	"containers" : [{
	    "name": "ubuntu",
	    "image": "ubuntu:latest",
	    "workdir": "/",
	    "command": ["/bin/bash"],
	    "sysctl":{"net.ipv4.ip_forward": "1", "net.core.somaxconn": "256"}
	}],
	"resource": {
	    "vcpu": 1,
	    "memory": 512
	},
	"tty": true
}

Signed-off-by: Gao feng <omarapazanadi@gmail.com>
This commit is contained in:
Gao feng
2015-10-19 17:03:28 +08:00
parent 48c6e2248a
commit 350997b03c
3 changed files with 91 additions and 1 deletions
+53 -1
View File
@@ -167,6 +167,45 @@ static int container_setup_mount(struct hyper_container *container)
return 0;
}
static int container_setup_sysctl(struct hyper_container *container)
{
int i, size, len, l, fd;
struct sysctl *sys;
for (i = 0; i < container->sys_num; i++) {
char path[256];
len = 0;
sys = &container->sys[i];
size = strlen(sys->value);
sprintf(path, "/proc/sys/%s", sys->path);
fprintf(stdout, "sysctl %s value %s\n", sys->path, sys->value);
fd = open(path, O_WRONLY);
if (fd < 0) {
perror("open file failed");
goto out;
}
while (len < size) {
l = write(fd, sys->value + len, size - len);
if (l < 0) {
perror("fail to write sysctl");
close(fd);
goto out;
}
len += l;
}
close(fd);
}
return 0;
out:
return -1;
}
static int container_setup_dns(struct hyper_container *container)
{
int fd;
@@ -366,7 +405,12 @@ static int hyper_container_init(void *data)
}
if (container_setup_mount(container) < 0) {
fprintf(stderr, "container sets up mount ns failed\n");
fprintf(stderr, "container sets up mount failed\n");
goto fail;
}
if (container_setup_sysctl(container) < 0) {
fprintf(stderr, "container sets up sysctl failed\n");
goto fail;
}
@@ -535,6 +579,7 @@ void hyper_cleanup_container(struct hyper_pod *pod)
struct volume *vol;
struct env *env;
struct fsmap *map;
struct sysctl *sys;
char root[512];
for (i = 0; i < pod->c_num; i++) {
@@ -565,6 +610,13 @@ void hyper_cleanup_container(struct hyper_pod *pod)
}
free(c->envs);
for (j = 0; j < c->sys_num; j++) {
sys = &(c->sys[j]);
free(sys->path);
free(sys->value);
}
free(c->sys);
for (j = 0; j < c->maps_num; j++) {
map = &(c->maps[j]);
free(map->source);
+7
View File
@@ -21,6 +21,11 @@ struct fsmap {
int readonly;
};
struct sysctl {
char *path;
char *value;
};
struct hyper_container {
char *id;
char *rootfs;
@@ -30,9 +35,11 @@ struct hyper_container {
struct volume *vols;
struct env *envs;
struct fsmap *maps;
struct sysctl *sys;
int vols_num;
int envs_num;
int maps_num;
int sys_num;
int ns;
uint32_t code;
struct hyper_exec exec;
+31
View File
@@ -186,6 +186,32 @@ static int container_parse_envs(struct hyper_container *c, char *json, jsmntok_t
return i;
}
static int container_parse_sysctl(struct hyper_container *c, char *json, jsmntok_t *toks)
{
int i = 1, j;
char *p;
if (toks[i].type != JSMN_OBJECT) {
fprintf(stdout, "sysctl need object\n");
return -1;
}
c->sys_num = toks[i].size;
fprintf(stdout, "sysctl size %d\n", c->sys_num);
c->sys = calloc(c->sys_num, sizeof(*c->sys));
for (j = 0; j < c->sys_num; j++) {
c->sys[j].path = strdup(json_token_str(json, &toks[++i]));
while((p = strchr(c->sys[j].path, '.')) != NULL) {
*p = '/';
}
c->sys[j].value = strdup(json_token_str(json, &toks[++i]));
fprintf(stdout, "sysctl %s:%s\n", c->sys[j].path, c->sys[j].value);
}
return i;
}
static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *c,
char *json, jsmntok_t *toks)
{
@@ -259,6 +285,11 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
if (next < 0)
return -1;
i += next;
} else if (json_token_streq(json, t, "sysctl") && t->size == 1) {
next = container_parse_sysctl(c, json, &toks[i]);
if (next < 0)
return -1;
i += next;
} else if (json_token_streq(json, t, "restartPolicy") && t->size == 1) {
i++;
/*