lxc-ubuntu.in revision 4d7bcfb638c5c4907e8539aa09d41bb1de08a097
2ronwalf# template script for generating ubuntu container for LXC 2ronwalf# This script consolidates and extends the existing lxc ubuntu scripts 2ronwalf# Copyright � 2011 Serge Hallyn <serge.hallyn@canonical.com> 2ronwalf# Copyright � 2010 Wilhelm Meier 2ronwalf# Author: Wilhelm Meier <wilhelm.meier@fh-kl.de> 2ronwalf# This library is free software; you can redistribute it and/or 2ronwalf# modify it under the terms of the GNU Lesser General Public 2ronwalf# License as published by the Free Software Foundation; either 2ronwalf# version 2.1 of the License, or (at your option) any later version. 2ronwalf# This library is distributed in the hope that it will be useful, 2ronwalf# but WITHOUT ANY WARRANTY; without even the implied warranty of 2ronwalf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 2ronwalf# Lesser General Public License for more details. 2ronwalf# You should have received a copy of the GNU Lesser General Public 2ronwalf# License along with this library; if not, write to the Free Software 2ronwalf# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # 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 cat <<EOF > $rootfs/etc/hostname cat <<EOF > $rootfs/etc/hosts # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback # suppress log level output for udev # remove jobs for consoles 5 and 6 since we only create 4 consoles in # make sure we have the current locale defined in the container # 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. # Choose proxies for container # http_proxy will be used by debootstrap on the host. # $1 => path to the rootfs # $2 => architecture we want to add # $3 => whether to use the multi-arch syntax or not Acquire::http::Proxy "$APT_PROXY" ; 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 deb $MIRROR ${release} main restricted universe multiverse deb $MIRROR ${release}-updates main restricted universe multiverse deb $SECURITY_MIRROR ${release}-security main restricted universe multiverse echo "Container upgrade failed. The container cache may be out of date," echo "in which case flushing the case (see -F in the hep output) may help." # Try to guess a list of langpacks to install dpkg -l | grep -E "^ii language-pack-[a-z]* " | # check the mini ubuntu was not already downloaded echo "Failed to create '$cache/partial-$arch' directory" # download a mini ubuntu into a cache echo "Downloading ubuntu $release minimal ..." echo "Failed to download the rootfs, aborting." # Serge isn't sure whether we should avoid doing this when # $release == `distro-info -d` echo "Installing updates" echo "Failed to update the apt cache" # make a local copy of the miniubuntu echo "Copying rootfs to $rootfs ..." echo "Cache repository is busy." echo "Checking cache download in $cache/rootfs-$arch ... " echo "Failed to download 'ubuntu $release base'" echo "Failed to copy rootfs" if [ $arch = "i386" ]; then # if there is exactly one veth network entry, make sure it has an cat <<EOF >> $path/config lxc.pivotdir = lxc_putold lxc.cap.drop = sys_module mac_admin mac_override sys_time # When using LXC with apparmor, uncomment the next line to run unconfined: #lxc.aa_profile = unconfined # To support container nesting on an Ubuntu host, uncomment next two lines: #lxc.aa_profile = lxc-container-default-with-nesting #lxc.hook.mount = /usr/share/lxc/hooks/mountcgroups lxc.cgroup.devices.deny = a # Allow any mknod (but not using the node) lxc.cgroup.devices.allow = c *:* m lxc.cgroup.devices.allow = b *:* m lxc.cgroup.devices.allow = c 1:3 rwm lxc.cgroup.devices.allow = c 1:5 rwm lxc.cgroup.devices.allow = c 5:1 rwm lxc.cgroup.devices.allow = c 5:0 rwm lxc.cgroup.devices.allow = c 1:9 rwm lxc.cgroup.devices.allow = c 1:8 rwm lxc.cgroup.devices.allow = c 136:* rwm lxc.cgroup.devices.allow = c 5:2 rwm lxc.cgroup.devices.allow = c 254:0 rm lxc.cgroup.devices.allow = c 10:229 rwm lxc.cgroup.devices.allow = c 10:200 rwm lxc.cgroup.devices.allow = c 1:7 rwm lxc.cgroup.devices.allow = c 10:228 rwm lxc.cgroup.devices.allow = c 10:232 rwm proc proc proc nodev,noexec,nosuid 0 0 sysfs sys sysfs defaults 0 0 /sys/fs/fuse/connections sys/fs/fuse/connections none bind 0 0 /sys/kernel/debug sys/kernel/debug none bind 0 0 /sys/kernel/security sys/kernel/security none bind 0 0 /sys/fs/pstore sys/fs/pstore none bind,optional 0 0 echo "Failed to add configuration" # provide the lxc service cat <<EOF > $rootfs/etc/init/lxc.conf # fake some events needed for correct startup other services description "Container Upstart" rm -rf /var/run/network/* /sbin/initctl emit stopped JOB=udevtrigger --no-wait /sbin/initctl emit started JOB=udev --no-wait # fix buggus runlevel with sshd cat <<EOF > $rootfs/etc/init/ssh.conf # ssh - OpenBSD Secure Shell server # The OpenSSH server provides secure shell access to the system. description "OpenSSH server" # replaces SSHD_OOM_ADJUST in /etc/default/ssh test -x /usr/sbin/sshd || { stop; exit 0; } test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; } test -c /dev/null || { stop; exit 0; } mkdir -p -m0755 /var/run/sshd # if you used to set SSHD_OPTS in /etc/default/ssh, you can change the # 'exec' line here instead cat <<EOF > $rootfs/etc/init/console.conf # This service maintains a console on tty1 from the point the system is # started until it is shut down again. start on stopped rc RUNLEVEL=[2345] exec /sbin/getty -8 38400 /dev/console cat <<EOF > $rootfs/lib/init/fstab # /lib/init/fstab: cleared out for bare-bones lxc # remove pointless services in a container # if this isn't lucid, then we need to twiddle the network upstart bits :( # for lucid, if not trimming, then add the ubuntu-virt # ppa and install lxcguest # If the container isn't running a native architecture, setup multiarch # Save existing value of MIRROR and SECURITY_MIRROR # Write a new sources.list containing both native and multiarch entries # Finally update the lists and install upstart using the host architecture HOST_PACKAGES="upstart:${hostarch} mountall:${hostarch} isc-dhcp-client:${hostarch}" # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did # get bind mounted to the host's /run/shm. So try to rmdir # it, and in case that fails move it out of the way. # make sure user's shell exists in the container echo "shell $shell for user $user was not found in the container." # bind-mount the user's path into the container's /home # use relative path in container while [ ${h2:0:1} = "/" ]; do # Make sure the group exists in container grp=`echo $pwd | cut -d: -f 4` # group number for $user $1 -h|--help [-a|--arch] [-b|--bindhome <user>] [--trim] [-d|--debug] [-F | --flush-cache] [-r|--release <release>] [ -S | --auth-key <keyfile>] release: the ubuntu release (e.g. precise): defaults to host release on ubuntu, otherwise uses latest LTS trim: make a minimal (faster, but not upgrade-safe) container bindhome: bind <user>'s home into the container The ubuntu user will not be created, and <user> will have arch: the container architecture (e.g. amd64): defaults to host arch auth-key: SSH Public key file to inject into container options=
$(getopt -o a:b:hp:r:xn:FS:d -l arch:,bindhome:,help,path:,release:,trim,name:,flush-cache,auth-key:,debug,rootfs: -- "$@") release=
precise # Default to the last Ubuntu LTS release for non-Ubuntu systems # Code taken from debootstrap if [
"$arch" =
"i686" ];
then elif [
"$arch" =
"x86_64" ];
then elif [
"$arch" =
"armv7l" ];
then echo "Error: no password entry found for $bindhome" if [
"$arch" ==
"i686" ];
then echo "'path' parameter is required" if [
"$(id -u)" !=
"0" ];
then echo "This script should be run as 'root'" # if $rootfs exists here, it was passed in with --rootfs echo "failed to install ubuntu $release" echo "failed to configure ubuntu $release for a container" echo "failed write configuration file" echo "# The default user is 'ubuntu' with password 'ubuntu'!" echo "# Use the 'sudo' command to run tasks as root in the container."