lxc-ubuntu.in revision ae0aeadeaab6b2535952dd699efdf889c4f7464f
#
# template script for generating ubuntu container for LXC
#
# This script consolidates and extends the existing lxc ubuntu scripts
#
# Copyright © 2011 Serge Hallyn <serge.hallyn@canonical.com>
# Copyright © 2010 Wilhelm Meier
# Author: Wilhelm Meier <wilhelm.meier@fh-kl.de>
#
# 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
set -e
LOCALSTATEDIR="@LOCALSTATEDIR@"
LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
fi
# Check if given path is in a btrfs partition
is_btrfs()
{
}
# Check if given path is the root of a btrfs subvolume
{
}
{
path=$1
[ -d $path ] && return 0
else
fi
}
{
path=$1
[ -d $path ] || return 0
else
fi
}
{
rootfs=$1
hostname=$2
release=$3
user=$4
password=$5
# configure the network using the dhcp
cat <<EOF > $rootfs/etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOF
# set the hostname
cat <<EOF > $rootfs/etc/hostname
$hostname
EOF
# set minimal hosts
cat <<EOF > $rootfs/etc/hosts
127.0.0.1 localhost
127.0.1.1 $hostname
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF
if [ ! -f $rootfs/etc/init/container-detect.conf ]; then
# suppress log level output for udev
# remove jobs for consoles 5 and 6 since we only create 4 consoles in
# this template
fi
fi
# make sure we have the current locale defined in the container
else
fi
# generate new SSH keys
cat > $rootfs/usr/sbin/policy-rc.d << EOF
#!/bin/sh
exit 101
EOF
DPKG_MAINTSCRIPT_PACKAGE=openssh DPKG_MAINTSCRIPT_NAME=postinst chroot $rootfs /var/lib/dpkg/info/openssh-server.postinst configure
rm -f $rootfs/usr/sbin/policy-rc.d
fi
return 0
}
# finish setting up the user in the container by injecting ssh key and
# adding sudo group membership.
# passed-in user is either 'ubuntu' or the user to bind in from host.
{
user=$1
groups="sudo"
else
groups="sudo admin"
fi
done
u_path="/home/${user}/.ssh"
cp $auth_key "$root_u_path/authorized_keys"
echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
fi
return 0
}
# A function to try and autodetect squid-deb-proxy servers on the local network
# if either the squid-deb-proxy-client package is installed on the host or
# a parent container set the 50squid-deb-proxy-client file.
{
local proxy_file=/etc/apt/apt.conf.d/50squid-deb-proxy-client
squid_proxy_line= # That's a global :/
# Maybe the host is aware of a squid-deb-proxy?
if [ -f $apt_discover ]; then
echo -n "Discovering squid-deb-proxy..."
echo "found squid-deb-proxy: $squid_proxy_line"
else
echo "no squid-deb-proxy found"
fi
fi
# Are we in a nested container, and the parent already knows of a proxy?
if [ -f $proxy_file ]; then
# Extract the squid URL from the file (whatever is between "")
fi
}
#
# Choose proxies for container
# http_proxy will be used by debootstrap on the host.
# APT_PROXY will be used to set /etc/apt/apt.conf.d/70proxy in the container.
#
{
local rootfs=$1
local arch=$2
HTTP_PROXY="none"
fi
none)
export http_proxy=$squid_proxy_line
else
fi
;;
apt)
eval $RES
;;
*)
export http_proxy=$HTTP_PROXY
;;
esac
}
{
# $1 => path to the partial cache or the rootfs
# $2 => architecture we want to add
# $3 => whether to use the multi-arch syntax or not
mkdir -p $1/etc/apt/apt.conf.d
cat > $1/etc/apt/apt.conf.d/70proxy << EOF
Acquire::http::Proxy "$APT_PROXY" ;
EOF
fi
case $2 in
MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
;;
*)
;;
esac
if [ -n "$3" ]; then
deb [arch=$2] $MIRROR ${release} main restricted universe multiverse
deb [arch=$2] $MIRROR ${release}-updates main restricted universe multiverse
deb [arch=$2] $SECURITY_MIRROR ${release}-security main restricted universe multiverse
EOF
else
deb $MIRROR ${release} main restricted universe multiverse
deb $MIRROR ${release}-updates main restricted universe multiverse
deb $SECURITY_MIRROR ${release}-security main restricted universe multiverse
EOF
fi
}
{
local rootfs="$1"
shift
local packages="$*"
if [ -z $update ]
then
update=true
fi
if [ -n "${packages}" ]
then
fi
}
cleanup()
{
}
{
echo "Container upgrade failed. The container cache may be out of date,"
echo "in which case flushing the cache (see -F in the help output) may help."
}
{
cache=$1
arch=$2
release=$3
# Try to guess a list of langpacks to install
langpacks="language-pack-en"
fi
echo "Installing packages in template: ${packages_template}"
# check the mini ubuntu was not already downloaded
try_mksubvolume "$cache/partial-$arch"
if [ $? -ne 0 ]; then
return 1
fi
# download a mini ubuntu into a cache
echo "Downloading ubuntu $release minimal ..."
qemu-debootstrap --verbose --components=main,universe --arch=$arch --include=${packages_template} $release $cache/partial-$arch $MIRROR
else
debootstrap --verbose --components=main,universe --arch=$arch --include=${packages_template} $release $cache/partial-$arch $MIRROR
fi
if [ $? -ne 0 ]; then
echo "Failed to download the rootfs, aborting."
return 1
fi
# Serge isn't sure whether we should avoid doing this when
# $release == `distro-info -d`
echo "Installing updates"
if [ $? -ne 0 ]; then
echo "Failed to update the apt cache"
return 1
fi
#!/bin/sh
exit 101
EOF
lxc-unshare -s MOUNT -- chroot "$1/partial-${arch}" apt-get dist-upgrade -y || { suggest_flush; false; }
trap EXIT
trap SIGINT
trap SIGTERM
trap SIGHUP
echo "Download complete"
return 0
}
{
cache=$1
arch=$2
rootfs=$3
# make a local copy of the miniubuntu
echo "Copying rootfs to $rootfs ..."
if which btrfs >/dev/null 2>&1 && is_btrfs_subvolume $cache/rootfs-$arch && is_btrfs_subvolume $rootfs; then
btrfs subvolume delete $realrootfs || return 1
else
fi
return 0
}
{
rootfs=$1
release=$2
flushcache=$3
(
flock -x 9
if [ $? -ne 0 ]; then
echo "Cache repository is busy."
return 1
fi
if [ $flushcache -eq 1 ]; then
echo "Flushing cache..."
fi
if [ $? -ne 0 ]; then
echo "Failed to download 'ubuntu $release base'"
return 1
fi
fi
if [ $? -ne 0 ]; then
echo "Failed to copy rootfs"
return 1
fi
return 0
return $?
}
{
path=$1
rootfs=$2
name=$3
arch=$4
release=$5
arch="i686"
fi
# 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
# Generate the configuration file
## Relocate all the network config entries
## Relocate any other config entries
## Add all the includes
fi
if [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu.${release}.conf" ]; then
fi
## Add the container-specific config
cat <<EOF >> $path/config
lxc.utsname = $name
lxc.arch = $arch
EOF
## Re-add the previously removed network config
if [ $? -ne 0 ]; then
echo "Failed to add configuration"
return 1
fi
return 0
}
{
rootfs=$1
release=$2
packages=$3
# Disable service startup
cat > $rootfs/usr/sbin/policy-rc.d << EOF
#!/bin/sh
exit 101
EOF
# If the container isn't running a native architecture, setup multiarch
else
mkdir -p ${rootfs}/etc/dpkg/dpkg.cfg.d
fi
# Save existing value of MIRROR and SECURITY_MIRROR
# Write a new sources.list containing both native and multiarch entries
> ${rootfs}/etc/apt/sources.list
write_sourceslist $rootfs $arch "native"
write_sourceslist $rootfs $hostarch "multiarch"
# Finally update the lists and install upstart using the host architecture
HOST_PACKAGES="upstart:${hostarch} mountall:${hostarch} isc-dhcp-client:${hostarch}"
HOST_PACKAGES="$HOST_PACKAGES iproute2:${hostarch}"
else
HOST_PACKAGES="$HOST_PACKAGES iproute:${hostarch}"
fi
fi
# Install Packages in container
then
echo "Installing packages: ${packages}"
fi
# Set initial timezone as on host
else
echo "Timezone in container is not configured. Adjust it manually."
fi
# it, and in case that fails move it out of the way.
# NOTE: This can only be removed once 12.04 goes out of support
fi
# Re-enable service startup
}
{
rootfs=$1
user=$2
# make sure user's shell exists in the container
echo "Installing $pkg"
fi
# bind-mount the user's path into the container's /home
# use relative path in container
h2=${h#/}
done
# Make sure the group exists in container
}
usage()
{
cat <<EOF
$1 -h|--help [-a|--arch] [-b|--bindhome <user>] [-d|--debug]
[-F | --flush-cache] [-r|--release <release>] [ -S | --auth-key <keyfile>]
[--rootfs <rootfs>] [--packages <packages>] [-u|--user <user>] [--password <password>]
[--mirror <url>] [--security-mirror <url>]
release: the ubuntu release (e.g. precise): defaults to host release on ubuntu, otherwise uses latest LTS
bindhome: bind <user>'s home into the container
The ubuntu user will not be created, and <user> will have
sudo access.
arch: the container architecture (e.g. amd64): defaults to host arch
auth-key: SSH Public key file to inject into container
packages: list of packages to add comma separated
mirror,security-mirror: mirror for download and /etc/apt/sources.list
EOF
return 0
}
options=$(getopt -o a:b:hp:r:n:FS:du: -l arch:,bindhome:,help,path:,release:,name:,flush-cache,auth-key:,debug,rootfs:,packages:,user:,password:,mirror:,security-mirror: -- "$@")
if [ $? -ne 0 ]; then
exit 1
fi
eval set -- "$options"
if [ -f /etc/lsb-release ]; then
. /etc/lsb-release
if [ "$DISTRIB_ID" = "Ubuntu" ]; then
fi
fi
# Code taken from debootstrap
arch=`/usr/bin/dpkg --print-architecture`
arch=`/usr/bin/udpkg --print-architecture`
else
if [ "$arch" = "i686" ]; then
arch="i386"
elif [ "$arch" = "x86_64" ]; then
arch="amd64"
elif [ "$arch" = "armv7l" ]; then
arch="armhf"
elif [ "$arch" = "aarch64" ]; then
arch="arm64"
elif [ "$arch" = "ppc64le" ]; then
arch="ppc64el"
fi
fi
debug=0
packages=""
user="ubuntu"
password="ubuntu"
while true
do
case "$1" in
--) shift 1; break ;;
*) break ;;
esac
done
set -x
fi
if [ -n "$bindhome" ]; then
if [ $? -ne 0 ]; then
echo "Error: no password entry found for $bindhome"
exit 1
fi
fi
if [ "$arch" = "i686" ]; then
fi
exit 1
fi
exit 1
fi
exit 1
fi
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
# detect rootfs
# if $rootfs exists here, it was passed in with --rootfs
if [ -z "$rootfs" ]; then
else
fi
fi
if [ $? -ne 0 ]; then
echo "failed to install ubuntu $release"
exit 1
fi
if [ $? -ne 0 ]; then
echo "failed to configure ubuntu $release for a container"
exit 1
fi
if [ $? -ne 0 ]; then
echo "failed write configuration file"
exit 1
fi
if [ -n "$bindhome" ]; then
else
fi
echo ""
echo "##"
if [ -n "$bindhome" ]; then
echo "# Log in as user $bindhome"
else
echo "# Use the 'sudo' command to run tasks as root in the container."
fi
echo "##"
echo ""