lxc-centos.in revision a2780518da9102cda2d261bd866237710559d348
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# template script for generating centos container for LXC
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# lxc: linux Container library
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# Daniel Lezcano <daniel.lezcano@free.fr>
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# Ramez Hanna <rhanna@informatiq.org>
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# Fajar A. Nugraha <github@fajar.net>
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# Michael H. Warfield <mhw@WittsEnd.com>
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# This library is free software; you can redistribute it and/or
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# modify it under the terms of the GNU Lesser General Public
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# License as published by the Free Software Foundation; either
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# version 2.1 of the License, or (at your option) any later version.
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# This library is distributed in the hope that it will be useful,
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# but WITHOUT ANY WARRANTY; without even the implied warranty of
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# Lesser General Public License for more details.
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# You should have received a copy of the GNU Lesser General Public
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# License along with this library; if not, write to the Free Software
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek#Configurations
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# Some combinations of the tuning knobs below do not exactly make sense.
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# but that's ok.
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# If the "root_password" is non-blank, use it, else set a default.
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# This can be passed to the script as an environment variable and is
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# set by a shell conditional assignment. Looks weird but it is what it is.
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# If the root password contains a ding ($) then try to expand it.
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# That will pick up things like ${name} and ${RANDOM}.
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# If the root password contains more than 3 consecutive X's, pass it as
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# a template to mktemp and take the result.
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# If root_display_password = yes, display the temporary root password at exit.
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# If root_store_password = yes, store it in the configuration directory
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# If root_prompt_password = yes, invoke "passwd" to force the user to change
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# the root password after the container is created.
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# If root_expire_password = yes, you will be prompted to change the root
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# password at the first login.
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# These are conditional assignments... The can be overridden from the
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# preexisting environment variables...
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# Make sure this is in single quotes to defer expansion to later!
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# :{root_password='Root-${name}-${RANDOM}'}
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# Now, it doesn't make much sense to display, store, and force change
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# together. But, we gotta test, right???
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# Prompting for something interactive has potential for mayhem
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# with users running under the API... Don't default to "yes"
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# Expire root password? Default to yes, but can be overridden from
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek# the environment variable
# This may be in /etc/os-release or /etc/system-release-cpe. We
# should be able to use EITHER. Give preference to /etc/os-release for now.
echo "Host CPE ID from /etc/os-release: ${CPE_NAME}"
echo "Host CPE ID from /etc/system-release-cpe: ${CPE_NAME}"
is_centos=true
is_redhat=true
centos_host_ver=$( sed -e '/^CentOS /!d' -e 's/CentOS.*\srelease\s*\([0-9][0-9.]*\)\s.*/\1/' < /etc/centos-release )
is_centos=true
echo 0 > $rootfs_path/selinux/enforce
if [ -f $rootfs_path/etc/selinux/config ]
if [ -f $rootfs_path/usr/sbin/selinuxenabled ]; then
if [ -f ${rootfs_path}/etc/pam.d/crond ]
if [ -f ${rootfs_path}/lib/security/pam_loginuid.so ]
if [ -f ${rootfs_path}/lib64/security/pam_loginuid.so ]
if [ -e /etc/localtime -a ! -e ${rootfs_path}/etc/localtime ]
if [ -f ${rootfs_path}/etc/init.d/halt ]
< ${rootfs_path}/etc/init.d/halt \
> ${rootfs_path}/etc/init.d/lxc-halt
chmod 755 ${rootfs_path}/etc/init.d/lxc-halt
cd ${rootfs_path}/etc/rc.d/rc0.d
cd ${rootfs_path}/etc/rc.d/rc6.d
sed -i 's|si::sysinit:/etc/rc.d/rc.sysinit|si::bootwait:/etc/rc.d/lxc.sysinit|' $rootfs_path/etc/inittab
chmod 600 ${config_path}/tmp_root_pass
echo ${root_password} > ${config_path}/tmp_root_pass
if [ -d ${rootfs_path}/etc/init ]
if [ $? -ne 0 ]; then
PKG_LIST="yum initscripts passwd rsyslog vim-minimal openssh-server openssh-clients dhclient chkconfig rootfiles policycoreutils"
force_mknod 666 $INSTALL_ROOT/dev/null c 1 3
force_mknod 666 $INSTALL_ROOT/dev/urandom c 1 9
if [ $? -ne 0 ]; then
if [ ! -e $INSTALL_ROOT/var/lib/rpm/Packages -a -e $INSTALL_ROOT/$HOME/.rpmdb/Packages ]; then
mv $INSTALL_ROOT/$HOME/.rpmdb/[A-Z]* $INSTALL_ROOT/var/lib/rpm/
ret=$?
mknod -m 666 $INSTALL_ROOT/$INSTALL_ROOT/dev/null c 1 3
mknod -m 666 $INSTALL_ROOT/$INSTALL_ROOT/dev/urandom c 1 9
if [ $? -ne 0 ]; then
rm -f $REPO_FILE
rsync -a $cache/rootfs/ $rootfs_path/
if [ $? -ne 0 ]; then
flock -x 9
if [ $? -ne 0 ]; then
if [ $? -ne 0 ]; then
if [ $? -ne 0 ]; then
if [ $? -ne 0 ]; then
while read LINE
echo ${LINE} >> $config_path/config
done < $config_path/config.def
lxc.include = @LXCTEMPLATECONFIG@/centos.common.conf
if [ $? -ne 0 ]; then
if [ ! -e $cache ]; then
flock -x 9
cat <<EOF
-R,--release Centos release for the new container. if the host is Centos, then it will defaultto the host's release.
options=$(getopt -o a:hp:n:cR: -l help,path:,rootfs:,name:,clean,release:,repo:,arch:,fqdn: -- "$@")
eval set -- "$options"
# utsname and hostname = Container_Name.Domain_Name
if [ -z "$path" ]; then
if [ -z "$release" ]; then
echo "This is not a CentOS or Redhat host and release is missing, defaulting to 6 use -R|--release to specify release"
if [ -z "$rootfs_path" ]; then
# check for 'lxc.rootfs' passed in through default config by lxc-create
if [ ! -z "$clean" ]; then
echo "The temporary password for root is: '$root_password'