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 <dwight.engen@oracle.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
Dwight Engen 2013-07-09 16:19:45 -04:00 committed by Serge Hallyn
parent b9b3a92f66
commit 1143ed392d
4 changed files with 39 additions and 3 deletions

View 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 argument 4. The <filename>LXC_ROOTFS_MOUNT</filename> environment variable gives
the path under which the container's root filesystem is mounted. The the path under which the container's root filesystem is mounted. The
configuration file pathname is stored in <filename>LXC_CONFIG_FILE</filename>, 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>. the rootfs is located is in <filename>LXC_ROOTFS_PATH</filename>.
</para> </para>
</refsect1> </refsect1>

View File

@ -1,6 +1,7 @@
hooksdir=@LXCHOOKDIR@ hooksdir=@LXCHOOKDIR@
hooks_SCRIPTS = \ hooks_SCRIPTS = \
clonehostname \
mountcgroups \ mountcgroups \
mountecryptfsroot mountecryptfsroot

29
hooks/clonehostname Executable file
View File

@ -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

View File

@ -1770,7 +1770,9 @@ static int copy_storage(struct lxc_container *c0, struct lxc_container *c,
return 0; 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; int ret = -1;
char path[MAXPATHLEN]; 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])) { if (!lxc_list_empty(&conf->hooks[LXCHOOK_CLONE])) {
/* Start of environment variable setup for hooks */ /* 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)) { if (setenv("LXC_NAME", c->name, 1)) {
SYSERROR("failed to set environment variable for container name"); 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)) if (!c2->save_config(c2, NULL))
goto out; goto out;
if (clone_update_rootfs(c2, flags, hookargs) < 0) if (clone_update_rootfs(c, c2, flags, hookargs) < 0)
goto out; goto out;
// TODO: update c's lxc.snapshot = count // TODO: update c's lxc.snapshot = count