lxc-debian.in revision 207bf0e475f1dc6e9a2dac2cee3a209b56427855
#
# 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
# 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
}
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
}
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] [-r|--release=<release>] [-c|--clean]
release: the debian release (e.g. wheezy): defaults to current stable
arch: the container architecture (e.g. amd64): defaults to host arch
EOF
return 0
}
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
then
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