]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
conf, confile: add parsing of a shmounts config parameter
authorLiza Tretyakova <elizabet.tretyakova@gmail.com>
Wed, 2 May 2018 07:28:39 +0000 (10:28 +0300)
committerChristian Brauner <christian.brauner@ubuntu.com>
Sun, 22 Jul 2018 13:25:15 +0000 (15:25 +0200)
Signed-off-by: Liza Tretyakova <elizabet.tretyakova@gmail.com>
src/lxc/conf.c
src/lxc/confile.c
src/lxc/confile.h

index 723910cb22af9df95934ef8fcb1a7efeb646fece..fa3286a902a42767af596ed3549fe5b4086c6b44 100644 (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;
 }
 
index 86dcdb90baa5e1c458da22fb4d9bb4bb4b4c57f3..84a7ab30fb18ab3b62166d6c5cd0fc4febb80577 100644 (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)
 {
index 4e05db16e7d7bfd3d42c45d11320e3107e7b5aaf..9a245e890f3c23d4685886abb842178f2aed0f97 100644 (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 */