4 # lxc: linux Container library
7 # Daniel Lezcano <daniel.lezcano@free.fr>
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 # Detect use under userns (unsupported)
25 [ "$arg" = "--" ] && break
26 if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
27 echo "This template can't be used for unprivileged containers." 1>&2
28 echo "You may want to try the \"download\" template instead." 1>&2
33 # Make sure the usual locations are in PATH
34 export PATH
=$PATH:/usr
/sbin
:/usr
/bin
:/sbin
:/bin
36 MIRROR
=${MIRROR:-http://http.debian.net/debian}
37 SECURITY_MIRROR
=${SECURITY_MIRROR:-http://security.debian.org/}
38 LOCALSTATEDIR
="@LOCALSTATEDIR@"
39 LXC_TEMPLATE_CONFIG
="@LXCTEMPLATECONFIG@"
46 # squeeze only has /dev/tty and /dev/tty0 by default,
47 # therefore creating missing device nodes for tty1-4.
48 for tty
in $
(seq 1 4); do
49 if [ ! -e $rootfs/dev
/tty
$tty ]; then
50 mknod
$rootfs/dev
/tty
$tty c
4 $tty
54 # configure the inittab
55 cat <<EOF > $rootfs/etc/inittab
57 si::sysinit:/etc/init.d/rcS
58 l0:0:wait:/etc/init.d/rc 0
59 l1:1:wait:/etc/init.d/rc 1
60 l2:2:wait:/etc/init.d/rc 2
61 l3:3:wait:/etc/init.d/rc 3
62 l4:4:wait:/etc/init.d/rc 4
63 l5:5:wait:/etc/init.d/rc 5
64 l6:6:wait:/etc/init.d/rc 6
65 # Normally not reached, but fallthrough in case of emergency.
66 z6:6:respawn:/sbin/sulogin
67 1:2345:respawn:/sbin/getty 38400 console
68 c1:12345:respawn:/sbin/getty 38400 tty1 linux
69 c2:12345:respawn:/sbin/getty 38400 tty2 linux
70 c3:12345:respawn:/sbin/getty 38400 tty3 linux
71 c4:12345:respawn:/sbin/getty 38400 tty4 linux
72 p6::ctrlaltdel:/sbin/init 6
73 p0::powerfail:/sbin/init 0
77 [ -e "$rootfs/etc/mtab" ] && rm $rootfs/etc
/mtab
78 ln -s /proc
/self
/mounts
$rootfs/etc
/mtab
80 # disable selinux in debian
81 mkdir
-p $rootfs/selinux
82 echo 0 > $rootfs/selinux
/enforce
84 # configure the network using the dhcp
85 cat <<EOF > $rootfs/etc/network/interfaces
87 iface lo inet loopback
94 cat <<EOF > $rootfs/etc/hostname
98 # reconfigure some services
99 if [ -z "$LANG" ]; then
100 chroot
$rootfs locale-gen en_US.UTF-8 UTF-8
101 chroot
$rootfs update-locale LANG
=en_US.UTF-8
103 encoding
=$
(echo $LANG | cut
-d.
-f2)
104 chroot
$rootfs sed -e "s/^# \(${LANG} ${encoding}\)/\1/" \
105 -i /etc
/locale.gen
2> /dev
/null
106 chroot
$rootfs locale-gen
$LANG $encoding
107 chroot
$rootfs update-locale LANG
=$LANG
110 # remove pointless services in a container
111 chroot
$rootfs /usr
/sbin
/update-rc.d
-f checkroot.sh disable
112 chroot
$rootfs /usr
/sbin
/update-rc.d
-f umountfs disable
113 chroot
$rootfs /usr
/sbin
/update-rc.d
-f hwclock.sh disable
114 chroot
$rootfs /usr
/sbin
/update-rc.d
-f hwclockfirst.sh disable
116 # generate new SSH keys
117 if [ -x $rootfs/var
/lib
/dpkg
/info
/openssh-server.postinst
]; then
118 cat > $rootfs/usr
/sbin
/policy-rc.d
<< EOF
122 chmod +x
$rootfs/usr
/sbin
/policy-rc.d
124 if [ -f $rootfs/etc
/init
/ssh.conf
]; then
125 mv $rootfs/etc
/init
/ssh.conf
$rootfs/etc
/init
/ssh.conf.disabled
128 rm -f $rootfs/etc
/ssh
/ssh_host_
*key
*
130 DPKG_MAINTSCRIPT_PACKAGE
=openssh DPKG_MAINTSCRIPT_NAME
=postinst chroot
$rootfs /var
/lib
/dpkg
/info
/openssh-server.postinst configure
131 sed -i "s/root@$(hostname)/root@$hostname/g" $rootfs/etc
/ssh
/ssh_host_
*.pub
133 if [ -f "$rootfs/etc/init/ssh.conf.disabled" ]; then
134 mv $rootfs/etc
/init
/ssh.conf.disabled
$rootfs/etc
/init
/ssh.conf
137 rm -f $rootfs/usr
/sbin
/policy-rc.d
140 # set initial timezone as on host
141 if [ -f /etc
/timezone
]; then
142 cat /etc
/timezone
> $rootfs/etc
/timezone
143 chroot
$rootfs dpkg-reconfigure
-f noninteractive tzdata
144 elif [ -f /etc
/sysconfig
/clock ]; then
145 .
/etc
/sysconfig
/clock
146 echo $ZONE > $rootfs/etc
/timezone
147 chroot
$rootfs dpkg-reconfigure
-f noninteractive tzdata
149 echo "Timezone in container is not configured. Adjust it manually."
152 echo "root:root" | chroot
$rootfs chpasswd
153 echo "Root password is 'root', please change !"
160 local rootfs
="$1"; shift
161 local release
="$1"; shift
162 local arch
="$1"; shift
165 if [ -n "${arch}" ]; then
166 prefix
="deb [arch=${arch}]"
169 cat >> "${rootfs}/etc/apt/sources.list" << EOF
170 ${prefix} $MIRROR ${release} main contrib non-free
171 ${prefix} $SECURITY_MIRROR ${release}/updates main contrib non-free
177 local rootfs
="$1"; shift
180 chroot
${rootfs} apt-get update
181 if [ -n "${packages}" ]; then
182 chroot
${rootfs} apt-get
install --force-yes -y --no-install-recommends ${packages}
186 configure_debian_systemd
()
191 init
="$(chroot ${rootfs} dpkg-query --search /sbin/init | cut -d : -f 1)"
192 if [ "$init" = "systemd-sysv" ]; then
193 # only appropriate when systemd is PID 1
194 echo 'lxc.autodev = 1' >> "$path/config"
195 echo 'lxc.kmsg = 0' >> "$path/config"
198 # this only works if we have getty@.service to manipulate
199 if [ -f ${rootfs}/lib
/systemd
/system
/getty\@.service
]; then
200 sed -e 's/^ConditionPathExists=/# ConditionPathExists=/' \
201 -e 's/After=dev-%i.device/After=/' \
202 < ${rootfs}/lib
/systemd
/system
/getty\@.service \
203 > ${rootfs}/etc
/systemd
/system
/getty\@.service
206 # just in case systemd is not installed
207 mkdir
-p ${rootfs}/{lib
,etc
}/systemd
/system
208 mkdir
-p ${rootfs}/etc
/systemd
/system
/getty.target.wants
210 # This function has been copied and adapted from lxc-fedora
211 rm -f ${rootfs}/etc
/systemd
/system
/default.target
212 touch ${rootfs}/etc
/fstab
213 chroot
${rootfs} ln -s /dev
/null
/etc
/systemd
/system
/udev.service
214 chroot
${rootfs} ln -s /dev
/null
/etc
/systemd
/system
/systemd-udevd.service
215 chroot
${rootfs} ln -s /lib
/systemd
/system
/multi-user.target
/etc
/systemd
/system
/default.target
216 # Make systemd honor SIGPWR
217 chroot
${rootfs} ln -s /lib
/systemd
/system
/halt.target
/etc
/systemd
/system
/sigpwr.target
218 # Setup getty service on the 4 ttys we are going to allow in the
219 # default config. Number should match lxc.tty
220 ( cd ${rootfs}/etc
/systemd
/system
/getty.target.wants
221 for i
in 1 2 3 4 ; do ln -sf ..
/getty\@.service getty@tty
${i}.service
; done )
228 rm -rf $cache/partial-
$release-$arch
229 rm -rf $cache/rootfs-
$release-$arch
249 trap cleanup EXIT SIGHUP SIGINT SIGTERM
250 # check the mini debian was not already downloaded
251 mkdir
-p "$cache/partial-$release-$arch"
252 if [ $?
-ne 0 ]; then
253 echo "Failed to create '$cache/partial-$release-$arch' directory"
257 # download a mini debian into a cache
258 echo "Downloading debian minimal ..."
259 debootstrap
--verbose --variant=minbase
--arch=$arch \
260 --include=$packages \
261 "$release" "$cache/partial-$release-$arch" $MIRROR
262 if [ $?
-ne 0 ]; then
263 echo "Failed to download the rootfs, aborting."
267 mv "$1/partial-$release-$arch" "$1/rootfs-$release-$arch"
268 echo "Download complete."
284 # make a local copy of the minidebian
285 echo -n "Copying rootfs to $rootfs..."
287 rsync
-Ha "$cache/rootfs-$release-$arch"/ $rootfs/ ||
return 1
293 cache
="$LOCALSTATEDIR/cache/lxc/debian"
297 mkdir
-p $LOCALSTATEDIR/lock
/subsys
/
300 if [ $?
-ne 0 ]; then
301 echo "Cache repository is busy."
305 echo "Checking cache download in $cache/rootfs-$release-$arch ... "
306 if [ ! -e "$cache/rootfs-$release-$arch" ]; then
307 download_debian
$cache $arch $release
308 if [ $?
-ne 0 ]; then
309 echo "Failed to download 'debian base'"
314 copy_debian
$cache $arch $rootfs $release
315 if [ $?
-ne 0 ]; then
316 echo "Failed to copy rootfs"
322 ) 9>$LOCALSTATEDIR/lock
/subsys
/lxc-debian
334 # Generate the configuration file
335 ## Create the fstab (empty by default)
338 # if there is exactly one veth network entry, make sure it has an
340 nics
=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
341 if [ $nics -eq 1 ]; then
342 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
345 ## Add all the includes
346 echo "" >> $path/config
347 echo "# Common configuration" >> $path/config
348 if [ -e "${LXC_TEMPLATE_CONFIG}/debian.common.conf" ]; then
349 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/debian.common.conf" >> $path/config
351 if [ -e "${LXC_TEMPLATE_CONFIG}/debian.${release}.conf" ]; then
352 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/debian.${release}.conf" >> $path/config
355 ## Add the container-specific config
356 echo "" >> $path/config
357 echo "# Container specific configuration" >> $path/config
358 grep -q "^lxc.rootfs" $path/config
2> /dev
/null ||
echo "lxc.rootfs = $rootfs" >> $path/config
360 cat <<EOF >> $path/config
361 lxc.mount = $path/fstab
362 lxc.utsname = $hostname
366 if [ $?
-ne 0 ]; then
367 echo "Failed to add configuration"
376 local rootfs
="$1"; shift
377 local release
="$1"; shift
378 local arch
="$1"; shift
379 local hostarch
="$1"; shift
382 # Disable service startup
383 cat > ${rootfs}/usr
/sbin
/policy-rc.d
<< EOF
387 chmod +x
${rootfs}/usr
/sbin
/policy-rc.d
389 # If the container isn't running a native architecture, setup multiarch
390 if [ "${arch}" != "${hostarch}" ]; then
391 mkdir
-p ${rootfs}/etc
/dpkg
/dpkg.cfg.d
392 echo "foreign-architecture ${hostarch}" > ${rootfs}/etc
/dpkg
/dpkg.cfg.d
/lxc-multiarch
395 # Write a new sources.list containing both native and multiarch entries
396 > ${rootfs}/etc
/apt
/sources.list
397 if [ "${arch}" = "${hostarch}" ]; then
398 write_sourceslist
${rootfs} ${release} ${arch}
400 write_sourceslist
${rootfs} ${release}
403 # Install Packages in container
404 if [ -n "${packages}" ]; then
405 local pack_list
="`echo ${packages} | sed 's/,/ /g'`"
406 echo "Installing packages: ${pack_list}"
407 install_packages
${rootfs} ${pack_list}
410 # Re-enable service startup
411 rm ${rootfs}/usr
/sbin
/policy-rc.d
416 cache
="$LOCALSTATEDIR/cache/lxc/debian"
418 if [ ! -e $cache ]; then
422 # lock, so we won't purge while someone is creating a repository
426 echo "Cache repository is busy."
430 echo -n "Purging the download cache..."
431 rm --preserve-root --one-file-system -rf $cache && echo "Done." ||
exit 1
434 ) 9>$LOCALSTATEDIR/lock
/subsys
/lxc-debian
440 $1 -h|--help -p|--path=<path> [-a|--arch] [-c|--clean] [--mirror=<mirror>] [-r|--release=<release>] [--security-mirror=<security mirror>]
441 arch: the container architecture (e.g. amd64): defaults to host arch
442 release: the debian release (e.g. wheezy): defaults to current stable
443 mirror: debain mirror to use during installation
444 security mirror: debain mirror to use for security updates
445 packages: list of packages to add comma separated
450 options
=$
(getopt
-o hp
:n
:a
:r
:c
-l arch
:,clean
,help,mirror
:,name
:,packages
:,path
:,release
:,rootfs
:,security-mirror
: -- "$@")
451 if [ $?
-ne 0 ]; then
455 eval set -- "$options"
457 if which dpkg
> /dev
/null
2>&1 ; then
458 arch
=$
(dpkg
--print-architecture)
461 if [ "$arch" = "i686" ]; then
463 elif [ "$arch" = "x86_64" ]; then
465 elif [ "$arch" = "armv7l" ]; then
474 -h|
--help) usage
$0 && exit 1;;
475 --) shift 1; break ;;
477 -a|
--arch) arch
=$2; shift 2;;
478 -c|
--clean) clean
=$2; shift 1;;
479 --mirror) MIRROR
=$2; shift 2;;
480 -n|
--name) name
=$2; shift 2;;
481 --packages) packages
=$2; shift 2;;
482 -p|
--path) path
=$2; shift 2;;
483 -r|
--release) release
=$2; shift 2;;
484 --rootfs) rootfs
=$2; shift 2;;
485 --security-mirror) SECURITY_MIRROR
=$2; shift 2;;
490 if [ ! -z "$clean" -a -z "$path" ]; then
495 if [ "$arch" = "i686" ]; then
499 if [ "$arch" = "x86_64" ]; then
503 if [ $hostarch = "i386" -a $arch = "amd64" ]; then
504 echo "can't create $arch container on $hostarch"
508 if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \
509 [ $arch != "armhf" -a $arch != "armel" ]; then
510 echo "can't create $arch container on $hostarch"
514 if [ $hostarch = "powerpc" -a $arch != "powerpc" ]; then
515 echo "can't create $arch container on $hostarch"
520 if [ $?
-ne 0 ]; then
521 echo "'debootstrap' command is missing"
525 if [ -z "$path" ]; then
526 echo "'path' parameter is required"
530 if [ "$(id -u)" != "0" ]; then
531 echo "This script should be run as 'root'"
535 current_release
=`wget ${MIRROR}/dists/stable/Release -O - 2> /dev/null | head |awk '/^Codename: (.*)$/ { print $2; }'`
536 release
=${release:-${current_release}}
537 valid_releases
=('squeeze' 'wheezy' 'jessie' 'sid')
538 if [[ ! "${valid_releases[*]}" =~
(^|
[^
[:alpha
:]])$release([^
[:alpha
:]]|$
) ]]; then
539 echo "Invalid release ${release}, valid ones are: ${valid_releases[*]}"
544 config
="$path/config"
545 if [ -z "$rootfs" ]; then
546 if grep -q '^lxc.rootfs' $config 2> /dev
/null
; then
547 rootfs
=$
(awk -F= '/^lxc.rootfs =/{ print $2 }' $config)
553 install_debian
$rootfs $release $arch
554 if [ $?
-ne 0 ]; then
555 echo "failed to install debian"
559 configure_debian
$rootfs $name
560 if [ $?
-ne 0 ]; then
561 echo "failed to configure debian for a container"
565 copy_configuration
$path $rootfs $name $arch
566 if [ $?
-ne 0 ]; then
567 echo "failed write configuration file"
571 configure_debian_systemd
$path $rootfs
573 post_process
${rootfs} ${release} ${arch} ${hostarch} ${packages}
575 if [ ! -z $clean ]; then