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