]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-ubuntu.in
ubuntu: Don't break when the locale is C.*
[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" ] || 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
87 else
88 chroot $rootfs locale-gen $LANG
89 chroot $rootfs update-locale LANG=$LANG
90 fi
91
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
98 fi
99
100 return 0
101 }
102
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.
106 finalize_user()
107 {
108 user=$1
109
110 sudo_version=$(chroot $rootfs dpkg-query -W -f='${Version}' sudo)
111
112 if chroot $rootfs dpkg --compare-versions $sudo_version gt "1.8.3p1-1"; then
113 groups="sudo"
114 else
115 groups="sudo admin"
116 fi
117
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
121 done
122
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"
129
130 echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
131 fi
132 return 0
133 }
134
135 #
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.
139 #
140 choose_container_proxy()
141 {
142 local rootfs=$1
143 local arch=$2
144
145 if [ -z "$HTTP_PROXY" ]; then
146 HTTP_PROXY="none"
147 fi
148 case "$HTTP_PROXY" in
149 none)
150 APT_PROXY=
151 ;;
152 apt)
153 RES=`apt-config shell APT_PROXY Acquire::http::Proxy`
154 eval $RES
155 [ -z "$APT_PROXY" ] || export http_proxy=$APT_PROXY
156 ;;
157 *)
158 APT_PROXY=$HTTP_PROXY
159 export http_proxy=$HTTP_PROXY
160 ;;
161 esac
162 }
163
164 write_sourceslist()
165 {
166 # $1 => path to the rootfs
167 # $2 => architecture we want to add
168 # $3 => whether to use the multi-arch syntax or not
169
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" ;
174 EOF
175 fi
176
177 case $2 in
178 amd64|i386)
179 MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
180 SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
181 ;;
182 *)
183 MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
184 SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
185 ;;
186 esac
187 if [ -n "$3" ]; then
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
192 EOF
193 else
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
198 EOF
199 fi
200 }
201
202 cleanup()
203 {
204 rm -rf $cache/partial-$arch
205 rm -rf $cache/rootfs-$arch
206 }
207
208 suggest_flush()
209 {
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."
212 }
213
214 download_ubuntu()
215 {
216 cache=$1
217 arch=$2
218 release=$3
219
220 packages=vim,ssh
221
222 # Try to guess a list of langpacks to install
223 langpacks="language-pack-en"
224
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`
229 fi
230 packages="$packages,$(echo $langpacks | sed 's/ /,/g')"
231
232
233 echo "installing packages: $packages"
234
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"
240 return 1
241 fi
242
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
248 else
249 debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
250 fi
251
252 if [ $? -ne 0 ]; then
253 echo "Failed to download the rootfs, aborting."
254 return 1
255 fi
256
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
262
263 chroot "$1/partial-${arch}" apt-get update
264 if [ $? -ne 0 ]; then
265 echo "Failed to update the apt cache"
266 return 1
267 fi
268 cat > "$1/partial-${arch}"/usr/sbin/policy-rc.d << EOF
269 #!/bin/sh
270 exit 101
271 EOF
272 chmod +x "$1/partial-${arch}"/usr/sbin/policy-rc.d
273
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
276
277 chroot "$1/partial-${arch}" apt-get clean
278
279 mv "$1/partial-$arch" "$1/rootfs-$arch"
280 trap EXIT
281 trap SIGINT
282 trap SIGTERM
283 trap SIGHUP
284 echo "Download complete"
285 return 0
286 }
287
288 copy_ubuntu()
289 {
290 cache=$1
291 arch=$2
292 rootfs=$3
293
294 # make a local copy of the miniubuntu
295 echo "Copying rootfs to $rootfs ..."
296 mkdir -p $rootfs
297 rsync -Ha $cache/rootfs-$arch/ $rootfs/ || return 1
298 return 0
299 }
300
301 install_ubuntu()
302 {
303 rootfs=$1
304 release=$2
305 flushcache=$3
306 cache="@LOCALSTATEDIR@/cache/lxc/$release"
307 mkdir -p @LOCALSTATEDIR@/lock/subsys/
308
309 (
310 flock -x 200
311 if [ $? -ne 0 ]; then
312 echo "Cache repository is busy."
313 return 1
314 fi
315
316
317 if [ $flushcache -eq 1 ]; then
318 echo "Flushing cache..."
319 rm -rf "$cache/partial-$arch"
320 rm -rf "$cache/rootfs-$arch"
321 fi
322
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'"
328 return 1
329 fi
330 fi
331
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"
336 return 1
337 fi
338
339 return 0
340
341 ) 200>@LOCALSTATEDIR@/lock/subsys/lxc-ubuntu
342
343 return $?
344 }
345
346 copy_configuration()
347 {
348 path=$1
349 rootfs=$2
350 name=$3
351 arch=$4
352 release=$5
353
354 if [ $arch = "i386" ]; then
355 arch="i686"
356 fi
357
358 ttydir=""
359 if [ -f $rootfs/etc/init/container-detect.conf ]; then
360 ttydir=" lxc"
361 fi
362
363 # if there is exactly one veth network entry, make sure it has an
364 # associated hwaddr.
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
368 fi
369
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
374
375 lxc.devttydir =$ttydir
376 lxc.tty = 4
377 lxc.pts = 1024
378
379 lxc.utsname = $name
380 lxc.arch = $arch
381 lxc.cap.drop = sys_module mac_admin mac_override
382
383 # When using LXC with apparmor, uncomment the next line to run unconfined:
384 #lxc.aa_profile = unconfined
385
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
390 # /dev/null and zero
391 lxc.cgroup.devices.allow = c 1:3 rwm
392 lxc.cgroup.devices.allow = c 1:5 rwm
393 # consoles
394 lxc.cgroup.devices.allow = c 5:1 rwm
395 lxc.cgroup.devices.allow = c 5:0 rwm
396 # /dev/{,u}random
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
401 # rtc
402 lxc.cgroup.devices.allow = c 254:0 rwm
403 # fuse
404 lxc.cgroup.devices.allow = c 10:229 rwm
405 # tun
406 lxc.cgroup.devices.allow = c 10:200 rwm
407 # full
408 lxc.cgroup.devices.allow = c 1:7 rwm
409 # hpet
410 lxc.cgroup.devices.allow = c 10:228 rwm
411 # kvm
412 lxc.cgroup.devices.allow = c 10:232 rwm
413 EOF
414
415 cat <<EOF > $path/fstab
416 proc proc proc nodev,noexec,nosuid 0 0
417 sysfs sys sysfs defaults 0 0
418 EOF
419
420 if [ $? -ne 0 ]; then
421 echo "Failed to add configuration"
422 return 1
423 fi
424
425 return 0
426 }
427
428 trim()
429 {
430 rootfs=$1
431 release=$2
432
433 # provide the lxc service
434 cat <<EOF > $rootfs/etc/init/lxc.conf
435 # fake some events needed for correct startup other services
436
437 description "Container Upstart"
438
439 start on startup
440
441 script
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
446 end script
447 EOF
448
449 # fix buggus runlevel with sshd
450 cat <<EOF > $rootfs/etc/init/ssh.conf
451 # ssh - OpenBSD Secure Shell server
452 #
453 # The OpenSSH server provides secure shell access to the system.
454
455 description "OpenSSH server"
456
457 start on filesystem
458 stop on runlevel [!2345]
459
460 expect fork
461 respawn
462 respawn limit 10 5
463 umask 022
464 # replaces SSHD_OOM_ADJUST in /etc/default/ssh
465 oom never
466
467 pre-start script
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; }
471
472 mkdir -p -m0755 /var/run/sshd
473 end script
474
475 # if you used to set SSHD_OPTS in /etc/default/ssh, you can change the
476 # 'exec' line here instead
477 exec /usr/sbin/sshd
478 EOF
479
480 cat <<EOF > $rootfs/etc/init/console.conf
481 # console - getty
482 #
483 # This service maintains a console on tty1 from the point the system is
484 # started until it is shut down again.
485
486 start on stopped rc RUNLEVEL=[2345]
487 stop on runlevel [!2345]
488
489 respawn
490 exec /sbin/getty -8 38400 /dev/console
491 EOF
492
493 cat <<EOF > $rootfs/lib/init/fstab
494 # /lib/init/fstab: cleared out for bare-bones lxc
495 EOF
496
497 # remove pointless services in a container
498 chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
499
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'
505
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
509 fi
510 }
511
512 post_process()
513 {
514 rootfs=$1
515 release=$2
516 trim_container=$3
517
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}
525
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
532 fi
533
534 chroot $rootfs apt-get update
535 chroot $rootfs apt-get install --force-yes -y lxcguest
536
537 # Restore old resolv.conf
538 rm -f ${cresolvonf}
539 mv ${cresolvonf}.lxcbak ${cresolvonf}
540 fi
541
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}
547 else
548 mkdir -p ${rootfs}/etc/dpkg/dpkg.cfg.d
549 echo "foreign-architecture ${hostarch}" > ${rootfs}/etc/dpkg/dpkg.cfg.d/lxc-multiarch
550 fi
551
552 # Save existing value of MIRROR and SECURITY_MIRROR
553 DEFAULT_MIRROR=$MIRROR
554 DEFAULT_SECURITY_MIRROR=$SECURITY_MIRROR
555
556 # Write a new sources.list containing both native and multiarch entries
557 > ${rootfs}/etc/apt/sources.list
558 write_sourceslist $rootfs $arch "native"
559
560 MIRROR=$DEFAULT_MIRROR
561 SECURITY_MIRROR=$DEFAULT_SECURITY_MIRROR
562 write_sourceslist $rootfs $hostarch "multiarch"
563
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}
567 fi
568
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
576 fi
577 }
578
579 do_bindhome()
580 {
581 rootfs=$1
582 user=$2
583
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
587
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
595 fi
596
597 shad=`getent shadow $user`
598 echo "$shad" >> $rootfs/etc/shadow
599
600 # bind-mount the user's path into the container's /home
601 h=`getent passwd $user | cut -d: -f 6`
602 mkdir -p $rootfs/$h
603
604 # use relative path in container
605 h2=${h#/}
606 while [ ${h2:0:1} = "/" ]; do
607 h2=${h2#/}
608 done
609 echo "$h $h2 none bind 0 0" >> $path/fstab
610
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
615 }
616
617 usage()
618 {
619 cat <<EOF
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
626 sudo access.
627 arch: the container architecture (e.g. amd64): defaults to host arch
628 auth-key: SSH Public key file to inject into container
629 EOF
630 return 0
631 }
632
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
635 usage $(basename $0)
636 exit 1
637 fi
638 eval set -- "$options"
639
640 release=precise # Default to the last Ubuntu LTS release for non-Ubuntu systems
641 if [ -f /etc/lsb-release ]; then
642 . /etc/lsb-release
643 if [ "$DISTRIB_ID" = "Ubuntu" ]; then
644 release=$DISTRIB_CODENAME
645 fi
646 fi
647
648 bindhome=
649
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`
655 else
656 arch=$(uname -m)
657 if [ "$arch" = "i686" ]; then
658 arch="i386"
659 elif [ "$arch" = "x86_64" ]; then
660 arch="amd64"
661 elif [ "$arch" = "armv7l" ]; then
662 arch="armhf"
663 fi
664 fi
665
666 debug=0
667 trim_container=0
668 hostarch=$arch
669 flushcache=0
670 while true
671 do
672 case "$1" in
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 ;;
684 *) break ;;
685 esac
686 done
687
688 if [ $debug -eq 1 ]; then
689 set -x
690 fi
691
692 if [ -n "$bindhome" ]; then
693 pwd=`getent passwd $bindhome`
694 if [ $? -ne 0 ]; then
695 echo "Error: no password entry found for $bindhome"
696 exit 1
697 fi
698 fi
699
700
701 if [ "$arch" == "i686" ]; then
702 arch=i386
703 fi
704
705 if [ $hostarch = "i386" -a $arch = "amd64" ]; then
706 echo "can't create $arch container on $hostarch"
707 exit 1
708 fi
709
710 if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \
711 [ $arch != "armhf" -a $arch != "armel" ]; then
712 echo "can't create $arch container on $hostarch"
713 exit 1
714 fi
715
716 if [ $hostarch = "powerpc" -a $arch != "powerpc" ]; then
717 echo "can't create $arch container on $hostarch"
718 exit 1
719 fi
720
721 which debootstrap >/dev/null 2>&1 || { echo "'debootstrap' command is missing" >&2; false; }
722
723 if [ -z "$path" ]; then
724 echo "'path' parameter is required"
725 exit 1
726 fi
727
728 if [ "$(id -u)" != "0" ]; then
729 echo "This script should be run as 'root'"
730 exit 1
731 fi
732
733 # detect rootfs
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 }'`
737 else
738 rootfs=$path/rootfs
739 fi
740
741 install_ubuntu $rootfs $release $flushcache
742 if [ $? -ne 0 ]; then
743 echo "failed to install ubuntu $release"
744 exit 1
745 fi
746
747 configure_ubuntu $rootfs $name $release
748 if [ $? -ne 0 ]; then
749 echo "failed to configure ubuntu $release for a container"
750 exit 1
751 fi
752
753 copy_configuration $path $rootfs $name $arch $release
754 if [ $? -ne 0 ]; then
755 echo "failed write configuration file"
756 exit 1
757 fi
758
759 post_process $rootfs $release $trim_container
760
761 if [ -n "$bindhome" ]; then
762 do_bindhome $rootfs $bindhome
763 finalize_user $bindhome
764 else
765 finalize_user ubuntu
766 fi
767
768 echo ""
769 echo "##"
770 if [ -n "$bindhome" ]; then
771 echo "# Log in as user $bindhome"
772 else
773 echo "# The default user is 'ubuntu' with password 'ubuntu'!"
774 echo "# Use the 'sudo' command to run tasks as root in the container."
775 fi
776 echo "##"
777 echo ""