4 # template script for generating ubuntu container for LXC
6 # This script consolidates and extends the existing lxc ubuntu scripts
9 # Copyright © 2011 Serge Hallyn <serge.hallyn@canonical.com>
10 # Copyright © 2010 Wilhelm Meier
11 # Author: Wilhelm Meier <wilhelm.meier@fh-kl.de>
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.
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.
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
27 # Detect use under userns (unsupported)
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
37 # Make sure the usual locations are in PATH
38 export PATH
=$PATH:/usr
/sbin
:/usr
/bin
:/sbin
:/bin
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"}
47 if [ -r /etc
/default
/lxc
]; then
51 # Check if given path is in a btrfs partition
54 [ -e $1 -a $
(stat
-f -c '%T' $1) = "btrfs" ]
57 # Check if given path is the root of a btrfs subvolume
60 [ -d $1 -a $
(stat
-f -c '%T' $1) = "btrfs" -a $
(stat
-c '%i' $1) -eq 256 ]
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
78 [ -d $path ] ||
return 0
79 if which btrfs
>/dev
/null
2>&1 && is_btrfs_subvolume
$path; then
80 btrfs subvolume delete
$path
94 # configure the network using the dhcp
95 if chroot
$rootfs which netplan
>/dev
/null
2>&1; then
96 cat <<EOF > $rootfs/etc/netplan/10-lxc.yaml
103 cat <<EOF > $rootfs/etc/network/interfaces
104 # This file describes the network interfaces available on your system
105 # and how to activate them. For more information, see interfaces(5).
107 # The loopback network interface
109 iface lo inet loopback
117 cat <<EOF > $rootfs/etc/hostname
121 cat <<EOF > $rootfs/etc/hosts
125 # The following lines are desirable for IPv6 capable hosts
126 ::1 ip6-localhost ip6-loopback
128 ff00::0 ip6-mcastprefix
130 ff02::2 ip6-allrouters
133 if [ ! -f $rootfs/etc
/init
/container-detect.conf
]; then
134 # suppress log level output for udev
135 sed -i "s/=\"err\"/=0/" $rootfs/etc
/udev
/udev.conf
137 # remove jobs for consoles 5 and 6 since we only create 4 consoles in
139 rm -f $rootfs/etc
/init
/tty
{5,6}.conf
142 if [ -z "$bindhome" ]; then
143 chroot
$rootfs useradd
--create-home -s /bin
/bash
$user
144 echo "$user:$password" | chroot
$rootfs chpasswd
147 # make sure we have the current locale defined in the container
148 if [ -z "$LANG" ] ||
echo $LANG |
grep -E -q "^C(\..+)*$"; then
149 chroot
$rootfs locale-gen en_US.UTF-8 || true
150 chroot
$rootfs update-locale LANG
=en_US.UTF-8 || true
152 chroot
$rootfs locale-gen
$LANG || true
153 chroot
$rootfs update-locale LANG
=$LANG || true
156 # generate new SSH keys
157 if [ -x $rootfs/var
/lib
/dpkg
/info
/openssh-server.postinst
]; then
158 cat > $rootfs/usr
/sbin
/policy-rc.d
<< EOF
162 chmod +x
$rootfs/usr
/sbin
/policy-rc.d
164 if [ -f "$rootfs/etc/init/ssh.conf" ]; then
165 mv "$rootfs/etc/init/ssh.conf" "$rootfs/etc/init/ssh.conf.disabled"
168 rm -f $rootfs/etc
/ssh
/ssh_host_
*key
*
170 DPKG_MAINTSCRIPT_PACKAGE
=openssh DPKG_MAINTSCRIPT_NAME
=postinst chroot
$rootfs /var
/lib
/dpkg
/info
/openssh-server.postinst configure
172 sed -i "s/root@$(hostname)/root@$hostname/g" $rootfs/etc
/ssh
/ssh_host_
*.pub
174 if [ -f "$rootfs/etc/init/ssh.conf.disabled" ]; then
175 mv "$rootfs/etc/init/ssh.conf.disabled" "$rootfs/etc/init/ssh.conf"
178 rm -f $rootfs/usr
/sbin
/policy-rc.d
184 # finish setting up the user in the container by injecting ssh key and
185 # adding sudo group membership.
186 # passed-in user is either 'ubuntu' or the user to bind in from host.
191 sudo_version
=$
(chroot
$rootfs dpkg-query
-W -f='${Version}' sudo
)
193 if chroot
$rootfs dpkg
--compare-versions $sudo_version gt
"1.8.3p1-1"; then
199 for group
in $groups; do
200 chroot
$rootfs groupadd
--system $group >/dev
/null
2>&1 || true
201 chroot
$rootfs adduser
${user} $group >/dev
/null
2>&1 || true
204 if [ -n "$auth_key" -a -f "$auth_key" ]; then
205 u_path
="/home/${user}/.ssh"
206 root_u_path
="$rootfs/$u_path"
207 mkdir
-p $root_u_path
208 cp $auth_key "$root_u_path/authorized_keys"
209 chroot
$rootfs chown
-R ${user}: "$u_path"
211 echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
216 # A function to try and autodetect squid-deb-proxy servers on the local network
217 # if either the squid-deb-proxy-client package is installed on the host or
218 # a parent container set the 50squid-deb-proxy-client file.
219 squid_deb_proxy_autodetect
()
221 local apt_discover
=/usr
/share
/squid-deb-proxy-client
/apt-avahi-discover
222 local proxy_file
=/etc
/apt
/apt.conf.d
/50squid-deb-proxy-client
223 squid_proxy_line
= # That's a global :/
225 # Maybe the host is aware of a squid-deb-proxy?
226 if [ -f $apt_discover ]; then
227 echo -n "Discovering squid-deb-proxy..."
228 squid_proxy_line
=$
($apt_discover)
229 if [ -n "$squid_proxy_line" ]; then
230 echo "found squid-deb-proxy: $squid_proxy_line"
232 echo "no squid-deb-proxy found"
236 # Are we in a nested container, and the parent already knows of a proxy?
237 if [ -f $proxy_file ]; then
238 # Extract the squid URL from the file (whatever is between "")
239 squid_proxy_line
=`cat $proxy_file | sed "s/.*\"\(.*\)\".*/\1/"`
244 # Choose proxies for container
245 # http_proxy will be used by debootstrap on the host.
246 # APT_PROXY will be used to set /etc/apt/apt.conf.d/70proxy in the container.
248 choose_container_proxy
()
253 if [ -z "$HTTP_PROXY" ]; then
256 case "$HTTP_PROXY" in
258 squid_deb_proxy_autodetect
259 if [ -n "$squid_proxy_line" ]; then
260 APT_PROXY
=$squid_proxy_line
261 export http_proxy
=$squid_proxy_line
267 RES
=`apt-config shell APT_PROXY Acquire::http::Proxy`
269 [ -z "$APT_PROXY" ] ||
export http_proxy
=$APT_PROXY
272 APT_PROXY
=$HTTP_PROXY
273 export http_proxy
=$HTTP_PROXY
280 # $1 => path to the partial cache or the rootfs
281 # $2 => architecture we want to add
282 # $3 => whether to use the multi-arch syntax or not
284 if [ -n "$APT_PROXY" ]; then
285 mkdir
-p $1/etc
/apt
/apt.conf.d
286 cat > $1/etc
/apt
/apt.conf.d
/70proxy
<< EOF
287 Acquire::http::Proxy "$APT_PROXY" ;
293 MIRROR
=${MIRROR:-http://archive.ubuntu.com/ubuntu}
294 SECURITY_MIRROR
=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
297 MIRROR
=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
298 SECURITY_MIRROR
=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
302 cat >> "$1/etc/apt/sources.list" << EOF
303 deb [arch=$2] $MIRROR ${release} main restricted universe multiverse
304 deb [arch=$2] $MIRROR ${release}-updates main restricted universe multiverse
305 deb [arch=$2] $SECURITY_MIRROR ${release}-security main restricted universe multiverse
308 cat >> "$1/etc/apt/sources.list" << EOF
309 deb $MIRROR ${release} main restricted universe multiverse
310 deb $MIRROR ${release}-updates main restricted universe multiverse
311 deb $SECURITY_MIRROR ${release}-security main restricted universe multiverse
323 chroot
$rootfs apt-get update
326 if [ -n "${packages}" ]
328 chroot
$rootfs apt-get
install --force-yes -y --no-install-recommends ${packages}
334 try_rmsubvolume
$cache/partial-
$arch
335 try_rmsubvolume
$cache/rootfs-
$arch
340 echo "Container upgrade failed. The container cache may be out of date,"
341 echo "in which case flushing the cache (see -F in the help output) may help."
352 MIRROR
=${MIRROR:-http://archive.ubuntu.com/ubuntu}
353 SECURITY_MIRROR
=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
356 MIRROR
=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
357 SECURITY_MIRROR
=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
361 packages_template
=${packages_template:-"apt-transport-https,ssh,vim"}
362 debootstrap_parameters
=
364 # Try to guess a list of langpacks to install
365 langpacks
="language-pack-en"
367 if which dpkg
>/dev
/null
2>&1; then
368 langpacks
=`(echo $langpacks &&
369 dpkg -l | grep -E "^ii language-pack-[a-z]* " |
370 cut -d ' ' -f3) | sort -u`
372 packages_template
="${packages_template},$(echo $langpacks | sed 's/ /,/g')"
374 if [ -n "$variant" ]; then
375 debootstrap_parameters
="$debootstrap_parameters --variant=$variant"
377 if [ "$variant" = 'minbase' ]; then
378 packages_template
="${packages_template},sudo"
379 # Newer releases use netplan, EOL releases not supported
382 packages_template
="${packages_template},ifupdown,isc-dhcp-client"
387 echo "Installing packages in template: ${packages_template}"
389 trap cleanup EXIT SIGHUP SIGINT SIGTERM
390 # check the mini ubuntu was not already downloaded
391 try_mksubvolume
"$cache/partial-$arch"
392 if [ $?
-ne 0 ]; then
393 echo "Failed to create '$cache/partial-$arch' directory"
397 choose_container_proxy
$cache/partial-
$arch/ $arch
398 # download a mini ubuntu into a cache
399 echo "Downloading ubuntu $release minimal ..."
400 if [ -n "$(which qemu-debootstrap)" ]; then
401 qemu-debootstrap
--verbose $debootstrap_parameters --components=main
,universe
--arch=$arch --include=${packages_template} $release $cache/partial-
$arch $MIRROR
403 debootstrap
--verbose $debootstrap_parameters --components=main
,universe
--arch=$arch --include=${packages_template} $release $cache/partial-
$arch $MIRROR
406 if [ $?
-ne 0 ]; then
407 echo "Failed to download the rootfs, aborting."
411 # Serge isn't sure whether we should avoid doing this when
412 # $release == `distro-info -d`
413 echo "Installing updates"
414 > $cache/partial-
$arch/etc
/apt
/sources.list
415 write_sourceslist
$cache/partial-
$arch/ $arch
417 chroot
"$1/partial-${arch}" apt-get update
418 if [ $?
-ne 0 ]; then
419 echo "Failed to update the apt cache"
422 cat > "$1/partial-${arch}"/usr
/sbin
/policy-rc.d
<< EOF
426 chmod +x
"$1/partial-${arch}"/usr
/sbin
/policy-rc.d
430 mount -t proc proc "${1}/partial-${arch}/proc"
431 chroot "${1}/partial-${arch}" apt-get dist-upgrade -y
433 ) | lxc-unshare
-s MOUNT
-- sh
-eu ||
(suggest_flush
; false
)
435 rm -f "$1/partial-${arch}"/usr
/sbin
/policy-rc.d
437 chroot
"$1/partial-${arch}" apt-get clean
439 mv "$1/partial-$arch" "$1/rootfs-$arch"
444 echo "Download complete"
454 # make a local copy of the miniubuntu
455 echo "Copying rootfs to $rootfs ..."
456 try_mksubvolume
$rootfs
457 if which btrfs
>/dev
/null
2>&1 && is_btrfs_subvolume
$cache/rootfs-
$arch && is_btrfs_subvolume
$rootfs; then
458 realrootfs
=$
(dirname $config)/rootfs
459 [ "$rootfs" = "$realrootfs" ] || umount
$rootfs ||
return 1
460 btrfs subvolume delete
$realrootfs ||
return 1
461 btrfs subvolume snapshot
$cache/rootfs-
$arch $realrootfs ||
return 1
462 [ "$rootfs" = "$realrootfs" ] || mount
--bind $realrootfs $rootfs ||
return 1
464 rsync
-SHaAX $cache/rootfs-
$arch/ $rootfs/ ||
return 1
475 mkdir
-p $LOCALSTATEDIR/lock
/subsys
/
479 if [ $?
-ne 0 ]; then
480 echo "Cache repository is busy."
485 if [ $flushcache -eq 1 ]; then
486 echo "Flushing cache..."
487 try_rmsubvolume
$cache/partial-
$arch
488 try_rmsubvolume
$cache/rootfs-
$arch
491 echo "Checking cache download in $cache/rootfs-$arch ... "
492 if [ ! -e "$cache/rootfs-$arch" ]; then
493 download_ubuntu
$cache $arch $release
494 if [ $?
-ne 0 ]; then
495 echo "Failed to download 'ubuntu $release base'"
500 echo "Copy $cache/rootfs-$arch to $rootfs ... "
501 copy_ubuntu
$cache $arch $rootfs
502 if [ $?
-ne 0 ]; then
503 echo "Failed to copy rootfs"
509 ) 9>$LOCALSTATEDIR/lock
/subsys
/lxc-ubuntu
$release
522 if [ $arch = "i386" ]; then
526 # if there is exactly one veth network entry, make sure it has an
528 nics
=`grep -e '^lxc\.net\.0\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
529 if [ $nics -eq 1 ]; then
530 grep -q "^lxc.net.0.hwaddr" $path/config ||
sed -i -e "/^lxc\.net\.0\.type[ \t]*=[ \t]*veth/a lxc.net.0.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')" $path/config
533 # Generate the configuration file
534 ## Relocate all the network config entries
535 sed -i -e "/lxc.net.0/{w ${path}/config-network" -e "d}" $path/config
537 ## Relocate any other config entries
538 sed -i -e "/lxc./{w ${path}/config-auto" -e "d}" $path/config
540 ## Add all the includes
541 echo "" >> $path/config
542 echo "# Common configuration" >> $path/config
543 if [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu.common.conf" ]; then
544 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu.common.conf" >> $path/config
546 if [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu.${release}.conf" ]; then
547 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu.${release}.conf" >> $path/config
550 ## Add the container-specific config
551 echo "" >> $path/config
552 echo "# Container specific configuration" >> $path/config
553 [ -e "$path/config-auto" ] && cat $path/config-auto
>> $path/config
&& rm $path/config-auto
554 grep -q "^lxc.rootfs.path" $path/config
2>/dev
/null ||
echo "lxc.rootfs.path = $rootfs" >> $path/config
555 cat <<EOF >> $path/config
560 ## Re-add the previously removed network config
561 echo "" >> $path/config
562 echo "# Network configuration" >> $path/config
563 cat $path/config-network
>> $path/config
564 rm $path/config-network
566 if [ $?
-ne 0 ]; then
567 echo "Failed to add configuration"
580 # Disable service startup
581 cat > $rootfs/usr
/sbin
/policy-rc.d
<< EOF
585 chmod +x
$rootfs/usr
/sbin
/policy-rc.d
587 # If the container isn't running a native architecture, setup multiarch
588 if [ -x "$(ls -1 ${rootfs}/usr/bin/qemu-*-static 2>/dev/null)" ]; then
589 dpkg_version
=$
(chroot
$rootfs dpkg-query
-W -f='${Version}' dpkg
)
590 if chroot
$rootfs dpkg
--compare-versions $dpkg_version ge
"1.16.2"; then
591 chroot
$rootfs dpkg
--add-architecture ${hostarch}
593 mkdir
-p ${rootfs}/etc
/dpkg
/dpkg.cfg.d
594 echo "foreign-architecture ${hostarch}" > ${rootfs}/etc
/dpkg
/dpkg.cfg.d
/lxc-multiarch
597 # Save existing value of MIRROR and SECURITY_MIRROR
598 DEFAULT_MIRROR
=$MIRROR
599 DEFAULT_SECURITY_MIRROR
=$SECURITY_MIRROR
601 # Write a new sources.list containing both native and multiarch entries
602 > ${rootfs}/etc
/apt
/sources.list
603 write_sourceslist
$rootfs $arch "native"
605 MIRROR
=$DEFAULT_MIRROR
606 SECURITY_MIRROR
=$DEFAULT_SECURITY_MIRROR
607 write_sourceslist
$rootfs $hostarch "multiarch"
609 # Finally update the lists and install upstart using the host architecture
610 HOST_PACKAGES
="upstart:${hostarch} mountall:${hostarch} isc-dhcp-client:${hostarch}"
611 chroot
$rootfs apt-get update
612 if chroot
$rootfs dpkg
-l iproute2 |
grep -q ^ii
; then
613 HOST_PACKAGES
="$HOST_PACKAGES iproute2:${hostarch}"
615 HOST_PACKAGES
="$HOST_PACKAGES iproute:${hostarch}"
617 install_packages
$rootfs $HOST_PACKAGES
620 # Install Packages in container
621 if [ -n "$packages" ]
623 local packages
="`echo $packages | sed 's/,/ /g'`"
624 echo "Installing packages: ${packages}"
625 install_packages
$rootfs $packages
628 # Set initial timezone as on host
629 if [ -f /etc
/timezone
]; then
630 cat /etc
/timezone
> $rootfs/etc
/timezone
631 chroot
$rootfs dpkg-reconfigure
-f noninteractive tzdata
632 elif [ -f /etc
/sysconfig
/clock ]; then
633 .
/etc
/sysconfig
/clock
634 echo $ZONE > $rootfs/etc
/timezone
635 chroot
$rootfs dpkg-reconfigure
-f noninteractive tzdata
637 echo "Timezone in container is not configured. Adjust it manually."
640 # rmdir /dev/shm for containers that have /run/shm
641 # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
642 # get bind mounted to the host's /run/shm. So try to rmdir
643 # it, and in case that fails move it out of the way.
644 # NOTE: This can only be removed once 12.04 goes out of support
645 if [ ! -L $rootfs/dev
/shm
] && [ -e $rootfs/dev
/shm
]; then
646 rmdir $rootfs/dev
/shm
2>/dev
/null ||
mv $rootfs/dev
/shm
$rootfs/dev
/shm.bak
647 ln -s /run
/shm
$rootfs/dev
/shm
650 # Re-enable service startup
651 rm $rootfs/usr
/sbin
/policy-rc.d
659 # copy /etc/passwd, /etc/shadow, and /etc/group entries into container
660 pwd=`getent passwd $user` ||
{ echo "Failed to copy password entry for $user"; false
; }
661 echo $pwd >> $rootfs/etc
/passwd
663 # make sure user's shell exists in the container
664 shell
=`echo $pwd | cut -d: -f 7`
665 if [ ! -x $rootfs/$shell ]; then
666 echo "shell $shell for user $user was not found in the container."
667 pkg
=`dpkg -S $(readlink -m $shell) | cut -d ':' -f1`
668 echo "Installing $pkg"
669 install_packages
$rootfs $pkg
672 shad
=`getent shadow $user`
673 echo "$shad" >> $rootfs/etc
/shadow
675 # bind-mount the user's path into the container's /home
676 h
=`getent passwd $user | cut -d: -f 6`
679 # use relative path in container
681 while [ ${h2:0:1} = "/" ]; do
684 echo "lxc.mount.entry = $h $h2 none bind 0 0" >> $path/config
686 # Make sure the group exists in container
687 grp
=`echo $pwd | cut -d: -f 4` # group number for $user
688 grpe
=`getent group $grp` ||
return 0 # if host doesn't define grp, ignore in container
689 chroot
$rootfs getent group
"$grpe" ||
echo "$grpe" >> $rootfs/etc
/group
695 $1 -h|--help [-a|--arch] [-b|--bindhome <user>] [-d|--debug]
696 [-F | --flush-cache] [-r|--release <release>] [-v|--variant] [ -S | --auth-key <keyfile>]
697 [--rootfs <rootfs>] [--packages <packages>] [-u|--user <user>] [--password <password>]
698 [--mirror <url>] [--security-mirror <url>]
699 release: the ubuntu release (e.g. xenial): defaults to host release on ubuntu, otherwise uses latest LTS
700 variant: debootstrap variant to use (see debootstrap(8))
701 bindhome: bind <user>'s home into the container
702 The ubuntu user will not be created, and <user> will have
704 arch: the container architecture (e.g. amd64): defaults to host arch
705 auth-key: SSH Public key file to inject into container
706 packages: list of packages to add comma separated
707 mirror,security-mirror: mirror for download and /etc/apt/sources.list
712 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
: -- "$@")
713 if [ $?
-ne 0 ]; then
717 eval set -- "$options"
719 release
=xenial
# Default to the last Ubuntu LTS release for non-Ubuntu systems
720 if [ -f /etc
/lsb-release
]; then
722 if [ "$DISTRIB_ID" = "Ubuntu" ]; then
723 release
=$DISTRIB_CODENAME
729 # Code taken from debootstrap
730 if [ -x /usr
/bin
/dpkg
] && /usr
/bin
/dpkg
--print-architecture >/dev
/null
2>&1; then
731 arch
=`/usr/bin/dpkg --print-architecture`
732 elif which udpkg
>/dev
/null
2>&1 && udpkg
--print-architecture >/dev
/null
2>&1; then
733 arch
=`/usr/bin/udpkg --print-architecture`
736 if [ "$arch" = "i686" ]; then
738 elif [ "$arch" = "x86_64" ]; then
740 elif [ "$arch" = "armv7l" ]; then
742 elif [ "$arch" = "aarch64" ]; then
744 elif [ "$arch" = "ppc64le" ]; then
759 -h|
--help) usage
$0 && exit 0;;
760 --rootfs) rootfs
=$2; shift 2;;
761 -p|
--path) path
=$2; shift 2;;
762 -n|
--name) name
=$2; shift 2;;
763 -u|
--user) user
=$2; shift 2;;
764 --password) password
=$2; shift 2;;
765 -F|
--flush-cache) flushcache
=1; shift 1;;
766 -r|
--release) release
=$2; shift 2;;
767 -v|
--variant) variant
=$2; shift 2;;
768 --packages) packages
=$2; shift 2;;
769 -b|
--bindhome) bindhome
=$2; shift 2;;
770 -a|
--arch) arch
=$2; shift 2;;
771 -S|
--auth-key) auth_key
=$2; shift 2;;
772 -d|
--debug) debug
=1; shift 1;;
773 --mirror) MIRROR
=$2; shift 2;;
774 --security-mirror) SECURITY_MIRROR
=$2; shift 2;;
775 --) shift 1; break ;;
780 if [ $debug -eq 1 ]; then
784 if [ -n "$bindhome" ]; then
785 pwd=`getent passwd $bindhome`
786 if [ $?
-ne 0 ]; then
787 echo "Error: no password entry found for $bindhome"
793 if [ "$arch" = "i686" ]; then
797 if [ $hostarch = "i386" -a $arch = "amd64" ]; then
798 echo "can't create $arch container on $hostarch"
802 if [ $hostarch = "armhf" -o $hostarch = "armel" -o $hostarch = "arm64" ] && \
803 [ $arch != "armhf" -a $arch != "armel" -a $arch != "arm64" ]; then
804 echo "can't create $arch container on $hostarch"
808 if [ $arch = "arm64" ] && [ $hostarch != "arm64" ]; then
809 echo "can't create $arch container on $hostarch"
813 if [ $hostarch = "powerpc" -a $arch != "powerpc" ]; then
814 echo "can't create $arch container on $hostarch"
818 which debootstrap
>/dev
/null
2>&1 ||
{ echo "'debootstrap' command is missing" >&2; false
; }
820 if [ -z "$path" ]; then
821 echo "'path' parameter is required"
825 if [ "$(id -u)" != "0" ]; then
826 echo "This script should be run as 'root'"
831 config
="$path/config"
832 # if $rootfs exists here, it was passed in with --rootfs
833 if [ -z "$rootfs" ]; then
834 if grep -q '^lxc.rootfs.path' $config 2>/dev
/null
; then
835 rootfs
=$
(awk -F= '/^lxc.rootfs.path =/{ print $2 }' $config)
841 install_ubuntu
$rootfs $release $flushcache $LXC_CACHE_PATH
842 if [ $?
-ne 0 ]; then
843 echo "failed to install ubuntu $release"
847 configure_ubuntu
$rootfs $name $release $user $password
848 if [ $?
-ne 0 ]; then
849 echo "failed to configure ubuntu $release for a container"
853 copy_configuration
$path $rootfs $name $arch $release
854 if [ $?
-ne 0 ]; then
855 echo "failed write configuration file"
859 post_process
$rootfs $release $trim_container $packages
861 if [ -n "$bindhome" ]; then
862 do_bindhome
$rootfs $bindhome
863 finalize_user
$bindhome
870 if [ -n "$bindhome" ]; then
871 echo "# Log in as user $bindhome"
873 echo "# The default user is '$user' with password '$password'!"
874 echo "# Use the 'sudo' command to run tasks as root in the container."