Commit 5c484f79 by Christian Brauner

Move remaining overlay helpers to overlay.{c,h}

Move - ovl_get_rootfs_dir() - mount_entry_create_overlay_dirs() from conf.h to overlay.{c,h} where they belong. Rename - mount_entry_create_overlay_dirs() --> ovl_mkdir() in accordance with the ovl_ prefix naming scheme for types and functions associated with overlay. Take the chance to add whitespace between operators where missing. Signed-off-by: 's avatarChristian Brauner <christian.brauner@mailbox.org>
parent 83e79752
...@@ -29,6 +29,12 @@ ...@@ -29,6 +29,12 @@
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#if IS_BIONIC
#include <../include/lxcmntent.h>
#else
#include <mntent.h>
#endif
/* defined in bdev.h */ /* defined in bdev.h */
struct bdev; struct bdev;
...@@ -38,15 +44,21 @@ struct bdev_specs; ...@@ -38,15 +44,21 @@ struct bdev_specs;
/* defined conf.h */ /* defined conf.h */
struct lxc_conf; struct lxc_conf;
int ovl_detect(const char *path); /* defined in conf.h */
int ovl_mount(struct bdev *bdev); struct lxc_rootfs;
int ovl_umount(struct bdev *bdev);
/*
* Functions associated with an overlay bdev struct.
*/
int ovl_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname, int ovl_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname,
const char *cname, const char *oldpath, const char *lxcpath, const char *cname, const char *oldpath, const char *lxcpath,
int snap, uint64_t newsize, struct lxc_conf *conf); int snap, uint64_t newsize, struct lxc_conf *conf);
int ovl_destroy(struct bdev *orig);
int ovl_create(struct bdev *bdev, const char *dest, const char *n, int ovl_create(struct bdev *bdev, const char *dest, const char *n,
struct bdev_specs *specs); struct bdev_specs *specs);
int ovl_destroy(struct bdev *orig);
int ovl_detect(const char *path);
int ovl_mount(struct bdev *bdev);
int ovl_umount(struct bdev *bdev);
/* /*
* To be called from lxcapi_clone() in lxccontainer.c: When we clone a container * To be called from lxcapi_clone() in lxccontainer.c: When we clone a container
...@@ -66,4 +78,16 @@ int ovl_update_abs_paths(struct lxc_conf *lxc_conf, const char *lxc_path, ...@@ -66,4 +78,16 @@ int ovl_update_abs_paths(struct lxc_conf *lxc_conf, const char *lxc_path,
*/ */
char *ovl_getlower(char *p); char *ovl_getlower(char *p);
/*
* Get rootfs path for overlay backed containers. Allocated memory must be freed
* by caller.
*/
char *ovl_get_rootfs(const char *rootfs_path, size_t *rootfslen);
/*
* Create upper- and workdirs for overlay mounts.
*/
int ovl_mkdir(const struct mntent *mntent, const struct lxc_rootfs *rootfs,
const char *lxc_name, const char *lxc_path);
#endif /* __LXC_OVERLAY_H */ #endif /* __LXC_OVERLAY_H */
...@@ -72,6 +72,7 @@ ...@@ -72,6 +72,7 @@
#include "log.h" #include "log.h"
#include "caps.h" /* for lxc_caps_last_cap() */ #include "caps.h" /* for lxc_caps_last_cap() */
#include "bdev/bdev.h" #include "bdev/bdev.h"
#include "bdev/overlay.h"
#include "cgroup.h" #include "cgroup.h"
#include "lxclock.h" #include "lxclock.h"
#include "namespace.h" #include "namespace.h"
...@@ -1815,107 +1816,6 @@ static void cull_mntent_opt(struct mntent *mntent) ...@@ -1815,107 +1816,6 @@ static void cull_mntent_opt(struct mntent *mntent)
} }
} }
static char *ovl_get_rootfs_dir(const char *rootfs_path, size_t *rootfslen)
{
char *rootfsdir = NULL;
char *s1 = NULL;
char *s2 = NULL;
char *s3 = NULL;
if (!rootfs_path || !rootfslen)
return NULL;
s1 = strdup(rootfs_path);
if (!s1)
return NULL;
if ((s2 = strstr(s1, ":/"))) {
s2 = s2 + 1;
if ((s3 = strstr(s2, ":/")))
*s3 = '\0';
rootfsdir = strdup(s2);
if (!rootfsdir) {
free(s1);
return NULL;
}
}
if (!rootfsdir)
rootfsdir = s1;
else
free(s1);
*rootfslen = strlen(rootfsdir);
return rootfsdir;
}
static int mount_entry_create_overlay_dirs(const struct mntent *mntent,
const struct lxc_rootfs *rootfs,
const char *lxc_name,
const char *lxc_path)
{
char lxcpath[MAXPATHLEN];
char *rootfsdir = NULL;
char *upperdir = NULL;
char *workdir = NULL;
char **opts = NULL;
int fret = -1;
int ret = 0;
size_t arrlen = 0;
size_t dirlen = 0;
size_t i;
size_t len = 0;
size_t rootfslen = 0;
if (!rootfs->path || !lxc_name || !lxc_path)
goto err;
opts = lxc_string_split(mntent->mnt_opts, ',');
if (opts)
arrlen = lxc_array_len((void **)opts);
else
goto err;
for (i = 0; i < arrlen; i++) {
if (strstr(opts[i], "upperdir=") && (strlen(opts[i]) > (len = strlen("upperdir="))))
upperdir = opts[i] + len;
else if (strstr(opts[i], "workdir=") && (strlen(opts[i]) > (len = strlen("workdir="))))
workdir = opts[i] + len;
}
ret = snprintf(lxcpath, MAXPATHLEN, "%s/%s", lxc_path, lxc_name);
if (ret < 0 || ret >= MAXPATHLEN)
goto err;
rootfsdir = ovl_get_rootfs_dir(rootfs->path, &rootfslen);
if (!rootfsdir)
goto err;
dirlen = strlen(lxcpath);
/* We neither allow users to create upperdirs and workdirs outside the
* containerdir nor inside the rootfs. The latter might be debatable. */
if (upperdir)
if ((strncmp(upperdir, lxcpath, dirlen) == 0) && (strncmp(upperdir, rootfsdir, rootfslen) != 0))
if (mkdir_p(upperdir, 0755) < 0) {
WARN("Failed to create upperdir");
}
if (workdir)
if ((strncmp(workdir, lxcpath, dirlen) == 0) && (strncmp(workdir, rootfsdir, rootfslen) != 0))
if (mkdir_p(workdir, 0755) < 0) {
WARN("Failed to create workdir");
}
fret = 0;
err:
free(rootfsdir);
lxc_free_array((void **)opts, free);
return fret;
}
static int mount_entry_create_aufs_dirs(const struct mntent *mntent, static int mount_entry_create_aufs_dirs(const struct mntent *mntent,
const struct lxc_rootfs *rootfs, const struct lxc_rootfs *rootfs,
const char *lxc_name, const char *lxc_name,
...@@ -1958,7 +1858,7 @@ static int mount_entry_create_aufs_dirs(const struct mntent *mntent, ...@@ -1958,7 +1858,7 @@ static int mount_entry_create_aufs_dirs(const struct mntent *mntent,
if (ret < 0 || ret >= MAXPATHLEN) if (ret < 0 || ret >= MAXPATHLEN)
goto err; goto err;
rootfsdir = ovl_get_rootfs_dir(rootfs->path, &rootfslen); rootfsdir = ovl_get_rootfs(rootfs->path, &rootfslen);
if (!rootfsdir) if (!rootfsdir)
goto err; goto err;
...@@ -1987,7 +1887,7 @@ static int mount_entry_create_dir_file(const struct mntent *mntent, ...@@ -1987,7 +1887,7 @@ static int mount_entry_create_dir_file(const struct mntent *mntent,
FILE *pathfile = NULL; FILE *pathfile = NULL;
if (strncmp(mntent->mnt_type, "overlay", 7) == 0) { if (strncmp(mntent->mnt_type, "overlay", 7) == 0) {
if (mount_entry_create_overlay_dirs(mntent, rootfs, lxc_name, lxc_path) < 0) if (ovl_mkdir(mntent, rootfs, lxc_name, lxc_path) < 0)
return -1; return -1;
} else if (strncmp(mntent->mnt_type, "aufs", 4) == 0) { } else if (strncmp(mntent->mnt_type, "aufs", 4) == 0) {
if (mount_entry_create_aufs_dirs(mntent, rootfs, lxc_name, lxc_path) < 0) if (mount_entry_create_aufs_dirs(mntent, rootfs, lxc_name, lxc_path) < 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment