]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
add clonehostname hook
authorDwight Engen <dwight.engen@oracle.com>
Tue, 9 Jul 2013 20:19:45 +0000 (16:19 -0400)
committerSerge Hallyn <serge.hallyn@ubuntu.com>
Wed, 10 Jul 2013 19:08:43 +0000 (14:08 -0500)
This hook script updates the hostname in various files under /etc in the
cloned container. In order to do so, the old container name is passed in
the LXC_SRC_NAME environment variable.

Signed-off-by: Dwight Engen <dwight.engen@oracle.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
doc/lxc-clone.sgml.in
hooks/Makefile.am
hooks/clonehostname [new file with mode: 0755]
src/lxc/lxccontainer.c

index 6885ff73caf03a8a31a8973a3a3e5bfe31627753..74982a1d1a013576e842b7110584398e88a326b1 100644 (file)
@@ -255,7 +255,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
       argument 4.  The <filename>LXC_ROOTFS_MOUNT</filename> environment variable gives
       the path under which the container's root filesystem is mounted.  The
       configuration file pathname is stored in <filename>LXC_CONFIG_FILE</filename>, the
-      container name in <filename>LXC_NAME</filename>, and the path or device on which
+      new container name in <filename>LXC_NAME</filename>, the old container name in
+      <filename>LXC_SRC_NAME</filename>, and the path or device on which
       the rootfs is located is in <filename>LXC_ROOTFS_PATH</filename>.
     </para>
   </refsect1>
index 196e632f4f327fd46e1795960818f0639d0cde30..3e32ce0902554f91e3c388b7a333bd3d35a8050f 100644 (file)
@@ -1,6 +1,7 @@
 hooksdir=@LXCHOOKDIR@
 
 hooks_SCRIPTS = \
+       clonehostname \
        mountcgroups \
        mountecryptfsroot
 
diff --git a/hooks/clonehostname b/hooks/clonehostname
new file mode 100755 (executable)
index 0000000..8865c2d
--- /dev/null
@@ -0,0 +1,29 @@
+#!/bin/sh
+#
+# Update the hostname in the cloned container's scripts
+#
+# Copyright ©  2013 Oracle.
+#
+# This library is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2, as
+# published by the Free Software Foundation.
+#
+# This program 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# Note that /etc/hostname is updated by lxc itself
+for file in \
+    $LXC_ROOTFS_PATH/etc/sysconfig/network \
+    $LXC_ROOTFS_PATH/etc/sysconfig/network-scripts/ifcfg-* ;
+do
+    if [ -f $file ]; then
+        sed -i "s|$LXC_SRC_NAME|$LXC_NAME|" $file
+    fi
+done
+exit 0
index aaa9f2aebe5616a9b20a671cf2555c7b037e4740..245d5eba58e804eec14a2abcaaf65086a7b60c83 100644 (file)
@@ -1770,7 +1770,9 @@ static int copy_storage(struct lxc_container *c0, struct lxc_container *c,
        return 0;
 }
 
-static int clone_update_rootfs(struct lxc_container *c, int flags, char **hookargs)
+static int clone_update_rootfs(struct lxc_container *c0,
+                              struct lxc_container *c, int flags,
+                              char **hookargs)
 {
        int ret = -1;
        char path[MAXPATHLEN];
@@ -1800,6 +1802,9 @@ static int clone_update_rootfs(struct lxc_container *c, int flags, char **hookar
 
        if (!lxc_list_empty(&conf->hooks[LXCHOOK_CLONE])) {
                /* Start of environment variable setup for hooks */
+               if (setenv("LXC_SRC_NAME", c0->name, 1)) {
+                       SYSERROR("failed to set environment variable for source container name");
+               }
                if (setenv("LXC_NAME", c->name, 1)) {
                        SYSERROR("failed to set environment variable for container name");
                }
@@ -1958,7 +1963,7 @@ struct lxc_container *lxcapi_clone(struct lxc_container *c, const char *newname,
        if (!c2->save_config(c2, NULL))
                goto out;
 
-       if (clone_update_rootfs(c2, flags, hookargs) < 0)
+       if (clone_update_rootfs(c, c2, flags, hookargs) < 0)
                goto out;
 
        // TODO: update c's lxc.snapshot = count