]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-ubuntu.in
Move container creation fully into the api
[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 sys_time
382
383 # When using LXC with apparmor, uncomment the next line to run unconfined:
384 #lxc.aa_profile = unconfined
385 # To support container nesting on an Ubuntu host, uncomment next two lines:
386 #lxc.aa_profile = lxc-container-default-with-nesting
387 #lxc.hook.mount = /usr/share/lxc/hooks/mountcgroups
388
389 lxc.cgroup.devices.deny = a
390 # Allow any mknod (but not using the node)
391 lxc.cgroup.devices.allow = c *:* m
392 lxc.cgroup.devices.allow = b *:* m
393 # /dev/null and zero
394 lxc.cgroup.devices.allow = c 1:3 rwm
395 lxc.cgroup.devices.allow = c 1:5 rwm
396 # consoles
397 lxc.cgroup.devices.allow = c 5:1 rwm
398 lxc.cgroup.devices.allow = c 5:0 rwm
399 # /dev/{,u}random
400 lxc.cgroup.devices.allow = c 1:9 rwm
401 lxc.cgroup.devices.allow = c 1:8 rwm
402 lxc.cgroup.devices.allow = c 136:* rwm
403 lxc.cgroup.devices.allow = c 5:2 rwm
404 # rtc
405 lxc.cgroup.devices.allow = c 254:0 rm
406 # fuse
407 lxc.cgroup.devices.allow = c 10:229 rwm
408 # tun
409 lxc.cgroup.devices.allow = c 10:200 rwm
410 # full
411 lxc.cgroup.devices.allow = c 1:7 rwm
412 # hpet
413 lxc.cgroup.devices.allow = c 10:228 rwm
414 # kvm
415 lxc.cgroup.devices.allow = c 10:232 rwm
416 EOF
417
418 cat <<EOF > $path/fstab
419 proc proc proc nodev,noexec,nosuid 0 0
420 sysfs sys sysfs defaults 0 0
421 EOF
422
423 if [ $? -ne 0 ]; then
424 echo "Failed to add configuration"
425 return 1
426 fi
427
428 return 0
429 }
430
431 trim()
432 {
433 rootfs=$1
434 release=$2
435
436 # provide the lxc service
437 cat <<EOF > $rootfs/etc/init/lxc.conf
438 # fake some events needed for correct startup other services
439
440 description "Container Upstart"
441
442 start on startup
443
444 script
445 rm -rf /var/run/*.pid
446 rm -rf /var/run/network/*
447 /sbin/initctl emit stopped JOB=udevtrigger --no-wait
448 /sbin/initctl emit started JOB=udev --no-wait
449 end script
450 EOF
451
452 # fix buggus runlevel with sshd
453 cat <<EOF > $rootfs/etc/init/ssh.conf
454 # ssh - OpenBSD Secure Shell server
455 #
456 # The OpenSSH server provides secure shell access to the system.
457
458 description "OpenSSH server"
459
460 start on filesystem
461 stop on runlevel [!2345]
462
463 expect fork
464 respawn
465 respawn limit 10 5
466 umask 022
467 # replaces SSHD_OOM_ADJUST in /etc/default/ssh
468 oom never
469
470 pre-start script
471 test -x /usr/sbin/sshd || { stop; exit 0; }
472 test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; }
473 test -c /dev/null || { stop; exit 0; }
474
475 mkdir -p -m0755 /var/run/sshd
476 end script
477
478 # if you used to set SSHD_OPTS in /etc/default/ssh, you can change the
479 # 'exec' line here instead
480 exec /usr/sbin/sshd
481 EOF
482
483 cat <<EOF > $rootfs/etc/init/console.conf
484 # console - getty
485 #
486 # This service maintains a console on tty1 from the point the system is
487 # started until it is shut down again.
488
489 start on stopped rc RUNLEVEL=[2345]
490 stop on runlevel [!2345]
491
492 respawn
493 exec /sbin/getty -8 38400 /dev/console
494 EOF
495
496 cat <<EOF > $rootfs/lib/init/fstab
497 # /lib/init/fstab: cleared out for bare-bones lxc
498 EOF
499
500 # remove pointless services in a container
501 chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
502
503 chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
504 chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
505 chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
506 chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
507 chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
508
509 # if this isn't lucid, then we need to twiddle the network upstart bits :(
510 if [ $release != "lucid" ]; then
511 sed -i 's/^.*emission handled.*$/echo Emitting lo/' $rootfs/etc/network/if-up.d/upstart
512 fi
513 }
514
515 post_process()
516 {
517 rootfs=$1
518 release=$2
519 trim_container=$3
520
521 if [ $trim_container -eq 1 ]; then
522 trim $rootfs $release
523 elif [ ! -f $rootfs/etc/init/container-detect.conf ]; then
524 # Make sure we have a working resolv.conf
525 cresolvonf="${rootfs}/etc/resolv.conf"
526 mv $cresolvonf ${cresolvonf}.lxcbak
527 cat /etc/resolv.conf > ${cresolvonf}
528
529 # for lucid, if not trimming, then add the ubuntu-virt
530 # ppa and install lxcguest
531 if [ $release = "lucid" ]; then
532 chroot $rootfs apt-get update
533 chroot $rootfs apt-get install --force-yes -y python-software-properties
534 chroot $rootfs add-apt-repository ppa:ubuntu-virt/ppa
535 fi
536
537 chroot $rootfs apt-get update
538 chroot $rootfs apt-get install --force-yes -y lxcguest
539
540 # Restore old resolv.conf
541 rm -f ${cresolvonf}
542 mv ${cresolvonf}.lxcbak ${cresolvonf}
543 fi
544
545 # If the container isn't running a native architecture, setup multiarch
546 if [ -x "$(ls -1 ${rootfs}/usr/bin/qemu-*-static 2>/dev/null)" ]; then
547 dpkg_version=$(chroot $rootfs dpkg-query -W -f='${Version}' dpkg)
548 if chroot $rootfs dpkg --compare-versions $dpkg_version ge "1.16.2"; then
549 chroot $rootfs dpkg --add-architecture ${hostarch}
550 else
551 mkdir -p ${rootfs}/etc/dpkg/dpkg.cfg.d
552 echo "foreign-architecture ${hostarch}" > ${rootfs}/etc/dpkg/dpkg.cfg.d/lxc-multiarch
553 fi
554
555 # Save existing value of MIRROR and SECURITY_MIRROR
556 DEFAULT_MIRROR=$MIRROR
557 DEFAULT_SECURITY_MIRROR=$SECURITY_MIRROR
558
559 # Write a new sources.list containing both native and multiarch entries
560 > ${rootfs}/etc/apt/sources.list
561 write_sourceslist $rootfs $arch "native"
562
563 MIRROR=$DEFAULT_MIRROR
564 SECURITY_MIRROR=$DEFAULT_SECURITY_MIRROR
565 write_sourceslist $rootfs $hostarch "multiarch"
566
567 # Finally update the lists and install upstart using the host architecture
568 chroot $rootfs apt-get update
569 chroot $rootfs apt-get install --force-yes -y --no-install-recommends upstart:${hostarch} mountall:${hostarch} iproute:${hostarch} isc-dhcp-client:${hostarch}
570 fi
571
572 # rmdir /dev/shm for containers that have /run/shm
573 # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
574 # get bind mounted to the host's /run/shm. So try to rmdir
575 # it, and in case that fails move it out of the way.
576 if [ ! -L $rootfs/dev/shm ] && [ -d $rootfs/run/shm ] && [ -e $rootfs/dev/shm ]; then
577 mv $rootfs/dev/shm $rootfs/dev/shm.bak
578 ln -s /run/shm $rootfs/dev/shm
579 fi
580 }
581
582 do_bindhome()
583 {
584 rootfs=$1
585 user=$2
586
587 # copy /etc/passwd, /etc/shadow, and /etc/group entries into container
588 pwd=`getent passwd $user` || { echo "Failed to copy password entry for $user"; false; }
589 echo $pwd >> $rootfs/etc/passwd
590
591 # make sure user's shell exists in the container
592 shell=`echo $pwd | cut -d: -f 7`
593 if [ ! -x $rootfs/$shell ]; then
594 echo "shell $shell for user $user was not found in the container."
595 pkg=`dpkg -S $(readlink -m $shell) | cut -d ':' -f1`
596 echo "Installing $pkg"
597 chroot $rootfs apt-get --force-yes -y install $pkg
598 fi
599
600 shad=`getent shadow $user`
601 echo "$shad" >> $rootfs/etc/shadow
602
603 # bind-mount the user's path into the container's /home
604 h=`getent passwd $user | cut -d: -f 6`
605 mkdir -p $rootfs/$h
606
607 # use relative path in container
608 h2=${h#/}
609 while [ ${h2:0:1} = "/" ]; do
610 h2=${h2#/}
611 done
612 echo "$h $h2 none bind 0 0" >> $path/fstab
613
614 # Make sure the group exists in container
615 grp=`echo $pwd | cut -d: -f 4` # group number for $user
616 grpe=`getent group $grp` || return 0 # if host doesn't define grp, ignore in container
617 chroot $rootfs getent group "$grpe" || echo "$grpe" >> $rootfs/etc/group
618 }
619
620 usage()
621 {
622 cat <<EOF
623 $1 -h|--help [-a|--arch] [-b|--bindhome <user>] [--trim] [-d|--debug]
624 [-F | --flush-cache] [-r|--release <release>] [ -S | --auth-key <keyfile>]
625 [--rootfs <rootfs>]
626 release: the ubuntu release (e.g. precise): defaults to host release on ubuntu, otherwise uses latest LTS
627 trim: make a minimal (faster, but not upgrade-safe) container
628 bindhome: bind <user>'s home into the container
629 The ubuntu user will not be created, and <user> will have
630 sudo access.
631 arch: the container architecture (e.g. amd64): defaults to host arch
632 auth-key: SSH Public key file to inject into container
633 EOF
634 return 0
635 }
636
637 options=$(getopt -o a:b:hp:r:xn:FS:d -l arch:,bindhome:,help,path:,release:,trim,name:,flush-cache,auth-key:,debug,rootfs: -- "$@")
638 if [ $? -ne 0 ]; then
639 usage $(basename $0)
640 exit 1
641 fi
642 eval set -- "$options"
643
644 release=precise # Default to the last Ubuntu LTS release for non-Ubuntu systems
645 if [ -f /etc/lsb-release ]; then
646 . /etc/lsb-release
647 if [ "$DISTRIB_ID" = "Ubuntu" ]; then
648 release=$DISTRIB_CODENAME
649 fi
650 fi
651
652 bindhome=
653
654 # Code taken from debootstrap
655 if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
656 arch=`/usr/bin/dpkg --print-architecture`
657 elif which udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
658 arch=`/usr/bin/udpkg --print-architecture`
659 else
660 arch=$(uname -m)
661 if [ "$arch" = "i686" ]; then
662 arch="i386"
663 elif [ "$arch" = "x86_64" ]; then
664 arch="amd64"
665 elif [ "$arch" = "armv7l" ]; then
666 arch="armhf"
667 fi
668 fi
669
670 debug=0
671 trim_container=0
672 hostarch=$arch
673 flushcache=0
674 while true
675 do
676 case "$1" in
677 -h|--help) usage $0 && exit 0;;
678 --rootfs) rootfs=$2; shift 2;;
679 -p|--path) path=$2; shift 2;;
680 -n|--name) name=$2; shift 2;;
681 -F|--flush-cache) flushcache=1; shift 1;;
682 -r|--release) release=$2; shift 2;;
683 -b|--bindhome) bindhome=$2; shift 2;;
684 -a|--arch) arch=$2; shift 2;;
685 -x|--trim) trim_container=1; shift 1;;
686 -S|--auth-key) auth_key=$2; shift 2;;
687 -d|--debug) debug=1; shift 1;;
688 --) shift 1; break ;;
689 *) break ;;
690 esac
691 done
692
693 if [ $debug -eq 1 ]; then
694 set -x
695 fi
696
697 if [ -n "$bindhome" ]; then
698 pwd=`getent passwd $bindhome`
699 if [ $? -ne 0 ]; then
700 echo "Error: no password entry found for $bindhome"
701 exit 1
702 fi
703 fi
704
705
706 if [ "$arch" == "i686" ]; then
707 arch=i386
708 fi
709
710 if [ $hostarch = "i386" -a $arch = "amd64" ]; then
711 echo "can't create $arch container on $hostarch"
712 exit 1
713 fi
714
715 if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \
716 [ $arch != "armhf" -a $arch != "armel" ]; then
717 echo "can't create $arch container on $hostarch"
718 exit 1
719 fi
720
721 if [ $hostarch = "powerpc" -a $arch != "powerpc" ]; then
722 echo "can't create $arch container on $hostarch"
723 exit 1
724 fi
725
726 which debootstrap >/dev/null 2>&1 || { echo "'debootstrap' command is missing" >&2; false; }
727
728 if [ -z "$path" ]; then
729 echo "'path' parameter is required"
730 exit 1
731 fi
732
733 if [ "$(id -u)" != "0" ]; then
734 echo "This script should be run as 'root'"
735 exit 1
736 fi
737
738 # detect rootfs
739 config="$path/config"
740 # if $rootfs exists here, it was passed in with --rootfs
741 if [ -z "$rootfs" ]; then
742 if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
743 rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
744 else
745 rootfs=$path/rootfs
746 fi
747 fi
748
749 install_ubuntu $rootfs $release $flushcache
750 if [ $? -ne 0 ]; then
751 echo "failed to install ubuntu $release"
752 exit 1
753 fi
754
755 configure_ubuntu $rootfs $name $release
756 if [ $? -ne 0 ]; then
757 echo "failed to configure ubuntu $release for a container"
758 exit 1
759 fi
760
761 copy_configuration $path $rootfs $name $arch $release
762 if [ $? -ne 0 ]; then
763 echo "failed write configuration file"
764 exit 1
765 fi
766
767 post_process $rootfs $release $trim_container
768
769 if [ -n "$bindhome" ]; then
770 do_bindhome $rootfs $bindhome
771 finalize_user $bindhome
772 else
773 finalize_user ubuntu
774 fi
775
776 echo ""
777 echo "##"
778 if [ -n "$bindhome" ]; then
779 echo "# Log in as user $bindhome"
780 else
781 echo "# The default user is 'ubuntu' with password 'ubuntu'!"
782 echo "# Use the 'sudo' command to run tasks as root in the container."
783 fi
784 echo "##"
785 echo ""