[lxc-devel] [PATCH 1/2] add clonehostname hook

Dwight Engen dwight.engen at oracle.com
Tue Jul 9 18:44:30 UTC 2013


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 at oracle.com>
---
 doc/lxc-clone.sgml.in  |  3 ++-
 hooks/Makefile.am      |  1 +
 hooks/clonehostname    | 29 +++++++++++++++++++++++++++++
 src/lxc/lxccontainer.c |  9 +++++++--
 4 files changed, 39 insertions(+), 3 deletions(-)
 create mode 100755 hooks/clonehostname

diff --git a/doc/lxc-clone.sgml.in b/doc/lxc-clone.sgml.in
index 6885ff7..74982a1 100644
--- a/doc/lxc-clone.sgml.in
+++ b/doc/lxc-clone.sgml.in
@@ -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>
diff --git a/hooks/Makefile.am b/hooks/Makefile.am
index 196e632..3e32ce0 100644
--- a/hooks/Makefile.am
+++ b/hooks/Makefile.am
@@ -1,6 +1,7 @@
 hooksdir=@LXCHOOKDIR@
 
 hooks_SCRIPTS = \
+	clonehostname \
 	mountcgroups \
 	mountecryptfsroot
 
diff --git a/hooks/clonehostname b/hooks/clonehostname
new file mode 100755
index 0000000..79d14a5
--- /dev/null
+++ b/hooks/clonehostname
@@ -0,0 +1,29 @@
+#!/bin/bash
+#
+# 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
diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index 4e71fb1..43acf47 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -1767,7 +1767,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];
@@ -1797,6 +1799,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 container name");
+		}
 		if (setenv("LXC_NAME", c->name, 1)) {
 			SYSERROR("failed to set environment variable for container name");
 		}
@@ -1955,7 +1960,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
-- 
1.8.1.4





More information about the lxc-devel mailing list