]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
update hwaddr to fill in xx at create time
authorSerge Hallyn <serge.hallyn@ubuntu.com>
Tue, 20 Jan 2015 16:59:27 +0000 (16:59 +0000)
committerStéphane Graber <stgraber@ubuntu.com>
Tue, 20 Jan 2015 21:40:27 +0000 (16:40 -0500)
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@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
src/lxc/confile.c
src/tests/Makefile.am
src/tests/lxc-test-createconfig [new file with mode: 0644]

index 1d429415a921a89730f41054323e432c7fc5c0db..a21ee1a6bc6b5e74d2cd0918fae311472d4515a2 100644 (file)
@@ -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)
index 495f1cfcb76fe6a7844f72221aca3ffafec64252..461d86984ebc94d52166719e912d065a42b03d77 100644 (file)
@@ -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 (file)
index 0000000..6b74d1e
--- /dev/null
@@ -0,0 +1,42 @@
+#!/bin/bash
+
+# lxc: linux Container library
+
+# Authors:
+# Serge Hallyn <serge.hallyn@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"