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
# Serge isn't sure whether we should avoid doing this when
109
# $release == `distro-info -d`
110
echo "Installing updates"
113
MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
114
SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
119
MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
120
SECURITY_MIRROR=${SECURITY_MIRRORMIRROR:-http://security.ubuntu.com/ubuntu}
123
MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
124
SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
129
MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
130
SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
133
cat >> "$1/partial-${arch}/etc/apt/sources.list" << EOF
134
deb $MIRROR ${release}-updates main universe
135
deb $SECURITY_MIRROR ${release}-security main universe
137
chroot "$1/partial-${arch}" apt-get update
138
if [ $? -ne 0 ]; then
139
echo "Failed to update the apt cache"
142
cat > "$1/partial-${arch}"/usr/sbin/policy-rc.d << EOF
146
chmod +x "$1/partial-${arch}"/usr/sbin/policy-rc.d
148
lxc-unshare -s MOUNT -- chroot "$1/partial-${arch}" apt-get dist-upgrade -y
150
rm -f "$1/partial-${arch}"/usr/sbin/policy-rc.d
152
if [ $ret -ne 0 ]; then
153
echo "Failed to upgrade the cache"
157
mv "$1/partial-$arch" "$1/rootfs-$arch"
158
echo "Download complete"
168
# make a local copy of the miniubuntu
169
echo -n "Copying rootfs to $rootfs ..."
170
cp -a $cache/rootfs-$arch $rootfs || return 1
178
cache="/var/cache/lxc/$release"
179
mkdir -p /var/lock/subsys/
182
if [ $? -ne 0 ]; then
183
echo "Cache repository is busy."
188
echo "Checking cache download in $cache/rootfs-$arch ... "
189
if [ ! -e "$cache/rootfs-$arch" ]; then
190
download_ubuntu $cache $arch $release
191
if [ $? -ne 0 ]; then
192
echo "Failed to download 'ubuntu $release base'"
197
echo "Copy $cache/rootfs-$arch to $rootfs ... "
198
copy_ubuntu $cache $arch $rootfs
199
if [ $? -ne 0 ]; then
200
echo "Failed to copy rootfs"
206
) 200>/var/lock/subsys/lxc
218
if [ $arch = "i386" ]; then
222
cat <<EOF >> $path/config
228
lxc.mount = $path/fstab
230
lxc.cap.drop = sys_module mac_admin
232
lxc.cgroup.devices.deny = a
233
# Allow any mknod (but not using the node)
234
lxc.cgroup.devices.allow = c *:* m
235
lxc.cgroup.devices.allow = b *:* m
237
lxc.cgroup.devices.allow = c 1:3 rwm
238
lxc.cgroup.devices.allow = c 1:5 rwm
240
lxc.cgroup.devices.allow = c 5:1 rwm
241
lxc.cgroup.devices.allow = c 5:0 rwm
242
#lxc.cgroup.devices.allow = c 4:0 rwm
243
#lxc.cgroup.devices.allow = c 4:1 rwm
245
lxc.cgroup.devices.allow = c 1:9 rwm
246
lxc.cgroup.devices.allow = c 1:8 rwm
247
lxc.cgroup.devices.allow = c 136:* rwm
248
lxc.cgroup.devices.allow = c 5:2 rwm
250
lxc.cgroup.devices.allow = c 254:0 rwm
252
lxc.cgroup.devices.allow = c 10:229 rwm
254
lxc.cgroup.devices.allow = c 10:200 rwm
257
cat <<EOF > $path/fstab
258
proc $rootfs/proc proc nodev,noexec,nosuid 0 0
259
sysfs $rootfs/sys sysfs defaults 0 0
262
if [ $? -ne 0 ]; then
263
echo "Failed to add configuration"
275
# provide the lxc service
276
cat <<EOF > $rootfs/etc/init/lxc.conf
277
# fake some events needed for correct startup other services
279
description "Container Upstart"
284
rm -rf /var/run/*.pid
285
rm -rf /var/run/network/*
286
/sbin/initctl emit stopped JOB=udevtrigger --no-wait
287
/sbin/initctl emit started JOB=udev --no-wait
291
# fix buggus runlevel with sshd
292
cat <<EOF > $rootfs/etc/init/ssh.conf
293
# ssh - OpenBSD Secure Shell server
295
# The OpenSSH server provides secure shell access to the system.
297
description "OpenSSH server"
300
stop on runlevel [!2345]
306
# replaces SSHD_OOM_ADJUST in /etc/default/ssh
310
test -x /usr/sbin/sshd || { stop; exit 0; }
311
test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; }
312
test -c /dev/null || { stop; exit 0; }
314
mkdir -p -m0755 /var/run/sshd
317
# if you used to set SSHD_OPTS in /etc/default/ssh, you can change the
318
# 'exec' line here instead
322
cat <<EOF > $rootfs/etc/init/console.conf
325
# This service maintains a console on tty1 from the point the system is
326
# started until it is shut down again.
328
start on stopped rc RUNLEVEL=[2345]
329
stop on runlevel [!2345]
332
exec /sbin/getty -8 38400 /dev/console
335
cat <<EOF > $rootfs/lib/init/fstab
336
# /lib/init/fstab: cleared out for bare-bones lxc
339
# reconfigure some services
340
if [ -z "$LANG" ]; then
341
chroot $rootfs locale-gen en_US.UTF-8
342
chroot $rootfs update-locale LANG=en_US.UTF-8
344
chroot $rootfs locale-gen $LANG
345
chroot $rootfs update-locale LANG=$LANG
348
# remove pointless services in a container
349
chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
351
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
352
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
353
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
354
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
355
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
357
# if this isn't lucid, then we need to twiddle the network upstart bits :(
358
if [ $release != "lucid" ]; then
359
sed -i 's/^.*emission handled.*$/echo Emitting lo/' $rootfs/etc/network/if-up.d/upstart
369
if [ $trim_container -eq 1 ]; then
370
trim $rootfs $release
372
# for lucid and maverick, if not trimming, then add the ubuntu-virt
373
# ppa and install lxcguest
374
if [ $release = "lucid" -o $release = "maverick" ]; then
375
chroot $rootfs apt-get install --force-yes -y python-software-properties
376
chroot $rootfs add-apt-repository ppa:ubuntu-virt/ppa
378
cp /etc/resolv.conf "${rootfs}/etc"
379
chroot $rootfs apt-get update
380
chroot $rootfs apt-get install --force-yes -y lxcguest
389
# copy /etc/passwd, /etc/shadow, and /etc/group entries into container
390
pwd=`getent passwd $user`
391
if [ $? -ne 0 ]; then
392
echo 'Warning: failed to copy password entry for $user'
395
echo $pwd >> $rootfs/etc/passwd
397
shad=`getent shadow $user`
398
echo $shad >> $rootfs/etc/shadow
400
# bind-mount the user's path into the container's /home
401
h=`getent passwd $user | cut -d: -f 6`
403
echo "$h $rootfs/$h none bind 0 0" >> $path/fstab
409
cache="/var/cache/lxc/$release"
411
if [ ! -e $cache ]; then
415
# lock, so we won't purge while someone is creating a repository
419
echo "Cache repository is busy."
423
echo -n "Purging the download cache..."
424
rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
427
) 200>/var/lock/subsys/lxc
433
$1 -h|--help -p|--path=<path> --clean [-a|--arch] [-b|--bindhome <user>] [--trim] [-r|--release]
434
release: lucid | maverick | natty | oneiric | precise
435
trim: make a minimal (faster, but not upgrade-safe) container
436
bindhome: bind <user>'s home into the container
437
arch: amd64 or i386: defaults to host arch
442
options=$(getopt -o a:b:hp:r:xn:c -l arch:,bindhome:,help,path:,release:,trim,name:,clean -- "$@")
443
if [ $? -ne 0 ]; then
447
eval set -- "$options"
450
if [ -f /etc/lsb-release ]; then
452
case "$DISTRIB_CODENAME" in
453
lucid|maverick|natty|oneiric|precise)
454
release=$DISTRIB_CODENAME
462
# Code taken from debootstrap
463
if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
464
arch=`/usr/bin/dpkg --print-architecture`
465
elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
466
arch=`/usr/bin/udpkg --print-architecture`
469
if [ "$arch" = "i686" ]; then
471
elif [ "$arch" = "x86_64" ]; then
473
elif [ "$arch" = "armv7l" ]; then
483
-h|--help) usage $0 && exit 0;;
484
-p|--path) path=$2; shift 2;;
485
-n|--name) name=$2; shift 2;;
486
-c|--clean) clean=$2; shift 2;;
487
-r|--release) release=$2; shift 2;;
488
-b|--bindhome) bindhome=$2; shift 2;;
489
-a|--arch) arch=$2; shift 2;;
490
-x|--trim) trim_container=1; shift 1;;
491
--) shift 1; break ;;
496
pwd=`getent passwd $bindhome`
497
if [ $? -ne 0 ]; then
498
echo "Error: no password entry found for $bindhome"
503
if [ "$arch" == "i686" ]; then
507
if [ ! -z "$clean" -a -z "$path" ]; then
512
if [ $hostarch = "i386" -a $arch = "amd64" ]; then
513
echo "can't create amd64 container on i386"
518
if [ $? -ne 0 ]; then
519
echo "'debootstrap' command is missing"
523
if [ -z "$path" ]; then
524
echo "'path' parameter is required"
528
if [ "$(id -u)" != "0" ]; then
529
echo "This script should be run as 'root'"
535
install_ubuntu $rootfs $release
536
if [ $? -ne 0 ]; then
537
echo "failed to install ubuntu $release"
541
configure_ubuntu $rootfs $name
542
if [ $? -ne 0 ]; then
543
echo "failed to configure ubuntu $release for a container"
547
copy_configuration $path $rootfs $name $arch
548
if [ $? -ne 0 ]; then
549
echo "failed write configuration file"
553
post_process $rootfs $release $trim_container
554
if [ ! -z $bindhome ]; then
555
do_bindhome $rootfs $bindhome
558
if [ ! -z $clean ]; then
559
clean $release || exit 1