From: Dwight Engen Date: Tue, 9 Jul 2013 20:19:45 +0000 (-0400) Subject: add clonehostname hook X-Git-Tag: lxc-1.0.0.alpha1~1^2~131 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1143ed392d2760e8f7aeee88d570bb0ba151885f;p=thirdparty%2Flxc.git add clonehostname hook 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 Acked-by: Stéphane Graber Signed-off-by: Serge Hallyn --- diff --git a/doc/lxc-clone.sgml.in b/doc/lxc-clone.sgml.in index 6885ff73c..74982a1d1 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 LXC_ROOTFS_MOUNT environment variable gives the path under which the container's root filesystem is mounted. The configuration file pathname is stored in LXC_CONFIG_FILE, the - container name in LXC_NAME, and the path or device on which + new container name in LXC_NAME, the old container name in + LXC_SRC_NAME, and the path or device on which the rootfs is located is in LXC_ROOTFS_PATH. diff --git a/hooks/Makefile.am b/hooks/Makefile.am index 196e632f4..3e32ce090 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 000000000..8865c2df8 --- /dev/null +++ b/hooks/clonehostname @@ -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 diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index aaa9f2aeb..245d5eba5 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -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