[lxc-devel] [PATCH 3/5] encapsulate rootfs data in a structure
Daniel Lezcano
dlezcano at fr.ibm.com
Wed May 12 11:00:24 UTC 2010
From: Daniel Lezcano <daniel.lezcano at free.fr>
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 at fr.ibm.com>
---
src/lxc/conf.c | 32 +++++++++++++++++---------------
src/lxc/conf.h | 14 ++++++++++++--
src/lxc/confile.c | 8 ++++----
src/lxc/console.c | 6 +++---
src/lxc/utmp.c | 6 +++---
5 files changed, 39 insertions(+), 27 deletions(-)
diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 2575413..55eb715 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -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;
}
diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index d0232db..14c931d 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -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
* @rootfs : root directory to run the container
* @pivotdir : pivotdir path, if not set default will be used
@@ -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;
};
/*
diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index 90ccfcd..dd9f2cb 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -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;
}
diff --git a/src/lxc/console.c b/src/lxc/console.c
index 123dc31..d40a511 100644
--- a/src/lxc/console.c
+++ b/src/lxc/console.c
@@ -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;
}
diff --git a/src/lxc/utmp.c b/src/lxc/utmp.c
index 86cd5a0..9b79656 100644
--- a/src/lxc/utmp.c
+++ b/src/lxc/utmp.c
@@ -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;
--
1.6.3.3
More information about the lxc-devel
mailing list