lxc-fedora.in revision c61eaadcf87ef9d336992651ff85387de62b8a1a
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# template script for generating fedora container for LXC
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# lxc: linux Container library
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# Daniel Lezcano <daniel.lezcano@free.fr>
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# Ramez Hanna <rhanna@informatiq.org>
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# This library is free software; you can redistribute it and/or
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# modify it under the terms of the GNU Lesser General Public
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# License as published by the Free Software Foundation; either
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# version 2.1 of the License, or (at your option) any later version.
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# This library is distributed in the hope that it will be useful,
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# but WITHOUT ANY WARRANTY; without even the implied warranty of
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# Lesser General Public License for more details.
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# You should have received a copy of the GNU Lesser General Public
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# License along with this library; if not, write to the Free Software
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
99e4008cad9e959b683c6f48411fcf15a92be3b5Michel Normand#Configurations
99e4008cad9e959b683c6f48411fcf15a92be3b5Michel Normandcache_base=@LOCALSTATEDIR@/cache/lxc/fedora/$arch
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# is this fedora?
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# Alow for weird remixes like the Raspberry Pi
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# Use the Mitre standard CPE identifier for the release ID if possible...
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# This may be in /etc/os-release or /etc/system-release-cpe. We
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# should be able to use EITHER. Give preference to /etc/os-release for now.
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# This is a shell friendly configuration file. We can just source it.
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# What we're looking for in here is the ID, VERSION_ID and the CPE_NAME
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano echo "Host CPE ID from /etc/os-release: ${CPE_NAME}"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanoif [ "${CPE_NAME}" = "" -a -e /etc/system-release-cpe ]
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber CPE_URI=$(expr ${CPE_NAME} : '\([^:]*:[^:*]\)')
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber echo "Host CPE ID from /etc/system-release-cpe: ${CPE_NAME}"
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber # Probably a better way to do this but sill remain posix
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber # compatible but this works, shrug...
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano # Must be nice and not introduce convenient bashisms here.
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano ID=$(expr ${CPE_NAME} : '[^:]*:[^:]*:[^:]*:\([^:]*\)')
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano VERSION_ID=$(expr ${CPE_NAME} : '[^:]*:[^:]*:[^:]*:[^:]*:\([^:]*\)')
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanoif [ "${CPE_NAME}" != "" -a "${ID}" = "fedora" -a "${VERSION_ID}" != "" ]
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano # Only if all other methods fail, try to parse the redhat-release file.
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano fedora_host_ver=$( sed -e '/^Fedora /!d' -e 's/Fedora.*\srelease\s*\([0-9][0-9]*\)\s.*/\1/' < /etc/redhat-release )
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber# Map a few architectures to their generic Fedora repository archs.
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber# The two ARM archs are a bit of a guesstimate for the v5 and v6
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber# archs. V6 should have hardware floating point (Rasberry Pi).
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber# The "arm" arch is safer (no hardware floating point). So
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# there may be cases where we "get it wrong" for some v6 other
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-eth0
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane GraberBOOTPROTO=dhcp
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane GraberHOSTNAME=${utsname}
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanoNM_CONTROLLED=no
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanoTYPE=Ethernet
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber cat <<EOF > ${rootfs_path}/etc/sysconfig/network
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane GraberNETWORKING=yes
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane GraberHOSTNAME=${utsname}
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano if [ $release -gt 14 ]; then
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber echo "${utsname}" > ${rootfs_path}/etc/hostname
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber127.0.0.1 localhost.localdomain localhost $utsname
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber::1 localhost6.localdomain6 localhost6
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber mknod -m 666 ${dev_path}/null c 1 3
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber mknod -m 666 ${dev_path}/zero c 1 5
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber mknod -m 666 ${dev_path}/random c 1 8
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber mknod -m 666 ${dev_path}/urandom c 1 9
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber mknod -m 666 ${dev_path}/tty c 5 0
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber mknod -m 666 ${dev_path}/tty0 c 4 0
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano mknod -m 666 ${dev_path}/tty1 c 4 1
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano mknod -m 666 ${dev_path}/tty2 c 4 2
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano mknod -m 666 ${dev_path}/tty3 c 4 3
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber mknod -m 666 ${dev_path}/tty4 c 4 4
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber mknod -m 600 ${dev_path}/console c 5 1
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber mknod -m 666 ${dev_path}/full c 1 7
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber mknod -m 666 ${dev_path}/ptmx c 5 2
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber echo "setting root passwd to $root_password"
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber echo "root:$root_password" | chroot $rootfs_path chpasswd
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber # specifying this in the initial packages doesn't always work.
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano chroot ${rootfs_path} yum --releasever=${release} -y install fedora-release
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber sed -i 's/ACTIVE_CONSOLES=.*$/ACTIVE_CONSOLES="\/dev\/console \/dev\/tty[1-4]"/' \
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.sysinit
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.d/rc.sysinit
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano sed -i 's/^.*dev.pts.*$/#\0/' ${rootfs_path}/etc/rc.sysinit
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano sed -i 's/^.*dev.pts.*$/#\0/' ${rootfs_path}/etc/rc.d/rc.sysinit
f79d43bbe70a01454049b77d6f15f6369744959eStéphane Graber chroot ${rootfs_path} chkconfig udev-post off
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano unlink ${rootfs_path}/etc/systemd/system/default.target
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber chroot ${rootfs_path} ln -s /dev/null /etc/systemd/system/udev.service
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber chroot ${rootfs_path} ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber #dependency on a device unit fails it specially that we disabled udev
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber # sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber # Actually, the After=dev-%i.device line does not appear in the
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber # Fedora 17 or Fedora 18 systemd getty\@.service file. It may be left
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber # over from an earlier version and it's not doing any harm. We do need
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber # to disable the "ConditionalPathExists=/dev/tty0" line or no gettys are
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber # started on the ttys in the container. Lets do it in an override copy of
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano # the service so it can still pass rpm verifies and not be automatically
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber # updated by a new systemd version. -- mhw /\/\|=mhw=|\/\/
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber sed -e 's/^ConditionPathExists=/# ConditionPathExists=/' \
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber < ${rootfs_path}/lib/systemd/system/getty\@.service \
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber > ${rootfs_path}/etc/systemd/system/getty\@.service
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber # Setup getty service on the 4 ttys we are going to allow in the
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber # default config. Number should match lxc.tty
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber ( cd ${rootfs_path}/etc/systemd/system/getty.target.wants
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber for i in 1 2 3 4 ; do ln -sf ../getty\@.service getty@tty${i}.service; done )
4e7186c53554cdbf4b4b85568daf84740c120505Stéphane Graber # check the mini fedora was not already downloaded
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano if [ $? -ne 0 ]; then
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano YUM="yum --installroot $INSTALL_ROOT -y --nogpgcheck"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano PKG_LIST="yum initscripts passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils fedora-release"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano MIRRORLIST_URL="http://mirrors.fedoraproject.org/mirrorlist?repo=fedora-$release&arch=$arch"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano # We're splitting the old loop into two loops plus a directory retrival.
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano # First loop... Try and retrive a mirror list with retries and a slight
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano for trynumber in 1 2 3 4; do
sleep 3
for MIRROR_URL in ${MIRROR_URLS}
RELEASE_RPM=$(curl -L -f "$RELEASE_URL" | sed -e "/fedora-release-${release}-/!d" -e 's/.*<a href=\"//' -e 's/\">.*//' )
if [ $? -ne 0 ]; then
if [ $DOWNLOAD_OK != yes ]; then
mkdir -p $INSTALL_ROOT/var/lib/rpm
rpm --root $INSTALL_ROOT --initdb
rpm --root $INSTALL_ROOT -ivh ${INSTALL_ROOT}/${RELEASE_RPM}
if [ $? -ne 0 ]; then
mkdir -p @LOCALSTATEDIR@/lock/subsys/
flock -x 200
if [ $? -ne 0 ]; then
if [ $? -ne 0 ]; then
if [ $? -ne 0 ]; then
if [ $? -ne 0 ]; then
grep -q "^lxc.rootfs" $config_path/config 2>/dev/null || echo "lxc.rootfs = $rootfs_path" >> $config_path/config
if [ $? -ne 0 ]; then
if [ ! -e $cache ]; then
flock -x 200
cat <<EOF
[-p|--path=<path>] [-c|--clean] [-R|--release=<Fedora_release>] [--fqdn=<network name of container>] [-A|--arch=<arch of the container>]
-p,--path path to where the container will be created, defaults to @LXCPATH@. The container config will go under @LXCPATH@ in that case
-R,--release Fedora release for the new container. if the host is Fedora, then it will default to the host's release.
eval set -- "$options"
if [ -z "$name" ]; then
# utsname and hostname = Container_Name.Domain_Name
if [ -n "$needed_pkgs" ]; then
echo "Missing commands: $needed_pkgs"
echo "Please install these using \"sudo yum install $needed_pkgs\""
if [ -z "$path" ]; then
if [ -z "$release" ]; then
echo "This is not a fedora host and release missing, defaulting to 18. use -R|--release to specify release"
if [ -z "$rootfs_path" ]; then
# check for 'lxc.rootfs' passed in through default config by lxc-create
# This configuration (rc.sysinit) is not inconsistent with the systemd stuff
if [ ! -z $clean ]; then