[lxc-devel] [patch 5/6] [draft] Change conf.c for new mount interface fstab

Andrian Nord nightnord at gmail.com
Mon Nov 16 22:31:29 UTC 2009


This patch removes all mount functions from conf.c, replacing their use
with new interface. Also, there is no need for temporary file for
mount.entries, as now they are parsed not via mntent (let's hope, that
fstab format/mntent structure will change not very soon ;))

Signed-off-by: Andrian Nord <NightNord at gmail.com>

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 369a132..c59a0ee 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -27,17 +27,15 @@
 #include <errno.h>
 #include <string.h>
 #include <dirent.h>
-#include <mntent.h>
 #include <unistd.h>
 #include <pty.h>
 
 #include <sys/types.h>
 #include <sys/utsname.h>
 #include <sys/param.h>
-#include <sys/stat.h>
 #include <sys/socket.h>
-#include <sys/mount.h>
 #include <sys/mman.h>
+#include <sys/mount.h>
 
 #include <arpa/inet.h>
 #include <fcntl.h>
@@ -46,6 +44,7 @@
 #include <libgen.h>
 
 #include "network.h"
+#include "mount.h"
 #include "error.h"
 #include "parse.h"
 #include "config.h"
@@ -66,12 +65,6 @@ lxc_log_define(lxc_conf, lxc);
 
 typedef int (*instanciate_cb)(struct lxc_netdev *);
 
-struct mount_opt {
-	char *name;
-	int clear;
-	int flag;
-};
-
 static int instanciate_veth(struct lxc_netdev *);
 static int instanciate_macvlan(struct lxc_netdev *);
 static int instanciate_phys(struct lxc_netdev *);
@@ -84,30 +77,6 @@ static  instanciate_cb netdev_conf[MAXCONFTYPE + 1] = {
 	[EMPTY]   = instanciate_empty,
 };
 
-static struct mount_opt mount_opt[] = {
-	{ "defaults",   0, 0              },
-	{ "ro",         0, MS_RDONLY      },
-	{ "rw",         1, MS_RDONLY      },
-	{ "suid",       1, MS_NOSUID      },
-	{ "nosuid",     0, MS_NOSUID      },
-	{ "dev",        1, MS_NODEV       },
-	{ "nodev",      0, MS_NODEV       },
-	{ "exec",       1, MS_NOEXEC      },
-	{ "noexec",     0, MS_NOEXEC      },
-	{ "sync",       0, MS_SYNCHRONOUS },
-	{ "async",      1, MS_SYNCHRONOUS },
-	{ "remount",    0, MS_REMOUNT     },
-	{ "mand",       0, MS_MANDLOCK    },
-	{ "nomand",     1, MS_MANDLOCK    },
-	{ "atime",      1, MS_NOATIME     },
-	{ "noatime",    0, MS_NOATIME     },
-	{ "diratime",   1, MS_NODIRATIME  },
-	{ "nodiratime", 0, MS_NODIRATIME  },
-	{ "bind",       0, MS_BIND        },
-	{ "rbind",      0, MS_BIND|MS_REC },
-	{ NULL,         0, 0              },
-};
-
 static int configure_find_fstype_cb(void* buffer, void *data)
 {
 	struct cbarg {
@@ -478,131 +447,19 @@ out:
 	return ret;
 }
 
-static void parse_mntopt(char *opt, unsigned long *flags, char **data)
-{
-	struct mount_opt *mo;
-
-	/* If opt is found in mount_opt, set or clear flags.
-	 * Otherwise append it to data. */
-
-	for (mo = &mount_opt[0]; mo->name != NULL; mo++) {
-		if (!strncmp(opt, mo->name, strlen(mo->name))) {
-			if (mo->clear)
-				*flags &= ~mo->flag;
-			else
-				*flags |= mo->flag;
-			return;
-		}
-	}
-
-	if (strlen(*data))
-		strcat(*data, ",");
-	strcat(*data, opt);
-}
-
-static int parse_mntopts(struct mntent *mntent, unsigned long *mntflags,
-			 char **mntdata)
-{
-	char *s, *data;
-	char *p, *saveptr = NULL;
-
-	if (!mntent->mnt_opts)
-		return 0;
-
-	s = strdup(mntent->mnt_opts);
-	if (!s) {
-		SYSERROR("failed to allocate memory");
-		return -1;
-	}
-
-	data = malloc(strlen(s) + 1);
-	if (!data) {
-		SYSERROR("failed to allocate memory");
-		free(s);
-		return -1;
-	}
-	*data = 0;
-
-	for (p = strtok_r(s, ",", &saveptr); p != NULL;
-	     p = strtok_r(NULL, ",", &saveptr))
-		parse_mntopt(p, mntflags, &data);
-
-	if (*data)
-		*mntdata = data;
-	else
-		free(data);
-	free(s);
-
-	return 0;
-}
-
-static int mount_file_entries(FILE *file, const char *filename)
-{
-	struct mntent *mntent;
-	int ret = -1;
-	unsigned long mntflags;
-	char *mntdata;
-
-	while ((mntent = getmntent(file))) {
-
-		mntflags = 0;
-		mntdata = NULL;
-		if (parse_mntopts(mntent, &mntflags, &mntdata) < 0) {
-			ERROR("%s: failed to parse mount option '%s'",
-						filename, mntent->mnt_opts);
-			goto out;
-		}
-
-		if (mount(mntent->mnt_fsname, mntent->mnt_dir,
-			  mntent->mnt_type, mntflags, mntdata)) {
-			SYSERROR("%s: failed to mount '%s' on '%s'", filename,
-					mntent->mnt_fsname, mntent->mnt_dir);
-			goto out;
-		}
-
-		DEBUG("%s: mounted %s on %s, type %s", filename,
-			mntent->mnt_fsname, mntent->mnt_dir, mntent->mnt_type);
-
-		free(mntdata);
-	}
-
-	ret = 0;
-
-out:
-	return ret;
-}
-
-static int process_fstab(const char *fstab)
-{
-	FILE *file;
-	int ret;
-
-	file = setmntent(fstab, "r");
-	if (!file) {
-		SYSERROR("failed to use '%s'", fstab);
-		return -1;
-	}
-
-	ret = mount_file_entries(file, fstab);
-
-	endmntent(file);
-
-	DEBUG("fstab file '%s' has been setup", fstab);
-
-	return ret;
-}
-
-static int setup_mount(struct lxc_list *fstab_files)
+static int setup_mount(struct lxc_conf *lxc_conf)
 {
 	struct lxc_list *iterator;
 	char *fstab;
 	int ret = 0;
 
-	lxc_list_for_each(iterator, fstab_files) {
+	lxc_list_for_each(iterator, &lxc_conf->fstab_files) {
 		fstab = iterator->elem;
 
-		if ((ret = process_fstab(fstab)))
+		if ((ret = lxc_mount_fstab(fstab, lxc_conf))) {
+			ERROR("mounting fstab '%s' failed", fstab);
 			goto out;
+		}
 	}
 
 	INFO("fstab files have been setup");
@@ -611,32 +468,24 @@ out:
 	return ret;
 }
 
-static int setup_mount_entries(struct lxc_list *mount)
+static int setup_mount_entries(struct lxc_list *mounts)
 {
-	FILE *file;
 	struct lxc_list *iterator;
 	char *mount_entry;
 	int ret;
 
-	file = tmpfile();
-	if (!file) {
-		ERROR("tmpfile error: %m");
-		return -1;
-	}
-
-	lxc_list_for_each(iterator, mount) {
+	lxc_list_for_each(iterator, mounts) {
 		mount_entry = iterator->elem;
-		fprintf(file, "%s", mount_entry);
-	}
-
-	rewind(file);
-
-	ret = mount_file_entries(file, "lxc.mount.entry");
 
-	fclose(file);
+		if ((ret = lxc_mount_line(mount_entry))) {
+			ERROR("mounting entry '%s' failed", mount_entry);
+			goto out;
+		}
+	}
 
 	INFO("mount entries have been setup");
 
+out:
 	return ret;
 }
 
@@ -1103,7 +952,7 @@ int lxc_setup(const char *name, struct lxc_conf *lxc_conf)
 		return -1;
 	}
 
-	if (setup_mount(&lxc_conf->fstab_files)) {
+	if (setup_mount(lxc_conf)) {
 		ERROR("failed to setup the mounts for '%s'", name);
 		return -1;
 	}




More information about the lxc-devel mailing list