]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-ubuntu.in
ubuntu: Tweak architecture support
[mirror_lxc.git] / templates / lxc-ubuntu.in
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 # 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
87 else
88 chroot $rootfs locale-gen $LANG
89 chroot $rootfs update-locale LANG=$LANG
90 fi
91
92 return 0
93 }
94
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.
98 finalize_user()
99 {
100 user=$1
101
102 sudo_version=$(chroot $rootfs dpkg-query -W -f='${Version}' sudo)
103
104 if chroot $rootfs dpkg --compare-versions $sudo_version gt "1.8.3p1-1"; then
105 groups="sudo"
106 else
107 groups="sudo admin"
108 fi
109
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
113 done
114
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"
121
122 echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
123 fi
124 return 0
125 }
126
127 write_sourceslist()
128 {
129 # $1 => path to the rootfs
130 # $2 => architecture we want to add
131 # $3 => whether to use the multi-arch syntax or not
132
133 case $2 in
134 amd64|i386)
135 MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
136 SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
137 ;;
138 *)
139 MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
140 SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
141 ;;
142 esac
143 if [ -n "$3" ]; then
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
148 EOF
149 else
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
154 EOF
155 fi
156 }
157
158 cleanup()
159 {
160 rm -rf $cache/partial-$arch
161 rm -rf $cache/rootfs-$arch
162 }
163
164 suggest_flush()
165 {
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."
168 }
169
170 download_ubuntu()
171 {
172 cache=$1
173 arch=$2
174 release=$3
175
176 packages=vim,ssh
177
178 # Try to guess a list of langpacks to install
179 langpacks="language-pack-en"
180
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`
185 fi
186 packages="$packages,$(echo $langpacks | sed 's/ /,/g')"
187
188
189 echo "installing packages: $packages"
190
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"
196 return 1
197 fi
198
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
203 else
204 debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
205 fi
206
207 if [ $? -ne 0 ]; then
208 echo "Failed to download the rootfs, aborting."
209 return 1
210 fi
211
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
217
218 chroot "$1/partial-${arch}" apt-get update
219 if [ $? -ne 0 ]; then
220 echo "Failed to update the apt cache"
221 return 1
222 fi
223 cat > "$1/partial-${arch}"/usr/sbin/policy-rc.d << EOF
224 #!/bin/sh
225 exit 101
226 EOF
227 chmod +x "$1/partial-${arch}"/usr/sbin/policy-rc.d
228
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
231
232 chroot "$1/partial-${arch}" apt-get clean
233
234 mv "$1/partial-$arch" "$1/rootfs-$arch"
235 trap EXIT
236 trap SIGINT
237 trap SIGTERM
238 trap SIGHUP
239 echo "Download complete"
240 return 0
241 }
242
243 copy_ubuntu()
244 {
245 cache=$1
246 arch=$2
247 rootfs=$3
248
249 # make a local copy of the miniubuntu
250 echo "Copying rootfs to $rootfs ..."
251 mkdir -p $rootfs
252 rsync -Ha $cache/rootfs-$arch/ $rootfs/ || return 1
253 return 0
254 }
255
256 install_ubuntu()
257 {
258 rootfs=$1
259 release=$2
260 flushcache=$3
261 cache="@LOCALSTATEDIR@/cache/lxc/$release"
262 mkdir -p @LOCALSTATEDIR@/lock/subsys/
263
264 (
265 flock -x 200
266 if [ $? -ne 0 ]; then
267 echo "Cache repository is busy."
268 return 1
269 fi
270
271
272 if [ $flushcache -eq 1 ]; then
273 echo "Flushing cache..."
274 rm -rf "$cache/partial-$arch"
275 rm -rf "$cache/rootfs-$arch"
276 fi
277
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'"
283 return 1
284 fi
285 fi
286
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"
291 return 1
292 fi
293
294 return 0
295
296 ) 200>@LOCALSTATEDIR@/lock/subsys/lxc-ubuntu
297
298 return $?
299 }
300
301 copy_configuration()
302 {
303 path=$1
304 rootfs=$2
305 name=$3
306 arch=$4
307 release=$5
308
309 if [ $arch = "i386" ]; then
310 arch="i686"
311 fi
312
313 ttydir=""
314 if [ -f $rootfs/etc/init/container-detect.conf ]; then
315 ttydir=" lxc"
316 fi
317
318 # if there is exactly one veth network entry, make sure it has an
319 # associated hwaddr.
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
323 fi
324
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
329
330 lxc.devttydir =$ttydir
331 lxc.tty = 4
332 lxc.pts = 1024
333
334 lxc.utsname = $name
335 lxc.arch = $arch
336 lxc.cap.drop = sys_module mac_admin mac_override
337
338 # When using LXC with apparmor, uncomment the next line to run unconfined:
339 #lxc.aa_profile = unconfined
340
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
345 # /dev/null and zero
346 lxc.cgroup.devices.allow = c 1:3 rwm
347 lxc.cgroup.devices.allow = c 1:5 rwm
348 # consoles
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
353 # /dev/{,u}random
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
358 # rtc
359 lxc.cgroup.devices.allow = c 254:0 rwm
360 #fuse
361 lxc.cgroup.devices.allow = c 10:229 rwm
362 #tun
363 lxc.cgroup.devices.allow = c 10:200 rwm
364 #full
365 lxc.cgroup.devices.allow = c 1:7 rwm
366 #hpet
367 lxc.cgroup.devices.allow = c 10:228 rwm
368 #kvm
369 lxc.cgroup.devices.allow = c 10:232 rwm
370 EOF
371
372 cat <<EOF > $path/fstab
373 proc proc proc nodev,noexec,nosuid 0 0
374 sysfs sys sysfs defaults 0 0
375 EOF
376
377 if [ $? -ne 0 ]; then
378 echo "Failed to add configuration"
379 return 1
380 fi
381
382 return 0
383 }
384
385 trim()
386 {
387 rootfs=$1
388 release=$2
389
390 # provide the lxc service
391 cat <<EOF > $rootfs/etc/init/lxc.conf
392 # fake some events needed for correct startup other services
393
394 description "Container Upstart"
395
396 start on startup
397
398 script
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
403 end script
404 EOF
405
406 # fix buggus runlevel with sshd
407 cat <<EOF > $rootfs/etc/init/ssh.conf
408 # ssh - OpenBSD Secure Shell server
409 #
410 # The OpenSSH server provides secure shell access to the system.
411
412 description "OpenSSH server"
413
414 start on filesystem
415 stop on runlevel [!2345]
416
417 expect fork
418 respawn
419 respawn limit 10 5
420 umask 022
421 # replaces SSHD_OOM_ADJUST in /etc/default/ssh
422 oom never
423
424 pre-start script
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; }
428
429 mkdir -p -m0755 /var/run/sshd
430 end script
431
432 # if you used to set SSHD_OPTS in /etc/default/ssh, you can change the
433 # 'exec' line here instead
434 exec /usr/sbin/sshd
435 EOF
436
437 cat <<EOF > $rootfs/etc/init/console.conf
438 # console - getty
439 #
440 # This service maintains a console on tty1 from the point the system is
441 # started until it is shut down again.
442
443 start on stopped rc RUNLEVEL=[2345]
444 stop on runlevel [!2345]
445
446 respawn
447 exec /sbin/getty -8 38400 /dev/console
448 EOF
449
450 cat <<EOF > $rootfs/lib/init/fstab
451 # /lib/init/fstab: cleared out for bare-bones lxc
452 EOF
453
454 # remove pointless services in a container
455 chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
456
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'
462
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
466 fi
467 }
468
469 post_process()
470 {
471 rootfs=$1
472 release=$2
473 trim_container=$3
474
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}
482
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
489 fi
490
491 chroot $rootfs apt-get update
492 chroot $rootfs apt-get install --force-yes -y lxcguest
493
494 # Restore old resolv.conf
495 rm -f ${cresolvonf}
496 mv ${cresolvonf}.lxcbak ${cresolvonf}
497 fi
498
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}
504 else
505 mkdir -p ${rootfs}/etc/dpkg/dpkg.cfg.d
506 echo "foreign-architecture ${hostarch}" > ${rootfs}/etc/dpkg/dpkg.cfg.d/lxc-multiarch
507 fi
508
509 # Save existing value of MIRROR and SECURITY_MIRROR
510 DEFAULT_MIRROR=$MIRROR
511 DEFAULT_SECURITY_MIRROR=$SECURITY_MIRROR
512
513 # Write a new sources.list containing both native and multiarch entries
514 > ${rootfs}/etc/apt/sources.list
515 write_sourceslist $rootfs $arch "native"
516
517 MIRROR=$DEFAULT_MIRROR
518 SECURITY_MIRROR=$DEFAULT_SECURITY_MIRROR
519 write_sourceslist $rootfs $hostarch "multiarch"
520
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}
524 fi
525
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
533 fi
534 }
535
536 do_bindhome()
537 {
538 rootfs=$1
539 user=$2
540
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
544
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
552 fi
553
554 shad=`getent shadow $user`
555 echo "$shad" >> $rootfs/etc/shadow
556
557 # bind-mount the user's path into the container's /home
558 h=`getent passwd $user | cut -d: -f 6`
559 mkdir -p $rootfs/$h
560
561 # use relative path in container
562 h2=${h#/}
563 while [ ${h2:0:1} = "/" ]; do
564 h2=${h2#/}
565 done
566 echo "$h $h2 none bind 0 0" >> $path/fstab
567
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
572 }
573
574 usage()
575 {
576 cat <<EOF
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
583 sudo access.
584 arch: the container architecture (e.g. amd64): defaults to host arch
585 auth-key: SSH Public key file to inject into container
586 EOF
587 return 0
588 }
589
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
592 usage $(basename $0)
593 exit 1
594 fi
595 eval set -- "$options"
596
597 release=precise # Default to the last Ubuntu LTS release for non-Ubuntu systems
598 if [ -f /etc/lsb-release ]; then
599 . /etc/lsb-release
600 if [ "$DISTRIB_ID" = "Ubuntu" ]; then
601 release=$DISTRIB_CODENAME
602 fi
603 fi
604
605 bindhome=
606 arch=$(arch)
607
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`
613 else
614 arch=$(arch)
615 if [ "$arch" = "i686" ]; then
616 arch="i386"
617 elif [ "$arch" = "x86_64" ]; then
618 arch="amd64"
619 elif [ "$arch" = "armv7l" ]; then
620 arch="armhf"
621 fi
622 fi
623
624 debug=0
625 trim_container=0
626 hostarch=$arch
627 flushcache=0
628 while true
629 do
630 case "$1" in
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 ;;
642 *) break ;;
643 esac
644 done
645
646 if [ $debug -eq 1 ]; then
647 set -x
648 fi
649
650 if [ -n "$bindhome" ]; then
651 pwd=`getent passwd $bindhome`
652 if [ $? -ne 0 ]; then
653 echo "Error: no password entry found for $bindhome"
654 exit 1
655 fi
656 fi
657
658
659 if [ "$arch" == "i686" ]; then
660 arch=i386
661 fi
662
663 if [ $hostarch = "i386" -a $arch = "amd64" ]; then
664 echo "can't create $arch container on $hostarch"
665 exit 1
666 fi
667
668 if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \
669 [ $arch != "armhf" -a $arch != "armel" ]; then
670 echo "can't create $arch container on $hostarch"
671 exit 1
672 fi
673
674 if [ $hostarch = "powerpc" -a $arch != "powerpc" ]; then
675 echo "can't create $arch container on $hostarch"
676 exit 1
677 fi
678
679 which debootstrap >/dev/null 2>&1 || { echo "'debootstrap' command is missing" >&2; false; }
680
681 if [ -z "$path" ]; then
682 echo "'path' parameter is required"
683 exit 1
684 fi
685
686 if [ "$(id -u)" != "0" ]; then
687 echo "This script should be run as 'root'"
688 exit 1
689 fi
690
691 # detect rootfs
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 }'`
695 else
696 rootfs=$path/rootfs
697 fi
698
699 install_ubuntu $rootfs $release $flushcache
700 if [ $? -ne 0 ]; then
701 echo "failed to install ubuntu $release"
702 exit 1
703 fi
704
705 configure_ubuntu $rootfs $name $release
706 if [ $? -ne 0 ]; then
707 echo "failed to configure ubuntu $release for a container"
708 exit 1
709 fi
710
711 copy_configuration $path $rootfs $name $arch $release
712 if [ $? -ne 0 ]; then
713 echo "failed write configuration file"
714 exit 1
715 fi
716
717 post_process $rootfs $release $trim_container
718
719 if [ -n "$bindhome" ]; then
720 do_bindhome $rootfs $bindhome
721 finalize_user $bindhome
722 else
723 finalize_user ubuntu
724 fi
725
726 echo ""
727 echo "##"
728 if [ -n "$bindhome" ]; then
729 echo "# Log in as user $bindhome"
730 else
731 echo "# The default user is 'ubuntu' with password 'ubuntu'!"
732 echo "# Use the 'sudo' command to run tasks as root in the container."
733 fi
734 echo "##"
735 echo ""