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" ] ||
echo $LANG |
grep -E -q "^C(\..+)*$"; 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
92 # generate new SSH keys
93 if [ -x $rootfs@LOCALSTATEDIR@
/lib
/dpkg
/info
/openssh-server.postinst
]; then
94 rm -f $rootfs/etc
/ssh
/ssh_host_
*key
*
95 mv $rootfs/etc
/init
/ssh.conf
$rootfs/etc
/init
/ssh.conf.disabled
96 chroot
$rootfs @LOCALSTATEDIR@
/lib
/dpkg
/info
/openssh-server.postinst configure
97 mv $rootfs/etc
/init
/ssh.conf.disabled
$rootfs/etc
/init
/ssh.conf
103 # finish setting up the user in the container by injecting ssh key and
104 # adding sudo group membership.
105 # passed-in user is either 'ubuntu' or the user to bind in from host.
110 sudo_version
=$
(chroot
$rootfs dpkg-query
-W -f='${Version}' sudo
)
112 if chroot
$rootfs dpkg
--compare-versions $sudo_version gt
"1.8.3p1-1"; then
118 for group
in $groups; do
119 chroot
$rootfs groupadd
--system $group >/dev
/null
2>&1 || true
120 chroot
$rootfs adduser
${user} $group >/dev
/null
2>&1 || true
123 if [ -n "$auth_key" -a -f "$auth_key" ]; then
124 u_path
="/home/${user}/.ssh"
125 root_u_path
="$rootfs/$u_path"
126 mkdir
-p $root_u_path
127 cp $auth_key "$root_u_path/authorized_keys"
128 chroot
$rootfs chown
-R ${user}: "$u_path"
130 echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
136 # Choose proxies for container
137 # http_proxy will be used by debootstrap on the host.
138 # APT_PROXY will be used to set /etc/apt/apt.conf.d/70proxy in the container.
140 choose_container_proxy
()
145 if [ -z "$HTTP_PROXY" ]; then
148 case "$HTTP_PROXY" in
153 RES
=`apt-config shell APT_PROXY Acquire::http::Proxy`
155 [ -z "$APT_PROXY" ] ||
export http_proxy
=$APT_PROXY
158 APT_PROXY
=$HTTP_PROXY
159 export http_proxy
=$HTTP_PROXY
166 # $1 => path to the rootfs
167 # $2 => architecture we want to add
168 # $3 => whether to use the multi-arch syntax or not
170 if [ -n "$APT_PROXY" ]; then
171 mkdir
-p $rootfs/etc
/apt
/apt.conf.d
172 cat > $rootfs/etc
/apt
/apt.conf.d
/70proxy
<< EOF
173 Acquire::http::Proxy "$APT_PROXY" ;
179 MIRROR
=${MIRROR:-http://archive.ubuntu.com/ubuntu}
180 SECURITY_MIRROR
=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
183 MIRROR
=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
184 SECURITY_MIRROR
=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
188 cat >> "$1/etc/apt/sources.list" << EOF
189 deb [arch=$2] $MIRROR ${release} main restricted universe multiverse
190 deb [arch=$2] $MIRROR ${release}-updates main restricted universe multiverse
191 deb [arch=$2] $SECURITY_MIRROR ${release}-security main restricted universe multiverse
194 cat >> "$1/etc/apt/sources.list" << EOF
195 deb $MIRROR ${release} main restricted universe multiverse
196 deb $MIRROR ${release}-updates main restricted universe multiverse
197 deb $SECURITY_MIRROR ${release}-security main restricted universe multiverse
204 rm -rf $cache/partial-
$arch
205 rm -rf $cache/rootfs-
$arch
210 echo "Container upgrade failed. The container cache may be out of date,"
211 echo "in which case flushing the case (see -F in the hep output) may help."
222 # Try to guess a list of langpacks to install
223 langpacks
="language-pack-en"
225 if which dpkg
>/dev
/null
2>&1; then
226 langpacks
=`(echo $langpacks &&
227 dpkg -l | grep -E "^ii language-pack-[a-z]* " |
228 cut -d ' ' -f3) | sort -u`
230 packages
="$packages,$(echo $langpacks | sed 's/ /,/g')"
233 echo "installing packages: $packages"
235 trap cleanup EXIT SIGHUP SIGINT SIGTERM
236 # check the mini ubuntu was not already downloaded
237 mkdir
-p "$cache/partial-$arch"
238 if [ $?
-ne 0 ]; then
239 echo "Failed to create '$cache/partial-$arch' directory"
243 choose_container_proxy
$cache/partial-
$arch/ $arch
244 # download a mini ubuntu into a cache
245 echo "Downloading ubuntu $release minimal ..."
246 if [ -n "$(which qemu-debootstrap)" ]; then
247 qemu-debootstrap
--verbose --components=main
,universe
--arch=$arch --include=$packages $release $cache/partial-
$arch $MIRROR
249 debootstrap
--verbose --components=main
,universe
--arch=$arch --include=$packages $release $cache/partial-
$arch $MIRROR
252 if [ $?
-ne 0 ]; then
253 echo "Failed to download the rootfs, aborting."
257 # Serge isn't sure whether we should avoid doing this when
258 # $release == `distro-info -d`
259 echo "Installing updates"
260 > $cache/partial-
$arch/etc
/apt
/sources.list
261 write_sourceslist
$cache/partial-
$arch/ $arch
263 chroot
"$1/partial-${arch}" apt-get update
264 if [ $?
-ne 0 ]; then
265 echo "Failed to update the apt cache"
268 cat > "$1/partial-${arch}"/usr
/sbin
/policy-rc.d
<< EOF
272 chmod +x
"$1/partial-${arch}"/usr
/sbin
/policy-rc.d
274 lxc-unshare
-s MOUNT
-- chroot
"$1/partial-${arch}" apt-get dist-upgrade
-y ||
{ suggest_flush
; false
; }
275 rm -f "$1/partial-${arch}"/usr
/sbin
/policy-rc.d
277 chroot
"$1/partial-${arch}" apt-get clean
279 mv "$1/partial-$arch" "$1/rootfs-$arch"
284 echo "Download complete"
294 # make a local copy of the miniubuntu
295 echo "Copying rootfs to $rootfs ..."
297 rsync
-Ha $cache/rootfs-
$arch/ $rootfs/ ||
return 1
306 cache
="@LOCALSTATEDIR@/cache/lxc/$release"
307 mkdir
-p @LOCALSTATEDIR@
/lock
/subsys
/
311 if [ $?
-ne 0 ]; then
312 echo "Cache repository is busy."
317 if [ $flushcache -eq 1 ]; then
318 echo "Flushing cache..."
319 rm -rf "$cache/partial-$arch"
320 rm -rf "$cache/rootfs-$arch"
323 echo "Checking cache download in $cache/rootfs-$arch ... "
324 if [ ! -e "$cache/rootfs-$arch" ]; then
325 download_ubuntu
$cache $arch $release
326 if [ $?
-ne 0 ]; then
327 echo "Failed to download 'ubuntu $release base'"
332 echo "Copy $cache/rootfs-$arch to $rootfs ... "
333 copy_ubuntu
$cache $arch $rootfs
334 if [ $?
-ne 0 ]; then
335 echo "Failed to copy rootfs"
341 ) 200>@LOCALSTATEDIR@
/lock
/subsys
/lxc-ubuntu
354 if [ $arch = "i386" ]; then
359 if [ -f $rootfs/etc
/init
/container-detect.conf
]; then
363 # if there is exactly one veth network entry, make sure it has an
365 nics
=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
366 if [ $nics -eq 1 ]; then
367 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
370 grep -q "^lxc.rootfs" $path/config
2>/dev
/null ||
echo "lxc.rootfs = $rootfs" >> $path/config
371 cat <<EOF >> $path/config
372 lxc.mount = $path/fstab
373 lxc.pivotdir = lxc_putold
375 lxc.devttydir =$ttydir
381 lxc.cap.drop = sys_module mac_admin mac_override
383 # When using LXC with apparmor, uncomment the next line to run unconfined:
384 #lxc.aa_profile = unconfined
386 lxc.cgroup.devices.deny = a
387 # Allow any mknod (but not using the node)
388 lxc.cgroup.devices.allow = c *:* m
389 lxc.cgroup.devices.allow = b *:* m
391 lxc.cgroup.devices.allow = c 1:3 rwm
392 lxc.cgroup.devices.allow = c 1:5 rwm
394 lxc.cgroup.devices.allow = c 5:1 rwm
395 lxc.cgroup.devices.allow = c 5:0 rwm
397 lxc.cgroup.devices.allow = c 1:9 rwm
398 lxc.cgroup.devices.allow = c 1:8 rwm
399 lxc.cgroup.devices.allow = c 136:* rwm
400 lxc.cgroup.devices.allow = c 5:2 rwm
402 lxc.cgroup.devices.allow = c 254:0 rwm
404 lxc.cgroup.devices.allow = c 10:229 rwm
406 lxc.cgroup.devices.allow = c 10:200 rwm
408 lxc.cgroup.devices.allow = c 1:7 rwm
410 lxc.cgroup.devices.allow = c 10:228 rwm
412 lxc.cgroup.devices.allow = c 10:232 rwm
415 cat <<EOF > $path/fstab
416 proc proc proc nodev,noexec,nosuid 0 0
417 sysfs sys sysfs defaults 0 0
420 if [ $?
-ne 0 ]; then
421 echo "Failed to add configuration"
433 # provide the lxc service
434 cat <<EOF > $rootfs/etc/init/lxc.conf
435 # fake some events needed for correct startup other services
437 description "Container Upstart"
442 rm -rf /var/run/*.pid
443 rm -rf /var/run/network/*
444 /sbin/initctl emit stopped JOB=udevtrigger --no-wait
445 /sbin/initctl emit started JOB=udev --no-wait
449 # fix buggus runlevel with sshd
450 cat <<EOF > $rootfs/etc/init/ssh.conf
451 # ssh - OpenBSD Secure Shell server
453 # The OpenSSH server provides secure shell access to the system.
455 description "OpenSSH server"
458 stop on runlevel [!2345]
464 # replaces SSHD_OOM_ADJUST in /etc/default/ssh
468 test -x /usr/sbin/sshd || { stop; exit 0; }
469 test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; }
470 test -c /dev/null || { stop; exit 0; }
472 mkdir -p -m0755 /var/run/sshd
475 # if you used to set SSHD_OPTS in /etc/default/ssh, you can change the
476 # 'exec' line here instead
480 cat <<EOF > $rootfs/etc/init/console.conf
483 # This service maintains a console on tty1 from the point the system is
484 # started until it is shut down again.
486 start on stopped rc RUNLEVEL=[2345]
487 stop on runlevel [!2345]
490 exec /sbin/getty -8 38400 /dev/console
493 cat <<EOF > $rootfs/lib/init/fstab
494 # /lib/init/fstab: cleared out for bare-bones lxc
497 # remove pointless services in a container
498 chroot
$rootfs /usr
/sbin
/update-rc.d
-f ondemand remove
500 chroot
$rootfs /bin
/bash
-c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
501 chroot
$rootfs /bin
/bash
-c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
502 chroot
$rootfs /bin
/bash
-c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
503 chroot
$rootfs /bin
/bash
-c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
504 chroot
$rootfs /bin
/bash
-c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
506 # if this isn't lucid, then we need to twiddle the network upstart bits :(
507 if [ $release != "lucid" ]; then
508 sed -i 's/^.*emission handled.*$/echo Emitting lo/' $rootfs/etc
/network
/if-up.d
/upstart
518 if [ $trim_container -eq 1 ]; then
519 trim
$rootfs $release
520 elif [ ! -f $rootfs/etc
/init
/container-detect.conf
]; then
521 # Make sure we have a working resolv.conf
522 cresolvonf
="${rootfs}/etc/resolv.conf"
523 mv $cresolvonf ${cresolvonf}.lxcbak
524 cat /etc
/resolv.conf
> ${cresolvonf}
526 # for lucid, if not trimming, then add the ubuntu-virt
527 # ppa and install lxcguest
528 if [ $release = "lucid" ]; then
529 chroot
$rootfs apt-get update
530 chroot
$rootfs apt-get
install --force-yes -y python-software-properties
531 chroot
$rootfs add-apt-repository ppa
:ubuntu-virt
/ppa
534 chroot
$rootfs apt-get update
535 chroot
$rootfs apt-get
install --force-yes -y lxcguest
537 # Restore old resolv.conf
539 mv ${cresolvonf}.lxcbak
${cresolvonf}
542 # If the container isn't running a native architecture, setup multiarch
543 if [ -x "$(ls -1 ${rootfs}/usr/bin/qemu-*-static 2>/dev/null)" ]; then
544 dpkg_version
=$
(chroot
$rootfs dpkg-query
-W -f='${Version}' dpkg
)
545 if chroot
$rootfs dpkg
--compare-versions $dpkg_version ge
"1.16.2"; then
546 chroot
$rootfs dpkg
--add-architecture ${hostarch}
548 mkdir
-p ${rootfs}/etc
/dpkg
/dpkg.cfg.d
549 echo "foreign-architecture ${hostarch}" > ${rootfs}/etc
/dpkg
/dpkg.cfg.d
/lxc-multiarch
552 # Save existing value of MIRROR and SECURITY_MIRROR
553 DEFAULT_MIRROR
=$MIRROR
554 DEFAULT_SECURITY_MIRROR
=$SECURITY_MIRROR
556 # Write a new sources.list containing both native and multiarch entries
557 > ${rootfs}/etc
/apt
/sources.list
558 write_sourceslist
$rootfs $arch "native"
560 MIRROR
=$DEFAULT_MIRROR
561 SECURITY_MIRROR
=$DEFAULT_SECURITY_MIRROR
562 write_sourceslist
$rootfs $hostarch "multiarch"
564 # Finally update the lists and install upstart using the host architecture
565 chroot
$rootfs apt-get update
566 chroot
$rootfs apt-get
install --force-yes -y --no-install-recommends upstart
:${hostarch} mountall:${hostarch} iproute:${hostarch} isc-dhcp-client:${hostarch}
569 # rmdir /dev/shm for containers that have /run/shm
570 # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
571 # get bind mounted to the host's /run/shm. So try to rmdir
572 # it, and in case that fails move it out of the way.
573 if [ ! -L $rootfs/dev
/shm
] && [ -d $rootfs/run
/shm
] && [ -e $rootfs/dev
/shm
]; then
574 mv $rootfs/dev
/shm
$rootfs/dev
/shm.bak
575 ln -s /run
/shm
$rootfs/dev
/shm
584 # copy /etc/passwd, /etc/shadow, and /etc/group entries into container
585 pwd=`getent passwd $user` ||
{ echo "Failed to copy password entry for $user"; false
; }
586 echo $pwd >> $rootfs/etc
/passwd
588 # make sure user's shell exists in the container
589 shell
=`echo $pwd | cut -d: -f 7`
590 if [ ! -x $rootfs/$shell ]; then
591 echo "shell $shell for user $user was not found in the container."
592 pkg
=`dpkg -S $(readlink -m $shell) | cut -d ':' -f1`
593 echo "Installing $pkg"
594 chroot
$rootfs apt-get
--force-yes -y install $pkg
597 shad
=`getent shadow $user`
598 echo "$shad" >> $rootfs/etc
/shadow
600 # bind-mount the user's path into the container's /home
601 h
=`getent passwd $user | cut -d: -f 6`
604 # use relative path in container
606 while [ ${h2:0:1} = "/" ]; do
609 echo "$h $h2 none bind 0 0" >> $path/fstab
611 # Make sure the group exists in container
612 grp
=`echo $pwd | cut -d: -f 4` # group number for $user
613 grpe
=`getent group $grp` ||
return 0 # if host doesn't define grp, ignore in container
614 chroot
$rootfs getent group
"$grpe" ||
echo "$grpe" >> $rootfs/etc
/group
620 $1 -h|--help [-a|--arch] [-b|--bindhome <user>] [--trim] [-d|--debug]
621 [-F | --flush-cache] [-r|--release <release>] [ -S | --auth-key <keyfile>]
622 release: the ubuntu release (e.g. precise): defaults to host release on ubuntu, otherwise uses latest LTS
623 trim: make a minimal (faster, but not upgrade-safe) container
624 bindhome: bind <user>'s home into the container
625 The ubuntu user will not be created, and <user> will have
627 arch: the container architecture (e.g. amd64): defaults to host arch
628 auth-key: SSH Public key file to inject into container
633 options
=$
(getopt
-o a
:b
:hp
:r
:xn
:FS
:d
-l arch
:,bindhome
:,help,path
:,release
:,trim
,name
:,flush-cache
,auth-key
:,debug
-- "$@")
634 if [ $?
-ne 0 ]; then
638 eval set -- "$options"
640 release
=precise
# Default to the last Ubuntu LTS release for non-Ubuntu systems
641 if [ -f /etc
/lsb-release
]; then
643 if [ "$DISTRIB_ID" = "Ubuntu" ]; then
644 release
=$DISTRIB_CODENAME
650 # Code taken from debootstrap
651 if [ -x /usr
/bin
/dpkg
] && /usr
/bin
/dpkg
--print-architecture >/dev
/null
2>&1; then
652 arch
=`/usr/bin/dpkg --print-architecture`
653 elif which udpkg
>/dev
/null
2>&1 && udpkg
--print-architecture >/dev
/null
2>&1; then
654 arch
=`/usr/bin/udpkg --print-architecture`
657 if [ "$arch" = "i686" ]; then
659 elif [ "$arch" = "x86_64" ]; then
661 elif [ "$arch" = "armv7l" ]; then
673 -h|
--help) usage
$0 && exit 0;;
674 -p|
--path) path
=$2; shift 2;;
675 -n|
--name) name
=$2; shift 2;;
676 -F|
--flush-cache) flushcache
=1; shift 1;;
677 -r|
--release) release
=$2; shift 2;;
678 -b|
--bindhome) bindhome
=$2; shift 2;;
679 -a|
--arch) arch
=$2; shift 2;;
680 -x|
--trim) trim_container
=1; shift 1;;
681 -S|
--auth-key) auth_key
=$2; shift 2;;
682 -d|
--debug) debug
=1; shift 1;;
683 --) shift 1; break ;;
688 if [ $debug -eq 1 ]; then
692 if [ -n "$bindhome" ]; then
693 pwd=`getent passwd $bindhome`
694 if [ $?
-ne 0 ]; then
695 echo "Error: no password entry found for $bindhome"
701 if [ "$arch" == "i686" ]; then
705 if [ $hostarch = "i386" -a $arch = "amd64" ]; then
706 echo "can't create $arch container on $hostarch"
710 if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \
711 [ $arch != "armhf" -a $arch != "armel" ]; then
712 echo "can't create $arch container on $hostarch"
716 if [ $hostarch = "powerpc" -a $arch != "powerpc" ]; then
717 echo "can't create $arch container on $hostarch"
721 which debootstrap
>/dev
/null
2>&1 ||
{ echo "'debootstrap' command is missing" >&2; false
; }
723 if [ -z "$path" ]; then
724 echo "'path' parameter is required"
728 if [ "$(id -u)" != "0" ]; then
729 echo "This script should be run as 'root'"
734 config
="$path/config"
735 if grep -q '^lxc.rootfs' $config 2>/dev
/null
; then
736 rootfs
=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
741 install_ubuntu
$rootfs $release $flushcache
742 if [ $?
-ne 0 ]; then
743 echo "failed to install ubuntu $release"
747 configure_ubuntu
$rootfs $name $release
748 if [ $?
-ne 0 ]; then
749 echo "failed to configure ubuntu $release for a container"
753 copy_configuration
$path $rootfs $name $arch $release
754 if [ $?
-ne 0 ]; then
755 echo "failed write configuration file"
759 post_process
$rootfs $release $trim_container
761 if [ -n "$bindhome" ]; then
762 do_bindhome
$rootfs $bindhome
763 finalize_user
$bindhome
770 if [ -n "$bindhome" ]; then
771 echo "# Log in as user $bindhome"
773 echo "# The default user is 'ubuntu' with password 'ubuntu'!"
774 echo "# Use the 'sudo' command to run tasks as root in the container."