lxc-centos.in revision 6976826fed04d006608f87ba902a8517358c15ec
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#!/bin/bash
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# template script for generating centos container for LXC
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch#
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# lxc: linux Container library
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch# Authors:
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch# Daniel Lezcano <daniel.lezcano@free.fr>
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch# Ramez Hanna <rhanna@informatiq.org>
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch# Fajar A. Nugraha <github@fajar.net>
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# Michael H. Warfield <mhw@WittsEnd.com>
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# This library is free software; you can redistribute it and/or
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# modify it under the terms of the GNU Lesser General Public
5257840e8d31a6cb7051703b4cb0931c82aba638Stephan Bosch# License as published by the Free Software Foundation; either
5257840e8d31a6cb7051703b4cb0931c82aba638Stephan Bosch# version 2.1 of the License, or (at your option) any later version.
fe2b0e3de834dd40b698bb579adc5357d5789ec9Stephan Bosch
94d1b08c9e20d637db568a3eab3dfc2b9e96e62aStephan Bosch# This library is distributed in the hope that it will be useful,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# but WITHOUT ANY WARRANTY; without even the implied warranty of
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch# Lesser General Public License for more details.
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch# You should have received a copy of the GNU Lesser General Public
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch# License along with this library; if not, write to the Free Software
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch#Configurations
7384b4e78eaab44693c985192276e31322155e32Stephan Boschdefault_path=@LXCPATH@
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# Some combinations of the tuning knobs below do not exactly make sense.
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# but that's ok.
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch#
fc94140acba51adafedafbc8491a3223a51db7a8Stephan Bosch# If the "root_password" is non-blank, use it, else set a default.
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# This can be passed to the script as an environment variable and is
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# set by a shell conditional assignment. Looks weird but it is what it is.
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# If the root password contains a ding ($) then try to expand it.
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# That will pick up things like ${name} and ${RANDOM}.
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# If the root password contains more than 3 consecutive X's, pass it as
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# a template to mktemp and take the result.
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch# If root_display_password = yes, display the temporary root password at exit.
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch# If root_store_password = yes, store it in the configuration directory
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch# If root_prompt_password = yes, invoke "passwd" to force the user to change
e47c2f17d8136c4d972d1074a3f84ba2ecef4fdcStephan Bosch# the root password after the container is created.
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch# If root_expire_password = yes, you will be prompted to change the root
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch# password at the first login.
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch#
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch# These are conditional assignments... The can be overridden from the
fc94140acba51adafedafbc8491a3223a51db7a8Stephan Bosch# preexisting environment variables...
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch#
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch# Make sure this is in single quotes to defer expansion to later!
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch# :{root_password='Root-${name}-${RANDOM}'}
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch: ${root_password='Root-${name}-XXXXXX'}
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch# Now, it doesn't make much sense to display, store, and force change
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch# together. But, we gotta test, right???
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch: ${root_display_password='no'}
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch: ${root_store_password='yes'}
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch# Prompting for something interactive has potential for mayhem
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch# with users running under the API... Don't default to "yes"
fc94140acba51adafedafbc8491a3223a51db7a8Stephan Bosch: ${root_prompt_password='no'}
fc94140acba51adafedafbc8491a3223a51db7a8Stephan Bosch
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch# Expire root password? Default to yes, but can be overridden from
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch# the environment variable
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch: ${root_expire_password='yes'}
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# These are only going into comments in the resulting config...
7384b4e78eaab44693c985192276e31322155e32Stephan Boschlxc_network_type=veth
7384b4e78eaab44693c985192276e31322155e32Stephan Boschlxc_network_link=lxcbr0
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch
47a53a80656dc400ff8effdc1432a69fbf5ae8baTimo Sirainen# is this centos?
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# Alow for weird remixes like the Raspberry Pi
a991cfe2157e58ee43bc580f517ce9ef0dfb7acfStephan Bosch#
a991cfe2157e58ee43bc580f517ce9ef0dfb7acfStephan Bosch# Use the Mitre standard CPE identifier for the release ID if possible...
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch# This may be in /etc/os-release or /etc/system-release-cpe. We
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch# should be able to use EITHER. Give preference to /etc/os-release for now.
30f35cf5d1e1374d7fab4231e86144fc106a8e79Stephan Bosch
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch# Detect use under userns (unsupported)
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Boschfor arg in "$@"; do
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch [ "$arg" = "--" ] && break
4219de12b28f1936219e27501b9c4b27a4f8d53cStephan Bosch if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch echo "This template can't be used for unprivileged containers." 1>&2
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch echo "You may want to try the \"download\" template instead." 1>&2
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch exit 1
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch fi
7384b4e78eaab44693c985192276e31322155e32Stephan Boschdone
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# Make sure the usual locations are in PATH
7384b4e78eaab44693c985192276e31322155e32Stephan Boschexport PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
6d573191bea1a64d6046be070487a5705a2d0204Stephan Bosch
6d573191bea1a64d6046be070487a5705a2d0204Stephan Boschif [ -e /etc/os-release ]
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainenthen
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen# This is a shell friendly configuration file. We can just source it.
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen# What we're looking for in here is the ID, VERSION_ID and the CPE_NAME
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch . /etc/os-release
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch echo "Host CPE ID from /etc/os-release: ${CPE_NAME}"
31fa529029f35f65451fb1d119ed1d5435b62e46Timo Sirainenfi
31fa529029f35f65451fb1d119ed1d5435b62e46Timo Sirainen
31fa529029f35f65451fb1d119ed1d5435b62e46Timo Sirainenif [ "${CPE_NAME}" = "" -a -e /etc/system-release-cpe ]
b66def5dadd3e7c250313a938d26ad113663f86bStephan Boschthen
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch CPE_NAME=$(head -n1 /etc/system-release-cpe)
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch CPE_URI=$(expr ${CPE_NAME} : '\([^:]*:[^:]*\)')
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if [ "${CPE_URI}" != "cpe:/o" ]
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch then
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch CPE_NAME=
6bc9fb43cc1ac24693d030a6cbfa43bc7cbc82cbTimo Sirainen else
6bc9fb43cc1ac24693d030a6cbfa43bc7cbc82cbTimo Sirainen # Probably a better way to do this but sill remain posix
6bc9fb43cc1ac24693d030a6cbfa43bc7cbc82cbTimo Sirainen # compatible but this works, shrug...
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch # Must be nice and not introduce convenient bashisms here.
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch #
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch # According to the official registration at Mitre and NIST,
4124bebe6daab2cd05acb0416096fc47cb9abd92Timo Sirainen # this should have been something like this for CentOS:
4124bebe6daab2cd05acb0416096fc47cb9abd92Timo Sirainen # cpe:/o:centos:centos:6
4124bebe6daab2cd05acb0416096fc47cb9abd92Timo Sirainen # or this:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch # cpe:/o:centos:centos:6.5
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch #
30f35cf5d1e1374d7fab4231e86144fc106a8e79Stephan Bosch ID=$(expr ${CPE_NAME} : '[^:]*:[^:]*:[^:]*:\([^:]*\)')
87c121a4c05b9cee46f1f757ec6999d441519abfStephan Bosch # The "enterprise_linux" is a bone toss back to RHEL.
8aa91c0f70ea2893d82a16a8b8d0171caccce74cStephan Bosch # Since CentOS and RHEL are so tightly coupled, we'll
8aa91c0f70ea2893d82a16a8b8d0171caccce74cStephan Bosch # take the RHEL version if we're running on it and do the
8aa91c0f70ea2893d82a16a8b8d0171caccce74cStephan Bosch # equivalent version for CentOS.
8aa91c0f70ea2893d82a16a8b8d0171caccce74cStephan Bosch if [ ${ID} = "linux" -o ${ID} = "enterprise_linux" ]
b99130e4cf4af4e6b103b949456222f3a2dff424Timo Sirainen then
87c121a4c05b9cee46f1f757ec6999d441519abfStephan Bosch # Instead we got this: cpe:/o:centos:linux:6
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch ID=$(expr ${CPE_NAME} : '[^:]*:[^:]*:\([^:]*\)')
e1d8d185fa74752ba6807e415a9c82ebfdb2082cStephan Bosch fi
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen
eb325a5a90c1d2655e74972bde0de6a699d2c864Stephan Bosch VERSION_ID=$(expr ${CPE_NAME} : '[^:]*:[^:]*:[^:]*:[^:]*:\([^:]*\)')
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch echo "Host CPE ID from /etc/system-release-cpe: ${CPE_NAME}"
6bc9fb43cc1ac24693d030a6cbfa43bc7cbc82cbTimo Sirainen fi
6ab81c81be13f33486746deeffe02a1ef2bcc821Stephan Boschfi
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Boschif [ "${CPE_NAME}" != "" -a "${ID}" = "centos" -a "${VERSION_ID}" != "" ]
e47c2f17d8136c4d972d1074a3f84ba2ecef4fdcStephan Boschthen
93c9909f68f7d057e38cca3b4612ec8d0bf42999Timo Sirainen centos_host_ver=${VERSION_ID}
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch is_centos=true
7384b4e78eaab44693c985192276e31322155e32Stephan Boschelif [ "${CPE_NAME}" != "" -a "${ID}" = "redhat" -a "${VERSION_ID}" != "" ]
de96afeeaa5242cffe89f1966457e935806b5746Stephan Boschthen
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch redhat_host_ver=${VERSION_ID}
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch is_redhat=true
de96afeeaa5242cffe89f1966457e935806b5746Stephan Boschelif [ -e /etc/centos-release ]
de96afeeaa5242cffe89f1966457e935806b5746Stephan Boschthen
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # Only if all other methods fail, try to parse the redhat-release file.
7abab3b191860a3d77af5192b0649833c8a0c803Stephan Bosch centos_host_ver=$( sed -e '/^CentOS /!d' -e 's/CentOS.*\srelease\s*\([0-9][0-9.]*\)\s.*/\1/' < /etc/centos-release )
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch if [ "$centos_host_ver" != "" ]
7abab3b191860a3d77af5192b0649833c8a0c803Stephan Bosch then
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch is_centos=true
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch fi
de96afeeaa5242cffe89f1966457e935806b5746Stephan Boschfi
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Boschforce_mknod()
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch{
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # delete a device node if exists, and create a new one
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch rm -f $2 && mknod -m $1 $2 $3 $4 $5
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch}
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Boschconfigure_centos()
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch{
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch
22215c2d7538f4367c93e2d8b6ec4722463ac757Stephan Bosch # disable selinux in centos
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mkdir -p $rootfs_path/selinux
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch echo 0 > $rootfs_path/selinux/enforce
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # Also kill it in the /etc/selinux/config file if it's there...
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch if [ -f $rootfs_path/etc/selinux/config ]
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch then
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch sed -i '/^SELINUX=/s/.*/SELINUX=disabled/' $rootfs_path/etc/selinux/config
0d24f1550f5f70eb199018926a7bf0a12971695cStephan Bosch fi
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # Nice catch from Dwight Engen in the Oracle template.
8a6dc50255a27bf887bbd9ed7c3a87bf629d4179Stephan Bosch # Wantonly plagerized here with much appreciation.
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch if [ -f $rootfs_path/usr/sbin/selinuxenabled ]; then
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mv $rootfs_path/usr/sbin/selinuxenabled $rootfs_path/usr/sbin/selinuxenabled.lxcorig
30ccfe4fdc2212d4341fa0edd6e8e73609ef1418Stephan Bosch ln -s /bin/false $rootfs_path/usr/sbin/selinuxenabled
9f4f35957d2839d487d14b42bcb3f1a1de098b4bStephan Bosch fi
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # This is a known problem and documented in RedHat bugzilla as relating
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # to a problem with auditing enabled. This prevents an error in
ebe0f5e387744621b73c4db79d5891ccbe0a1321Stephan Bosch # the container "Cannot make/remove an entry for the specified session"
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch sed -i '/^session.*pam_loginuid.so/s/^session/# session/' ${rootfs_path}/etc/pam.d/login
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch sed -i '/^session.*pam_loginuid.so/s/^session/# session/' ${rootfs_path}/etc/pam.d/sshd
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch
7abab3b191860a3d77af5192b0649833c8a0c803Stephan Bosch if [ -f ${rootfs_path}/etc/pam.d/crond ]
7abab3b191860a3d77af5192b0649833c8a0c803Stephan Bosch then
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch sed -i '/^session.*pam_loginuid.so/s/^session/# session/' ${rootfs_path}/etc/pam.d/crond
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch fi
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # In addition to disabling pam_loginuid in the above config files
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # we'll also disable it by linking it to pam_permit to catch any
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # we missed or any that get installed after the container is built.
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch #
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # Catch either or both 32 and 64 bit archs.
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch if [ -f ${rootfs_path}/lib/security/pam_loginuid.so ]
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch then
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch ( cd ${rootfs_path}/lib/security/
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mv pam_loginuid.so pam_loginuid.so.disabled
fe2b0e3de834dd40b698bb579adc5357d5789ec9Stephan Bosch ln -s pam_permit.so pam_loginuid.so
fe2b0e3de834dd40b698bb579adc5357d5789ec9Stephan Bosch )
fe2b0e3de834dd40b698bb579adc5357d5789ec9Stephan Bosch fi
fe2b0e3de834dd40b698bb579adc5357d5789ec9Stephan Bosch
fe2b0e3de834dd40b698bb579adc5357d5789ec9Stephan Bosch if [ -f ${rootfs_path}/lib64/security/pam_loginuid.so ]
ebe0f5e387744621b73c4db79d5891ccbe0a1321Stephan Bosch then
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch ( cd ${rootfs_path}/lib64/security/
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mv pam_loginuid.so pam_loginuid.so.disabled
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch ln -s pam_permit.so pam_loginuid.so
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch )
1f1ba791ba565d4a31b031fee95617490fe96b11Stephan Bosch fi
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # Set default localtime to the host localtime if not set...
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch if [ -e /etc/localtime -a ! -e ${rootfs_path}/etc/localtime ]
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch then
70505f4839520ac67895992621c97d2480c22e7fTimo Sirainen # if /etc/localtime is a symlink, this should preserve it.
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch cp -a /etc/localtime ${rootfs_path}/etc/localtime
70505f4839520ac67895992621c97d2480c22e7fTimo Sirainen fi
fc94140acba51adafedafbc8491a3223a51db7a8Stephan Bosch
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch # Deal with some dain bramage in the /etc/init.d/halt script.
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch # Trim it and make it our own and link it in before the default
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch # halt script so we can intercept it. This also preventions package
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch # updates from interferring with our interferring with it.
1a64096dcbea2765f370c9d35a3480d0e60c643bTimo Sirainen #
524d89a40565709964b7ee5e8324a400dc53656bTimo Sirainen # There's generally not much in the halt script that useful but what's
1a64096dcbea2765f370c9d35a3480d0e60c643bTimo Sirainen # in there from resetting the hardware clock down is generally very bad.
1a64096dcbea2765f370c9d35a3480d0e60c643bTimo Sirainen # So we just eliminate the whole bottom half of that script in making
fc94140acba51adafedafbc8491a3223a51db7a8Stephan Bosch # ourselves a copy. That way a major update to the init scripts won't
9284599e2d12b08170be81441bcfc53fa5b71a73Timo Sirainen # trash what we've set up.
9d746c6785d17e421c3f3c74cf29d059ae2ab233Stephan Bosch if [ -f ${rootfs_path}/etc/init.d/halt ]
9d746c6785d17e421c3f3c74cf29d059ae2ab233Stephan Bosch then
fc94140acba51adafedafbc8491a3223a51db7a8Stephan Bosch sed -e '/hwclock/,$d' \
fc94140acba51adafedafbc8491a3223a51db7a8Stephan Bosch < ${rootfs_path}/etc/init.d/halt \
fc94140acba51adafedafbc8491a3223a51db7a8Stephan Bosch > ${rootfs_path}/etc/init.d/lxc-halt
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch echo '$command -f' >> ${rootfs_path}/etc/init.d/lxc-halt
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch chmod 755 ${rootfs_path}/etc/init.d/lxc-halt
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch # Link them into the rc directories...
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch (
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch cd ${rootfs_path}/etc/rc.d/rc0.d
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch ln -s ../init.d/lxc-halt S00lxc-halt
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch cd ${rootfs_path}/etc/rc.d/rc6.d
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch ln -s ../init.d/lxc-halt S00lxc-reboot
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch )
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch fi
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch # configure the network using the dhcp
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-eth0
aacf2a69acc59e9382578d6f4e030788abc79706Timo SirainenDEVICE=eth0
aacf2a69acc59e9382578d6f4e030788abc79706Timo SirainenBOOTPROTO=dhcp
7384b4e78eaab44693c985192276e31322155e32Stephan BoschONBOOT=yes
7384b4e78eaab44693c985192276e31322155e32Stephan BoschHOSTNAME=${UTSNAME}
7384b4e78eaab44693c985192276e31322155e32Stephan BoschNM_CONTROLLED=no
7384b4e78eaab44693c985192276e31322155e32Stephan BoschTYPE=Ethernet
7384b4e78eaab44693c985192276e31322155e32Stephan BoschMTU=${MTU}
7384b4e78eaab44693c985192276e31322155e32Stephan BoschDHCP_HOSTNAME=$name
7384b4e78eaab44693c985192276e31322155e32Stephan BoschEOF
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # set the hostname
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch cat <<EOF > ${rootfs_path}/etc/sysconfig/network
7384b4e78eaab44693c985192276e31322155e32Stephan BoschNETWORKING=yes
7384b4e78eaab44693c985192276e31322155e32Stephan BoschHOSTNAME=${UTSNAME}
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan BoschEOF
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch # set minimal hosts
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch cat <<EOF > $rootfs_path/etc/hosts
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch127.0.0.1 localhost $name
7384b4e78eaab44693c985192276e31322155e32Stephan BoschEOF
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch # set minimal fstab
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch cat <<EOF > $rootfs_path/etc/fstab
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch/dev/root / rootfs defaults 0 0
415e16c3dc185578695b7d88e561a52de6c8b1b1Timo Sirainennone /dev/shm tmpfs nosuid,nodev 0 0
7384b4e78eaab44693c985192276e31322155e32Stephan BoschEOF
fca68889b287d8eed4babe72a231bd6079da012dStephan Bosch
fca68889b287d8eed4babe72a231bd6079da012dStephan Bosch # create lxc compatibility init script
fca68889b287d8eed4babe72a231bd6079da012dStephan Bosch if [ "$release" = "6" ]; then
fca68889b287d8eed4babe72a231bd6079da012dStephan Bosch cat <<EOF > $rootfs_path/etc/init/lxc-sysinit.conf
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstart on startup
7384b4e78eaab44693c985192276e31322155e32Stephan Boschenv container
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Boschpre-start script
aacf2a69acc59e9382578d6f4e030788abc79706Timo Sirainen if [ "x$container" != "xlxc" -a "x$container" != "xlibvirt" ]; then
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch stop;
9dc01e0d10a61cab14867b26bf0d2d1dcf8ad978Timo Sirainen fi
a991cfe2157e58ee43bc580f517ce9ef0dfb7acfStephan Bosch initctl start tty TTY=console
a991cfe2157e58ee43bc580f517ce9ef0dfb7acfStephan Bosch rm -f /var/lock/subsys/*
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch rm -f /var/run/*.pid
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch telinit 3
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch exit 0;
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Boschend script
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan BoschEOF
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch elif [ "$release" = "5" ]; then
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch cat <<EOF > $rootfs_path/etc/rc.d/lxc.sysinit
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch#! /bin/bash
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Boschrm -f /etc/mtab /var/run/*.{pid,lock} /var/lock/subsys/*
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Boschrm -rf {/,/var}/tmp/*
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Boschecho "/dev/root / rootfs defaults 0 0" > /etc/mtab
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Boschexit 0
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan BoschEOF
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch chmod 755 $rootfs_path/etc/rc.d/lxc.sysinit
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch sed -i 's|si::sysinit:/etc/rc.d/rc.sysinit|si::bootwait:/etc/rc.d/lxc.sysinit|' $rootfs_path/etc/inittab
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch # prevent mingetty from calling vhangup(2) since it fails with userns.
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch # Same issue as oracle template: prevent mingetty from calling vhangup(2)
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch # commit 2e83f7201c5d402478b9849f0a85c62d5b9f1589.
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch sed -i 's|^1:|co:2345:respawn:/sbin/mingetty --nohangup console\n1:|' $rootfs_path/etc/inittab
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch sed -i 's|^\([56]:\)|#\1|' $rootfs_path/etc/inittab
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch fi
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch dev_path="${rootfs_path}/dev"
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch rm -rf $dev_path
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mkdir -p $dev_path
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mknod -m 666 ${dev_path}/null c 1 3
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mknod -m 666 ${dev_path}/zero c 1 5
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mknod -m 666 ${dev_path}/random c 1 8
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mknod -m 666 ${dev_path}/urandom c 1 9
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mkdir -m 755 ${dev_path}/pts
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mkdir -m 1777 ${dev_path}/shm
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mknod -m 666 ${dev_path}/tty c 5 0
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mknod -m 666 ${dev_path}/tty0 c 4 0
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mknod -m 666 ${dev_path}/tty1 c 4 1
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mknod -m 666 ${dev_path}/tty2 c 4 2
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mknod -m 666 ${dev_path}/tty3 c 4 3
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mknod -m 666 ${dev_path}/tty4 c 4 4
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mknod -m 600 ${dev_path}/console c 5 1
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mknod -m 666 ${dev_path}/full c 1 7
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mknod -m 600 ${dev_path}/initctl p
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mknod -m 666 ${dev_path}/ptmx c 5 2
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch # setup console and tty[1-4] for login. note that /dev/console and
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch # /dev/tty[1-4] will be symlinks to the ptys /dev/lxc/console and
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch # /dev/lxc/tty[1-4] so that package updates can overwrite the symlinks.
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch # lxc will maintain these links and bind mount ptys over /dev/lxc/*
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch # since lxc.devttydir is specified in the config.
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch # allow root login on console, tty[1-4], and pts/0 for libvirt
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch echo "# LXC (Linux Containers)" >>${rootfs_path}/etc/securetty
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch echo "lxc/console" >>${rootfs_path}/etc/securetty
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch echo "lxc/tty1" >>${rootfs_path}/etc/securetty
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch echo "lxc/tty2" >>${rootfs_path}/etc/securetty
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch echo "lxc/tty3" >>${rootfs_path}/etc/securetty
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch echo "lxc/tty4" >>${rootfs_path}/etc/securetty
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch echo "# For libvirt/Virtual Machine Monitor" >>${rootfs_path}/etc/securetty
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch echo "pts/0" >>${rootfs_path}/etc/securetty
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch # prevent mingetty from calling vhangup(2) since it fails with userns.
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch # Same issue as oracle template: prevent mingetty from calling vhangup(2)
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch # commit 2e83f7201c5d402478b9849f0a85c62d5b9f1589.
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch sed -i 's|mingetty|mingetty --nohangup|' $container_rootfs/etc/init/tty.conf
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch if [ ${root_display_password} = "yes" ]
415e16c3dc185578695b7d88e561a52de6c8b1b1Timo Sirainen then
7abab3b191860a3d77af5192b0649833c8a0c803Stephan Bosch echo "Setting root password to '$root_password'"
7abab3b191860a3d77af5192b0649833c8a0c803Stephan Bosch fi
7abab3b191860a3d77af5192b0649833c8a0c803Stephan Bosch if [ ${root_store_password} = "yes" ]
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch then
d1f964d3f1dd9c5868b134c4f44dd63f3722eef7Timo Sirainen touch ${config_path}/tmp_root_pass
d1f964d3f1dd9c5868b134c4f44dd63f3722eef7Timo Sirainen chmod 600 ${config_path}/tmp_root_pass
79f8a20424633e806447bc9375a5ab403aabc758Stephan Bosch echo ${root_password} > ${config_path}/tmp_root_pass
79f8a20424633e806447bc9375a5ab403aabc758Stephan Bosch echo "Storing root password in '${config_path}/tmp_root_pass'"
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch fi
93cc87bb22386e020cee1093b6bd59295e0b33f0Stephan Bosch
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch echo "root:$root_password" | chroot $rootfs_path chpasswd
ede750711f27ca9d9037a7ab9f016411b57f1ad9Stephan Bosch
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch if [ ${root_expire_password} = "yes" ]
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch then
99feb6521535a7dc59d8dda89981ceac084b3e88Timo Sirainen # Also set this password as expired to force the user to change it!
173d53863744e8ed87e8fa4c32dfcf3759e2ceb0Stephan Bosch chroot $rootfs_path passwd -e root
99feb6521535a7dc59d8dda89981ceac084b3e88Timo Sirainen fi
173d53863744e8ed87e8fa4c32dfcf3759e2ceb0Stephan Bosch
6c768e0e1ca2da178e79f7435c32ced01f6bcb24Timo Sirainen # This will need to be enhanced for CentOS 7 when systemd
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch # comes into play... /\/\|=mhw=|\/\/
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch return 0
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch}
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Boschconfigure_centos_init()
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch{
129596c93692b21d6c6b1313b389774af24c2983Stephan Bosch sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.sysinit
d5c665cf2989d49922b63439ac45714e9755838aTimo Sirainen sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.d/rc.sysinit
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if [ "$release" = "6" ]; then
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch chroot ${rootfs_path} chkconfig udev-post off
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch fi
d1f964d3f1dd9c5868b134c4f44dd63f3722eef7Timo Sirainen chroot ${rootfs_path} chkconfig network on
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch if [ -d ${rootfs_path}/etc/init ]
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch then
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch # This is to make upstart honor SIGPWR
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch cat <<EOF >${rootfs_path}/etc/init/power-status-changed.conf
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch# power-status-changed - shutdown on SIGPWR
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstart on power-status-changed
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
eb325a5a90c1d2655e74972bde0de6a699d2c864Stephan Boschexec /sbin/shutdown -h now "SIGPWR received"
1dead6eacb9302a8419bfc8f4b571ea9a3c00296Timo SirainenEOF
1dead6eacb9302a8419bfc8f4b571ea9a3c00296Timo Sirainen fi
60381d37ec7213c21bd6ada7babb769f572646f9Stephan Bosch}
8a6dc50255a27bf887bbd9ed7c3a87bf629d4179Stephan Bosch
8a6dc50255a27bf887bbd9ed7c3a87bf629d4179Stephan Boschdownload_centos()
8a6dc50255a27bf887bbd9ed7c3a87bf629d4179Stephan Bosch{
a4e186e3ef267fc7a6b592788067c8c9c87d0785Stephan Bosch
3d1edb8e3a07d91860cc6b4b3cec8282caa70891Stephan Bosch # check the mini centos was not already downloaded
3d1edb8e3a07d91860cc6b4b3cec8282caa70891Stephan Bosch INSTALL_ROOT=$cache/partial
3d1edb8e3a07d91860cc6b4b3cec8282caa70891Stephan Bosch mkdir -p $INSTALL_ROOT
3d1edb8e3a07d91860cc6b4b3cec8282caa70891Stephan Bosch if [ $? -ne 0 ]; then
3d1edb8e3a07d91860cc6b4b3cec8282caa70891Stephan Bosch echo "Failed to create '$INSTALL_ROOT' directory"
3d1edb8e3a07d91860cc6b4b3cec8282caa70891Stephan Bosch return 1
6a33d6385601c89fdaeb437537f714b3a01dcea4Timo Sirainen fi
6a33d6385601c89fdaeb437537f714b3a01dcea4Timo Sirainen
57c339f4e3404355a8924103317a6158a709ecf1Stephan Bosch # download a mini centos into a cache
eb325a5a90c1d2655e74972bde0de6a699d2c864Stephan Bosch echo "Downloading centos minimal ..."
fc94140acba51adafedafbc8491a3223a51db7a8Stephan Bosch YUM="yum --installroot $INSTALL_ROOT -y --nogpgcheck"
fc94140acba51adafedafbc8491a3223a51db7a8Stephan Bosch PKG_LIST="yum initscripts passwd rsyslog vim-minimal openssh-server openssh-clients dhclient chkconfig rootfiles policycoreutils"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch # use temporary repository definition
a62fe4b300e2f591e939993aec4cac1e7ae30ad1Stephan Bosch REPO_FILE=$INSTALL_ROOT/etc/yum.repos.d/lxc-centos-temp.repo
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch mkdir -p $(dirname $REPO_FILE)
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch if [ -n "$repo" ]; then
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch cat <<EOF > $REPO_FILE
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch[base]
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Boschname=local repository
7384b4e78eaab44693c985192276e31322155e32Stephan Boschbaseurl="$repo"
7384b4e78eaab44693c985192276e31322155e32Stephan BoschEOF
7384b4e78eaab44693c985192276e31322155e32Stephan Boschelse
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch cat <<EOF > $REPO_FILE
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch[base]
7384b4e78eaab44693c985192276e31322155e32Stephan Boschname=CentOS-$release - Base
7abab3b191860a3d77af5192b0649833c8a0c803Stephan Boschmirrorlist=http://mirrorlist.centos.org/?release=$release&arch=$basearch&repo=os
7abab3b191860a3d77af5192b0649833c8a0c803Stephan Bosch
7abab3b191860a3d77af5192b0649833c8a0c803Stephan Bosch[updates]
7384b4e78eaab44693c985192276e31322155e32Stephan Boschname=CentOS-$release - Updates
7384b4e78eaab44693c985192276e31322155e32Stephan Boschmirrorlist=http://mirrorlist.centos.org/?release=$release&arch=$basearch&repo=updates
7384b4e78eaab44693c985192276e31322155e32Stephan BoschEOF
ebe0f5e387744621b73c4db79d5891ccbe0a1321Stephan Bosch fi
ebe0f5e387744621b73c4db79d5891ccbe0a1321Stephan Bosch
ebe0f5e387744621b73c4db79d5891ccbe0a1321Stephan Bosch # create minimal device nodes, needed for "yum install" and "yum update" process
ebe0f5e387744621b73c4db79d5891ccbe0a1321Stephan Bosch mkdir -p $INSTALL_ROOT/dev
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch force_mknod 666 $INSTALL_ROOT/dev/null c 1 3
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch force_mknod 666 $INSTALL_ROOT/dev/urandom c 1 9
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch $YUM install $PKG_LIST
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch if [ $? -ne 0 ]; then
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch echo "Failed to download the rootfs, aborting."
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 1
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch fi
fc94140acba51adafedafbc8491a3223a51db7a8Stephan Bosch
70505f4839520ac67895992621c97d2480c22e7fTimo Sirainen # use same nameservers as hosts, needed for "yum update later"
2a889fc596b12639ac55e657816b8a4f90440f88Timo Sirainen cp /etc/resolv.conf $INSTALL_ROOT/etc/
2a889fc596b12639ac55e657816b8a4f90440f88Timo Sirainen
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch # check whether rpmdb is under $HOME
fc94140acba51adafedafbc8491a3223a51db7a8Stephan Bosch if [ ! -e $INSTALL_ROOT/var/lib/rpm/Packages -a -e $INSTALL_ROOT/$HOME/.rpmdb/Packages ]; then
a40a555561b3378c12a986b27f2651c632c8afa4Stephan Bosch echo "Fixing rpmdb location ..."
a40a555561b3378c12a986b27f2651c632c8afa4Stephan Bosch mv $INSTALL_ROOT/$HOME/.rpmdb/[A-Z]* $INSTALL_ROOT/var/lib/rpm/
a40a555561b3378c12a986b27f2651c632c8afa4Stephan Bosch rm -rf $INSTALL_ROOT/$HOME/.rpmdb
a40a555561b3378c12a986b27f2651c632c8afa4Stephan Bosch chroot $INSTALL_ROOT rpm --rebuilddb 2>/dev/null
fc94140acba51adafedafbc8491a3223a51db7a8Stephan Bosch fi
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch # check whether rpmdb version is correct
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch chroot $INSTALL_ROOT rpm --quiet -q yum 2>/dev/null
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch ret=$?
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # if "rpm -q" doesn't work due to rpmdb version difference,
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # then we need to redo the process using the newly-installed yum
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch if [ $ret -gt 0 ]; then
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch echo "Reinstalling packages ..."
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mv $REPO_FILE $REPO_FILE.tmp
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mkdir $INSTALL_ROOT/etc/yum.repos.disabled
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mv $INSTALL_ROOT/etc/yum.repos.d/*.repo $INSTALL_ROOT/etc/yum.repos.disabled/
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mv $REPO_FILE.tmp $REPO_FILE
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mkdir -p $INSTALL_ROOT/$INSTALL_ROOT/etc
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch cp /etc/resolv.conf $INSTALL_ROOT/$INSTALL_ROOT/etc/
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mkdir -p $INSTALL_ROOT/$INSTALL_ROOT/dev
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mknod -m 666 $INSTALL_ROOT/$INSTALL_ROOT/dev/null c 1 3
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mknod -m 666 $INSTALL_ROOT/$INSTALL_ROOT/dev/urandom c 1 9
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mkdir -p $INSTALL_ROOT/$INSTALL_ROOT/var/cache/yum
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch cp -al $INSTALL_ROOT/var/cache/yum/* $INSTALL_ROOT/$INSTALL_ROOT/var/cache/yum/
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch chroot $INSTALL_ROOT $YUM install $PKG_LIST
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch if [ $? -ne 0 ]; then
b66def5dadd3e7c250313a938d26ad113663f86bStephan Bosch echo "Failed to download the rootfs, aborting."
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch return 1
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch fi
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mv $INSTALL_ROOT/$INSTALL_ROOT $INSTALL_ROOT.tmp
e4e4b8544a492cf90bd7a93c9a26e8285fc7c00bStephan Bosch rm -rf $INSTALL_ROOT
e4e4b8544a492cf90bd7a93c9a26e8285fc7c00bStephan Bosch mv $INSTALL_ROOT.tmp $INSTALL_ROOT
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch fi
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch rm -f $REPO_FILE
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch rm -rf $INSTALL_ROOT/var/cache/yum/*
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mv "$INSTALL_ROOT" "$cache/rootfs"
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch echo "Download complete."
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch return 0
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch}
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Boschcopy_centos()
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch{
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # make a local copy of the mini centos
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch echo -n "Copying rootfs to $rootfs_path ..."
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch #cp -a $cache/rootfs-$arch $rootfs_path || return 1
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch # i prefer rsync (no reason really)
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch mkdir -p $rootfs_path
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch rsync -a $cache/rootfs/ $rootfs_path/
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch echo
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch return 0
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch}
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Boschupdate_centos()
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch{
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch YUM="chroot $cache/rootfs yum -y --nogpgcheck"
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch $YUM update
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch if [ $? -ne 0 ]; then
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch return 1
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch fi
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch $YUM clean packages
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch}
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Boschinstall_centos()
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch{
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch mkdir -p /var/lock/subsys/
5a37824675033747fcae3fe3fc3c0dd7ef0ca1cdStephan Bosch (
de96afeeaa5242cffe89f1966457e935806b5746Stephan Bosch flock -x 9
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if [ $? -ne 0 ]; then
echo "Cache repository is busy."
return 1
fi
echo "Checking cache download in $cache/rootfs ... "
if [ ! -e "$cache/rootfs" ]; then
download_centos
if [ $? -ne 0 ]; then
echo "Failed to download 'centos base'"
return 1
fi
else
echo "Cache found. Updating..."
update_centos
if [ $? -ne 0 ]; then
echo "Failed to update 'centos base', continuing with last known good cache"
else
echo "Update finished"
fi
fi
echo "Copy $cache/rootfs to $rootfs_path ... "
copy_centos
if [ $? -ne 0 ]; then
echo "Failed to copy rootfs"
return 1
fi
return 0
) 9>/var/lock/subsys/lxc-centos
return $?
}
create_hwaddr()
{
openssl rand -hex 5 | sed -e 's/\(..\)/:\1/g; s/^/fe/'
}
copy_configuration()
{
mkdir -p $config_path
grep -q "^lxc.rootfs" $config_path/config 2>/dev/null || echo "
lxc.rootfs = $rootfs_path
" >> $config_path/config
# The following code is to create static MAC addresses for each
# interface in the container. This code will work for multiple
# interfaces in the default config.
mv $config_path/config $config_path/config.def
while read LINE
do
# This should catch variable expansions from the default config...
if expr "${LINE}" : '.*\$' > /dev/null 2>&1
then
LINE=$(eval "echo \"${LINE}\"")
fi
# There is a tab and a space in the regex bracket below!
# Seems that \s doesn't work in brackets.
KEY=$(expr "${LINE}" : '\s*\([^ ]*\)\s*=')
if [[ "${KEY}" != "lxc.network.hwaddr" ]]
then
echo ${LINE} >> $config_path/config
if [[ "${KEY}" == "lxc.network.link" ]]
then
echo "lxc.network.hwaddr = $(create_hwaddr)" >> $config_path/config
fi
fi
done < $config_path/config.def
rm -f $config_path/config.def
if [ -e "@LXCTEMPLATECONFIG@/centos.common.conf" ]; then
echo "
# Include common configuration
lxc.include = @LXCTEMPLATECONFIG@/centos.common.conf
" >> $config_path/config
fi
# Append things which require expansion here...
cat <<EOF >> $config_path/config
lxc.arch = $arch
lxc.utsname = $utsname
lxc.autodev = $auto_dev
# When using LXC with apparmor, uncomment the next line to run unconfined:
#lxc.aa_profile = unconfined
# example simple networking setup, uncomment to enable
#lxc.network.type = $lxc_network_type
#lxc.network.flags = up
#lxc.network.link = $lxc_network_link
#lxc.network.name = eth0
# Additional example for veth network type
# static MAC address,
#lxc.network.hwaddr = 00:16:3e:77:52:20
# persistent veth device name on host side
# Note: This may potentially collide with other containers of same name!
#lxc.network.veth.pair = v-$name-e0
EOF
if [ $? -ne 0 ]; then
echo "Failed to add configuration"
return 1
fi
return 0
}
clean()
{
if [ ! -e $cache ]; then
exit 0
fi
# lock, so we won't purge while someone is creating a repository
(
flock -x 9
if [ $? != 0 ]; then
echo "Cache repository is busy."
exit 1
fi
echo -n "Purging the download cache for centos-$release..."
rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
exit 0
) 9>@LOCALSTATEDIR@/lock/subsys/lxc-centos
}
usage()
{
cat <<EOF
usage:
$1 -n|--name=<container_name>
[-p|--path=<path>] [-c|--clean] [-R|--release=<CentOS_release>] [-a|--arch=<arch of the container>]
[-h|--help]
Mandatory args:
-n,--name container name, used to as an identifier for that container from now on
Optional args:
-p,--path path to where the container rootfs will be created, defaults to /var/lib/lxc/name.
-c,--clean clean the cache
-R,--release Centos release for the new container. if the host is Centos, then it will defaultto the host's release.
--fqdn fully qualified domain name (FQDN) for DNS and system naming
--repo repository to use (url)
-a,--arch Define what arch the container will be [i686,x86_64]
-h,--help print this help
EOF
return 0
}
options=$(getopt -o a:hp:n:cR: -l help,path:,rootfs:,name:,clean,release:,repo:,arch:,fqdn: -- "$@")
if [ $? -ne 0 ]; then
usage $(basename $0)
exit 1
fi
arch=$(uname -m)
eval set -- "$options"
while true
do
case "$1" in
-h|--help) usage $0 && exit 0;;
-p|--path) path=$2; shift 2;;
--rootfs) rootfs=$2; shift 2;;
-n|--name) name=$2; shift 2;;
-c|--clean) clean=1; shift 1;;
-R|--release) release=$2; shift 2;;
--repo) repo="$2"; shift 2;;
-a|--arch) newarch=$2; shift 2;;
--fqdn) utsname=$2; shift 2;;
--) shift 1; break ;;
*) break ;;
esac
done
if [ ! -z "$clean" -a -z "$path" ]; then
clean || exit 1
exit 0
fi
basearch=${arch}
# Map a few architectures to their generic CentOS repository archs.
# The two ARM archs are a bit of a guesstimate for the v5 and v6
# archs. V6 should have hardware floating point (Rasberry Pi).
# The "arm" arch is safer (no hardware floating point). So
# there may be cases where we "get it wrong" for some v6 other
# than RPi.
case "$arch" in
i686) basearch=i386 ;;
armv3l|armv4l|armv5l) basearch=arm ;;
armv6l|armv7l|armv8l) basearch=armhfp ;;
*) ;;
esac
# Somebody wants to specify an arch. This is very limited case.
# i386/i586/i686 on i386/x86_64
# - or -
# x86_64 on x86_64
if [ "${newarch}" != "" -a "${newarch}" != "${arch}" ]
then
case "${newarch}" in
i386|i586|i686)
if [ "${basearch}" = "i386" -o "${basearch}" = "x86_64" ]
then
# Make the arch a generic x86 32 bit...
arch=${newarch}
basearch=i386
else
basearch=bad
fi
;;
*)
basearch=bad
;;
esac
if [ "${basearch}" = "bad" ]
then
echo "You cannot build a ${newarch} CentOS container on a ${arch} host. Sorry!"
exit 1
fi
fi
cache_base=@LOCALSTATEDIR@/cache/lxc/centos/$basearch
# Let's do something better for the initial root password.
# It's not perfect but it will defeat common scanning brute force
# attacks in the case where ssh is exposed. It will also be set to
# expired, forcing the user to change it at first login.
if [ "${root_password}" = "" ]
then
root_password=Root-${name}-${RANDOM}
else
# If it's got a ding in it, try and expand it!
if [ $(expr "${root_password}" : '.*$.') != 0 ]
then
root_password=$(eval echo "${root_password}")
fi
# If it has more than 3 consecutive X's in it, feed it
# through mktemp as a template.
if [ $(expr "${root_password}" : '.*XXXX') != 0 ]
then
root_password=$(mktemp -u ${root_password})
fi
fi
if [ -z "${utsname}" ]; then
utsname=${name}
fi
# This follows a standard "resolver" convention that an FQDN must have
# at least two dots or it is considered a local relative host name.
# If it doesn't, append the dns domain name of the host system.
#
# This changes one significant behavior when running
# "lxc_create -n Container_Name" without using the
# --fqdn option.
#
# Old behavior:
# utsname and hostname = Container_Name
# New behavior:
# utsname and hostname = Container_Name.Domain_Name
if [ $(expr "$utsname" : '.*\..*\.') = 0 ]; then
if [[ "$(dnsdomainname)" != "" && "$(dnsdomainname)" != "localdomain" ]]; then
utsname=${utsname}.$(dnsdomainname)
fi
fi
type yum >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "'yum' command is missing"
exit 1
fi
if [ -z "$path" ]; then
path=$default_path/$name
fi
if [ -z "$release" ]; then
if [ "$is_centos" -a "$centos_host_ver" ]; then
release=$centos_host_ver
elif [ "$is_redhat" -a "$redhat_host_ver" ]; then
# This is needed to clean out bullshit like 6workstation and 6server.
release=$(expr $redhat_host_ver : '\([0-9.]*\)')
else
echo "This is not a CentOS or Redhat host and release is missing, defaulting to 6 use -R|--release to specify release"
release=6
fi
fi
# CentOS 7 and above should run systemd. We need autodev enabled to keep
# systemd from causing problems.
#
# There is some ambiguity here due to the differnce between versioning
# of point specific releases such as 6.5 and the rolling release 6. We
# only want the major number here if it's a point release...
mrelease=$(expr $release : '\([0-9]*\)')
if [ $mrelease -gt 6 ]; then
auto_dev="1"
else
auto_dev="0"
fi
if [ "$(id -u)" != "0" ]; then
echo "This script should be run as 'root'"
exit 1
fi
if [ -z "$rootfs_path" ]; then
rootfs_path=$path/rootfs
# check for 'lxc.rootfs' passed in through default config by lxc-create
if grep -q '^lxc.rootfs' $path/config 2>/dev/null ; then
rootfs_path=$(sed -e '/^lxc.rootfs\s*=/!d' -e 's/\s*#.*//' \
-e 's/^lxc.rootfs\s*=\s*//' -e q $path/config)
fi
fi
config_path=$path
cache=$cache_base/$release
revert()
{
echo "Interrupted, so cleaning up"
lxc-destroy -n $name
# maybe was interrupted before copy config
rm -rf $path
echo "exiting..."
exit 1
}
trap revert SIGHUP SIGINT SIGTERM
copy_configuration
if [ $? -ne 0 ]; then
echo "failed write configuration file"
exit 1
fi
install_centos
if [ $? -ne 0 ]; then
echo "failed to install centos"
exit 1
fi
configure_centos
if [ $? -ne 0 ]; then
echo "failed to configure centos for a container"
exit 1
fi
configure_centos_init
if [ ! -z $clean ]; then
clean || exit 1
exit 0
fi
echo "
Container rootfs and config have been created.
Edit the config file to check/enable networking setup.
"
if [ ${root_display_password} = "yes" ]
then
echo "The temporary password for root is: '$root_password'
You may want to note that password down before starting the container.
"
fi
if [ ${root_store_password} = "yes" ]
then
echo "The temporary root password is stored in:
'${config_path}/tmp_root_pass'
"
fi
if [ ${root_prompt_password} = "yes" ]
then
echo "Invoking the passwd command in the container to set the root password.
chroot ${rootfs_path} passwd
"
chroot ${rootfs_path} passwd
else
if [ ${root_expire_password} = "yes" ]
then
echo "
The root password is set up as "expired" and will require it to be changed
at first login, which you should do as soon as possible. If you lose the
root password or wish to change it without starting the container, you
can change it from the host by running the following command (which will
also reset the expired flag):
chroot ${rootfs_path} passwd
"
fi
fi