]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-ubuntu.in
Move some common Ubuntu config
[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 library is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU Lesser General Public
15 # License as published by the Free Software Foundation; either
16 # version 2.1 of the License, or (at your option) any later version.
17
18 # This library is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # Lesser General Public License for more details.
22
23 # You should have received a copy of the GNU Lesser General Public
24 # License along with this library; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26
27 set -e
28
29 LOCALSTATEDIR="@LOCALSTATEDIR@"
30 LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
31
32 if [ -r /etc/default/lxc ]; then
33 . /etc/default/lxc
34 fi
35
36 configure_ubuntu()
37 {
38 rootfs=$1
39 hostname=$2
40 release=$3
41 user=$4
42 password=$5
43
44 # configure the network using the dhcp
45 cat <<EOF > $rootfs/etc/network/interfaces
46 # This file describes the network interfaces available on your system
47 # and how to activate them. For more information, see interfaces(5).
48
49 # The loopback network interface
50 auto lo
51 iface lo inet loopback
52
53 auto eth0
54 iface eth0 inet dhcp
55 EOF
56
57 # set the hostname
58 cat <<EOF > $rootfs/etc/hostname
59 $hostname
60 EOF
61 # set minimal hosts
62 cat <<EOF > $rootfs/etc/hosts
63 127.0.0.1 localhost
64 127.0.1.1 $hostname
65
66 # The following lines are desirable for IPv6 capable hosts
67 ::1 ip6-localhost ip6-loopback
68 fe00::0 ip6-localnet
69 ff00::0 ip6-mcastprefix
70 ff02::1 ip6-allnodes
71 ff02::2 ip6-allrouters
72 EOF
73
74 if [ ! -f $rootfs/etc/init/container-detect.conf ]; then
75 # suppress log level output for udev
76 sed -i "s/=\"err\"/=0/" $rootfs/etc/udev/udev.conf
77
78 # remove jobs for consoles 5 and 6 since we only create 4 consoles in
79 # this template
80 rm -f $rootfs/etc/init/tty{5,6}.conf
81 fi
82
83 if [ -z "$bindhome" ]; then
84 chroot $rootfs useradd --create-home -s /bin/bash $user
85 echo "$user:$password" | chroot $rootfs chpasswd
86 fi
87
88 # make sure we have the current locale defined in the container
89 if [ -z "$LANG" ] || echo $LANG | grep -E -q "^C(\..+)*$"; then
90 chroot $rootfs locale-gen en_US.UTF-8
91 chroot $rootfs update-locale LANG=en_US.UTF-8
92 else
93 chroot $rootfs locale-gen $LANG
94 chroot $rootfs update-locale LANG=$LANG
95 fi
96
97 # generate new SSH keys
98 if [ -x $rootfs$LOCALSTATEDIR/lib/dpkg/info/openssh-server.postinst ]; then
99 cat > $rootfs/usr/sbin/policy-rc.d << EOF
100 #!/bin/sh
101 exit 101
102 EOF
103 chmod +x $rootfs/usr/sbin/policy-rc.d
104
105 rm -f $rootfs/etc/ssh/ssh_host_*key*
106 mv $rootfs/etc/init/ssh.conf $rootfs/etc/init/ssh.conf.disabled
107 DPKG_MAINTSCRIPT_PACKAGE=openssh DPKG_MAINTSCRIPT_NAME=postinst chroot $rootfs $LOCALSTATEDIR/lib/dpkg/info/openssh-server.postinst configure
108 mv $rootfs/etc/init/ssh.conf.disabled $rootfs/etc/init/ssh.conf
109
110 sed -i "s/root@$(hostname)/root@$hostname/g" $rootfs/etc/ssh/ssh_host_*.pub
111
112 rm -f $rootfs/usr/sbin/policy-rc.d
113 fi
114
115 return 0
116 }
117
118 # finish setting up the user in the container by injecting ssh key and
119 # adding sudo group membership.
120 # passed-in user is either 'ubuntu' or the user to bind in from host.
121 finalize_user()
122 {
123 user=$1
124
125 sudo_version=$(chroot $rootfs dpkg-query -W -f='${Version}' sudo)
126
127 if chroot $rootfs dpkg --compare-versions $sudo_version gt "1.8.3p1-1"; then
128 groups="sudo"
129 else
130 groups="sudo admin"
131 fi
132
133 for group in $groups; do
134 chroot $rootfs groupadd --system $group >/dev/null 2>&1 || true
135 chroot $rootfs adduser ${user} $group >/dev/null 2>&1 || true
136 done
137
138 if [ -n "$auth_key" -a -f "$auth_key" ]; then
139 u_path="/home/${user}/.ssh"
140 root_u_path="$rootfs/$u_path"
141 mkdir -p $root_u_path
142 cp $auth_key "$root_u_path/authorized_keys"
143 chroot $rootfs chown -R ${user}: "$u_path"
144
145 echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
146 fi
147 return 0
148 }
149
150 #
151 # Choose proxies for container
152 # http_proxy will be used by debootstrap on the host.
153 # APT_PROXY will be used to set /etc/apt/apt.conf.d/70proxy in the container.
154 #
155 choose_container_proxy()
156 {
157 local rootfs=$1
158 local arch=$2
159
160 if [ -z "$HTTP_PROXY" ]; then
161 HTTP_PROXY="none"
162 fi
163 case "$HTTP_PROXY" in
164 none)
165 APT_PROXY=
166 ;;
167 apt)
168 RES=`apt-config shell APT_PROXY Acquire::http::Proxy`
169 eval $RES
170 [ -z "$APT_PROXY" ] || export http_proxy=$APT_PROXY
171 ;;
172 *)
173 APT_PROXY=$HTTP_PROXY
174 export http_proxy=$HTTP_PROXY
175 ;;
176 esac
177 }
178
179 write_sourceslist()
180 {
181 # $1 => path to the rootfs
182 # $2 => architecture we want to add
183 # $3 => whether to use the multi-arch syntax or not
184
185 if [ -n "$APT_PROXY" ]; then
186 mkdir -p $rootfs/etc/apt/apt.conf.d
187 cat > $rootfs/etc/apt/apt.conf.d/70proxy << EOF
188 Acquire::http::Proxy "$APT_PROXY" ;
189 EOF
190 fi
191
192 case $2 in
193 amd64|i386)
194 MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
195 SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
196 ;;
197 *)
198 MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
199 SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
200 ;;
201 esac
202 if [ -n "$3" ]; then
203 cat >> "$1/etc/apt/sources.list" << EOF
204 deb [arch=$2] $MIRROR ${release} main restricted universe multiverse
205 deb [arch=$2] $MIRROR ${release}-updates main restricted universe multiverse
206 deb [arch=$2] $SECURITY_MIRROR ${release}-security main restricted universe multiverse
207 EOF
208 else
209 cat >> "$1/etc/apt/sources.list" << EOF
210 deb $MIRROR ${release} main restricted universe multiverse
211 deb $MIRROR ${release}-updates main restricted universe multiverse
212 deb $SECURITY_MIRROR ${release}-security main restricted universe multiverse
213 EOF
214 fi
215 }
216
217 install_packages()
218 {
219 local rootfs="$1"
220 shift
221 local packages="$*"
222 if [ -z $update ]
223 then
224 chroot $rootfs apt-get update
225 update=true
226 fi
227 if [ -n "${packages}" ]
228 then
229 chroot $rootfs apt-get install --force-yes -y --no-install-recommends ${packages}
230 fi
231 }
232
233 cleanup()
234 {
235 rm -rf $cache/partial-$arch
236 rm -rf $cache/rootfs-$arch
237 }
238
239 suggest_flush()
240 {
241 echo "Container upgrade failed. The container cache may be out of date,"
242 echo "in which case flushing the case (see -F in the hep output) may help."
243 }
244
245 download_ubuntu()
246 {
247 cache=$1
248 arch=$2
249 release=$3
250
251 packages_template=${packages_template:-"ssh"}
252
253 # Try to guess a list of langpacks to install
254 langpacks="language-pack-en"
255
256 if which dpkg >/dev/null 2>&1; then
257 langpacks=`(echo $langpacks &&
258 dpkg -l | grep -E "^ii language-pack-[a-z]* " |
259 cut -d ' ' -f3) | sort -u`
260 fi
261 packages_template="${packages_template},$(echo $langpacks | sed 's/ /,/g')"
262
263
264 echo "Installing packages in template: ${packages_template}"
265
266 trap cleanup EXIT SIGHUP SIGINT SIGTERM
267 # check the mini ubuntu was not already downloaded
268 mkdir -p "$cache/partial-$arch"
269 if [ $? -ne 0 ]; then
270 echo "Failed to create '$cache/partial-$arch' directory"
271 return 1
272 fi
273
274 choose_container_proxy $cache/partial-$arch/ $arch
275 # download a mini ubuntu into a cache
276 echo "Downloading ubuntu $release minimal ..."
277 if [ -n "$(which qemu-debootstrap)" ]; then
278 qemu-debootstrap --verbose --components=main,universe --arch=$arch --include=${packages_template} $release $cache/partial-$arch $MIRROR
279 else
280 debootstrap --verbose --components=main,universe --arch=$arch --include=${packages_template} $release $cache/partial-$arch $MIRROR
281 fi
282
283 if [ $? -ne 0 ]; then
284 echo "Failed to download the rootfs, aborting."
285 return 1
286 fi
287
288 # Serge isn't sure whether we should avoid doing this when
289 # $release == `distro-info -d`
290 echo "Installing updates"
291 > $cache/partial-$arch/etc/apt/sources.list
292 write_sourceslist $cache/partial-$arch/ $arch
293
294 chroot "$1/partial-${arch}" apt-get update
295 if [ $? -ne 0 ]; then
296 echo "Failed to update the apt cache"
297 return 1
298 fi
299 cat > "$1/partial-${arch}"/usr/sbin/policy-rc.d << EOF
300 #!/bin/sh
301 exit 101
302 EOF
303 chmod +x "$1/partial-${arch}"/usr/sbin/policy-rc.d
304
305 lxc-unshare -s MOUNT -- chroot "$1/partial-${arch}" apt-get dist-upgrade -y || { suggest_flush; false; }
306 rm -f "$1/partial-${arch}"/usr/sbin/policy-rc.d
307
308 chroot "$1/partial-${arch}" apt-get clean
309
310 mv "$1/partial-$arch" "$1/rootfs-$arch"
311 trap EXIT
312 trap SIGINT
313 trap SIGTERM
314 trap SIGHUP
315 echo "Download complete"
316 return 0
317 }
318
319 copy_ubuntu()
320 {
321 cache=$1
322 arch=$2
323 rootfs=$3
324
325 # make a local copy of the miniubuntu
326 echo "Copying rootfs to $rootfs ..."
327 mkdir -p $rootfs
328 rsync -Ha $cache/rootfs-$arch/ $rootfs/ || return 1
329 return 0
330 }
331
332 install_ubuntu()
333 {
334 rootfs=$1
335 release=$2
336 flushcache=$3
337 cache="$LOCALSTATEDIR/cache/lxc/$release"
338 mkdir -p $LOCALSTATEDIR/lock/subsys/
339
340 (
341 flock -x 200
342 if [ $? -ne 0 ]; then
343 echo "Cache repository is busy."
344 return 1
345 fi
346
347
348 if [ $flushcache -eq 1 ]; then
349 echo "Flushing cache..."
350 rm -rf "$cache/partial-$arch"
351 rm -rf "$cache/rootfs-$arch"
352 fi
353
354 echo "Checking cache download in $cache/rootfs-$arch ... "
355 if [ ! -e "$cache/rootfs-$arch" ]; then
356 download_ubuntu $cache $arch $release
357 if [ $? -ne 0 ]; then
358 echo "Failed to download 'ubuntu $release base'"
359 return 1
360 fi
361 fi
362
363 echo "Copy $cache/rootfs-$arch to $rootfs ... "
364 copy_ubuntu $cache $arch $rootfs
365 if [ $? -ne 0 ]; then
366 echo "Failed to copy rootfs"
367 return 1
368 fi
369
370 return 0
371
372 ) 200>$LOCALSTATEDIR/lock/subsys/lxc-ubuntu
373
374 return $?
375 }
376
377 copy_configuration()
378 {
379 path=$1
380 rootfs=$2
381 name=$3
382 arch=$4
383 release=$5
384
385 if [ $arch = "i386" ]; then
386 arch="i686"
387 fi
388
389 # if there is exactly one veth network entry, make sure it has an
390 # associated hwaddr.
391 nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
392 if [ $nics -eq 1 ]; then
393 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
394 fi
395
396 # Generate the configuration file
397 ## Create the fstab (empty by default)
398 touch $path/fstab
399
400 ## Relocate all the network config entries
401 sed -i -e "/lxc.network/{w ${path}/config-network" -e "d}" $path/config
402
403 ## Relocate any other config entries
404 sed -i -e "/lxc./{w ${path}/config-auto" -e "d}" $path/config
405
406 ## Add all the includes
407 echo "" >> $path/config
408 echo "# Common configuration" >> $path/config
409 if [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu.common.conf" ]; then
410 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu.common.conf" >> $path/config
411 fi
412 if [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu.${release}.conf" ]; then
413 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu.${release}.conf" >> $path/config
414 fi
415
416 ## Add the container-specific config
417 echo "" >> $path/config
418 echo "# Container specific configuration" >> $path/config
419 [ -e "$path/config-auto" ] && cat $path/config-auto >> $path/config && rm $path/config-auto
420 grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
421 cat <<EOF >> $path/config
422 lxc.mount = $path/fstab
423 lxc.utsname = $name
424 lxc.arch = $arch
425 EOF
426
427 ## Re-add the previously removed network config
428 echo "" >> $path/config
429 echo "# Network configuration" >> $path/config
430 cat $path/config-network >> $path/config
431 rm $path/config-network
432
433 if [ $? -ne 0 ]; then
434 echo "Failed to add configuration"
435 return 1
436 fi
437
438 return 0
439 }
440
441 post_process()
442 {
443 rootfs=$1
444 release=$2
445 packages=$3
446
447 # Disable service startup
448 cat > $rootfs/usr/sbin/policy-rc.d << EOF
449 #!/bin/sh
450 exit 101
451 EOF
452 chmod +x $rootfs/usr/sbin/policy-rc.d
453
454 if [ ! -f $rootfs/etc/init/container-detect.conf ]; then
455 # Make sure we have a working resolv.conf
456 cresolvonf="${rootfs}/etc/resolv.conf"
457 mv $cresolvonf ${cresolvonf}.lxcbak
458 cat /etc/resolv.conf > ${cresolvonf}
459
460 # for lucid, if not trimming, then add the ubuntu-virt
461 # ppa and install lxcguest
462 if [ $release = "lucid" ]; then
463 chroot $rootfs apt-get update
464 install_packages $rootfs "python-software-properties"
465 chroot $rootfs add-apt-repository ppa:ubuntu-virt/ppa
466 fi
467
468 chroot $rootfs apt-get update
469 chroot $rootfs apt-get install --force-yes -y lxcguest
470
471 # Restore old resolv.conf
472 rm -f ${cresolvonf}
473 mv ${cresolvonf}.lxcbak ${cresolvonf}
474 fi
475
476 # If the container isn't running a native architecture, setup multiarch
477 if [ -x "$(ls -1 ${rootfs}/usr/bin/qemu-*-static 2>/dev/null)" ]; then
478 dpkg_version=$(chroot $rootfs dpkg-query -W -f='${Version}' dpkg)
479 if chroot $rootfs dpkg --compare-versions $dpkg_version ge "1.16.2"; then
480 chroot $rootfs dpkg --add-architecture ${hostarch}
481 else
482 mkdir -p ${rootfs}/etc/dpkg/dpkg.cfg.d
483 echo "foreign-architecture ${hostarch}" > ${rootfs}/etc/dpkg/dpkg.cfg.d/lxc-multiarch
484 fi
485
486 # Save existing value of MIRROR and SECURITY_MIRROR
487 DEFAULT_MIRROR=$MIRROR
488 DEFAULT_SECURITY_MIRROR=$SECURITY_MIRROR
489
490 # Write a new sources.list containing both native and multiarch entries
491 > ${rootfs}/etc/apt/sources.list
492 write_sourceslist $rootfs $arch "native"
493
494 MIRROR=$DEFAULT_MIRROR
495 SECURITY_MIRROR=$DEFAULT_SECURITY_MIRROR
496 write_sourceslist $rootfs $hostarch "multiarch"
497
498 # Finally update the lists and install upstart using the host architecture
499 HOST_PACKAGES="upstart:${hostarch} mountall:${hostarch} isc-dhcp-client:${hostarch}"
500 chroot $rootfs apt-get update
501 if chroot $rootfs dpkg -l iproute2 | grep -q ^ii; then
502 HOST_PACKAGES="$HOST_PACKAGES iproute2:${hostarch}"
503 else
504 HOST_PACKAGES="$HOST_PACKAGES iproute:${hostarch}"
505 fi
506 install_packages $rootfs $HOST_PACKAGES
507 fi
508
509 # Install Packages in container
510 if [ -n $packages ]
511 then
512 local packages="`echo $packages | sed 's/,/ /g'`"
513 echo "Installing packages: ${packages}"
514 install_packages $rootfs $packages
515 fi
516
517 # Set initial timezone as on host
518 if [ -f /etc/timezone ]; then
519 cat /etc/timezone > $rootfs/etc/timezone
520 chroot $rootfs dpkg-reconfigure -f noninteractive tzdata
521 elif [ -f /etc/sysconfig/clock ]; then
522 source /etc/sysconfig/clock
523 echo $ZONE > $rootfs/etc/timezone
524 chroot $rootfs dpkg-reconfigure -f noninteractive tzdata
525 else
526 echo "Timezone in container is not configured. Adjust it manually."
527 fi
528
529 # rmdir /dev/shm for containers that have /run/shm
530 # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
531 # get bind mounted to the host's /run/shm. So try to rmdir
532 # it, and in case that fails move it out of the way.
533 # NOTE: This can only be removed once 12.04 goes out of support
534 if [ ! -L $rootfs/dev/shm ] && [ -d $rootfs/run/shm ] && [ -e $rootfs/dev/shm ]; then
535 rmdir $rootfs/dev/shm 2>/dev/null || mv $rootfs/dev/shm $rootfs/dev/shm.bak
536 ln -s /run/shm $rootfs/dev/shm
537 fi
538
539 # Re-enable service startup
540 rm $rootfs/usr/sbin/policy-rc.d
541 }
542
543 do_bindhome()
544 {
545 rootfs=$1
546 user=$2
547
548 # copy /etc/passwd, /etc/shadow, and /etc/group entries into container
549 pwd=`getent passwd $user` || { echo "Failed to copy password entry for $user"; false; }
550 echo $pwd >> $rootfs/etc/passwd
551
552 # make sure user's shell exists in the container
553 shell=`echo $pwd | cut -d: -f 7`
554 if [ ! -x $rootfs/$shell ]; then
555 echo "shell $shell for user $user was not found in the container."
556 pkg=`dpkg -S $(readlink -m $shell) | cut -d ':' -f1`
557 echo "Installing $pkg"
558 install_packages $rootfs $pkg
559 fi
560
561 shad=`getent shadow $user`
562 echo "$shad" >> $rootfs/etc/shadow
563
564 # bind-mount the user's path into the container's /home
565 h=`getent passwd $user | cut -d: -f 6`
566 mkdir -p $rootfs/$h
567
568 # use relative path in container
569 h2=${h#/}
570 while [ ${h2:0:1} = "/" ]; do
571 h2=${h2#/}
572 done
573 echo "$h $h2 none bind 0 0" >> $path/fstab
574
575 # Make sure the group exists in container
576 grp=`echo $pwd | cut -d: -f 4` # group number for $user
577 grpe=`getent group $grp` || return 0 # if host doesn't define grp, ignore in container
578 chroot $rootfs getent group "$grpe" || echo "$grpe" >> $rootfs/etc/group
579 }
580
581 usage()
582 {
583 cat <<EOF
584 $1 -h|--help [-a|--arch] [-b|--bindhome <user>] [-d|--debug]
585 [-F | --flush-cache] [-r|--release <release>] [ -S | --auth-key <keyfile>]
586 [--rootfs <rootfs>] [--packages <packages>] [-u|--user <user>] [--password <password>]
587 release: the ubuntu release (e.g. precise): defaults to host release on ubuntu, otherwise uses latest LTS
588 bindhome: bind <user>'s home into the container
589 The ubuntu user will not be created, and <user> will have
590 sudo access.
591 arch: the container architecture (e.g. amd64): defaults to host arch
592 auth-key: SSH Public key file to inject into container
593 packages: list of packages to add comma separated
594 EOF
595 return 0
596 }
597
598 options=$(getopt -o a:b:hp:r:n:FS:du: -l arch:,bindhome:,help,path:,release:,name:,flush-cache,auth-key:,debug,rootfs:,packages:,user:,password:,mirror:,security-mirror: -- "$@")
599 if [ $? -ne 0 ]; then
600 usage $(basename $0)
601 exit 1
602 fi
603 eval set -- "$options"
604
605 release=precise # Default to the last Ubuntu LTS release for non-Ubuntu systems
606 if [ -f /etc/lsb-release ]; then
607 . /etc/lsb-release
608 if [ "$DISTRIB_ID" = "Ubuntu" ]; then
609 release=$DISTRIB_CODENAME
610 fi
611 fi
612
613 bindhome=
614
615 # Code taken from debootstrap
616 if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
617 arch=`/usr/bin/dpkg --print-architecture`
618 elif which udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
619 arch=`/usr/bin/udpkg --print-architecture`
620 else
621 arch=$(uname -m)
622 if [ "$arch" = "i686" ]; then
623 arch="i386"
624 elif [ "$arch" = "x86_64" ]; then
625 arch="amd64"
626 elif [ "$arch" = "armv7l" ]; then
627 arch="armhf"
628 fi
629 fi
630
631 debug=0
632 hostarch=$arch
633 flushcache=0
634 packages="vim,ssh"
635 user="ubuntu"
636 password="ubuntu"
637 while true
638 do
639 case "$1" in
640 -h|--help) usage $0 && exit 0;;
641 --rootfs) rootfs=$2; shift 2;;
642 -p|--path) path=$2; shift 2;;
643 -n|--name) name=$2; shift 2;;
644 -u|--user) user=$2; shift 2;;
645 --password) password=$2; shift 2;;
646 -F|--flush-cache) flushcache=1; shift 1;;
647 -r|--release) release=$2; shift 2;;
648 --packages) packages=$2; shift 2;;
649 -b|--bindhome) bindhome=$2; shift 2;;
650 -a|--arch) arch=$2; shift 2;;
651 -S|--auth-key) auth_key=$2; shift 2;;
652 -d|--debug) debug=1; shift 1;;
653 --mirror) MIRROR=$2; shift 2;;
654 --security-mirror) SECURITY_MIRROR=$2; shift 2;;
655 --) shift 1; break ;;
656 *) break ;;
657 esac
658 done
659
660 if [ $debug -eq 1 ]; then
661 set -x
662 fi
663
664 if [ -n "$bindhome" ]; then
665 pwd=`getent passwd $bindhome`
666 if [ $? -ne 0 ]; then
667 echo "Error: no password entry found for $bindhome"
668 exit 1
669 fi
670 fi
671
672
673 if [ "$arch" == "i686" ]; then
674 arch=i386
675 fi
676
677 if [ $hostarch = "i386" -a $arch = "amd64" ]; then
678 echo "can't create $arch container on $hostarch"
679 exit 1
680 fi
681
682 if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \
683 [ $arch != "armhf" -a $arch != "armel" ]; then
684 echo "can't create $arch container on $hostarch"
685 exit 1
686 fi
687
688 if [ $hostarch = "powerpc" -a $arch != "powerpc" ]; then
689 echo "can't create $arch container on $hostarch"
690 exit 1
691 fi
692
693 which debootstrap >/dev/null 2>&1 || { echo "'debootstrap' command is missing" >&2; false; }
694
695 if [ -z "$path" ]; then
696 echo "'path' parameter is required"
697 exit 1
698 fi
699
700 if [ "$(id -u)" != "0" ]; then
701 echo "This script should be run as 'root'"
702 exit 1
703 fi
704
705 # detect rootfs
706 config="$path/config"
707 # if $rootfs exists here, it was passed in with --rootfs
708 if [ -z "$rootfs" ]; then
709 if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
710 rootfs=$(awk -F= '/^lxc.rootfs =/{ print $2 }' $config)
711 else
712 rootfs=$path/rootfs
713 fi
714 fi
715
716 install_ubuntu $rootfs $release $flushcache
717 if [ $? -ne 0 ]; then
718 echo "failed to install ubuntu $release"
719 exit 1
720 fi
721
722 configure_ubuntu $rootfs $name $release $user $password
723 if [ $? -ne 0 ]; then
724 echo "failed to configure ubuntu $release for a container"
725 exit 1
726 fi
727
728 copy_configuration $path $rootfs $name $arch $release
729 if [ $? -ne 0 ]; then
730 echo "failed write configuration file"
731 exit 1
732 fi
733
734 post_process $rootfs $release $trim_container $packages
735
736 if [ -n "$bindhome" ]; then
737 do_bindhome $rootfs $bindhome
738 finalize_user $bindhome
739 else
740 finalize_user $user
741 fi
742
743 echo ""
744 echo "##"
745 if [ -n "$bindhome" ]; then
746 echo "# Log in as user $bindhome"
747 else
748 echo "# The default user is '$user' with password '$password'!"
749 echo "# Use the 'sudo' command to run tasks as root in the container."
750 fi
751 echo "##"
752 echo ""