lxc-debian.in revision 6a082d5a1370cd57ed05cf24aa800ae73112e818
76b43e4417bab52e913da39b5f5bc2a130d3f149Timo Sirainen#!/bin/bash
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen#
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen# lxc: linux Container library
ffd9a1898a18fadfc5dce399162c25d50548f905Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen# Authors:
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen# Daniel Lezcano <daniel.lezcano@free.fr>
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen# This library is free software; you can redistribute it and/or
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen# modify it under the terms of the GNU Lesser General Public
a2f250a332dfc1e6cd4ffd196c621eb9dbf7b8a1Timo Sirainen# License as published by the Free Software Foundation; either
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen# version 2.1 of the License, or (at your option) any later version.
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen# This library is distributed in the hope that it will be useful,
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen# but WITHOUT ANY WARRANTY; without even the implied warranty of
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen# Lesser General Public License for more details.
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen# You should have received a copy of the GNU Lesser General Public
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen# License along with this library; if not, write to the Free Software
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen# Detect use under userns (unsupported)
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenfor arg in "$@"; do
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen [ "$arg" = "--" ] && break
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen echo "This template can't be used for unprivileged containers." 1>&2
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen echo "You may want to try the \"download\" template instead." 1>&2
221351ed85c839e0b03d82c47654c3d17202e3dbTimo Sirainen exit 1
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen fi
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainendone
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen# Make sure the usual locations are in PATH
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenexport PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenexport GREP_OPTIONS=""
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo SirainenMIRROR=${MIRROR:-http://http.debian.net/debian}
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo SirainenSECURITY_MIRROR=${SECURITY_MIRROR:-http://security.debian.org/}
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo SirainenLOCALSTATEDIR="@LOCALSTATEDIR@"
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo SirainenLXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen# Allows the lxc-cache directory to be set by environment variable
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo SirainenLXC_CACHE_PATH=${LXC_CACHE_PATH:-"$LOCALSTATEDIR/cache/lxc"}
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenconfigure_debian()
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen{
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen rootfs=$1
7ded22760598b78ee29f9418eacc0abe3fb51055Timo Sirainen hostname=$2
7ded22760598b78ee29f9418eacc0abe3fb51055Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # squeeze only has /dev/tty and /dev/tty0 by default,
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # therefore creating missing device nodes for tty1-4.
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen for tty in $(seq 1 4); do
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen if [ ! -e $rootfs/dev/tty$tty ]; then
7ded22760598b78ee29f9418eacc0abe3fb51055Timo Sirainen mknod $rootfs/dev/tty$tty c 4 $tty
7ded22760598b78ee29f9418eacc0abe3fb51055Timo Sirainen fi
7ded22760598b78ee29f9418eacc0abe3fb51055Timo Sirainen done
7ded22760598b78ee29f9418eacc0abe3fb51055Timo Sirainen
7ded22760598b78ee29f9418eacc0abe3fb51055Timo Sirainen # configure the inittab
7ded22760598b78ee29f9418eacc0abe3fb51055Timo Sirainen cat <<EOF > $rootfs/etc/inittab
7ded22760598b78ee29f9418eacc0abe3fb51055Timo Sirainenid:3:initdefault:
7ded22760598b78ee29f9418eacc0abe3fb51055Timo Sirainensi::sysinit:/etc/init.d/rcS
7ded22760598b78ee29f9418eacc0abe3fb51055Timo Sirainenl0:0:wait:/etc/init.d/rc 0
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenl1:1:wait:/etc/init.d/rc 1
7ded22760598b78ee29f9418eacc0abe3fb51055Timo Sirainenl2:2:wait:/etc/init.d/rc 2
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenl3:3:wait:/etc/init.d/rc 3
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenl4:4:wait:/etc/init.d/rc 4
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainenl5:5:wait:/etc/init.d/rc 5
cfa9359fbd6a967ccdcd553c5e483a093885ab6fTimo Sirainenl6:6:wait:/etc/init.d/rc 6
cfa9359fbd6a967ccdcd553c5e483a093885ab6fTimo Sirainen# Normally not reached, but fallthrough in case of emergency.
cfa9359fbd6a967ccdcd553c5e483a093885ab6fTimo Sirainenz6:6:respawn:/sbin/sulogin
cfa9359fbd6a967ccdcd553c5e483a093885ab6fTimo Sirainen1:2345:respawn:/sbin/getty 38400 console
cfa9359fbd6a967ccdcd553c5e483a093885ab6fTimo Sirainenc1:12345:respawn:/sbin/getty 38400 tty1 linux
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenc2:12345:respawn:/sbin/getty 38400 tty2 linux
3675a7e9bd3775ba13fe8bc93915902513a0f1a4Timo Sirainenc3:12345:respawn:/sbin/getty 38400 tty3 linux
3675a7e9bd3775ba13fe8bc93915902513a0f1a4Timo Sirainenc4:12345:respawn:/sbin/getty 38400 tty4 linux
3675a7e9bd3775ba13fe8bc93915902513a0f1a4Timo Sirainenp6::ctrlaltdel:/sbin/init 6
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenp0::powerfail:/sbin/init 0
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo SirainenEOF
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # symlink mtab
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen [ -e "$rootfs/etc/mtab" ] && rm $rootfs/etc/mtab
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen ln -s /proc/self/mounts $rootfs/etc/mtab
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # disable selinux in debian
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen mkdir -p $rootfs/selinux
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen echo 0 > $rootfs/selinux/enforce
7ded22760598b78ee29f9418eacc0abe3fb51055Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # configure the network using the dhcp
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen cat <<EOF > $rootfs/etc/network/interfaces
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenauto lo
cfa9359fbd6a967ccdcd553c5e483a093885ab6fTimo Siraineniface lo inet loopback
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainenauto eth0
3675a7e9bd3775ba13fe8bc93915902513a0f1a4Timo Siraineniface eth0 inet dhcp
3675a7e9bd3775ba13fe8bc93915902513a0f1a4Timo SirainenEOF
3675a7e9bd3775ba13fe8bc93915902513a0f1a4Timo Sirainen
3675a7e9bd3775ba13fe8bc93915902513a0f1a4Timo Sirainen # set the hostname
3675a7e9bd3775ba13fe8bc93915902513a0f1a4Timo Sirainen cat <<EOF > $rootfs/etc/hostname
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen$hostname
3675a7e9bd3775ba13fe8bc93915902513a0f1a4Timo SirainenEOF
3675a7e9bd3775ba13fe8bc93915902513a0f1a4Timo Sirainen
3675a7e9bd3775ba13fe8bc93915902513a0f1a4Timo Sirainen # reconfigure some services
3675a7e9bd3775ba13fe8bc93915902513a0f1a4Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # remove pointless services in a container
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen chroot $rootfs /usr/sbin/update-rc.d -f checkroot.sh disable
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen chroot $rootfs /usr/sbin/update-rc.d -f umountfs disable
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen chroot $rootfs /usr/sbin/update-rc.d -f hwclock.sh disable
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen chroot $rootfs /usr/sbin/update-rc.d -f hwclockfirst.sh disable
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # generate new SSH keys
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen if [ -x $rootfs/var/lib/dpkg/info/openssh-server.postinst ]; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen cat > $rootfs/usr/sbin/policy-rc.d << EOF
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen#!/bin/sh
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainenexit 101
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo SirainenEOF
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen chmod +x $rootfs/usr/sbin/policy-rc.d
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen if [ -f $rootfs/etc/init/ssh.conf ]; then
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen mv $rootfs/etc/init/ssh.conf $rootfs/etc/init/ssh.conf.disabled
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen fi
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen rm -f $rootfs/etc/ssh/ssh_host_*key*
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen DPKG_MAINTSCRIPT_PACKAGE=openssh DPKG_MAINTSCRIPT_NAME=postinst chroot $rootfs /var/lib/dpkg/info/openssh-server.postinst configure
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen sed -i "s/root@$(hostname)/root@$hostname/g" $rootfs/etc/ssh/ssh_host_*.pub
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen if [ -f "$rootfs/etc/init/ssh.conf.disabled" ]; then
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen mv $rootfs/etc/init/ssh.conf.disabled $rootfs/etc/init/ssh.conf
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen fi
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen rm -f $rootfs/usr/sbin/policy-rc.d
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen fi
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen # set initial timezone as on host
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen if [ -f /etc/timezone ]; then
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen cat /etc/timezone > $rootfs/etc/timezone
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen chroot $rootfs dpkg-reconfigure -f noninteractive tzdata
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen elif [ -f /etc/sysconfig/clock ]; then
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen . /etc/sysconfig/clock
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen echo $ZONE > $rootfs/etc/timezone
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen chroot $rootfs dpkg-reconfigure -f noninteractive tzdata
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen else
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen echo "Timezone in container is not configured. Adjust it manually."
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen fi
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen echo "root:root" | chroot $rootfs chpasswd
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen echo "Root password is 'root', please change !"
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen return 0
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen}
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainenwrite_sourceslist()
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen{
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen local rootfs="$1"; shift
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen local release="$1"; shift
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen local arch="$1"; shift
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen local prefix="deb"
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen if [ -n "${arch}" ]; then
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen prefix="deb [arch=${arch}]"
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen fi
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen cat >> "${rootfs}/etc/apt/sources.list" << EOF
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen${prefix} $MIRROR ${release} main contrib non-free
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo SirainenEOF
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen if [ "$release" != "unstable" -a "$release" != "sid" ]; then
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen cat >> "${rootfs}/etc/apt/sources.list" << EOF
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen${prefix} $SECURITY_MIRROR ${release}/updates main contrib non-free
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo SirainenEOF
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen fi
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen}
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Siraineninstall_packages()
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen{
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen local rootfs="$1"; shift
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen local packages="$*"
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen chroot ${rootfs} apt-get update
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen if [ -n "${packages}" ]; then
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen chroot ${rootfs} apt-get install --force-yes -y --no-install-recommends ${packages}
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen fi
6c9699d37fbe4d8af3682ee7f341ede8d54faa87Timo Sirainen}
9573f4283f9118315b0865998d43223fedee0246Timo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainenconfigure_debian_systemd()
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen{
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen path=$1
6c9699d37fbe4d8af3682ee7f341ede8d54faa87Timo Sirainen rootfs=$2
6c9699d37fbe4d8af3682ee7f341ede8d54faa87Timo Sirainen
6c9699d37fbe4d8af3682ee7f341ede8d54faa87Timo Sirainen # this only works if we have getty@.service to manipulate
6c9699d37fbe4d8af3682ee7f341ede8d54faa87Timo Sirainen if [ -f ${rootfs}/lib/systemd/system/getty\@.service ]; then
6c9699d37fbe4d8af3682ee7f341ede8d54faa87Timo Sirainen sed -e 's/^ConditionPathExists=/# ConditionPathExists=/' \
6c9699d37fbe4d8af3682ee7f341ede8d54faa87Timo Sirainen -e 's/After=dev-%i.device/After=/' \
6c9699d37fbe4d8af3682ee7f341ede8d54faa87Timo Sirainen < ${rootfs}/lib/systemd/system/getty\@.service \
6c9699d37fbe4d8af3682ee7f341ede8d54faa87Timo Sirainen > ${rootfs}/etc/systemd/system/getty\@.service
6c9699d37fbe4d8af3682ee7f341ede8d54faa87Timo Sirainen fi
ed50658501d9ae8c85f6264831056b1debed11c3Timo Sirainen
ed50658501d9ae8c85f6264831056b1debed11c3Timo Sirainen # just in case systemd is not installed
ed50658501d9ae8c85f6264831056b1debed11c3Timo Sirainen mkdir -p ${rootfs}/{lib,etc}/systemd/system
ed50658501d9ae8c85f6264831056b1debed11c3Timo Sirainen mkdir -p ${rootfs}/etc/systemd/system/getty.target.wants
ed50658501d9ae8c85f6264831056b1debed11c3Timo Sirainen
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen # Fix getty-static-service as debootstrap does not install dbus
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen if [ -e $rootfs//lib/systemd/system/getty-static.service ] ; then
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen sed 's/ getty@tty[5-9].service//g' $rootfs/lib/systemd/system/getty-static.service | sed 's/\(tty2-tty\)[5-9]/\14/g' > $rootfs/etc/systemd/system/getty-static.service
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen fi
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
b0be0bead3d6963149f7f2a9504b8ab5aced9af5Timo Sirainen # This function has been copied and adapted from lxc-fedora
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen rm -f ${rootfs}/etc/systemd/system/default.target
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen chroot ${rootfs} ln -s /dev/null /etc/systemd/system/udev.service
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen chroot ${rootfs} ln -s /dev/null /etc/systemd/system/systemd-udevd.service
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen chroot ${rootfs} ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # Make systemd honor SIGPWR
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen chroot ${rootfs} ln -s /lib/systemd/system/halt.target /etc/systemd/system/sigpwr.target
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # Setup getty service on the 4 ttys we are going to allow in the
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # default config. Number should match lxc.tty
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen ( cd ${rootfs}/etc/systemd/system/getty.target.wants
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen for i in 1 2 3 4 ; do ln -sf ../getty\@.service getty@tty${i}.service; done )
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen return 0
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen}
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainencleanup()
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen{
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen rm -rf $cache/partial-$release-$arch
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen rm -rf $cache/rootfs-$release-$arch
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen}
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainendownload_debian()
cfa9359fbd6a967ccdcd553c5e483a093885ab6fTimo Sirainen{
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen packages=\
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainenifupdown,\
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainenlocales,\
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenlibui-dialog-perl,\
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainendialog,\
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainenisc-dhcp-client,\
ffd9a1898a18fadfc5dce399162c25d50548f905Timo Sirainennetbase,\
ffd9a1898a18fadfc5dce399162c25d50548f905Timo Sirainennet-tools,\
ffd9a1898a18fadfc5dce399162c25d50548f905Timo Siraineniproute,\
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainenopenssh-server
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen cache=$1
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen arch=$2
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen release=$3
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen trap cleanup EXIT SIGHUP SIGINT SIGTERM
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen # check the mini debian was not already downloaded
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen mkdir -p "$cache/partial-$release-$arch"
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen if [ $? -ne 0 ]; then
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen echo "Failed to create '$cache/partial-$release-$arch' directory"
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen return 1
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen fi
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
3675a7e9bd3775ba13fe8bc93915902513a0f1a4Timo Sirainen # download a mini debian into a cache
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen echo "Downloading debian minimal ..."
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen debootstrap --verbose --variant=minbase --arch=$arch \
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen --include=$packages \
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen "$release" "$cache/partial-$release-$arch" $MIRROR
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen if [ $? -ne 0 ]; then
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen echo "Failed to download the rootfs, aborting."
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen return 1
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen fi
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen mv "$1/partial-$release-$arch" "$1/rootfs-$release-$arch"
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen echo "Download complete."
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen trap EXIT
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen trap SIGINT
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen trap SIGTERM
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen trap SIGHUP
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen return 0
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen}
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainencopy_debian()
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen{
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen cache=$1
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen arch=$2
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen rootfs=$3
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen release=$4
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen # make a local copy of the minidebian
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen echo -n "Copying rootfs to $rootfs..."
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen mkdir -p $rootfs
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen rsync -Ha "$cache/rootfs-$release-$arch"/ $rootfs/ || return 1
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen return 0
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen}
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Siraineninstall_debian()
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen{
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen rootfs=$1
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen release=$2
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen arch=$3
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen cache="$4/debian"
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen mkdir -p $LOCALSTATEDIR/lock/subsys/
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen (
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen flock -x 9
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen if [ $? -ne 0 ]; then
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen echo "Cache repository is busy."
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen return 1
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen fi
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen echo "Checking cache download in $cache/rootfs-$release-$arch ... "
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen if [ ! -e "$cache/rootfs-$release-$arch" ]; then
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen download_debian $cache $arch $release
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen if [ $? -ne 0 ]; then
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen echo "Failed to download 'debian base'"
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen return 1
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen fi
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen fi
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen copy_debian $cache $arch $rootfs $release
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen if [ $? -ne 0 ]; then
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen echo "Failed to copy rootfs"
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen return 1
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen fi
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen return 0
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen ) 9>$LOCALSTATEDIR/lock/subsys/lxc-debian
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen return $?
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen}
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainencopy_configuration()
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen{
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen path=$1
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen rootfs=$2
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen hostname=$3
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen arch=$4
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen # Generate the configuration file
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen # if there is exactly one veth network entry, make sure it has an
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen # associated hwaddr.
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen if [ $nics -eq 1 ]; then
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen grep -q "^lxc.network.hwaddr" $path/config || sed -i -e "/^lxc\.network\.type[ \t]*=[ \t]*veth/a lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')" $path/config
b8d232d88018c5cafd2f3be5a181d318137a45f2Timo Sirainen fi
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen ## Add all the includes
b8d232d88018c5cafd2f3be5a181d318137a45f2Timo Sirainen echo "" >> $path/config
b8d232d88018c5cafd2f3be5a181d318137a45f2Timo Sirainen echo "# Common configuration" >> $path/config
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen if [ -e "${LXC_TEMPLATE_CONFIG}/debian.common.conf" ]; then
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/debian.common.conf" >> $path/config
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen fi
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen if [ -e "${LXC_TEMPLATE_CONFIG}/debian.${release}.conf" ]; then
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/debian.${release}.conf" >> $path/config
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen fi
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen ## Add the container-specific config
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen echo "" >> $path/config
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen echo "# Container specific configuration" >> $path/config
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen grep -q "^lxc.rootfs" $path/config 2> /dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
b8d232d88018c5cafd2f3be5a181d318137a45f2Timo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen cat <<EOF >> $path/config
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainenlxc.utsname = $hostname
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainenlxc.arch = $arch
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo SirainenEOF
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen if [ $? -ne 0 ]; then
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen echo "Failed to add configuration"
b8d232d88018c5cafd2f3be5a181d318137a45f2Timo Sirainen return 1
b8d232d88018c5cafd2f3be5a181d318137a45f2Timo Sirainen fi
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen return 0
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen}
2bd2ca14d18819b77201ab2b46910d9ae6858abeTimo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenpost_process()
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen{
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen local rootfs="$1"; shift
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen local release="$1"; shift
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen local arch="$1"; shift
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen local hostarch="$1"; shift
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen local packages="$*"
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # Disable service startup
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen cat > ${rootfs}/usr/sbin/policy-rc.d << EOF
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen#!/bin/sh
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenexit 101
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo SirainenEOF
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen chmod +x ${rootfs}/usr/sbin/policy-rc.d
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen # If the container isn't running a native architecture, setup multiarch
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen if [ "${arch}" != "${hostarch}" ]; then
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen mkdir -p ${rootfs}/etc/dpkg/dpkg.cfg.d
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen echo "foreign-architecture ${hostarch}" > ${rootfs}/etc/dpkg/dpkg.cfg.d/lxc-multiarch
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen fi
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen # Write a new sources.list containing both native and multiarch entries
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen > ${rootfs}/etc/apt/sources.list
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen if [ "${arch}" = "${hostarch}" ]; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen write_sourceslist ${rootfs} ${release} ${arch}
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen else
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen write_sourceslist ${rootfs} ${release}
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen fi
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen # Install Packages in container
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen if [ -n "${packages}" ]; then
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen local pack_list="`echo ${packages} | sed 's/,/ /g'`"
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen echo "Installing packages: ${pack_list}"
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen install_packages ${rootfs} ${pack_list}
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen fi
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen # Re-enable service startup
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen rm ${rootfs}/usr/sbin/policy-rc.d
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen # reconfigure locales
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen if [ -z "$LANG" ]; then
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen cat >> $rootfs/etc/locale.gen << EOF
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainenen_US.UTF-8 UTF-8
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo SirainenEOF
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen chroot $rootfs locale-gen en_US.UTF-8 UTF-8
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen chroot $rootfs update-locale LANG=en_US.UTF-8
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen else
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen encoding=$(echo $LANG | cut -d. -f2)
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen chroot $rootfs sed -e "s/^# \(${LANG} ${encoding}\)/\1/" \
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen -i /etc/locale.gen 2> /dev/null
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen cat >> $rootfs/etc/locale.gen << EOF
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen$LANG $encoding
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo SirainenEOF
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen chroot $rootfs locale-gen $LANG $encoding
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen chroot $rootfs update-locale LANG=$LANG
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen fi
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # end
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen}
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainenclean()
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen{
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen cache=${LXC_CACHE_PATH:-"$LOCALSTATEDIR/cache/lxc/debian"}
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen if [ ! -e $cache ]; then
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen exit 0
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen fi
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen # lock, so we won't purge while someone is creating a repository
f7656d7bc15510a4259ed74ddda3c560de8a51c1Timo Sirainen (
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen flock -x 9
b0be0bead3d6963149f7f2a9504b8ab5aced9af5Timo Sirainen if [ $? != 0 ]; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen echo "Cache repository is busy."
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen exit 1
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen fi
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen echo -n "Purging the download cache..."
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen exit 0
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen ) 9>$LOCALSTATEDIR/lock/subsys/lxc-debian
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen}
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenusage()
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen{
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen cat <<EOF
7eff76882487ef39803446f75a709a97a3cafc53Timo SirainenTemplate specific options can be passed to lxc-create after a '--' like this:
7eff76882487ef39803446f75a709a97a3cafc53Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen lxc-create --name=NAME [-lxc-create-options] -- [-template-options]
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2aa3ac8bfb8fea49fd0ab6d56afcb3af3375d22cTimo SirainenUsage: $1 -h|--help -p|--path=<path> [-c|--clean] [-a|--arch=<arch>] [-r|--release=<release>]
2aa3ac8bfb8fea49fd0ab6d56afcb3af3375d22cTimo Sirainen [--mirror=<mirror>] [--security-mirror=<security mirror>]
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen [--package=<package_name1,package_name2,...>]
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo SirainenOptions :
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen -h, --help print this help text
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen -p, --path=PATH directory where config and rootfs of this VM will be kept
3e559edb905bb57d1f1fbfda3431b339f45ea791Timo Sirainen -a, --arch=ARCH The container architecture. Can be one of: i686, x86_64,
608ae99c5b03989df263d72e49aa83e1f9d8a50eTimo Sirainen amd64, armhf, armel, powerpc. Defaults to host arch.
608ae99c5b03989df263d72e49aa83e1f9d8a50eTimo Sirainen -r, --release=RELEASE Debian release. Can be one of: squeeze, wheezy, jessie, sid.
608ae99c5b03989df263d72e49aa83e1f9d8a50eTimo Sirainen Defaults to current stable.
608ae99c5b03989df263d72e49aa83e1f9d8a50eTimo Sirainen --mirror=MIRROR Debian mirror to use during installation. Overrides the MIRROR
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen environment variable (see below).
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen --security-mirror=SECURITY_MIRROR
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen Debian mirror to use for security updates. Overrides the
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen SECURITY_MIRROR environment variable (see below).
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen --packages=PACKAGE_NAME1,PACKAGE_NAME2,...
3e559edb905bb57d1f1fbfda3431b339f45ea791Timo Sirainen List of additional packages to install. Comma separated, without space.
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen -c, --clean only clean up the cache and terminate
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo SirainenEnvironment variables:
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen MIRROR The Debian package mirror to use. See also the --mirror switch above.
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen Defaults to '$MIRROR'
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainen SECURITY_MIRROR The Debian package security mirror to use. See also the --security-mirror switch above.
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen Defaults to '$SECURITY_MIRROR'
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo SirainenEOF
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen return 0
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainen}
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenoptions=$(getopt -o hp:n:a:r:c -l arch:,clean,help,mirror:,name:,packages:,path:,release:,rootfs:,security-mirror: -- "$@")
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenif [ $? -ne 0 ]; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen usage $(basename $0)
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen exit 1
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenfi
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Siraineneval set -- "$options"
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenif which dpkg > /dev/null 2>&1 ; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen arch=$(dpkg --print-architecture)
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenelse
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen arch=$(uname -m)
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen if [ "$arch" = "i686" ]; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen arch="i386"
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen elif [ "$arch" = "x86_64" ]; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen arch="amd64"
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen elif [ "$arch" = "armv7l" ]; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen arch="armhf"
91add6e26ed2b91e15276c96ffe02d9f71476d71Timo Sirainen fi
91add6e26ed2b91e15276c96ffe02d9f71476d71Timo Sirainenfi
91add6e26ed2b91e15276c96ffe02d9f71476d71Timo Sirainenhostarch=$arch
91add6e26ed2b91e15276c96ffe02d9f71476d71Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenwhile true
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainendo
b0be0bead3d6963149f7f2a9504b8ab5aced9af5Timo Sirainen case "$1" in
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen -h|--help) usage $0 && exit 1;;
b0be0bead3d6963149f7f2a9504b8ab5aced9af5Timo Sirainen --) shift 1; break ;;
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen -a|--arch) arch=$2; shift 2;;
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen -c|--clean) clean=1; shift 1;;
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainen --mirror) MIRROR=$2; shift 2;;
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainen -n|--name) name=$2; shift 2;;
0f66f12eb4cdbf47670975044c88d8f388bf92dfTimo Sirainen --packages) packages=$2; shift 2;;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen -p|--path) path=$2; shift 2;;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen -r|--release) release=$2; shift 2;;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen --rootfs) rootfs=$2; shift 2;;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen --security-mirror) SECURITY_MIRROR=$2; shift 2;;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen *) break ;;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen esac
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainendone
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainen
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainenif [ ! -z "$clean" -a -z "$path" ]; then
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainen clean || exit 1
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainen exit 0
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenfi
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenif [ "$arch" = "i686" ]; then
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainen arch=i386
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenfi
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainen
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainenif [ "$arch" = "x86_64" ]; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen arch=amd64
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenfi
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenif [ $hostarch = "i386" -a $arch = "amd64" ]; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen echo "can't create $arch container on $hostarch"
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen exit 1
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainenfi
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenif [ $hostarch = "armhf" -o $hostarch = "armel" ] && \
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainen [ $arch != "armhf" -a $arch != "armel" ]; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen echo "can't create $arch container on $hostarch"
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen exit 1
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenfi
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenif [ $hostarch = "powerpc" -a $arch != "powerpc" ]; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen echo "can't create $arch container on $hostarch"
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen exit 1
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenfi
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainentype debootstrap
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenif [ $? -ne 0 ]; then
2aa3ac8bfb8fea49fd0ab6d56afcb3af3375d22cTimo Sirainen echo "'debootstrap' command is missing"
2aa3ac8bfb8fea49fd0ab6d56afcb3af3375d22cTimo Sirainen exit 1
2aa3ac8bfb8fea49fd0ab6d56afcb3af3375d22cTimo Sirainenfi
2aa3ac8bfb8fea49fd0ab6d56afcb3af3375d22cTimo Sirainen
2aa3ac8bfb8fea49fd0ab6d56afcb3af3375d22cTimo Sirainenif [ -z "$path" ]; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen echo "'path' parameter is required"
2aa3ac8bfb8fea49fd0ab6d56afcb3af3375d22cTimo Sirainen exit 1
2aa3ac8bfb8fea49fd0ab6d56afcb3af3375d22cTimo Sirainenfi
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenif [ "$(id -u)" != "0" ]; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen echo "This script should be run as 'root'"
2aa3ac8bfb8fea49fd0ab6d56afcb3af3375d22cTimo Sirainen exit 1
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenfi
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainencurrent_release=`wget ${MIRROR}/dists/stable/Release -O - 2> /dev/null | head |awk '/^Codename: (.*)$/ { print $2; }'`
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenrelease=${release:-${current_release}}
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainenvalid_releases=('squeeze' 'wheezy' 'jessie' 'stretch' 'sid')
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainenif [[ ! "${valid_releases[*]}" =~ (^|[^[:alpha:]])$release([^[:alpha:]]|$) ]]; then
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen echo "Invalid release ${release}, valid ones are: ${valid_releases[*]}"
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen exit 1
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenfi
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen# detect rootfs
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenconfig="$path/config"
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenif [ -z "$rootfs" ]; then
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen if grep -q '^lxc.rootfs' $config 2> /dev/null ; then
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen rootfs=$(awk -F= '/^lxc.rootfs =/{ print $2 }' $config)
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen else
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen rootfs=$path/rootfs
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen fi
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenfi
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Siraineninstall_debian $rootfs $release $arch $LXC_CACHE_PATH
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenif [ $? -ne 0 ]; then
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen echo "failed to install debian"
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen exit 1
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenfi
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenconfigure_debian $rootfs $name
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenif [ $? -ne 0 ]; then
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen echo "failed to configure debian for a container"
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen exit 1
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenfi
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainencopy_configuration $path $rootfs $name $arch
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenif [ $? -ne 0 ]; then
b0be0bead3d6963149f7f2a9504b8ab5aced9af5Timo Sirainen echo "failed write configuration file"
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen exit 1
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenfi
b0be0bead3d6963149f7f2a9504b8ab5aced9af5Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenconfigure_debian_systemd $path $rootfs
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenpost_process ${rootfs} ${release} ${arch} ${hostarch} ${packages}
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenif [ ! -z "$clean" ]; then
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen clean || exit 1
2aa3ac8bfb8fea49fd0ab6d56afcb3af3375d22cTimo Sirainen exit 0
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenfi
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainen