* /usr/lib/lxc/templates of lxc 0.8.0~rc1-4ubuntu37 on Ubuntu 12.10 (beta)
[lab.git] / lxc / 0.8.0~rc1-4ubuntu37 / templates / lxc-ubuntu
1 #!/bin/bash
2
3 #
4 # template script for generating ubuntu container for LXC
5 #
6 # This script consolidates and extends the existing lxc ubuntu scripts
7 #
8
9 # Copyright © 2011 Serge Hallyn <serge.hallyn@canonical.com>
10 # Copyright © 2010 Wilhelm Meier
11 # Author: Wilhelm Meier <wilhelm.meier@fh-kl.de>
12 #
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.
16
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.
21
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.
25 #
26
27 set -e
28
29 if [ -r /etc/default/lxc ]; then
30     . /etc/default/lxc
31 fi
32
33 configure_ubuntu()
34 {
35     rootfs=$1
36     hostname=$2
37     release=$3
38
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).
43
44 # The loopback network interface
45 auto lo
46 iface lo inet loopback
47
48 auto eth0
49 iface eth0 inet dhcp
50 EOF
51
52     # set the hostname
53     cat <<EOF > $rootfs/etc/hostname
54 $hostname
55 EOF
56     # set minimal hosts
57     cat <<EOF > $rootfs/etc/hosts
58 127.0.0.1   localhost
59 127.0.1.1   $hostname
60
61 # The following lines are desirable for IPv6 capable hosts
62 ::1     ip6-localhost ip6-loopback
63 fe00::0 ip6-localnet
64 ff00::0 ip6-mcastprefix
65 ff02::1 ip6-allnodes
66 ff02::2 ip6-allrouters
67 EOF
68
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
72
73         # remove jobs for consoles 5 and 6 since we only create 4 consoles in
74         # this template
75         rm -f $rootfs/etc/init/tty{5,6}.conf
76     fi
77
78     if [ -z "$bindhome" ]; then
79         chroot $rootfs useradd --create-home -s /bin/bash ubuntu
80         echo "ubuntu:ubuntu" | chroot $rootfs chpasswd
81     fi
82
83     return 0
84 }
85
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.
89 finalize_user()
90 {
91     user=$1
92
93     sudo_version=$(chroot $rootfs dpkg-query -W -f='${Version}' sudo)
94
95     if chroot $rootfs dpkg --compare-versions $sudo_version gt "1.8.3p1-1"; then
96         groups="sudo"
97     else
98         groups="sudo admin"
99     fi
100
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
104     done
105
106     if [ -n "$auth_key" -a -f "$auth_key" ]; then
107         u_path="/home/${user}/.ssh"
108         root_u_path="$rootfs/$u_path"
109
110         mkdir -p $root_u_path
111         cp $auth_key "$root_u_path/authorized_keys"
112         chroot $rootfs chown -R ${user}: "$u_path"
113
114         echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
115     fi
116     return 0
117 }
118
119 write_sourceslist()
120 {
121     # $1 => path to the rootfs
122     # $2 => architecture we want to add
123     # $3 => whether to use the multi-arch syntax or not
124
125     case $2 in
126       amd64|i386)
127             MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
128             SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
129             ;;
130       *)
131             MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
132             SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
133             ;;
134     esac
135     if [ -n "$3" ]; then
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
140 EOF
141     else
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
146 EOF
147     fi
148 }
149
150 cleanup()
151 {
152     rm -rf $cache/partial-$arch
153     rm -rf $cache/rootfs-$arch
154 }
155
156 suggest_flush()
157 {
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."
160 }
161
162 download_ubuntu()
163 {
164     cache=$1
165     arch=$2
166     release=$3
167
168     packages=vim,ssh
169     echo "installing packages: $packages"
170
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"
176         return 1
177     fi
178
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
183     else
184         debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
185     fi
186
187     if [ $? -ne 0 ]; then
188         echo "Failed to download the rootfs, aborting."
189             return 1
190     fi
191
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
197
198     chroot "$1/partial-${arch}" apt-get update
199     if [ $? -ne 0 ]; then
200         echo "Failed to update the apt cache"
201         return 1
202     fi
203     cat > "$1/partial-${arch}"/usr/sbin/policy-rc.d << EOF
204 #!/bin/sh
205 exit 101
206 EOF
207     chmod +x "$1/partial-${arch}"/usr/sbin/policy-rc.d
208
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
211
212     chroot "$1/partial-${arch}" apt-get clean
213
214     mv "$1/partial-$arch" "$1/rootfs-$arch"
215     trap EXIT
216     trap SIGINT
217     trap SIGTERM
218     trap SIGHUP
219     echo "Download complete"
220     return 0
221 }
222
223 copy_ubuntu()
224 {
225     cache=$1
226     arch=$2
227     rootfs=$3
228
229     # make a local copy of the miniubuntu
230     echo "Copying rootfs to $rootfs ..."
231     mkdir -p $rootfs
232     rsync -a $cache/rootfs-$arch/ $rootfs/ || return 1
233     return 0
234 }
235
236 install_ubuntu()
237 {
238     rootfs=$1
239     release=$2
240     flushcache=$3
241     cache="/var/cache/lxc/$release"
242     mkdir -p /var/lock/subsys/
243
244     (
245         flock -x 200
246         if [ $? -ne 0 ]; then
247             echo "Cache repository is busy."
248             return 1
249         fi
250
251
252         if [ $flushcache -eq 1 ]; then
253             echo "Flushing cache..."
254             rm -rf "$cache/partial-$arch"
255             rm -rf "$cache/rootfs-$arch"
256         fi
257
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'"
263                 return 1
264             fi
265         fi
266
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"
271             return 1
272         fi
273
274         return 0
275
276     ) 200>/var/lock/subsys/lxc
277
278     return $?
279 }
280
281 copy_configuration()
282 {
283     path=$1
284     rootfs=$2
285     name=$3
286     arch=$4
287     release=$5
288
289     if [ $arch = "i386" ]; then
290         arch="i686"
291     fi
292
293     ttydir=""
294     if [ -f $rootfs/etc/init/container-detect.conf ]; then
295         ttydir=" lxc"
296     fi
297
298     # if there is exactly one veth network entry, make sure it has an
299     # associated hwaddr.
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/.$//')
304 EOF
305     fi
306
307     grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
308     cat <<EOF >> $path/config
309 lxc.utsname = $name
310
311 lxc.devttydir =$ttydir
312 lxc.tty = 4
313 lxc.pts = 1024
314 lxc.mount  = $path/fstab
315 lxc.arch = $arch
316 lxc.cap.drop = sys_module mac_admin mac_override
317 lxc.pivotdir = lxc_putold
318
319 # uncomment the next line to run the container unconfined:
320 #lxc.aa_profile = unconfined
321
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
326 # /dev/null and zero
327 lxc.cgroup.devices.allow = c 1:3 rwm
328 lxc.cgroup.devices.allow = c 1:5 rwm
329 # consoles
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
334 # /dev/{,u}random
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
339 # rtc
340 lxc.cgroup.devices.allow = c 254:0 rwm
341 #fuse
342 lxc.cgroup.devices.allow = c 10:229 rwm
343 #tun
344 lxc.cgroup.devices.allow = c 10:200 rwm
345 #full
346 lxc.cgroup.devices.allow = c 1:7 rwm
347 #hpet
348 lxc.cgroup.devices.allow = c 10:228 rwm
349 #kvm
350 lxc.cgroup.devices.allow = c 10:232 rwm
351 EOF
352
353     cat <<EOF > $path/fstab
354 proc            proc         proc    nodev,noexec,nosuid 0 0
355 sysfs           sys          sysfs defaults  0 0
356 devtmpfs        dev          devtmpfs defaults 0 0
357 EOF
358
359     if [ $? -ne 0 ]; then
360         echo "Failed to add configuration"
361         return 1
362     fi
363
364     return 0
365 }
366
367 trim()
368 {
369     rootfs=$1
370     release=$2
371
372     # provide the lxc service
373     cat <<EOF > $rootfs/etc/init/lxc.conf
374 # fake some events needed for correct startup other services
375
376 description     "Container Upstart"
377
378 start on startup
379
380 script
381         rm -rf /var/run/*.pid
382         rm -rf /var/run/network/*
383         /sbin/initctl emit stopped JOB=udevtrigger --no-wait
384         /sbin/initctl emit started JOB=udev --no-wait
385 end script
386 EOF
387
388     # fix buggus runlevel with sshd
389     cat <<EOF > $rootfs/etc/init/ssh.conf
390 # ssh - OpenBSD Secure Shell server
391 #
392 # The OpenSSH server provides secure shell access to the system.
393
394 description     "OpenSSH server"
395
396 start on filesystem
397 stop on runlevel [!2345]
398
399 expect fork
400 respawn
401 respawn limit 10 5
402 umask 022
403 # replaces SSHD_OOM_ADJUST in /etc/default/ssh
404 oom never
405
406 pre-start script
407     test -x /usr/sbin/sshd || { stop; exit 0; }
408     test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; }
409     test -c /dev/null || { stop; exit 0; }
410
411     mkdir -p -m0755 /var/run/sshd
412 end script
413
414 # if you used to set SSHD_OPTS in /etc/default/ssh, you can change the
415 # 'exec' line here instead
416 exec /usr/sbin/sshd
417 EOF
418
419     cat <<EOF > $rootfs/etc/init/console.conf
420 # console - getty
421 #
422 # This service maintains a console on tty1 from the point the system is
423 # started until it is shut down again.
424
425 start on stopped rc RUNLEVEL=[2345]
426 stop on runlevel [!2345]
427
428 respawn
429 exec /sbin/getty -8 38400 /dev/console
430 EOF
431
432     cat <<EOF > $rootfs/lib/init/fstab
433 # /lib/init/fstab: cleared out for bare-bones lxc
434 EOF
435
436     # reconfigure some services
437     if [ -z "$LANG" ]; then
438         chroot $rootfs locale-gen en_US.UTF-8
439         chroot $rootfs update-locale LANG=en_US.UTF-8
440     else
441         chroot $rootfs locale-gen $LANG
442         chroot $rootfs update-locale LANG=$LANG
443     fi
444
445     # remove pointless services in a container
446     chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
447
448     chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
449     chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
450     chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
451     chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
452     chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
453
454     # if this isn't lucid, then we need to twiddle the network upstart bits :(
455     if [ $release != "lucid" ]; then
456         sed -i 's/^.*emission handled.*$/echo Emitting lo/' $rootfs/etc/network/if-up.d/upstart
457     fi
458 }
459
460 post_process()
461 {
462     rootfs=$1
463     release=$2
464     trim_container=$3
465
466     if [ $trim_container -eq 1 ]; then
467         trim $rootfs $release
468     elif [ ! -f $rootfs/etc/init/container-detect.conf ]; then
469         # Make sure we have a working resolv.conf
470         cresolvonf="${rootfs}/etc/resolv.conf"
471         mv $cresolvonf ${cresolvonf}.lxcbak
472         cat /etc/resolv.conf > ${cresolvonf}
473
474         # for lucid, if not trimming, then add the ubuntu-virt
475         # ppa and install lxcguest
476         if [ $release = "lucid" ]; then
477             chroot $rootfs apt-get update
478             chroot $rootfs apt-get install --force-yes -y python-software-properties
479             chroot $rootfs add-apt-repository ppa:ubuntu-virt/ppa
480         fi
481
482         chroot $rootfs apt-get update
483         chroot $rootfs apt-get install --force-yes -y lxcguest
484
485         # Restore old resolv.conf
486         rm -f ${cresolvonf}
487         mv ${cresolvonf}.lxcbak ${cresolvonf}
488     fi
489
490     # If the container isn't running a native architecture, setup multiarch
491     if [ -x "$(ls -1 ${rootfs}/usr/bin/qemu-*-static 2>/dev/null)" ]; then
492         dpkg_version=$(chroot $rootfs dpkg-query -W -f='${Version}' dpkg)
493         if chroot $rootfs dpkg --compare-versions $dpkg_version ge "1.16.2"; then
494             chroot $rootfs dpkg --add-architecture ${hostarch}
495         else
496             mkdir -p ${rootfs}/etc/dpkg/dpkg.cfg.d
497             echo "foreign-architecture ${hostarch}" > ${rootfs}/etc/dpkg/dpkg.cfg.d/lxc-multiarch
498         fi
499
500         # Save existing value of MIRROR and SECURITY_MIRROR
501         DEFAULT_MIRROR=$MIRROR
502         DEFAULT_SECURITY_MIRROR=$SECURITY_MIRROR
503
504         # Write a new sources.list containing both native and multiarch entries
505         > ${rootfs}/etc/apt/sources.list
506         write_sourceslist $rootfs $arch "native"
507
508         MIRROR=$DEFAULT_MIRROR
509         SECURITY_MIRROR=$DEFAULT_SECURITY_MIRROR
510         write_sourceslist $rootfs $hostarch "multiarch"
511
512         # Finally update the lists and install upstart using the host architecture
513         chroot $rootfs apt-get update
514         chroot $rootfs apt-get install --force-yes -y --no-install-recommends upstart:${hostarch} mountall:${hostarch} iproute:${hostarch} isc-dhcp-client:${hostarch}
515     fi
516
517     # rmdir /dev/shm for containers that have /run/shm
518     # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
519     # get bind mounted to the host's /run/shm.  So try to rmdir
520     # it, and in case that fails move it out of the way.
521     if [ ! -L $rootfs/dev/shm ] && [ -d $rootfs/run/shm ] && [ -e $rootfs/dev/shm ]; then
522         mv $rootfs/dev/shm $rootfs/dev/shm.bak
523         ln -s /run/shm $rootfs/dev/shm
524     fi
525 }
526
527 do_bindhome()
528 {
529     rootfs=$1
530     user=$2
531
532     # copy /etc/passwd, /etc/shadow, and /etc/group entries into container
533     pwd=`getent passwd $user` || { echo "Failed to copy password entry for $user"; false; }
534     echo $pwd >> $rootfs/etc/passwd
535
536     # make sure user's shell exists in the container
537     shell=`echo $pwd | cut -d: -f 7`
538     if [ ! -x $rootfs/$shell ]; then
539         echo "shell $shell for user $user was not found in the container."
540         pkg=`dpkg -S $(readlink -m $shell) | cut -d ':' -f1`
541         echo "Installing $pkg"
542         chroot $rootfs apt-get --force-yes -y install $pkg
543     fi
544
545     shad=`getent shadow $user`
546     echo "$shad" >> $rootfs/etc/shadow
547
548     # bind-mount the user's path into the container's /home
549     h=`getent passwd $user | cut -d: -f 6`
550     mkdir -p $rootfs/$h
551
552     # use relative path in container
553     h2=${h#/}
554     while [ ${h2:0:1} = "/" ]; do
555         h2=${h2#/}
556     done
557     echo "$h $h2 none bind 0 0" >> $path/fstab
558
559     # Make sure the group exists in container
560     grp=`echo $pwd | cut -d: -f 4`  # group number for $user
561     grpe=`getent group $grp` || return 0  # if host doesn't define grp, ignore in container
562     chroot $rootfs getent group "$grpe" || echo "$grpe" >> $rootfs/etc/group
563 }
564
565 usage()
566 {
567     cat <<EOF
568 $1 -h|--help [-a|--arch] [-b|--bindhome <user>] [--trim] [-d|--debug]
569    [-F | --flush-cache] [-r|--release <release>] [ -S | --auth-key <keyfile>]
570 release: the ubuntu release (e.g. precise): defaults to host release on ubuntu, otherwise uses latest LTS
571 trim: make a minimal (faster, but not upgrade-safe) container
572 bindhome: bind <user>'s home into the container
573           The ubuntu user will not be created, and <user> will have
574           sudo access.
575 arch: the container architecture (e.g. amd64): defaults to host arch
576 auth-key: SSH Public key file to inject into container
577 EOF
578     return 0
579 }
580
581 options=$(getopt -o a:b:hp:r:xn:FS:d -l arch:,bindhome:,help,path:,release:,trim,name:,flush-cache,auth-key:,debug -- "$@")
582 if [ $? -ne 0 ]; then
583     usage $(basename $0)
584     exit 1
585 fi
586 eval set -- "$options"
587
588 release=precise # Default to the last Ubuntu LTS release for non-Ubuntu systems
589 if [ -f /etc/lsb-release ]; then
590     . /etc/lsb-release
591     if [ "$DISTRIB_ID" = "Ubuntu" ]; then
592         release=$DISTRIB_CODENAME
593     fi
594 fi
595
596 bindhome=
597 arch=$(arch)
598
599 # Code taken from debootstrap
600 if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
601     arch=`/usr/bin/dpkg --print-architecture`
602 elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
603     arch=`/usr/bin/udpkg --print-architecture`
604 else
605     arch=$(arch)
606     if [ "$arch" = "i686" ]; then
607         arch="i386"
608     elif [ "$arch" = "x86_64" ]; then
609         arch="amd64"
610     elif [ "$arch" = "armv7l" ]; then
611         arch="armel"
612     fi
613 fi
614
615 debug=0
616 trim_container=0
617 hostarch=$arch
618 flushcache=0
619 while true
620 do
621     case "$1" in
622     -h|--help)      usage $0 && exit 0;;
623     -p|--path)      path=$2; shift 2;;
624     -n|--name)      name=$2; shift 2;;
625     -F|--flush-cache) flushcache=1; shift 1;;
626     -r|--release)   release=$2; shift 2;;
627     -b|--bindhome)  bindhome=$2; shift 2;;
628     -a|--arch)      arch=$2; shift 2;;
629     -x|--trim)      trim_container=1; shift 1;;
630     -S|--auth-key)  auth_key=$2; shift 2;;
631     -d|--debug)     debug=1; shift 1;;
632     --)             shift 1; break ;;
633         *)              break ;;
634     esac
635 done
636
637 if [ $debug -eq 1 ]; then
638     set -x
639 fi
640
641 if [ -n "$bindhome" ]; then
642     pwd=`getent passwd $bindhome`
643     if [ $? -ne 0 ]; then
644         echo "Error: no password entry found for $bindhome"
645         exit 1
646     fi
647 fi
648
649
650 if [ "$arch" == "i686" ]; then
651     arch=i386
652 fi
653
654 if [ $hostarch = "i386" -a $arch = "amd64" ]; then
655     echo "can't create amd64 container on i386"
656     exit 1
657 fi
658
659 type debootstrap
660 if [ $? -ne 0 ]; then
661     echo "'debootstrap' command is missing"
662     exit 1
663 fi
664
665 if [ -z "$path" ]; then
666     echo "'path' parameter is required"
667     exit 1
668 fi
669
670 if [ "$(id -u)" != "0" ]; then
671     echo "This script should be run as 'root'"
672     exit 1
673 fi
674
675 # detect rootfs
676 config="$path/config"
677 if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
678     rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
679 else
680     rootfs=$path/rootfs
681 fi
682
683 install_ubuntu $rootfs $release $flushcache
684 if [ $? -ne 0 ]; then
685     echo "failed to install ubuntu $release"
686     exit 1
687 fi
688
689 configure_ubuntu $rootfs $name $release
690 if [ $? -ne 0 ]; then
691     echo "failed to configure ubuntu $release for a container"
692     exit 1
693 fi
694
695 copy_configuration $path $rootfs $name $arch $release
696 if [ $? -ne 0 ]; then
697     echo "failed write configuration file"
698     exit 1
699 fi
700
701 post_process $rootfs $release $trim_container
702
703 if [ -n "$bindhome" ]; then
704     do_bindhome $rootfs $bindhome
705     finalize_user $bindhome
706 else
707     finalize_user ubuntu
708 fi
709
710 echo ""
711 echo "##"
712 if [ -n "$bindhome" ]; then
713         echo "# Log in as user $bindhome"
714 else
715         echo "# The default user is 'ubuntu' with password 'ubuntu'!"
716         echo "# Use the 'sudo' command to run tasks as root in the container."
717 fi
718 echo "##"
719 echo ""