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
41
# This file describes the network interfaces available on your system
42
# and how to activate them. For more information, see interfaces(5).
44
# The loopback network interface
46
iface lo inet loopback
53
cat <<EOF > $rootfs/etc/hostname
57
cat <<EOF > $rootfs/etc/hosts
61
# The following lines are desirable for IPv6 capable hosts
62
::1 ip6-localhost ip6-loopback
64
ff00::0 ip6-mcastprefix
66
ff02::2 ip6-allrouters
69
if [ ! -f $rootfs/etc/init/container-detect.conf ]; then
70
# suppress log level output for udev
71
sed -i "s/=\"err\"/=0/" $rootfs/etc/udev/udev.conf
73
# remove jobs for consoles 5 and 6 since we only create 4 consoles in
75
rm -f $rootfs/etc/init/tty{5,6}.conf
78
if [ -z "$bindhome" ]; then
79
chroot $rootfs useradd --create-home -s /bin/bash ubuntu
80
echo "ubuntu:ubuntu" | chroot $rootfs chpasswd
86
# finish setting up the user in the container by injecting ssh key and
87
# adding sudo group membership.
88
# passed-in user is either 'ubuntu' or the user to bind in from host.
93
sudo_version=$(chroot $rootfs dpkg-query -W -f='${Version}' sudo)
95
if chroot $rootfs dpkg --compare-versions $sudo_version gt "1.8.3p1-1"; then
101
for group in $groups; do
102
chroot $rootfs groupadd --system $group >/dev/null 2>&1 || true
103
chroot $rootfs adduser ${user} $group >/dev/null 2>&1 || true
106
if [ -n "$auth_key" -a -f "$auth_key" ]; then
107
u_path="/home/${user}/.ssh"
108
root_u_path="$rootfs/$u_path"
110
mkdir -p $root_u_path
111
cp $auth_key "$root_u_path/authorized_keys"
112
chroot $rootfs chown -R ${user}: "$u_path"
114
echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
121
# $1 => path to the rootfs
122
# $2 => architecture we want to add
123
# $3 => whether to use the multi-arch syntax or not
127
MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
128
SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
131
MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
132
SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
136
cat >> "$1/etc/apt/sources.list" << EOF
137
deb [arch=$2] $MIRROR ${release} main restricted universe multiverse
138
deb [arch=$2] $MIRROR ${release}-updates main restricted universe multiverse
139
deb [arch=$2] $SECURITY_MIRROR ${release}-security main restricted universe multiverse
142
cat >> "$1/etc/apt/sources.list" << EOF
143
deb $MIRROR ${release} main restricted universe multiverse
144
deb $MIRROR ${release}-updates main restricted universe multiverse
145
deb $SECURITY_MIRROR ${release}-security main restricted universe multiverse
152
rm -rf $cache/partial-$arch
153
rm -rf $cache/rootfs-$arch
158
echo "Container upgrade failed. The container cache may be out of date,"
159
echo "in which case flushing the case (see -F in the hep output) may help."
169
echo "installing packages: $packages"
171
trap cleanup EXIT SIGHUP SIGINT SIGTERM
172
# check the mini ubuntu was not already downloaded
173
mkdir -p "$cache/partial-$arch"
174
if [ $? -ne 0 ]; then
175
echo "Failed to create '$cache/partial-$arch' directory"
179
# download a mini ubuntu into a cache
180
echo "Downloading ubuntu $release minimal ..."
181
if [ -n "$(which qemu-debootstrap)" ]; then
182
qemu-debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
184
debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
187
if [ $? -ne 0 ]; then
188
echo "Failed to download the rootfs, aborting."
192
# Serge isn't sure whether we should avoid doing this when
193
# $release == `distro-info -d`
194
echo "Installing updates"
195
> $cache/partial-$arch/etc/apt/sources.list
196
write_sourceslist $cache/partial-$arch/ $arch
198
chroot "$1/partial-${arch}" apt-get update
199
if [ $? -ne 0 ]; then
200
echo "Failed to update the apt cache"
203
cat > "$1/partial-${arch}"/usr/sbin/policy-rc.d << EOF
207
chmod +x "$1/partial-${arch}"/usr/sbin/policy-rc.d
209
lxc-unshare -s MOUNT -- chroot "$1/partial-${arch}" apt-get dist-upgrade -y || { suggest_flush; false; }
210
rm -f "$1/partial-${arch}"/usr/sbin/policy-rc.d
212
chroot "$1/partial-${arch}" apt-get clean
214
mv "$1/partial-$arch" "$1/rootfs-$arch"
219
echo "Download complete"
229
# make a local copy of the miniubuntu
230
echo "Copying rootfs to $rootfs ..."
232
rsync -a $cache/rootfs-$arch/ $rootfs/ || return 1
241
cache="/var/cache/lxc/$release"
242
mkdir -p /var/lock/subsys/
246
if [ $? -ne 0 ]; then
247
echo "Cache repository is busy."
252
if [ $flushcache -eq 1 ]; then
253
echo "Flushing cache..."
254
rm -rf "$cache/partial-$arch"
255
rm -rf "$cache/rootfs-$arch"
258
echo "Checking cache download in $cache/rootfs-$arch ... "
259
if [ ! -e "$cache/rootfs-$arch" ]; then
260
download_ubuntu $cache $arch $release
261
if [ $? -ne 0 ]; then
262
echo "Failed to download 'ubuntu $release base'"
267
echo "Copy $cache/rootfs-$arch to $rootfs ... "
268
copy_ubuntu $cache $arch $rootfs
269
if [ $? -ne 0 ]; then
270
echo "Failed to copy rootfs"
276
) 200>/var/lock/subsys/lxc
289
if [ $arch = "i386" ]; then
294
if [ -f $rootfs/etc/init/container-detect.conf ]; then
298
# if there is exactly one veth network entry, make sure it has an
300
nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
301
if [ $nics -eq 1 ]; then
302
grep -q "^lxc.network.hwaddr" $path/config || cat <<EOF >> $path/config
303
lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')
307
grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
308
cat <<EOF >> $path/config
311
lxc.devttydir =$ttydir
314
lxc.mount = $path/fstab
316
lxc.cap.drop = sys_module mac_admin mac_override
317
lxc.pivotdir = lxc_putold
319
# uncomment the next line to run the container unconfined:
320
#lxc.aa_profile = unconfined
322
lxc.cgroup.devices.deny = a
323
# Allow any mknod (but not using the node)
324
lxc.cgroup.devices.allow = c *:* m
325
lxc.cgroup.devices.allow = b *:* m
327
lxc.cgroup.devices.allow = c 1:3 rwm
328
lxc.cgroup.devices.allow = c 1:5 rwm
330
lxc.cgroup.devices.allow = c 5:1 rwm
331
lxc.cgroup.devices.allow = c 5:0 rwm
332
#lxc.cgroup.devices.allow = c 4:0 rwm
333
#lxc.cgroup.devices.allow = c 4:1 rwm
335
lxc.cgroup.devices.allow = c 1:9 rwm
336
lxc.cgroup.devices.allow = c 1:8 rwm
337
lxc.cgroup.devices.allow = c 136:* rwm
338
lxc.cgroup.devices.allow = c 5:2 rwm
340
lxc.cgroup.devices.allow = c 254:0 rwm
342
lxc.cgroup.devices.allow = c 10:229 rwm
344
lxc.cgroup.devices.allow = c 10:200 rwm
346
lxc.cgroup.devices.allow = c 1:7 rwm
348
lxc.cgroup.devices.allow = c 10:228 rwm
350
lxc.cgroup.devices.allow = c 10:232 rwm
353
cat <<EOF > $path/fstab
354
proc proc proc nodev,noexec,nosuid 0 0
355
sysfs sys sysfs defaults 0 0
358
if [ $? -ne 0 ]; then
359
echo "Failed to add configuration"
371
# provide the lxc service
372
cat <<EOF > $rootfs/etc/init/lxc.conf
373
# fake some events needed for correct startup other services
375
description "Container Upstart"
380
rm -rf /var/run/*.pid
381
rm -rf /var/run/network/*
382
/sbin/initctl emit stopped JOB=udevtrigger --no-wait
383
/sbin/initctl emit started JOB=udev --no-wait
387
# fix buggus runlevel with sshd
388
cat <<EOF > $rootfs/etc/init/ssh.conf
389
# ssh - OpenBSD Secure Shell server
391
# The OpenSSH server provides secure shell access to the system.
393
description "OpenSSH server"
396
stop on runlevel [!2345]
402
# replaces SSHD_OOM_ADJUST in /etc/default/ssh
406
test -x /usr/sbin/sshd || { stop; exit 0; }
407
test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; }
408
test -c /dev/null || { stop; exit 0; }
410
mkdir -p -m0755 /var/run/sshd
413
# if you used to set SSHD_OPTS in /etc/default/ssh, you can change the
414
# 'exec' line here instead
418
cat <<EOF > $rootfs/etc/init/console.conf
421
# This service maintains a console on tty1 from the point the system is
422
# started until it is shut down again.
424
start on stopped rc RUNLEVEL=[2345]
425
stop on runlevel [!2345]
428
exec /sbin/getty -8 38400 /dev/console
431
cat <<EOF > $rootfs/lib/init/fstab
432
# /lib/init/fstab: cleared out for bare-bones lxc
435
# reconfigure some services
436
if [ -z "$LANG" ]; then
437
chroot $rootfs locale-gen en_US.UTF-8
438
chroot $rootfs update-locale LANG=en_US.UTF-8
440
chroot $rootfs locale-gen $LANG
441
chroot $rootfs update-locale LANG=$LANG
444
# remove pointless services in a container
445
chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
447
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
448
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
449
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
450
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
451
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
453
# if this isn't lucid, then we need to twiddle the network upstart bits :(
454
if [ $release != "lucid" ]; then
455
sed -i 's/^.*emission handled.*$/echo Emitting lo/' $rootfs/etc/network/if-up.d/upstart
465
if [ $trim_container -eq 1 ]; then
466
trim $rootfs $release
467
elif [ ! -f $rootfs/etc/init/container-detect.conf ]; then
468
# Make sure we have a working resolv.conf
469
cresolvonf="${rootfs}/etc/resolv.conf"
470
mv $cresolvonf ${cresolvonf}.lxcbak
471
cat /etc/resolv.conf > ${cresolvonf}
473
# for lucid, if not trimming, then add the ubuntu-virt
474
# ppa and install lxcguest
475
if [ $release = "lucid" ]; then
476
chroot $rootfs apt-get update
477
chroot $rootfs apt-get install --force-yes -y python-software-properties
478
chroot $rootfs add-apt-repository ppa:ubuntu-virt/ppa
481
chroot $rootfs apt-get update
482
chroot $rootfs apt-get install --force-yes -y lxcguest
484
# Restore old resolv.conf
486
mv ${cresolvonf}.lxcbak ${cresolvonf}
489
# If the container isn't running a native architecture, setup multiarch
490
if [ -x "$(ls -1 ${rootfs}/usr/bin/qemu-*-static 2>/dev/null)" ]; then
491
dpkg_version=$(chroot $rootfs dpkg-query -W -f='${Version}' dpkg)
492
if chroot $rootfs dpkg --compare-versions $dpkg_version ge "1.16.2"; then
493
chroot $rootfs dpkg --add-architecture ${hostarch}
495
mkdir -p ${rootfs}/etc/dpkg/dpkg.cfg.d
496
echo "foreign-architecture ${hostarch}" > ${rootfs}/etc/dpkg/dpkg.cfg.d/lxc-multiarch
499
# Save existing value of MIRROR and SECURITY_MIRROR
500
DEFAULT_MIRROR=$MIRROR
501
DEFAULT_SECURITY_MIRROR=$SECURITY_MIRROR
503
# Write a new sources.list containing both native and multiarch entries
504
> ${rootfs}/etc/apt/sources.list
505
write_sourceslist $rootfs $arch "native"
507
MIRROR=$DEFAULT_MIRROR
508
SECURITY_MIRROR=$DEFAULT_SECURITY_MIRROR
509
write_sourceslist $rootfs $hostarch "multiarch"
511
# Finally update the lists and install upstart using the host architecture
512
chroot $rootfs apt-get update
513
chroot $rootfs apt-get install --force-yes -y --no-install-recommends upstart:${hostarch} mountall:${hostarch} iproute:${hostarch} isc-dhcp-client:${hostarch}
516
# rmdir /dev/shm for containers that have /run/shm
517
# I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
518
# get bind mounted to the host's /run/shm. So try to rmdir
519
# it, and in case that fails move it out of the way.
520
if [ ! -L $rootfs/dev/shm ] && [ -d $rootfs/run/shm ] && [ -e $rootfs/dev/shm ]; then
521
mv $rootfs/dev/shm $rootfs/dev/shm.bak
522
ln -s /run/shm $rootfs/dev/shm
531
# copy /etc/passwd, /etc/shadow, and /etc/group entries into container
532
pwd=`getent passwd $user` || { echo "Failed to copy password entry for $user"; false; }
533
echo $pwd >> $rootfs/etc/passwd
535
# make sure user's shell exists in the container
536
shell=`echo $pwd | cut -d: -f 7`
537
if [ ! -x $rootfs/$shell ]; then
538
echo "shell $shell for user $user was not found in the container."
539
pkg=`dpkg -S $(readlink -m $shell) | cut -d ':' -f1`
540
echo "Installing $pkg"
541
chroot $rootfs apt-get --force-yes -y install $pkg
544
shad=`getent shadow $user`
545
echo "$shad" >> $rootfs/etc/shadow
547
# bind-mount the user's path into the container's /home
548
h=`getent passwd $user | cut -d: -f 6`
551
# use relative path in container
553
while [ ${h2:0:1} = "/" ]; do
556
echo "$h $h2 none bind 0 0" >> $path/fstab
558
# Make sure the group exists in container
559
grp=`echo $pwd | cut -d: -f 4` # group number for $user
560
grpe=`getent group $grp` || return 0 # if host doesn't define grp, ignore in container
561
chroot $rootfs getent group "$grpe" || echo "$grpe" >> $rootfs/etc/group
567
$1 -h|--help [-a|--arch] [-b|--bindhome <user>] [--trim] [-d|--debug]
568
[-F | --flush-cache] [-r|--release <release>] [ -S | --auth-key <keyfile>]
569
release: the ubuntu release (e.g. precise): defaults to host release on ubuntu, otherwise uses latest LTS
570
trim: make a minimal (faster, but not upgrade-safe) container
571
bindhome: bind <user>'s home into the container
572
The ubuntu user will not be created, and <user> will have
574
arch: the container architecture (e.g. amd64): defaults to host arch
575
auth-key: SSH Public key file to inject into container
580
options=$(getopt -o a:b:hp:r:xn:FS:d -l arch:,bindhome:,help,path:,release:,trim,name:,flush-cache,auth-key:,debug -- "$@")
581
if [ $? -ne 0 ]; then
585
eval set -- "$options"
587
release=precise # Default to the last Ubuntu LTS release for non-Ubuntu systems
588
if [ -f /etc/lsb-release ]; then
590
if [ "$DISTRIB_ID" = "Ubuntu" ]; then
591
release=$DISTRIB_CODENAME
598
# Code taken from debootstrap
599
if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
600
arch=`/usr/bin/dpkg --print-architecture`
601
elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
602
arch=`/usr/bin/udpkg --print-architecture`
605
if [ "$arch" = "i686" ]; then
607
elif [ "$arch" = "x86_64" ]; then
609
elif [ "$arch" = "armv7l" ]; then
621
-h|--help) usage $0 && exit 0;;
622
-p|--path) path=$2; shift 2;;
623
-n|--name) name=$2; shift 2;;
624
-F|--flush-cache) flushcache=1; shift 1;;
625
-r|--release) release=$2; shift 2;;
626
-b|--bindhome) bindhome=$2; shift 2;;
627
-a|--arch) arch=$2; shift 2;;
628
-x|--trim) trim_container=1; shift 1;;
629
-S|--auth-key) auth_key=$2; shift 2;;
630
-d|--debug) debug=1; shift 1;;
631
--) shift 1; break ;;
636
if [ $debug -eq 1 ]; then
640
if [ -n "$bindhome" ]; then
641
pwd=`getent passwd $bindhome`
642
if [ $? -ne 0 ]; then
643
echo "Error: no password entry found for $bindhome"
649
if [ "$arch" == "i686" ]; then
653
if [ $hostarch = "i386" -a $arch = "amd64" ]; then
654
echo "can't create amd64 container on i386"
659
if [ $? -ne 0 ]; then
660
echo "'debootstrap' command is missing"
664
if [ -z "$path" ]; then
665
echo "'path' parameter is required"
669
if [ "$(id -u)" != "0" ]; then
670
echo "This script should be run as 'root'"
675
config="$path/config"
676
if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
677
rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
682
install_ubuntu $rootfs $release $flushcache
683
if [ $? -ne 0 ]; then
684
echo "failed to install ubuntu $release"
688
configure_ubuntu $rootfs $name $release
689
if [ $? -ne 0 ]; then
690
echo "failed to configure ubuntu $release for a container"
694
copy_configuration $path $rootfs $name $arch $release
695
if [ $? -ne 0 ]; then
696
echo "failed write configuration file"
700
post_process $rootfs $release $trim_container
702
if [ -n "$bindhome" ]; then
703
do_bindhome $rootfs $bindhome
704
finalize_user $bindhome
711
echo "# The default user is 'ubuntu' with password 'ubuntu'!"
712
echo "# Use the 'sudo' command to run tasks as root in the container."