lxc-debian.in revision 28e58a6a4fc22a15e01b34874b5f8e9486e25e42
#
# 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
export GREP_OPTIONS=""
LOCALSTATEDIR="@LOCALSTATEDIR@"
LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
# Allows the lxc-cache directory to be set by environment variable
{
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
# 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
EOF
${prefix} $SECURITY_MIRROR ${release}/updates main contrib non-free
EOF
fi
}
{
local packages="$*"
if [ -n "${packages}" ]; then
fi
}
{
path=$1
rootfs=$2
# this only works if we have getty@.service to manipulate
fi
# just in case systemd is not installed
# Fix getty-static-service as debootstrap does not install dbus
if [ -e $rootfs//lib/systemd/system/getty-static.service ] ; then
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
fi
# 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
cache="$4/debian"
(
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
# 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.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
# Test if dpkg supports multiarch
echo "dpkg does not support multiarch: removing multiarch configuration file"
fi
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
# reconfigure locales
cat >> $rootfs/etc/locale.gen << EOF
en_US.UTF-8 UTF-8
EOF
else
-i /etc/locale.gen 2> /dev/null
cat >> $rootfs/etc/locale.gen << EOF
$LANG $encoding
EOF
fi
# end
}
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
Template specific options can be passed to lxc-create after a '--' like this:
lxc-create --name=NAME [-lxc-create-options] -- [-template-options]
Usage: $1 -h|--help -p|--path=<path> [-c|--clean] [-a|--arch=<arch>] [-r|--release=<release>]
[--mirror=<mirror>] [--security-mirror=<security mirror>]
[--package=<package_name1,package_name2,...>]
Options :
-h, --help print this help text
-p, --path=PATH directory where config and rootfs of this VM will be kept
-a, --arch=ARCH The container architecture. Can be one of: i686, x86_64,
amd64, armhf, armel, powerpc. Defaults to host arch.
-r, --release=RELEASE Debian release. Can be one of: squeeze, wheezy, jessie, sid.
Defaults to current stable.
--mirror=MIRROR Debian mirror to use during installation. Overrides the MIRROR
environment variable (see below).
--security-mirror=SECURITY_MIRROR
Debian mirror to use for security updates. Overrides the
SECURITY_MIRROR environment variable (see below).
--packages=PACKAGE_NAME1,PACKAGE_NAME2,...
List of additional packages to install. Comma separated, without space.
-c, --clean only clean up the cache and terminate
Environment variables:
MIRROR The Debian package mirror to use. See also the --mirror switch above.
Defaults to '$MIRROR'
SECURITY_MIRROR The Debian package security mirror to use. See also the --security-mirror switch above.
Defaults to '$SECURITY_MIRROR'
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