[lxc-devel] [patch 5b/6] [draft] Replace mount function in conf.c

Andrian Nord nightnord at gmail.com
Tue Nov 17 23:31:36 UTC 2009


Updated patch for new git head, with missing part of multifstab support.

git diff has done something very strange...

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

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 9d4285d..9fe3a31 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"
@@ -67,12 +66,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 *);
@@ -85,30 +78,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 {
@@ -479,144 +448,43 @@ out:
 	return ret;
 }
 
-static void parse_mntopt(char *opt, unsigned long *flags, char **data)
+static int setup_mount(struct lxc_conf *lxc_conf)
 {
-	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)
-{
-	struct mntent *mntent;
-	int ret = -1;
-	unsigned long mntflags;
-	char *mntdata;
-
-	while ((mntent = getmntent(file))) {
+	struct lxc_list *iterator;
+	char *fstab;
+	int ret;
 
-		mntflags = 0;
-		mntdata = NULL;
-		if (parse_mntopts(mntent, &mntflags, &mntdata) < 0) {
-			ERROR("failed to parse mount option '%s'",
-				      mntent->mnt_opts);
-			goto out;
-		}
+	lxc_list_for_each(iterator, &lxc_conf->fstab_files) {
+		fstab = iterator->elem;
 
-		if (mount(mntent->mnt_fsname, mntent->mnt_dir,
-			  mntent->mnt_type, mntflags, mntdata)) {
-			SYSERROR("failed to mount '%s' on '%s'",
-					 mntent->mnt_fsname, mntent->mnt_dir);
+		if ((ret = lxc_mount_fstab(fstab, lxc_conf))) {
+			ERROR("mounting fstab '%s' failed", fstab);
 			goto out;
 		}
-
-		DEBUG("mounted %s on %s, type %s", mntent->mnt_fsname,
-		      mntent->mnt_dir, mntent->mnt_type);
-
-		free(mntdata);
 	}
 
-	ret = 0;
-
-	INFO("mount points have been setup");
+	INFO("fstab files have been setup");
 out:
 	return ret;
 }
 
-static int setup_mount(const char *fstab)
+static int setup_mount_entries(struct lxc_list *mounts)
 {
-	FILE *file;
-	int ret;
-
-	if (!fstab)
-		return 0;
-
-	file = setmntent(fstab, "r");
-	if (!file) {
-		SYSERROR("failed to use '%s'", fstab);
-		return -1;
-	}
-
-	ret = mount_file_entries(file);
-
-	endmntent(file);
-	return ret;
-}
-
-static int setup_mount_entries(struct lxc_list *mount)
-{
-	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);
+		if ((ret = lxc_mount_line(mount_entry))) {
+			ERROR("mounting lxc.mount.entry failed");
+			goto out;
+		}
+	}
 
-	fclose(file);
+	INFO("fstab files have been setup");
+out:
 	return ret;
 }
 
@@ -1079,7 +947,7 @@ int lxc_setup(const char *name, struct lxc_conf *lxc_conf)
 		return -1;
 	}
 
-	if (setup_mount(lxc_conf->fstab)) {
+	if (setup_mount(lxc_conf)) {
 		ERROR("failed to setup the mounts for '%s'", name);
 		return -1;
 	}




More information about the lxc-devel mailing list