4
# template script for generating ubuntu container for LXC
6
# This script consolidates and extends the existing lxc ubuntu scripts
9
# XXX todo: add -lvm option
11
# Copyright � 2011 Serge Hallyn <serge.hallyn@canonical.com>
12
# Copyright � 2010 Wilhelm Meier
13
# Author: Wilhelm Meier <wilhelm.meier@fh-kl.de>
15
# This program is free software; you can redistribute it and/or modify
16
# it under the terms of the GNU General Public License version 2, as
17
# published by the Free Software Foundation.
19
# This program is distributed in the hope that it will be useful,
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
# GNU General Public License for more details.
24
# You should have received a copy of the GNU General Public License along
25
# with this program; if not, write to the Free Software Foundation, Inc.,
26
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29
if [ -r /etc/default/lxc ]; then
38
# configure the network using the dhcp
39
cat <<EOF > $rootfs/etc/network/interfaces
41
iface lo inet loopback
47
# so you can 'ssh $hostname.' or 'ssh $hostname.local'
48
if [ -f $rootfs/etc/dhcp/dhclient.conf ]; then
49
sed -i "s/<hostname>/$hostname/" $rootfs/etc/dhcp/dhclient.conf
50
elif [ -f $rootfs/etc/dhcp3/dhclient.conf ]; then
51
sed -i "s/<hostname>/$hostname/" $rootfs/etc/dhcp3/dhclient.conf
55
cat <<EOF > $rootfs/etc/hostname
59
cat <<EOF > $rootfs/etc/hosts
60
127.0.0.1 localhost $hostname
63
# suppress log level output for udev
64
sed -i "s/=\"err\"/=0/" $rootfs/etc/udev/udev.conf
66
# remove jobs for consoles 5 and 6 since we only create 4 consoles in
68
rm -f $rootfs/etc/init/tty{5,6}.conf
70
echo "Please change root-password !"
71
echo "root:root" | chroot $rootfs chpasswd
82
if [ $release = "lucid" ]; then
83
packages=dialog,apt,apt-utils,resolvconf,iproute,inetutils-ping,vim,dhcp3-client,ssh,lsb-release,gnupg
84
elif [ $release = "maverick" ]; then
85
packages=dialog,apt,apt-utils,resolvconf,iproute,inetutils-ping,vim,dhcp3-client,ssh,lsb-release,gnupg,netbase
86
elif [ $release = "natty" ]; then
87
packages=dialog,apt,apt-utils,resolvconf,iproute,inetutils-ping,vim,isc-dhcp-client,isc-dhcp-common,ssh,lsb-release,gnupg,netbase
89
packages=dialog,apt,apt-utils,iproute,inetutils-ping,vim,isc-dhcp-client,isc-dhcp-common,ssh,lsb-release,gnupg,netbase,ubuntu-keyring
91
echo "installing packages: $packages"
93
# check the mini ubuntu was not already downloaded
94
mkdir -p "$cache/partial-$arch"
96
echo "Failed to create '$cache/partial-$arch' directory"
100
# download a mini ubuntu into a cache
101
echo "Downloading ubuntu $release minimal ..."
102
debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
103
if [ $? -ne 0 ]; then
104
echo "Failed to download the rootfs, aborting."
108
mv "$1/partial-$arch" "$1/rootfs-$arch"
109
echo "Download complete."
120
# make a local copy of the miniubuntu
121
echo -n "Copying rootfs to $rootfs ..."
122
cp -a $cache/rootfs-$arch $rootfs || return 1
130
cache="/var/cache/lxc/$release"
131
mkdir -p /var/lock/subsys/
134
if [ $? -ne 0 ]; then
135
echo "Cache repository is busy."
140
echo "Checking cache download in $cache/rootfs-$arch ... "
141
if [ ! -e "$cache/rootfs-$arch" ]; then
142
download_ubuntu $cache $arch $release
143
if [ $? -ne 0 ]; then
144
echo "Failed to download 'ubuntu $release base'"
149
echo "Copy $cache/rootfs-$arch to $rootfs ... "
150
copy_ubuntu $cache $arch $rootfs
151
if [ $? -ne 0 ]; then
152
echo "Failed to copy rootfs"
158
) 200>/var/lock/subsys/lxc
170
if [ $arch = "i386" ]; then
174
cat <<EOF >> $path/config
180
lxc.mount = $path/fstab
182
lxc.cap.drop = sys_module
184
lxc.cgroup.devices.deny = a
185
# Allow any mknod (but not using the node)
186
lxc.cgroup.devices.allow = c *:* m
187
lxc.cgroup.devices.allow = b *:* m
189
lxc.cgroup.devices.allow = c 1:3 rwm
190
lxc.cgroup.devices.allow = c 1:5 rwm
192
lxc.cgroup.devices.allow = c 5:1 rwm
193
lxc.cgroup.devices.allow = c 5:0 rwm
194
#lxc.cgroup.devices.allow = c 4:0 rwm
195
#lxc.cgroup.devices.allow = c 4:1 rwm
197
lxc.cgroup.devices.allow = c 1:9 rwm
198
lxc.cgroup.devices.allow = c 1:8 rwm
199
lxc.cgroup.devices.allow = c 136:* rwm
200
lxc.cgroup.devices.allow = c 5:2 rwm
202
lxc.cgroup.devices.allow = c 254:0 rwm
204
lxc.cgroup.devices.allow = c 10:229 rwm
206
lxc.cgroup.devices.allow = c 10:200 rwm
209
cat <<EOF > $path/fstab
210
proc $rootfs/proc proc nodev,noexec,nosuid 0 0
211
sysfs $rootfs/sys sysfs defaults 0 0
214
if [ $? -ne 0 ]; then
215
echo "Failed to add configuration"
227
# provide the lxc service
228
cat <<EOF > $rootfs/etc/init/lxc.conf
229
# fake some events needed for correct startup other services
231
description "Container Upstart"
236
rm -rf /var/run/*.pid
237
rm -rf /var/run/network/*
238
/sbin/initctl emit stopped JOB=udevtrigger --no-wait
239
/sbin/initctl emit started JOB=udev --no-wait
243
# fix buggus runlevel with sshd
244
cat <<EOF > $rootfs/etc/init/ssh.conf
245
# ssh - OpenBSD Secure Shell server
247
# The OpenSSH server provides secure shell access to the system.
249
description "OpenSSH server"
252
stop on runlevel [!2345]
258
# replaces SSHD_OOM_ADJUST in /etc/default/ssh
262
test -x /usr/sbin/sshd || { stop; exit 0; }
263
test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; }
264
test -c /dev/null || { stop; exit 0; }
266
mkdir -p -m0755 /var/run/sshd
269
# if you used to set SSHD_OPTS in /etc/default/ssh, you can change the
270
# 'exec' line here instead
274
cat <<EOF > $rootfs/etc/init/console.conf
277
# This service maintains a console on tty1 from the point the system is
278
# started until it is shut down again.
280
start on stopped rc RUNLEVEL=[2345]
281
stop on runlevel [!2345]
284
exec /sbin/getty -8 38400 /dev/console
287
cat <<EOF > $rootfs/lib/init/fstab
288
# /lib/init/fstab: cleared out for bare-bones lxc
291
# reconfigure some services
292
if [ -z "$LANG" ]; then
293
chroot $rootfs locale-gen en_US.UTF-8
294
chroot $rootfs update-locale LANG=en_US.UTF-8
296
chroot $rootfs locale-gen $LANG
297
chroot $rootfs update-locale LANG=$LANG
300
# remove pointless services in a container
301
chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
303
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
304
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
305
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
306
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
307
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
309
# if this isn't lucid, then we need to twiddle the network upstart bits :(
310
if [ $release != "lucid" ]; then
311
sed -i 's/^.*emission handled.*$/echo Emitting lo/' $rootfs/etc/network/if-up.d/upstart
321
if [ $trim_container -eq 1 ]; then
322
trim $rootfs $release
324
# for lucid and maverick, if not trimming, then add the ubuntu-virt
325
# ppa and install lxcguest
326
if [ $release = "lucid" -o $release = "maverick" ]; then
327
chroot $rootfs apt-get install --force-yes -y python-software-properties
328
chroot $rootfs add-apt-repository ppa:ubuntu-virt/ppa
329
chroot $rootfs apt-get update
331
chroot $rootfs apt-get install --force-yes -y lxcguest
340
# copy /etc/passwd, /etc/shadow, and /etc/group entries into container
341
pwd=`getent passwd $user`
342
if [ $? -ne 0 ]; then
343
echo 'Warning: failed to copy password entry for $user'
346
echo $pwd >> $rootfs/etc/passwd
348
shad=`getent shadow $user`
349
echo $shad >> $rootfs/etc/shadow
351
# bind-mount the user's path into the container's /home
352
h=`getent passwd $user | cut -d: -f 6`
354
echo "$h $rootfs/$h none bind 0 0" >> $path/fstab
360
cache="/var/cache/lxc/$release"
362
if [ ! -e $cache ]; then
366
# lock, so we won't purge while someone is creating a repository
370
echo "Cache repository is busy."
374
echo -n "Purging the download cache..."
375
rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
378
) 200>/var/lock/subsys/lxc
384
$1 -h|--help -p|--path=<path> --clean [-a|--arch] [-b|--bindhome <user>] [--trim] [-r|--release]
385
release: lucid | maverick | natty | oneiric
386
trim: make a minimal (faster, but not upgrade-safe) container
387
bindhome: bind <user>'s home into the container
388
arch: amd64 or i386: defaults to host arch
393
options=$(getopt -o a:b:hp:r:xn:c -l arch:,bindhome:,help,path:,release:,trim,name:,clean -- "$@")
394
if [ $? -ne 0 ]; then
398
eval set -- "$options"
401
if [ -f /etc/lsb-release ]; then
403
case "$DISTRIB_CODENAME" in
404
lucid|maverick|natty|oneiric)
405
release=$DISTRIB_CODENAME
413
# Code taken from debootstrap
414
if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
415
arch=`/usr/bin/dpkg --print-architecture`
416
elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
417
arch=`/usr/bin/udpkg --print-architecture`
420
if [ "$arch" = "i686" ]; then
422
elif [ "$arch" = "x86_64" ]; then
424
elif [ "$arch" = "armv7l" ]; then
434
-h|--help) usage $0 && exit 0;;
435
-p|--path) path=$2; shift 2;;
436
-n|--name) name=$2; shift 2;;
437
-c|--clean) clean=$2; shift 2;;
438
-r|--release) release=$2; shift 2;;
439
-b|--bindhome) bindhome=$2; shift 2;;
440
-a|--arch) arch=$2; shift 2;;
441
-x|--trim) trim_container=1; shift 1;;
442
--) shift 1; break ;;
447
pwd=`getent passwd $bindhome`
448
if [ $? -ne 0 ]; then
449
echo "Error: no password entry found for $bindhome"
454
if [ "$arch" == "i686" ]; then
458
if [ ! -z "$clean" -a -z "$path" ]; then
463
if [ $hostarch = "i386" -a $arch = "amd64" ]; then
464
echo "can't create amd64 container on i386"
469
if [ $? -ne 0 ]; then
470
echo "'debootstrap' command is missing"
474
if [ -z "$path" ]; then
475
echo "'path' parameter is required"
479
if [ "$(id -u)" != "0" ]; then
480
echo "This script should be run as 'root'"
486
install_ubuntu $rootfs $release
487
if [ $? -ne 0 ]; then
488
echo "failed to install ubuntu $release"
492
configure_ubuntu $rootfs $name
493
if [ $? -ne 0 ]; then
494
echo "failed to configure ubuntu $release for a container"
498
copy_configuration $path $rootfs $name $arch
499
if [ $? -ne 0 ]; then
500
echo "failed write configuration file"
504
post_process $rootfs $release $trim_container
505
if [ ! -z $bindhome ]; then
506
do_bindhome $rootfs $bindhome
509
if [ ! -z $clean ]; then
510
clean $release || exit 1