Commit 33fcb7a0 by Daniel Lezcano Committed by Daniel Lezcano

encapsulate rootfs data in a structure

We have pivot_dir and rootfs defined in lxc_conf structure. Let's encapsulate them in a rootfs structure. Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent 196db713
...@@ -374,7 +374,8 @@ static int setup_utsname(struct utsname *utsname) ...@@ -374,7 +374,8 @@ static int setup_utsname(struct utsname *utsname)
return 0; return 0;
} }
static int setup_tty(const char *rootfs, const struct lxc_tty_info *tty_info) static int setup_tty(const struct lxc_rootfs *rootfs,
const struct lxc_tty_info *tty_info)
{ {
char path[MAXPATHLEN]; char path[MAXPATHLEN];
int i; int i;
...@@ -384,7 +385,7 @@ static int setup_tty(const char *rootfs, const struct lxc_tty_info *tty_info) ...@@ -384,7 +385,7 @@ static int setup_tty(const char *rootfs, const struct lxc_tty_info *tty_info)
struct lxc_pty_info *pty_info = &tty_info->pty_info[i]; struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
snprintf(path, sizeof(path), "%s/dev/tty%d", snprintf(path, sizeof(path), "%s/dev/tty%d",
rootfs ? rootfs : "", i + 1); rootfs->path ? rootfs->path : "", i + 1);
/* At this point I can not use the "access" function /* At this point I can not use the "access" function
* to check the file is present or not because it fails * to check the file is present or not because it fails
...@@ -579,22 +580,22 @@ static int setup_rootfs_pivot_root(const char *rootfs, const char *pivotdir) ...@@ -579,22 +580,22 @@ static int setup_rootfs_pivot_root(const char *rootfs, const char *pivotdir)
return 0; return 0;
} }
static int setup_rootfs(const char *rootfs, const char *pivotdir) static int setup_rootfs(const struct lxc_rootfs *rootfs)
{ {
const char *tmpfs = "/tmp"; const char *tmpfs = "/tmp";
if (!rootfs) if (!rootfs->path)
return 0; return 0;
if (mount(rootfs, tmpfs, "none", MS_BIND|MS_REC, NULL)) { if (mount(rootfs->path, tmpfs, "none", MS_BIND|MS_REC, NULL)) {
SYSERROR("failed to mount '%s'->'%s'", rootfs, "/tmp"); SYSERROR("failed to mount '%s'->'%s'", rootfs->path, "/tmp");
return -1; return -1;
} }
DEBUG("mounted '%s' on '%s'", rootfs, tmpfs); DEBUG("mounted '%s' on '%s'", rootfs->path, tmpfs);
if (setup_rootfs_pivot_root(tmpfs, pivotdir)) { if (setup_rootfs_pivot_root(tmpfs, rootfs->pivot)) {
ERROR("failed to pivot_root to '%s'", rootfs); ERROR("failed to pivot_root to '%s'", rootfs->pivot);
return -1; return -1;
} }
...@@ -640,16 +641,17 @@ out: ...@@ -640,16 +641,17 @@ out:
return 0; return 0;
} }
static int setup_console(const char *rootfs, const struct lxc_console *console) static int setup_console(const struct lxc_rootfs *rootfs,
const struct lxc_console *console)
{ {
char path[MAXPATHLEN]; char path[MAXPATHLEN];
struct stat s; struct stat s;
/* We don't have a rootfs, /dev/console will be shared */ /* We don't have a rootfs, /dev/console will be shared */
if (!rootfs) if (!rootfs->path)
return 0; return 0;
snprintf(path, sizeof(path), "%s/dev/console", rootfs); snprintf(path, sizeof(path), "%s/dev/console", rootfs->path);
if (access(path, F_OK)) { if (access(path, F_OK)) {
WARN("rootfs specified but no console found"); WARN("rootfs specified but no console found");
...@@ -1415,17 +1417,17 @@ int lxc_setup(const char *name, struct lxc_conf *lxc_conf) ...@@ -1415,17 +1417,17 @@ int lxc_setup(const char *name, struct lxc_conf *lxc_conf)
return -1; return -1;
} }
if (setup_console(lxc_conf->rootfs, &lxc_conf->console)) { if (setup_console(&lxc_conf->rootfs, &lxc_conf->console)) {
ERROR("failed to setup the console for '%s'", name); ERROR("failed to setup the console for '%s'", name);
return -1; return -1;
} }
if (setup_tty(lxc_conf->rootfs, &lxc_conf->tty_info)) { if (setup_tty(&lxc_conf->rootfs, &lxc_conf->tty_info)) {
ERROR("failed to setup the ttys for '%s'", name); ERROR("failed to setup the ttys for '%s'", name);
return -1; return -1;
} }
if (setup_rootfs(lxc_conf->rootfs, lxc_conf->pivotdir)) { if (setup_rootfs(&lxc_conf->rootfs)) {
ERROR("failed to set rootfs for '%s'", name); ERROR("failed to set rootfs for '%s'", name);
return -1; return -1;
} }
......
...@@ -163,6 +163,17 @@ struct lxc_console { ...@@ -163,6 +163,17 @@ struct lxc_console {
}; };
/* /*
* Defines a structure to store the rootfs location, the
* optionals pivot_root, rootfs mount paths
* @rootfs : a path to the rootfs
* @pivot_root : a path to a pivot_root location to be used
*/
struct lxc_rootfs {
char *path;
char *pivot;
};
/*
* Defines the global container configuration * Defines the global container configuration
* @rootfs : root directory to run the container * @rootfs : root directory to run the container
* @pivotdir : pivotdir path, if not set default will be used * @pivotdir : pivotdir path, if not set default will be used
...@@ -178,8 +189,6 @@ struct lxc_console { ...@@ -178,8 +189,6 @@ struct lxc_console {
* @console : console data * @console : console data
*/ */
struct lxc_conf { struct lxc_conf {
char *rootfs;
char *pivotdir;
char *fstab; char *fstab;
int tty; int tty;
int pts; int pts;
...@@ -191,6 +200,7 @@ struct lxc_conf { ...@@ -191,6 +200,7 @@ struct lxc_conf {
struct lxc_list caps; struct lxc_list caps;
struct lxc_tty_info tty_info; struct lxc_tty_info tty_info;
struct lxc_console console; struct lxc_console console;
struct lxc_rootfs rootfs;
}; };
/* /*
......
...@@ -643,8 +643,8 @@ static int config_rootfs(const char *key, char *value, struct lxc_conf *lxc_conf ...@@ -643,8 +643,8 @@ static int config_rootfs(const char *key, char *value, struct lxc_conf *lxc_conf
return -1; return -1;
} }
lxc_conf->rootfs = strdup(value); lxc_conf->rootfs.path = strdup(value);
if (!lxc_conf->rootfs) { if (!lxc_conf->rootfs.path) {
SYSERROR("failed to duplicate string %s", value); SYSERROR("failed to duplicate string %s", value);
return -1; return -1;
} }
...@@ -659,8 +659,8 @@ static int config_pivotdir(const char *key, char *value, struct lxc_conf *lxc_co ...@@ -659,8 +659,8 @@ static int config_pivotdir(const char *key, char *value, struct lxc_conf *lxc_co
return -1; return -1;
} }
lxc_conf->pivotdir = strdup(value); lxc_conf->rootfs.pivot = strdup(value);
if (!lxc_conf->pivotdir) { if (!lxc_conf->rootfs.pivot) {
SYSERROR("failed to duplicate string %s", value); SYSERROR("failed to duplicate string %s", value);
return -1; return -1;
} }
......
...@@ -145,7 +145,7 @@ int lxc_create_console(struct lxc_conf *conf) ...@@ -145,7 +145,7 @@ int lxc_create_console(struct lxc_conf *conf)
struct lxc_console *console = &conf->console; struct lxc_console *console = &conf->console;
int fd; int fd;
if (!conf->rootfs) if (!conf->rootfs.path)
return 0; return 0;
if (!console->path) if (!console->path)
...@@ -155,7 +155,7 @@ int lxc_create_console(struct lxc_conf *conf) ...@@ -155,7 +155,7 @@ int lxc_create_console(struct lxc_conf *conf)
console->name, NULL, NULL)) { console->name, NULL, NULL)) {
SYSERROR("failed to allocate a pty"); SYSERROR("failed to allocate a pty");
return -1; return -1;
} }
if (fcntl(console->master, F_SETFD, FD_CLOEXEC)) { if (fcntl(console->master, F_SETFD, FD_CLOEXEC)) {
SYSERROR("failed to set console master to close-on-exec"); SYSERROR("failed to set console master to close-on-exec");
...@@ -262,7 +262,7 @@ int lxc_console_mainloop_add(struct lxc_epoll_descr *descr, ...@@ -262,7 +262,7 @@ int lxc_console_mainloop_add(struct lxc_epoll_descr *descr,
struct lxc_conf *conf = handler->conf; struct lxc_conf *conf = handler->conf;
struct lxc_console *console = &conf->console; struct lxc_console *console = &conf->console;
if (!conf->rootfs) { if (!conf->rootfs.path) {
INFO("no rootfs, no console."); INFO("no rootfs, no console.");
return 0; return 0;
} }
......
...@@ -56,7 +56,7 @@ static int utmp_handler(int fd, void *data, struct lxc_epoll_descr *descr) ...@@ -56,7 +56,7 @@ static int utmp_handler(int fd, void *data, struct lxc_epoll_descr *descr)
return -1; return -1;
} }
if (snprintf(path, MAXPATHLEN, "%s/var/run/utmp", conf->rootfs) > if (snprintf(path, MAXPATHLEN, "%s/var/run/utmp", conf->rootfs.path) >
MAXPATHLEN) { MAXPATHLEN) {
ERROR("path is too long"); ERROR("path is too long");
return -1; return -1;
...@@ -114,10 +114,10 @@ int lxc_utmp_mainloop_add(struct lxc_epoll_descr *descr, ...@@ -114,10 +114,10 @@ int lxc_utmp_mainloop_add(struct lxc_epoll_descr *descr,
char path[MAXPATHLEN]; char path[MAXPATHLEN];
int fd, wd; int fd, wd;
if (!conf->rootfs) if (!conf->rootfs.path)
return 0; return 0;
if (snprintf(path, MAXPATHLEN, "%s/var/run/utmp", conf->rootfs) > if (snprintf(path, MAXPATHLEN, "%s/var/run/utmp", conf->rootfs.path) >
MAXPATHLEN) { MAXPATHLEN) {
ERROR("path is too long"); ERROR("path is too long");
return -1; return -1;
......
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