lxc-debian.in revision 227c560065b22f84e4e41724486863c31b77ac01
#
# lxc: linux Container library
# Authors:
# Daniel Lezcano <daniel.lezcano@free.fr>
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
# Detect use under userns (unsupported)
[ "$arg" = "--" ] && break
echo "This template can't be used for unprivileged containers." 1>&2
echo "You may want to try the \"download\" template instead." 1>&2
exit 1
fi
done
# Make sure the usual locations are in PATH
LOCALSTATEDIR="@LOCALSTATEDIR@"
LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
{
rootfs=$1
hostname=$2
# therefore creating missing device nodes for tty1-4.
fi
done
# configure the inittab
cat <<EOF > $rootfs/etc/inittab
id:3:initdefault:
si::sysinit:/etc/init.d/rcS
l0:0:wait:/etc/init.d/rc 0
l1:1:wait:/etc/init.d/rc 1
l2:2:wait:/etc/init.d/rc 2
l3:3:wait:/etc/init.d/rc 3
l4:4:wait:/etc/init.d/rc 4
l5:5:wait:/etc/init.d/rc 5
l6:6:wait:/etc/init.d/rc 6
# Normally not reached, but fallthrough in case of emergency.
z6:6:respawn:/sbin/sulogin
1:2345:respawn:/sbin/getty 38400 console
c1:12345:respawn:/sbin/getty 38400 tty1 linux
c2:12345:respawn:/sbin/getty 38400 tty2 linux
c3:12345:respawn:/sbin/getty 38400 tty3 linux
c4:12345:respawn:/sbin/getty 38400 tty4 linux
p6::ctrlaltdel:/sbin/init 6
p0::powerfail:/sbin/init 0
EOF
# symlink mtab
# disable selinux in debian
# configure the network using the dhcp
cat <<EOF > $rootfs/etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOF
# set the hostname
cat <<EOF > $rootfs/etc/hostname
$hostname
EOF
# reconfigure some services
else
-i /etc/locale.gen 2> /dev/null
fi
# remove pointless services in a container
# generate new SSH keys
cat > $rootfs/usr/sbin/policy-rc.d << EOF
#!/bin/sh
exit 101
EOF
fi
DPKG_MAINTSCRIPT_PACKAGE=openssh DPKG_MAINTSCRIPT_NAME=postinst chroot $rootfs /var/lib/dpkg/info/openssh-server.postinst configure
fi
rm -f $rootfs/usr/sbin/policy-rc.d
fi
# set initial timezone as on host
else
echo "Timezone in container is not configured. Adjust it manually."
fi
echo "Root password is 'root', please change !"
return 0
}
{
local prefix="deb"
if [ -n "${arch}" ]; then
prefix="deb [arch=${arch}]"
fi
${prefix} $MIRROR ${release} main contrib non-free
${prefix} $SECURITY_MIRROR ${release}/updates main contrib non-free
EOF
}
{
local packages="$*"
if [ -n "${packages}" ]; then
fi
}
{
path=$1
rootfs=$2
# only appropriate when systemd is PID 1
fi
# this only works if we have getty@.service to manipulate
fi
# just in case systemd is not installed
# This function has been copied and adapted from lxc-fedora
# Make systemd honor SIGPWR
# Setup getty service on the 4 ttys we are going to allow in the
# default config. Number should match lxc.tty
( cd ${rootfs}/etc/systemd/system/getty.target.wants
return 0
}
cleanup()
{
}
{
packages=\
ifupdown,\
locales,\
dialog,\
netbase,\
iproute,\
cache=$1
arch=$2
release=$3
# check the mini debian was not already downloaded
if [ $? -ne 0 ]; then
return 1
fi
# download a mini debian into a cache
echo "Downloading debian minimal ..."
if [ $? -ne 0 ]; then
echo "Failed to download the rootfs, aborting."
return 1
fi
echo "Download complete."
trap EXIT
trap SIGINT
trap SIGTERM
trap SIGHUP
return 0
}
{
cache=$1
arch=$2
rootfs=$3
release=$4
# make a local copy of the minidebian
echo -n "Copying rootfs to $rootfs..."
return 0
}
{
rootfs=$1
release=$2
arch=$3
(
flock -x 9
if [ $? -ne 0 ]; then
echo "Cache repository is busy."
return 1
fi
if [ $? -ne 0 ]; then
echo "Failed to download 'debian base'"
return 1
fi
fi
if [ $? -ne 0 ]; then
echo "Failed to copy rootfs"
return 1
fi
return 0
return $?
}
{
path=$1
rootfs=$2
hostname=$3
arch=$4
# Generate the configuration file
## Create the fstab (empty by default)
# if there is exactly one veth network entry, make sure it has an
# associated hwaddr.
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
fi
## Add all the includes
fi
if [ -e "${LXC_TEMPLATE_CONFIG}/debian.${release}.conf" ]; then
fi
## Add the container-specific config
cat <<EOF >> $path/config
lxc.mount = $path/fstab
lxc.utsname = $hostname
lxc.arch = $arch
EOF
if [ $? -ne 0 ]; then
echo "Failed to add configuration"
return 1
fi
return 0
}
{
local packages="$*"
# Disable service startup
cat > ${rootfs}/usr/sbin/policy-rc.d << EOF
#!/bin/sh
exit 101
EOF
chmod +x ${rootfs}/usr/sbin/policy-rc.d
# If the container isn't running a native architecture, setup multiarch
if [ "${arch}" != "${hostarch}" ]; then
mkdir -p ${rootfs}/etc/dpkg/dpkg.cfg.d
fi
# Write a new sources.list containing both native and multiarch entries
> ${rootfs}/etc/apt/sources.list
if [ "${arch}" = "${hostarch}" ]; then
write_sourceslist ${rootfs} ${release} ${arch}
else
write_sourceslist ${rootfs} ${release}
fi
# Install Packages in container
if [ -n "${packages}" ]; then
echo "Installing packages: ${pack_list}"
install_packages ${rootfs} ${pack_list}
fi
# Re-enable service startup
rm ${rootfs}/usr/sbin/policy-rc.d
}
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..."
exit 0
}
usage()
{
cat <<EOF
$1 -h|--help -p|--path=<path> [-a|--arch] [-c|--clean] [--mirror=<mirror>] [-r|--release=<release>] [--security-mirror=<security mirror>]
clean: purge the download cache after installation
arch: the container architecture (e.g. amd64): defaults to host arch
release: the debian release (e.g. wheezy): defaults to current stable
mirror: debain mirror to use during installation
security mirror: debain mirror to use for security updates
packages: list of packages to add comma separated
EOF
return 0
}
options=$(getopt -o hp:n:a:r:c -l arch:,clean,help,mirror:,name:,packages:,path:,release:,rootfs:,security-mirror: -- "$@")
if [ $? -ne 0 ]; then
exit 1
fi
eval set -- "$options"
arch=$(dpkg --print-architecture)
else
if [ "$arch" = "i686" ]; then
arch="i386"
elif [ "$arch" = "x86_64" ]; then
arch="amd64"
elif [ "$arch" = "armv7l" ]; then
arch="armhf"
fi
fi
while true
do
case "$1" in
--) shift 1; break ;;
*) break ;;
esac
done
clean || exit 1
exit 0
fi
if [ "$arch" = "i686" ]; then
fi
if [ "$arch" = "x86_64" ]; then
fi
exit 1
fi
exit 1
fi
exit 1
fi
type debootstrap
if [ $? -ne 0 ]; then
echo "'debootstrap' command is missing"
exit 1
fi
if [ -z "$path" ]; then
echo "'path' parameter is required"
exit 1
fi
echo "This script should be run as 'root'"
exit 1
fi
current_release=`wget ${MIRROR}/dists/stable/Release -O - 2> /dev/null | head |awk '/^Codename: (.*)$/ { print $2; }'`
echo "Invalid release ${release}, valid ones are: ${valid_releases[*]}"
exit 1
fi
# detect rootfs
if [ -z "$rootfs" ]; then
else
fi
fi
if [ $? -ne 0 ]; then
echo "failed to install debian"
exit 1
fi
if [ $? -ne 0 ]; then
echo "failed to configure debian for a container"
exit 1
fi
if [ $? -ne 0 ]; then
echo "failed write configuration file"
exit 1
fi
if [ ! -z "$clean" ]; then
clean || exit 1
exit 0
fi