[lxc-devel] [PATCH 1/1] update hwaddr to fill in xx at create time

Serge Hallyn serge.hallyn at ubuntu.com
Tue Jan 20 16:59:27 UTC 2015


Commit 67702c21 regressed the case where lxc-create use a config
file with 'xx:xx' in lxc.network.hwaddr, so that the 'xx' were
preserved in the container's configuration file.  Expand those
in the unexpanded_config file whenever we are reading a
config file which is not coming from a 'lxc.include'.

The config file will have \n-terminated lines, so update
rand_complete_hwaddr to also stop on \n.

Add a test case to make sure xx gets expanded at lxc-create.

Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 src/lxc/confile.c               | 33 +++++++++++++++++++++++++++++++-
 src/tests/Makefile.am           |  3 ++-
 src/tests/lxc-test-createconfig | 42 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 76 insertions(+), 2 deletions(-)
 create mode 100644 src/tests/lxc-test-createconfig

diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index 1d42941..a21ee1a 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -551,7 +551,7 @@ static int rand_complete_hwaddr(char *hwaddr)
 #else
 	unsigned int seed=randseed(false);
 #endif
-	while (*curs != '\0')
+	while (*curs != '\0' && *curs != '\n')
 	{
 		if ( *curs == 'x' || *curs == 'X' ) {
 			if (curs - hwaddr == 1) {
@@ -1642,10 +1642,41 @@ static int config_console_logfile(const char *key, const char *value,
 	return config_path_item(&lxc_conf->console.log_path, value);
 }
 
+/*
+ * If we find a lxc.network.hwaddr in the original config file,
+ * we expand it in the unexpanded_config, so that after a save_config
+ * we store the hwaddr for re-use.
+ * This is only called when reading the config file, not when executing
+ * a lxc.include.
+ * 'x' and 'X' are substituted in-place.
+ */
+static void update_hwaddr(const char *line)
+{
+	char *p;
+
+	line += lxc_char_left_gc(line, strlen(line));
+	if (line[0] == '#')
+		return;
+	if (strncmp(line, "lxc.network.hwaddr", 18) != 0)
+		return;
+	p = strchr(line, '=');
+	if (!p)
+		return;  // let config_network_hwaddr raise the error
+	p++;
+	while (isblank(*p))
+		p++;
+	if (!*p)
+		return;
+
+	rand_complete_hwaddr(p);
+}
+
 int append_unexp_config_line(const char *line, struct lxc_conf *conf)
 {
 	size_t len = conf->unexpanded_len, linelen = strlen(line);
 
+	update_hwaddr(line);
+
 	while (conf->unexpanded_alloced <= len + linelen + 2) {
 		char *tmp = realloc(conf->unexpanded_config, conf->unexpanded_alloced + 1024);
 		if (!tmp)
diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am
index 495f1cf..461d869 100644
--- a/src/tests/Makefile.am
+++ b/src/tests/Makefile.am
@@ -48,7 +48,7 @@ bin_PROGRAMS = lxc-test-containertests lxc-test-locktests lxc-test-startone \
 	lxc-test-reboot lxc-test-list lxc-test-attach lxc-test-device-add-remove \
 	lxc-test-apparmor
 
-bin_SCRIPTS = lxc-test-autostart lxc-test-cloneconfig
+bin_SCRIPTS = lxc-test-autostart lxc-test-cloneconfig lxc-test-createconfig
 
 if DISTRO_UBUNTU
 bin_SCRIPTS += \
@@ -79,6 +79,7 @@ EXTRA_DIST = \
 	lxc-test-apparmor-mount \
 	lxc-test-checkpoint-restore \
 	lxc-test-cloneconfig \
+	lxc-test-createconfig \
 	lxc-test-ubuntu \
 	lxc-test-unpriv \
 	may_control.c \
diff --git a/src/tests/lxc-test-createconfig b/src/tests/lxc-test-createconfig
new file mode 100644
index 0000000..6b74d1e
--- /dev/null
+++ b/src/tests/lxc-test-createconfig
@@ -0,0 +1,42 @@
+#!/bin/bash
+
+# lxc: linux Container library
+
+# Authors:
+# Serge Hallyn <serge.hallyn at ubuntu.com>
+#
+# This is a test script for the lxc-user-nic program
+
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+set -e
+
+s=`mktemp -d /tmp/lxctest-XXXXXX`
+f="$s/in.conf"
+
+cleanup() {
+	lxc-destroy -n lxctestc || true
+	rm -rf $s
+}
+
+trap cleanup EXIT
+
+cat > $f << EOF
+lxc.network.type = veth
+lxc.network.hwaddr = 00:16:3e:xx:xx:xx
+EOF
+lxc-create -t busybox -f $f -n lxctestc
+grep -q "xx:xx" /var/lib/lxc/lxctestc/config && { echo "hwaddr not expanded"; exit 1; }
+echo "Success"
-- 
2.1.0



More information about the lxc-devel mailing list