[lxc-devel] [PATCH RFC] add lxc-user-nic

Serge Hallyn serge.hallyn at ubuntu.com
Sat Aug 10 02:17:03 UTC 2013


Quoting Stéphane Graber (stgraber at ubuntu.com):
> Probably should be under /run as we don't want this to persist across
> reboots.

Good thinking, and

...

> > +/* TODO These should be set through configure.ac */
> > +#define CONF_FILE "/etc/lxc/lxc-usernet"
> > +#define DB_FILE "/var/lib/lxc/nics"
> > +#endif
> 
> ^ We should solve that TODO before applying to staging

Both done in this new commit.  I also create the db conf dir if it
doesn't exist, and fix some printfs to go to stderr.

Subject: [PATCH 1/1] lxc-user-nic: specify config and db files in autoconf

Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 configure.ac           | 14 ++++++++++++++
 src/lxc/Makefile.am    |  4 +++-
 src/lxc/lxc_user_nic.c | 40 ++++++++++++++++++++++++++++++++--------
 3 files changed, 49 insertions(+), 9 deletions(-)

diff --git a/configure.ac b/configure.ac
index 1131f8b..cdf5290 100644
--- a/configure.ac
+++ b/configure.ac
@@ -189,6 +189,18 @@ AC_ARG_WITH([global-conf],
 		[global lxc configuration file]
 	)], [], [with_global_conf=['${sysconfdir}/lxc/lxc.conf']])
 
+AC_ARG_WITH([usernic-conf],
+	[AC_HELP_STRING(
+		[--with-usernic-conf],
+		[user network interface configuration file]
+	)], [], [with_usernic_conf=['${sysconfdir}/lxc/lxc-usernet']])
+
+AC_ARG_WITH([usernic-db],
+	[AC_HELP_STRING(
+		[--with-usernic-db],
+		[lxc user nic database]
+	)], [], [with_usernic_db=['/run/lxc/nics']])
+
 # Rootfs path, where the container mount structure is assembled
 AC_ARG_WITH([rootfs-path],
 	[AC_HELP_STRING(
@@ -231,6 +243,8 @@ AS_AC_EXPAND(LXC_DISTRO_CONF, "$distroconf")
 AS_AC_EXPAND(LXC_GENERATE_DATE, "$(date)")
 AS_AC_EXPAND(LXCPATH, "$with_config_path")
 AS_AC_EXPAND(LXC_GLOBAL_CONF, "$with_global_conf")
+AS_AC_EXPAND(LXC_USERNIC_CONF, "$with_usernic_conf")
+AS_AC_EXPAND(LXC_USERNIC_DB, "$with_usernic_db")
 AS_AC_EXPAND(LXCROOTFSMOUNT, "$with_rootfs_path")
 AS_AC_EXPAND(LXCTEMPLATEDIR, "$datadir/lxc/templates")
 AS_AC_EXPAND(LXCHOOKDIR, "$datadir/lxc/hooks")
diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am
index 611917c..9d6b5b4 100644
--- a/src/lxc/Makefile.am
+++ b/src/lxc/Makefile.am
@@ -95,7 +95,9 @@ AM_CFLAGS=-I$(top_srcdir)/src \
 	-DLXCINITDIR=\"$(LXCINITDIR)\" \
 	-DLXCTEMPLATEDIR=\"$(LXCTEMPLATEDIR)\" \
 	-DLOGPATH=\"$(LOGPATH)\" \
-	-DLXC_DEFAULT_CONFIG=\"$(LXC_DEFAULT_CONFIG)\"
+	-DLXC_DEFAULT_CONFIG=\"$(LXC_DEFAULT_CONFIG)\" \
+	-DLXC_USERNIC_DB=\"$(LXC_USERNIC_DB)\" \
+	-DLXC_USERNIC_CONF=\"$(LXC_USERNIC_CONF)\"
 
 if ENABLE_APPARMOR
 AM_CFLAGS += -DHAVE_APPARMOR
diff --git a/src/lxc/lxc_user_nic.c b/src/lxc/lxc_user_nic.c
index 757e026..26bae54 100644
--- a/src/lxc/lxc_user_nic.c
+++ b/src/lxc/lxc_user_nic.c
@@ -45,9 +45,8 @@
 #define CONF_FILE "/tmp/lxc-usernet"
 #define DB_FILE "/tmp/nics"
 #else
-/* TODO These should be set through configure.ac */
-#define CONF_FILE "/etc/lxc/lxc-usernet"
-#define DB_FILE "/var/lib/lxc/nics"
+#define CONF_FILE LXC_USERNIC_CONF
+#define DB_FILE LXC_USERNIC_DB
 #endif
 
 
@@ -87,7 +86,7 @@
 
 void usage(char *me, bool fail)
 {
-	printf("Usage: %s pid type bridge\n", me);
+	fprintf(stderr, "Usage: %s pid type bridge\n", me);
 	exit(fail ? 1 : 0);
 }
 
@@ -716,6 +715,27 @@ bool get_nic_if_avail(int fd, char *me, char *pid, char *intype, char *br, int a
 	return true;
 }
 
+bool create_db_dir(char *fnam)
+{
+	char *p = alloca(strlen(fnam)+1);
+
+	strcpy(p, fnam);
+	fnam = p;
+	p = p + 1;
+again:
+	while (*p && *p != '/') p++;
+	if (!*p)
+		return true;
+	*p = '\0';
+	if (mkdir(fnam, 0755) && errno != EEXIST) {
+		fprintf(stderr, "failed to create %s\n", fnam);
+		*p = '/';
+		return false;
+	}
+	*(p++) = '/';
+	goto again;
+}
+
 int main(int argc, char *argv[])
 {
 	int n, fd;
@@ -724,15 +744,20 @@ int main(int argc, char *argv[])
 	char *nicname = alloca(40);
 
 	if ((me = get_username(&buf)) == NULL) {
-		printf("Failed to get username\n");
+		fprintf(stderr, "Failed to get username\n");
 		exit(1);
 	}
 
 	if (argc != 4)
 		usage(argv[0], true);
 
+	if (!create_db_dir(DB_FILE)) {
+		fprintf(stderr, "Failed to create directory for db file\n");
+		exit(1);
+	}
+
 	if ((fd = open_and_lock(DB_FILE)) < 0) {
-		printf("Failed to lock %s\n", DB_FILE);
+		fprintf(stderr, "Failed to lock %s\n", DB_FILE);
 		exit(1);
 	}
 
@@ -741,10 +766,9 @@ int main(int argc, char *argv[])
 		gotone = get_nic_if_avail(fd, me, argv[1], argv[2], argv[3], n, &nicname);
 	close(fd);
 	if (!gotone) {
-		printf("Quota reached\n");
+		fprintf(stderr, "Quota reached\n");
 		exit(1);
 	}
-	printf("got nic name %s\n", nicname);
 
 	// Now create the link
 
-- 
1.8.1.2





More information about the lxc-devel mailing list