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
83 # make sure we have the current locale defined in the container
84 if [ -z "$LANG" ]; then
85 chroot
$rootfs locale-gen en_US.UTF-8
86 chroot
$rootfs update-locale LANG
=en_US.UTF-8
88 chroot
$rootfs locale-gen
$LANG
89 chroot
$rootfs update-locale LANG
=$LANG
95 # finish setting up the user in the container by injecting ssh key and
96 # adding sudo group membership.
97 # passed-in user is either 'ubuntu' or the user to bind in from host.
102 sudo_version
=$
(chroot
$rootfs dpkg-query
-W -f='${Version}' sudo
)
104 if chroot
$rootfs dpkg
--compare-versions $sudo_version gt
"1.8.3p1-1"; then
110 for group
in $groups; do
111 chroot
$rootfs groupadd
--system $group >/dev
/null
2>&1 || true
112 chroot
$rootfs adduser
${user} $group >/dev
/null
2>&1 || true
115 if [ -n "$auth_key" -a -f "$auth_key" ]; then
116 u_path
="/home/${user}/.ssh"
117 root_u_path
="$rootfs/$u_path"
118 mkdir
-p $root_u_path
119 cp $auth_key "$root_u_path/authorized_keys"
120 chroot
$rootfs chown
-R ${user}: "$u_path"
122 echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
129 # $1 => path to the rootfs
130 # $2 => architecture we want to add
131 # $3 => whether to use the multi-arch syntax or not
135 MIRROR
=${MIRROR:-http://archive.ubuntu.com/ubuntu}
136 SECURITY_MIRROR
=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
139 MIRROR
=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
140 SECURITY_MIRROR
=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
144 cat >> "$1/etc/apt/sources.list" << EOF
145 deb [arch=$2] $MIRROR ${release} main restricted universe multiverse
146 deb [arch=$2] $MIRROR ${release}-updates main restricted universe multiverse
147 deb [arch=$2] $SECURITY_MIRROR ${release}-security main restricted universe multiverse
150 cat >> "$1/etc/apt/sources.list" << EOF
151 deb $MIRROR ${release} main restricted universe multiverse
152 deb $MIRROR ${release}-updates main restricted universe multiverse
153 deb $SECURITY_MIRROR ${release}-security main restricted universe multiverse
160 rm -rf $cache/partial-
$arch
161 rm -rf $cache/rootfs-
$arch
166 echo "Container upgrade failed. The container cache may be out of date,"
167 echo "in which case flushing the case (see -F in the hep output) may help."
178 # Try to guess a list of langpacks to install
179 langpacks
="language-pack-en"
181 if which dpkg
>/dev
/null
2>&1; then
182 langpacks
=`(echo $langpacks &&
183 dpkg -l | grep -E "^ii language-pack-[a-z]* " |
184 cut -d ' ' -f3) | sort -u`
186 packages
="$packages,$(echo $langpacks | sed 's/ /,/g')"
189 echo "installing packages: $packages"
191 trap cleanup EXIT SIGHUP SIGINT SIGTERM
192 # check the mini ubuntu was not already downloaded
193 mkdir
-p "$cache/partial-$arch"
194 if [ $?
-ne 0 ]; then
195 echo "Failed to create '$cache/partial-$arch' directory"
199 # download a mini ubuntu into a cache
200 echo "Downloading ubuntu $release minimal ..."
201 if [ -n "$(which qemu-debootstrap)" ]; then
202 qemu-debootstrap
--verbose --components=main
,universe
--arch=$arch --include=$packages $release $cache/partial-
$arch $MIRROR
204 debootstrap
--verbose --components=main
,universe
--arch=$arch --include=$packages $release $cache/partial-
$arch $MIRROR
207 if [ $?
-ne 0 ]; then
208 echo "Failed to download the rootfs, aborting."
212 # Serge isn't sure whether we should avoid doing this when
213 # $release == `distro-info -d`
214 echo "Installing updates"
215 > $cache/partial-
$arch/etc
/apt
/sources.list
216 write_sourceslist
$cache/partial-
$arch/ $arch
218 chroot
"$1/partial-${arch}" apt-get update
219 if [ $?
-ne 0 ]; then
220 echo "Failed to update the apt cache"
223 cat > "$1/partial-${arch}"/usr
/sbin
/policy-rc.d
<< EOF
227 chmod +x
"$1/partial-${arch}"/usr
/sbin
/policy-rc.d
229 lxc-unshare
-s MOUNT
-- chroot
"$1/partial-${arch}" apt-get dist-upgrade
-y ||
{ suggest_flush
; false
; }
230 rm -f "$1/partial-${arch}"/usr
/sbin
/policy-rc.d
232 chroot
"$1/partial-${arch}" apt-get clean
234 mv "$1/partial-$arch" "$1/rootfs-$arch"
239 echo "Download complete"
249 # make a local copy of the miniubuntu
250 echo "Copying rootfs to $rootfs ..."
252 rsync
-Ha $cache/rootfs-
$arch/ $rootfs/ ||
return 1
261 cache
="@LOCALSTATEDIR@/cache/lxc/$release"
262 mkdir
-p @LOCALSTATEDIR@
/lock
/subsys
/
266 if [ $?
-ne 0 ]; then
267 echo "Cache repository is busy."
272 if [ $flushcache -eq 1 ]; then
273 echo "Flushing cache..."
274 rm -rf "$cache/partial-$arch"
275 rm -rf "$cache/rootfs-$arch"
278 echo "Checking cache download in $cache/rootfs-$arch ... "
279 if [ ! -e "$cache/rootfs-$arch" ]; then
280 download_ubuntu
$cache $arch $release
281 if [ $?
-ne 0 ]; then
282 echo "Failed to download 'ubuntu $release base'"
287 echo "Copy $cache/rootfs-$arch to $rootfs ... "
288 copy_ubuntu
$cache $arch $rootfs
289 if [ $?
-ne 0 ]; then
290 echo "Failed to copy rootfs"
296 ) 200>@LOCALSTATEDIR@
/lock
/subsys
/lxc-ubuntu
309 if [ $arch = "i386" ]; then
314 if [ -f $rootfs/etc
/init
/container-detect.conf
]; then
318 # if there is exactly one veth network entry, make sure it has an
320 nics
=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
321 if [ $nics -eq 1 ]; then
322 grep -q "^lxc.network.hwaddr" $path/config ||
sed -i -e "/^lxc\.network\.type[ \t]*=[ \t]*veth/a lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')" $path/config
325 grep -q "^lxc.rootfs" $path/config
2>/dev
/null ||
echo "lxc.rootfs = $rootfs" >> $path/config
326 cat <<EOF >> $path/config
327 lxc.mount = $path/fstab
328 lxc.pivotdir = lxc_putold
330 lxc.devttydir =$ttydir
336 lxc.cap.drop = sys_module mac_admin mac_override
338 # When using LXC with apparmor, uncomment the next line to run unconfined:
339 #lxc.aa_profile = unconfined
341 lxc.cgroup.devices.deny = a
342 # Allow any mknod (but not using the node)
343 lxc.cgroup.devices.allow = c *:* m
344 lxc.cgroup.devices.allow = b *:* m
346 lxc.cgroup.devices.allow = c 1:3 rwm
347 lxc.cgroup.devices.allow = c 1:5 rwm
349 lxc.cgroup.devices.allow = c 5:1 rwm
350 lxc.cgroup.devices.allow = c 5:0 rwm
351 #lxc.cgroup.devices.allow = c 4:0 rwm
352 #lxc.cgroup.devices.allow = c 4:1 rwm
354 lxc.cgroup.devices.allow = c 1:9 rwm
355 lxc.cgroup.devices.allow = c 1:8 rwm
356 lxc.cgroup.devices.allow = c 136:* rwm
357 lxc.cgroup.devices.allow = c 5:2 rwm
359 lxc.cgroup.devices.allow = c 254:0 rwm
361 lxc.cgroup.devices.allow = c 10:229 rwm
363 lxc.cgroup.devices.allow = c 10:200 rwm
365 lxc.cgroup.devices.allow = c 1:7 rwm
367 lxc.cgroup.devices.allow = c 10:228 rwm
369 lxc.cgroup.devices.allow = c 10:232 rwm
372 cat <<EOF > $path/fstab
373 proc proc proc nodev,noexec,nosuid 0 0
374 sysfs sys sysfs defaults 0 0
377 if [ $?
-ne 0 ]; then
378 echo "Failed to add configuration"
390 # provide the lxc service
391 cat <<EOF > $rootfs/etc/init/lxc.conf
392 # fake some events needed for correct startup other services
394 description "Container Upstart"
399 rm -rf /var/run/*.pid
400 rm -rf /var/run/network/*
401 /sbin/initctl emit stopped JOB=udevtrigger --no-wait
402 /sbin/initctl emit started JOB=udev --no-wait
406 # fix buggus runlevel with sshd
407 cat <<EOF > $rootfs/etc/init/ssh.conf
408 # ssh - OpenBSD Secure Shell server
410 # The OpenSSH server provides secure shell access to the system.
412 description "OpenSSH server"
415 stop on runlevel [!2345]
421 # replaces SSHD_OOM_ADJUST in /etc/default/ssh
425 test -x /usr/sbin/sshd || { stop; exit 0; }
426 test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; }
427 test -c /dev/null || { stop; exit 0; }
429 mkdir -p -m0755 /var/run/sshd
432 # if you used to set SSHD_OPTS in /etc/default/ssh, you can change the
433 # 'exec' line here instead
437 cat <<EOF > $rootfs/etc/init/console.conf
440 # This service maintains a console on tty1 from the point the system is
441 # started until it is shut down again.
443 start on stopped rc RUNLEVEL=[2345]
444 stop on runlevel [!2345]
447 exec /sbin/getty -8 38400 /dev/console
450 cat <<EOF > $rootfs/lib/init/fstab
451 # /lib/init/fstab: cleared out for bare-bones lxc
454 # remove pointless services in a container
455 chroot
$rootfs /usr
/sbin
/update-rc.d
-f ondemand remove
457 chroot
$rootfs /bin
/bash
-c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
458 chroot
$rootfs /bin
/bash
-c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
459 chroot
$rootfs /bin
/bash
-c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
460 chroot
$rootfs /bin
/bash
-c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
461 chroot
$rootfs /bin
/bash
-c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
463 # if this isn't lucid, then we need to twiddle the network upstart bits :(
464 if [ $release != "lucid" ]; then
465 sed -i 's/^.*emission handled.*$/echo Emitting lo/' $rootfs/etc
/network
/if-up.d
/upstart
475 if [ $trim_container -eq 1 ]; then
476 trim
$rootfs $release
477 elif [ ! -f $rootfs/etc
/init
/container-detect.conf
]; then
478 # Make sure we have a working resolv.conf
479 cresolvonf
="${rootfs}/etc/resolv.conf"
480 mv $cresolvonf ${cresolvonf}.lxcbak
481 cat /etc
/resolv.conf
> ${cresolvonf}
483 # for lucid, if not trimming, then add the ubuntu-virt
484 # ppa and install lxcguest
485 if [ $release = "lucid" ]; then
486 chroot
$rootfs apt-get update
487 chroot
$rootfs apt-get
install --force-yes -y python-software-properties
488 chroot
$rootfs add-apt-repository ppa
:ubuntu-virt
/ppa
491 chroot
$rootfs apt-get update
492 chroot
$rootfs apt-get
install --force-yes -y lxcguest
494 # Restore old resolv.conf
496 mv ${cresolvonf}.lxcbak
${cresolvonf}
499 # If the container isn't running a native architecture, setup multiarch
500 if [ -x "$(ls -1 ${rootfs}/usr/bin/qemu-*-static 2>/dev/null)" ]; then
501 dpkg_version
=$
(chroot
$rootfs dpkg-query
-W -f='${Version}' dpkg
)
502 if chroot
$rootfs dpkg
--compare-versions $dpkg_version ge
"1.16.2"; then
503 chroot
$rootfs dpkg
--add-architecture ${hostarch}
505 mkdir
-p ${rootfs}/etc
/dpkg
/dpkg.cfg.d
506 echo "foreign-architecture ${hostarch}" > ${rootfs}/etc
/dpkg
/dpkg.cfg.d
/lxc-multiarch
509 # Save existing value of MIRROR and SECURITY_MIRROR
510 DEFAULT_MIRROR
=$MIRROR
511 DEFAULT_SECURITY_MIRROR
=$SECURITY_MIRROR
513 # Write a new sources.list containing both native and multiarch entries
514 > ${rootfs}/etc
/apt
/sources.list
515 write_sourceslist
$rootfs $arch "native"
517 MIRROR
=$DEFAULT_MIRROR
518 SECURITY_MIRROR
=$DEFAULT_SECURITY_MIRROR
519 write_sourceslist
$rootfs $hostarch "multiarch"
521 # Finally update the lists and install upstart using the host architecture
522 chroot
$rootfs apt-get update
523 chroot
$rootfs apt-get
install --force-yes -y --no-install-recommends upstart
:${hostarch} mountall:${hostarch} iproute:${hostarch} isc-dhcp-client:${hostarch}
526 # rmdir /dev/shm for containers that have /run/shm
527 # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
528 # get bind mounted to the host's /run/shm. So try to rmdir
529 # it, and in case that fails move it out of the way.
530 if [ ! -L $rootfs/dev
/shm
] && [ -d $rootfs/run
/shm
] && [ -e $rootfs/dev
/shm
]; then
531 mv $rootfs/dev
/shm
$rootfs/dev
/shm.bak
532 ln -s /run
/shm
$rootfs/dev
/shm
541 # copy /etc/passwd, /etc/shadow, and /etc/group entries into container
542 pwd=`getent passwd $user` ||
{ echo "Failed to copy password entry for $user"; false
; }
543 echo $pwd >> $rootfs/etc
/passwd
545 # make sure user's shell exists in the container
546 shell
=`echo $pwd | cut -d: -f 7`
547 if [ ! -x $rootfs/$shell ]; then
548 echo "shell $shell for user $user was not found in the container."
549 pkg
=`dpkg -S $(readlink -m $shell) | cut -d ':' -f1`
550 echo "Installing $pkg"
551 chroot
$rootfs apt-get
--force-yes -y install $pkg
554 shad
=`getent shadow $user`
555 echo "$shad" >> $rootfs/etc
/shadow
557 # bind-mount the user's path into the container's /home
558 h
=`getent passwd $user | cut -d: -f 6`
561 # use relative path in container
563 while [ ${h2:0:1} = "/" ]; do
566 echo "$h $h2 none bind 0 0" >> $path/fstab
568 # Make sure the group exists in container
569 grp
=`echo $pwd | cut -d: -f 4` # group number for $user
570 grpe
=`getent group $grp` ||
return 0 # if host doesn't define grp, ignore in container
571 chroot
$rootfs getent group
"$grpe" ||
echo "$grpe" >> $rootfs/etc
/group
577 $1 -h|--help [-a|--arch] [-b|--bindhome <user>] [--trim] [-d|--debug]
578 [-F | --flush-cache] [-r|--release <release>] [ -S | --auth-key <keyfile>]
579 release: the ubuntu release (e.g. precise): defaults to host release on ubuntu, otherwise uses latest LTS
580 trim: make a minimal (faster, but not upgrade-safe) container
581 bindhome: bind <user>'s home into the container
582 The ubuntu user will not be created, and <user> will have
584 arch: the container architecture (e.g. amd64): defaults to host arch
585 auth-key: SSH Public key file to inject into container
590 options
=$
(getopt
-o a
:b
:hp
:r
:xn
:FS
:d
-l arch
:,bindhome
:,help,path
:,release
:,trim
,name
:,flush-cache
,auth-key
:,debug
-- "$@")
591 if [ $?
-ne 0 ]; then
595 eval set -- "$options"
597 release
=precise
# Default to the last Ubuntu LTS release for non-Ubuntu systems
598 if [ -f /etc
/lsb-release
]; then
600 if [ "$DISTRIB_ID" = "Ubuntu" ]; then
601 release
=$DISTRIB_CODENAME
608 # Code taken from debootstrap
609 if [ -x /usr
/bin
/dpkg
] && /usr
/bin
/dpkg
--print-architecture >/dev
/null
2>&1; then
610 arch
=`/usr/bin/dpkg --print-architecture`
611 elif which udpkg
>/dev
/null
2>&1 && udpkg
--print-architecture >/dev
/null
2>&1; then
612 arch
=`/usr/bin/udpkg --print-architecture`
615 if [ "$arch" = "i686" ]; then
617 elif [ "$arch" = "x86_64" ]; then
619 elif [ "$arch" = "armv7l" ]; then
631 -h|
--help) usage
$0 && exit 0;;
632 -p|
--path) path
=$2; shift 2;;
633 -n|
--name) name
=$2; shift 2;;
634 -F|
--flush-cache) flushcache
=1; shift 1;;
635 -r|
--release) release
=$2; shift 2;;
636 -b|
--bindhome) bindhome
=$2; shift 2;;
637 -a|
--arch) arch
=$2; shift 2;;
638 -x|
--trim) trim_container
=1; shift 1;;
639 -S|
--auth-key) auth_key
=$2; shift 2;;
640 -d|
--debug) debug
=1; shift 1;;
641 --) shift 1; break ;;
646 if [ $debug -eq 1 ]; then
650 if [ -n "$bindhome" ]; then
651 pwd=`getent passwd $bindhome`
652 if [ $?
-ne 0 ]; then
653 echo "Error: no password entry found for $bindhome"
659 if [ "$arch" == "i686" ]; then
663 if [ $hostarch = "i386" -a $arch = "amd64" ]; then
664 echo "can't create $arch container on $hostarch"
668 if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \
669 [ $arch != "armhf" -a $arch != "armel" ]; then
670 echo "can't create $arch container on $hostarch"
674 if [ $hostarch = "powerpc" -a $arch != "powerpc" ]; then
675 echo "can't create $arch container on $hostarch"
679 which debootstrap
>/dev
/null
2>&1 ||
{ echo "'debootstrap' command is missing" >&2; false
; }
681 if [ -z "$path" ]; then
682 echo "'path' parameter is required"
686 if [ "$(id -u)" != "0" ]; then
687 echo "This script should be run as 'root'"
692 config
="$path/config"
693 if grep -q '^lxc.rootfs' $config 2>/dev
/null
; then
694 rootfs
=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
699 install_ubuntu
$rootfs $release $flushcache
700 if [ $?
-ne 0 ]; then
701 echo "failed to install ubuntu $release"
705 configure_ubuntu
$rootfs $name $release
706 if [ $?
-ne 0 ]; then
707 echo "failed to configure ubuntu $release for a container"
711 copy_configuration
$path $rootfs $name $arch $release
712 if [ $?
-ne 0 ]; then
713 echo "failed write configuration file"
717 post_process
$rootfs $release $trim_container
719 if [ -n "$bindhome" ]; then
720 do_bindhome
$rootfs $bindhome
721 finalize_user
$bindhome
728 if [ -n "$bindhome" ]; then
729 echo "# Log in as user $bindhome"
731 echo "# The default user is 'ubuntu' with password 'ubuntu'!"
732 echo "# Use the 'sudo' command to run tasks as root in the container."