[lxc-devel] [PATCH] implement backend drivers and container clone API (v2)

Serge Hallyn serge.hallyn at ubuntu.com
Thu Apr 25 00:02:51 UTC 2013


Quoting S.Çağlar Onur (caglar at 10ur.org):
> Hi Serge,
> 
> [resending as list rejected my first mail due to its size]
> 
> On Wed, Apr 24, 2013 at 9:26 AM, Serge Hallyn <serge.hallyn at ubuntu.com>wrote:
> >
> > +static void new_hwaddr(char *hwaddr)
> > +{
> > +       snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x",
> > +                       rand() % 255, rand() % 255, rand() % 255);
> > +}
> >
> 
> I believe we need to call srand here otherwise it will always give the same
> set of numbers with the same sequence

Subject: [PATCH 1/2] initialize random generator when picking new macaddr

Also fix wrong use of bitmask flags

Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 src/lxc/lxccontainer.c | 15 +++++++++++++--
 src/lxc/lxccontainer.h | 14 ++++++--------
 2 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index e4ff92c..ef111c0 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -1248,6 +1248,16 @@ static int copyhooks(struct lxc_container *oldc, struct lxc_container *c)
 
 static void new_hwaddr(char *hwaddr)
 {
+	FILE *f = fopen("/dev/urandom", "r");
+	if (f) {
+		unsigned int seed;
+		int ret = fread(&seed, sizeof(seed), 1, f);
+		if (ret != 1)
+			seed = time(NULL);
+		fclose(f);
+		srand(seed);
+	} else
+		srand(time(NULL));
 	snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x",
 			rand() % 255, rand() % 255, rand() % 255);
 }
@@ -1255,6 +1265,7 @@ static void new_hwaddr(char *hwaddr)
 static void network_new_hwaddrs(struct lxc_container *c)
 {
 	struct lxc_list *it;
+
 	lxc_list_for_each(it, &c->lxc_conf->network) {
 		struct lxc_netdev *n = it->elem;
 		if (n->hwaddr)
@@ -1395,7 +1406,7 @@ static int create_file_dirname(char *path)
 }
 
 struct lxc_container *lxcapi_clone(struct lxc_container *c, const char *newname,
-		const char *lxcpath, enum lxc_clone_flags flags,
+		const char *lxcpath, int flags,
 		const char *bdevtype, const char *bdevdata, unsigned long newsize)
 {
 	struct lxc_container *c2 = NULL;
@@ -1509,7 +1520,7 @@ out:
 	lxcunlock(c->privlock);
 	if (c2)
 		lxc_container_put(c2);
-		
+
 	return NULL;
 }
 
diff --git a/src/lxc/lxccontainer.h b/src/lxc/lxccontainer.h
index 3bebdf3..a4be753 100644
--- a/src/lxc/lxccontainer.h
+++ b/src/lxc/lxccontainer.h
@@ -6,13 +6,11 @@
 
 #include <stdbool.h>
 
-enum lxc_clone_flags {
-	LXC_CLONE_KEEPNAME,
-	LXC_CLONE_COPYHOOKS,
-	LXC_CLONE_KEEPMACADDR,
-	LXC_CLONE_SNAPSHOT,
-	LXC_CLONE_MAXFLAGS,
-};
+#define LXC_CLONE_KEEPNAME        (1 << 0)
+#define LXC_CLONE_COPYHOOKS       (1 << 1)
+#define LXC_CLONE_KEEPMACADDR     (1 << 2)
+#define LXC_CLONE_SNAPSHOT        (1 << 3)
+#define LXC_CLONE_MAXFLAGS        (1 << 4)
 
 struct lxc_container {
 	// private fields
@@ -106,7 +104,7 @@ struct lxc_container {
 	 *  will be duplicated.
 	 */
 	struct lxc_container *(*clone)(struct lxc_container *c, const char *newname,
-		const char *lxcpath, enum lxc_clone_flags flags, const char *bdevtype,
+		const char *lxcpath, int flags, const char *bdevtype,
 		const char *bdevdata, unsigned long newsize);
 
 #if 0
-- 
1.8.1.2





More information about the lxc-devel mailing list