4
# template script for generating ubuntu container for LXC
6
# This script consolidates and extends the existing lxc ubuntu scripts
9
# Copyright � 2011 Serge Hallyn <serge.hallyn@canonical.com>
10
# Copyright � 2010 Wilhelm Meier
11
# Author: Wilhelm Meier <wilhelm.meier@fh-kl.de>
13
# This program is free software; you can redistribute it and/or modify
14
# it under the terms of the GNU General Public License version 2, as
15
# published by the Free Software Foundation.
17
# This program is distributed in the hope that it will be useful,
18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
# GNU General Public License for more details.
22
# You should have received a copy of the GNU General Public License along
23
# with this program; if not, write to the Free Software Foundation, Inc.,
24
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29
if [ -r /etc/default/lxc ]; then
39
# configure the network using the dhcp
40
cat <<EOF > $rootfs/etc/network/interfaces
42
iface lo inet loopback
49
cat <<EOF > $rootfs/etc/hostname
53
cat <<EOF > $rootfs/etc/hosts
54
127.0.0.1 localhost $hostname
57
if [ "$release" != "precise" ]; then
58
# suppress log level output for udev
59
sed -i "s/=\"err\"/=0/" $rootfs/etc/udev/udev.conf
61
# remove jobs for consoles 5 and 6 since we only create 4 consoles in
63
rm -f $rootfs/etc/init/tty{5,6}.conf
66
if [ -z "$bindhome" ]; then
67
chroot $rootfs useradd --create-home -s /bin/bash ubuntu
68
echo "ubuntu:ubuntu" | chroot $rootfs chpasswd
74
# finish setting up the user in the container by injecting ssh key and
75
# adding sudo group membership.
76
# passed-in user is either 'ubuntu' or the user to bind in from host.
81
if [ "$release" = "precise" ]; then
87
for group in $groups; do
88
chroot $rootfs groupadd --system $group >/dev/null 2>&1 || true
89
chroot $rootfs adduser ${user} $group >/dev/null 2>&1 || true
92
if [ -n "$auth_key" -a -f "$auth_key" ]; then
93
u_path="/home/${user}/.ssh"
94
root_u_path="$rootfs/$u_path"
96
cp $auth_key "$root_u_path/authorized_keys"
97
chroot $rootfs chown -R ${user}: "$u_path"
99
echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
106
# $1 => path to the rootfs
107
# $2 => architecture we want to add
108
# $3 => whether to use the multi-arch syntax or not
112
MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
113
SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
118
MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
119
SECURITY_MIRROR=${SECURITY_MIRRORMIRROR:-http://security.ubuntu.com/ubuntu}
122
MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
123
SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
128
MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
129
SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
133
cat >> "$1/etc/apt/sources.list" << EOF
134
deb [arch=$2] $MIRROR ${release} main restricted universe multiverse
135
deb [arch=$2] $MIRROR ${release}-updates main restricted universe multiverse
136
deb [arch=$2] $SECURITY_MIRROR ${release}-security main restricted universe multiverse
139
cat >> "$1/etc/apt/sources.list" << EOF
140
deb $MIRROR ${release} main restricted universe multiverse
141
deb $MIRROR ${release}-updates main restricted universe multiverse
142
deb $SECURITY_MIRROR ${release}-security main restricted universe multiverse
153
if [ $release = "lucid" ]; then
154
packages=dialog,apt,apt-utils,resolvconf,iproute,inetutils-ping,vim,dhcp3-client,ssh,lsb-release,gnupg
155
elif [ $release = "maverick" ]; then
156
packages=dialog,apt,apt-utils,resolvconf,iproute,inetutils-ping,vim,dhcp3-client,ssh,lsb-release,gnupg,netbase
157
elif [ $release = "natty" ]; then
158
packages=dialog,apt,apt-utils,resolvconf,iproute,inetutils-ping,vim,isc-dhcp-client,isc-dhcp-common,ssh,lsb-release,gnupg,netbase
160
packages=dialog,apt,apt-utils,iproute,inetutils-ping,vim,isc-dhcp-client,isc-dhcp-common,ssh,lsb-release,gnupg,netbase,ubuntu-keyring
162
echo "installing packages: $packages"
164
# check the mini ubuntu was not already downloaded
165
mkdir -p "$cache/partial-$arch"
166
if [ $? -ne 0 ]; then
167
echo "Failed to create '$cache/partial-$arch' directory"
171
# download a mini ubuntu into a cache
172
echo "Downloading ubuntu $release minimal ..."
173
if [ -n "$(which qemu-debootstrap)" ]; then
174
qemu-debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
176
debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
179
if [ $? -ne 0 ]; then
180
echo "Failed to download the rootfs, aborting."
184
# Serge isn't sure whether we should avoid doing this when
185
# $release == `distro-info -d`
186
echo "Installing updates"
187
> $cache/partial-$arch/etc/apt/sources.list
188
write_sourceslist $cache/partial-$arch/ $arch
190
chroot "$1/partial-${arch}" apt-get update
191
if [ $? -ne 0 ]; then
192
echo "Failed to update the apt cache"
195
cat > "$1/partial-${arch}"/usr/sbin/policy-rc.d << EOF
199
chmod +x "$1/partial-${arch}"/usr/sbin/policy-rc.d
201
lxc-unshare -s MOUNT -- chroot "$1/partial-${arch}" apt-get dist-upgrade -y
203
rm -f "$1/partial-${arch}"/usr/sbin/policy-rc.d
205
if [ $ret -ne 0 ]; then
206
echo "Failed to upgrade the cache"
210
mv "$1/partial-$arch" "$1/rootfs-$arch"
211
echo "Download complete"
221
# make a local copy of the miniubuntu
222
echo "Copying rootfs to $rootfs ..."
224
rsync -a $cache/rootfs-$arch/ $rootfs/ || return 1
233
cache="/var/cache/lxc/$release"
234
mkdir -p /var/lock/subsys/
237
if [ $? -ne 0 ]; then
238
echo "Cache repository is busy."
243
if [ $flushcache -eq 1 ]; then
244
echo "Flushing cache..."
245
rm -rf "$cache/partial-$arch"
246
rm -rf "$cache/rootfs-$arch"
249
echo "Checking cache download in $cache/rootfs-$arch ... "
250
if [ ! -e "$cache/rootfs-$arch" ]; then
251
download_ubuntu $cache $arch $release
252
if [ $? -ne 0 ]; then
253
echo "Failed to download 'ubuntu $release base'"
258
echo "Copy $cache/rootfs-$arch to $rootfs ... "
259
copy_ubuntu $cache $arch $rootfs
260
if [ $? -ne 0 ]; then
261
echo "Failed to copy rootfs"
267
) 200>/var/lock/subsys/lxc
280
if [ $arch = "i386" ]; then
285
if [ $release = "precise" ]; then
289
# if there is exactly one veth network entry, make sure it has an
291
nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
292
if [ $nics -eq 1 ]; then
293
grep -q "^lxc.network.hwaddr" $path/config || cat <<EOF >> $path/config
294
lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')
298
cat <<EOF >> $path/config
301
lxc.devttydir =$ttydir
305
lxc.mount = $path/fstab
307
lxc.cap.drop = sys_module mac_admin
309
lxc.cgroup.devices.deny = a
310
# Allow any mknod (but not using the node)
311
lxc.cgroup.devices.allow = c *:* m
312
lxc.cgroup.devices.allow = b *:* m
314
lxc.cgroup.devices.allow = c 1:3 rwm
315
lxc.cgroup.devices.allow = c 1:5 rwm
317
lxc.cgroup.devices.allow = c 5:1 rwm
318
lxc.cgroup.devices.allow = c 5:0 rwm
319
#lxc.cgroup.devices.allow = c 4:0 rwm
320
#lxc.cgroup.devices.allow = c 4:1 rwm
322
lxc.cgroup.devices.allow = c 1:9 rwm
323
lxc.cgroup.devices.allow = c 1:8 rwm
324
lxc.cgroup.devices.allow = c 136:* rwm
325
lxc.cgroup.devices.allow = c 5:2 rwm
327
lxc.cgroup.devices.allow = c 254:0 rwm
329
lxc.cgroup.devices.allow = c 10:229 rwm
331
lxc.cgroup.devices.allow = c 10:200 rwm
333
lxc.cgroup.devices.allow = c 1:7 rwm
335
lxc.cgroup.devices.allow = c 10:228 rwm
337
lxc.cgroup.devices.allow = c 10:232 rwm
340
cat <<EOF > $path/fstab
341
proc $rootfs/proc proc nodev,noexec,nosuid 0 0
342
sysfs $rootfs/sys sysfs defaults 0 0
345
if [ $? -ne 0 ]; then
346
echo "Failed to add configuration"
358
# provide the lxc service
359
cat <<EOF > $rootfs/etc/init/lxc.conf
360
# fake some events needed for correct startup other services
362
description "Container Upstart"
367
rm -rf /var/run/*.pid
368
rm -rf /var/run/network/*
369
/sbin/initctl emit stopped JOB=udevtrigger --no-wait
370
/sbin/initctl emit started JOB=udev --no-wait
374
# fix buggus runlevel with sshd
375
cat <<EOF > $rootfs/etc/init/ssh.conf
376
# ssh - OpenBSD Secure Shell server
378
# The OpenSSH server provides secure shell access to the system.
380
description "OpenSSH server"
383
stop on runlevel [!2345]
389
# replaces SSHD_OOM_ADJUST in /etc/default/ssh
393
test -x /usr/sbin/sshd || { stop; exit 0; }
394
test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; }
395
test -c /dev/null || { stop; exit 0; }
397
mkdir -p -m0755 /var/run/sshd
400
# if you used to set SSHD_OPTS in /etc/default/ssh, you can change the
401
# 'exec' line here instead
405
cat <<EOF > $rootfs/etc/init/console.conf
408
# This service maintains a console on tty1 from the point the system is
409
# started until it is shut down again.
411
start on stopped rc RUNLEVEL=[2345]
412
stop on runlevel [!2345]
415
exec /sbin/getty -8 38400 /dev/console
418
cat <<EOF > $rootfs/lib/init/fstab
419
# /lib/init/fstab: cleared out for bare-bones lxc
422
# reconfigure some services
423
if [ -z "$LANG" ]; then
424
chroot $rootfs locale-gen en_US.UTF-8
425
chroot $rootfs update-locale LANG=en_US.UTF-8
427
chroot $rootfs locale-gen $LANG
428
chroot $rootfs update-locale LANG=$LANG
431
# remove pointless services in a container
432
chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
434
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
435
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
436
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
437
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
438
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
440
# if this isn't lucid, then we need to twiddle the network upstart bits :(
441
if [ $release != "lucid" ]; then
442
sed -i 's/^.*emission handled.*$/echo Emitting lo/' $rootfs/etc/network/if-up.d/upstart
452
if [ $trim_container -eq 1 ]; then
453
trim $rootfs $release
454
elif [ $release = "lucid" -o $release = "maverick" -o $release = "natty" \
455
-o $release = "oneiric" ]; then
456
# for lucid and maverick, if not trimming, then add the ubuntu-virt
457
# ppa and install lxcguest
458
if [ $release = "lucid" -o $release = "maverick" ]; then
459
chroot $rootfs apt-get install --force-yes -y python-software-properties
460
chroot $rootfs add-apt-repository ppa:ubuntu-virt/ppa
462
cresolvonf="${rootfs}/etc/resolv.conf"
463
mv $cresolvonf ${cresolvonf}.lxcbak
464
cat /etc/resolv.conf > ${cresolvonf}
465
chroot $rootfs apt-get update
466
chroot $rootfs apt-get install --force-yes -y lxcguest
468
mv ${cresolvonf}.lxcbak ${cresolvonf}
471
# If the container isn't running a native architecture, setup multiarch
472
if [ -x "$(ls -1 ${rootfs}/usr/bin/qemu-*-static 2>/dev/null)" ]; then
473
mkdir -p ${rootfs}/etc/dpkg/dpkg.cfg.d
474
echo "foreign-architecture ${hostarch}" > ${rootfs}/etc/dpkg/dpkg.cfg.d/lxc-multiarch
476
# Save existing value of MIRROR and SECURITY_MIRROR
477
DEFAULT_MIRROR=$MIRROR
478
DEFAULT_SECURITY_MIRROR=$SECURITY_MIRROR
480
# Write a new sources.list containing both native and multiarch entries
481
> ${rootfs}/etc/apt/sources.list
482
write_sourceslist $rootfs $arch "native"
484
MIRROR=$DEFAULT_MIRROR
485
SECURITY_MIRROR=$DEFAULT_SECURITY_MIRROR
486
write_sourceslist $rootfs $hostarch "multiarch"
488
# Finally update the lists and install upstart using the host architecture
489
chroot $rootfs apt-get update
490
chroot $rootfs apt-get install --force-yes -y --no-install-recommends upstart:${hostarch} mountall:amd64 iproute:amd64 isc-dhcp-client:amd64
499
# copy /etc/passwd, /etc/shadow, and /etc/group entries into container
500
pwd=`getent passwd $user` || { echo "Failed to copy password entry for $user"; false; }
501
echo $pwd >> $rootfs/etc/passwd
503
# make sure user's shell exists in the container
504
shell=`echo $pwd | cut -d: -f 7`
505
if [ ! -x $rootfs/$shell ]; then
506
echo "shell $shell for user $user was not found in the container."
507
pkg=`dpkg -S $(readlink -m $shell) | cut -d ':' -f1`
508
echo "Installing $pkg"
509
chroot $rootfs apt-get --force-yes -y install $pkg
512
shad=`getent shadow $user`
513
echo "$shad" >> $rootfs/etc/shadow
515
# bind-mount the user's path into the container's /home
516
h=`getent passwd $user | cut -d: -f 6`
518
echo "$h $rootfs/$h none bind 0 0" >> $path/fstab
520
# Make sure the group exists in container
521
grp=`echo $pwd | cut -d: -f 4` # group number for $user
522
grpe=`getent group $grp` || return 0 # if host doesn't define grp, ignore in container
523
chroot $rootfs getent group "$grpe" || echo "$grpe" >> $rootfs/etc/group
529
$1 -h|--help [-a|--arch] [-b|--bindhome <user>] [--trim] [-d|--debug]
530
[-F | --flush-cache] [-r|--release <release>] [ -S | --auth-key <keyfile>]
531
release: lucid | maverick | natty | oneiric | precise
532
trim: make a minimal (faster, but not upgrade-safe) container
533
bindhome: bind <user>'s home into the container
534
The ubuntu user will not be created, and <user> will have
536
arch: amd64 or i386: defaults to host arch
537
auth-key: SSH Public key file to inject into container
542
options=$(getopt -o a:b:hp:r:xn:FS:d -l arch:,bindhome:,help,path:,release:,trim,name:,flush-cache,auth-key:,debug -- "$@")
543
if [ $? -ne 0 ]; then
547
eval set -- "$options"
550
if [ -f /etc/lsb-release ]; then
552
case "$DISTRIB_CODENAME" in
553
lucid|maverick|natty|oneiric|precise)
554
release=$DISTRIB_CODENAME
562
# Code taken from debootstrap
563
if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
564
arch=`/usr/bin/dpkg --print-architecture`
565
elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
566
arch=`/usr/bin/udpkg --print-architecture`
569
if [ "$arch" = "i686" ]; then
571
elif [ "$arch" = "x86_64" ]; then
573
elif [ "$arch" = "armv7l" ]; then
585
-h|--help) usage $0 && exit 0;;
586
-p|--path) path=$2; shift 2;;
587
-n|--name) name=$2; shift 2;;
588
-F|--flush-cache) flushcache=1; shift 1;;
589
-r|--release) release=$2; shift 2;;
590
-b|--bindhome) bindhome=$2; shift 2;;
591
-a|--arch) arch=$2; shift 2;;
592
-x|--trim) trim_container=1; shift 1;;
593
-S|--auth-key) auth_key=$2; shift 2;;
594
-d|--debug) debug=1; shift 1;;
595
--) shift 1; break ;;
600
if [ $debug -eq 1 ]; then
604
if [ -n "$bindhome" ]; then
605
pwd=`getent passwd $bindhome`
606
if [ $? -ne 0 ]; then
607
echo "Error: no password entry found for $bindhome"
613
if [ "$arch" == "i686" ]; then
617
if [ $hostarch = "i386" -a $arch = "amd64" ]; then
618
echo "can't create amd64 container on i386"
623
if [ $? -ne 0 ]; then
624
echo "'debootstrap' command is missing"
628
if [ -z "$path" ]; then
629
echo "'path' parameter is required"
633
if [ "$(id -u)" != "0" ]; then
634
echo "This script should be run as 'root'"
640
install_ubuntu $rootfs $release $flushcache
641
if [ $? -ne 0 ]; then
642
echo "failed to install ubuntu $release"
646
configure_ubuntu $rootfs $name $release
647
if [ $? -ne 0 ]; then
648
echo "failed to configure ubuntu $release for a container"
652
copy_configuration $path $rootfs $name $arch $release
653
if [ $? -ne 0 ]; then
654
echo "failed write configuration file"
658
post_process $rootfs $release $trim_container
660
if [ -n "$bindhome" ]; then
661
do_bindhome $rootfs $bindhome
662
finalize_user $bindhome
669
echo "# The default user is 'ubuntu' with password 'ubuntu'!"
670
echo "# Use the 'sudo' command to run tasks as root in the container."