2
0
mirror of git://github.com/lxc/lxc synced 2025-08-31 20:43:36 +00:00

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: Daniel Lezcano <dlezcano@fr.ibm.com>
This commit is contained in:
Daniel Lezcano
2010-05-12 23:44:28 +02:00
committed by Daniel Lezcano
parent 196db713a9
commit 33fcb7a047
5 changed files with 39 additions and 27 deletions

View File

@@ -374,7 +374,8 @@ static int setup_utsname(struct utsname *utsname)
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];
int i;
@@ -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];
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
* 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)
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";
if (!rootfs)
if (!rootfs->path)
return 0;
if (mount(rootfs, tmpfs, "none", MS_BIND|MS_REC, NULL)) {
SYSERROR("failed to mount '%s'->'%s'", rootfs, "/tmp");
if (mount(rootfs->path, tmpfs, "none", MS_BIND|MS_REC, NULL)) {
SYSERROR("failed to mount '%s'->'%s'", rootfs->path, "/tmp");
return -1;
}
DEBUG("mounted '%s' on '%s'", rootfs, tmpfs);
DEBUG("mounted '%s' on '%s'", rootfs->path, tmpfs);
if (setup_rootfs_pivot_root(tmpfs, pivotdir)) {
ERROR("failed to pivot_root to '%s'", rootfs);
if (setup_rootfs_pivot_root(tmpfs, rootfs->pivot)) {
ERROR("failed to pivot_root to '%s'", rootfs->pivot);
return -1;
}
@@ -640,16 +641,17 @@ out:
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];
struct stat s;
/* We don't have a rootfs, /dev/console will be shared */
if (!rootfs)
if (!rootfs->path)
return 0;
snprintf(path, sizeof(path), "%s/dev/console", rootfs);
snprintf(path, sizeof(path), "%s/dev/console", rootfs->path);
if (access(path, F_OK)) {
WARN("rootfs specified but no console found");
@@ -1415,17 +1417,17 @@ int lxc_setup(const char *name, struct lxc_conf *lxc_conf)
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);
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);
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);
return -1;
}

View File

@@ -162,6 +162,17 @@ struct lxc_console {
struct termios *tios;
};
/*
* 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
* @rootfs : root directory to run the container
@@ -178,8 +189,6 @@ struct lxc_console {
* @console : console data
*/
struct lxc_conf {
char *rootfs;
char *pivotdir;
char *fstab;
int tty;
int pts;
@@ -191,6 +200,7 @@ struct lxc_conf {
struct lxc_list caps;
struct lxc_tty_info tty_info;
struct lxc_console console;
struct lxc_rootfs rootfs;
};
/*

View File

@@ -643,8 +643,8 @@ static int config_rootfs(const char *key, char *value, struct lxc_conf *lxc_conf
return -1;
}
lxc_conf->rootfs = strdup(value);
if (!lxc_conf->rootfs) {
lxc_conf->rootfs.path = strdup(value);
if (!lxc_conf->rootfs.path) {
SYSERROR("failed to duplicate string %s", value);
return -1;
}
@@ -659,8 +659,8 @@ static int config_pivotdir(const char *key, char *value, struct lxc_conf *lxc_co
return -1;
}
lxc_conf->pivotdir = strdup(value);
if (!lxc_conf->pivotdir) {
lxc_conf->rootfs.pivot = strdup(value);
if (!lxc_conf->rootfs.pivot) {
SYSERROR("failed to duplicate string %s", value);
return -1;
}

View File

@@ -145,7 +145,7 @@ int lxc_create_console(struct lxc_conf *conf)
struct lxc_console *console = &conf->console;
int fd;
if (!conf->rootfs)
if (!conf->rootfs.path)
return 0;
if (!console->path)
@@ -155,7 +155,7 @@ int lxc_create_console(struct lxc_conf *conf)
console->name, NULL, NULL)) {
SYSERROR("failed to allocate a pty");
return -1;
}
}
if (fcntl(console->master, F_SETFD, FD_CLOEXEC)) {
SYSERROR("failed to set console master to close-on-exec");
@@ -262,7 +262,7 @@ int lxc_console_mainloop_add(struct lxc_epoll_descr *descr,
struct lxc_conf *conf = handler->conf;
struct lxc_console *console = &conf->console;
if (!conf->rootfs) {
if (!conf->rootfs.path) {
INFO("no rootfs, no console.");
return 0;
}

View File

@@ -56,7 +56,7 @@ static int utmp_handler(int fd, void *data, struct lxc_epoll_descr *descr)
return -1;
}
if (snprintf(path, MAXPATHLEN, "%s/var/run/utmp", conf->rootfs) >
if (snprintf(path, MAXPATHLEN, "%s/var/run/utmp", conf->rootfs.path) >
MAXPATHLEN) {
ERROR("path is too long");
return -1;
@@ -114,10 +114,10 @@ int lxc_utmp_mainloop_add(struct lxc_epoll_descr *descr,
char path[MAXPATHLEN];
int fd, wd;
if (!conf->rootfs)
if (!conf->rootfs.path)
return 0;
if (snprintf(path, MAXPATHLEN, "%s/var/run/utmp", conf->rootfs) >
if (snprintf(path, MAXPATHLEN, "%s/var/run/utmp", conf->rootfs.path) >
MAXPATHLEN) {
ERROR("path is too long");
return -1;