]> git.proxmox.com Git - mirror_lxc.git/blame - templates/lxc-debian.in
lxc-debian: support systemd as PID 1
[mirror_lxc.git] / templates / lxc-debian.in
CommitLineData
bad69158 1#!/bin/bash
7afc269d 2
fb7460fe
DL
3#
4# lxc: linux Container library
06388011 5
fb7460fe
DL
6# Authors:
7# Daniel Lezcano <daniel.lezcano@free.fr>
8
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.
13
14# This library is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
14d9c0f0 16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
fb7460fe 17# Lesser General Public License for more details.
06388011 18
fb7460fe
DL
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
250b1eec 21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
06388011 22
8ec981fc 23# Detect use under userns (unsupported)
c63c04fc 24for arg in "$@"; do
96283b54
SG
25 [ "$arg" = "--" ] && break
26 if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
8ec981fc
SG
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
29 exit 1
30 fi
31done
32
207bf0e4
SG
33# Make sure the usual locations are in PATH
34export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
35
5d20559c
G
36MIRROR=${MIRROR:-http://http.debian.net/debian}
37SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.debian.org/}
00fe5e1d
SG
38LOCALSTATEDIR="@LOCALSTATEDIR@"
39LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
2a7c16dc 40
fb7460fe
DL
41configure_debian()
42{
43 rootfs=$1
44 hostname=$2
45
4e0eb765
DB
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
14d9c0f0
SG
49 if [ ! -e $rootfs/dev/tty$tty ]; then
50 mknod $rootfs/dev/tty$tty c 4 $tty
51 fi
4e0eb765
DB
52 done
53
fb7460fe
DL
54 # configure the inittab
55 cat <<EOF > $rootfs/etc/inittab
06388011 56id:3:initdefault:
57si::sysinit:/etc/init.d/rcS
58l0:0:wait:/etc/init.d/rc 0
59l1:1:wait:/etc/init.d/rc 1
60l2:2:wait:/etc/init.d/rc 2
61l3:3:wait:/etc/init.d/rc 3
62l4:4:wait:/etc/init.d/rc 4
63l5:5:wait:/etc/init.d/rc 5
64l6:6:wait:/etc/init.d/rc 6
65# Normally not reached, but fallthrough in case of emergency.
66z6:6:respawn:/sbin/sulogin
671:2345:respawn:/sbin/getty 38400 console
b0a33c1e 68c1:12345:respawn:/sbin/getty 38400 tty1 linux
69c2:12345:respawn:/sbin/getty 38400 tty2 linux
70c3:12345:respawn:/sbin/getty 38400 tty3 linux
71c4:12345:respawn:/sbin/getty 38400 tty4 linux
f79d43bb 72p6::ctrlaltdel:/sbin/init 6
6bf8daf9 73p0::powerfail:/sbin/init 0
06388011 74EOF
06388011 75
23e88083
HA
76 # symlink mtab
77 [ -e "$rootfs/etc/mtab" ] && rm $rootfs/etc/mtab
78 ln -s /proc/self/mounts $rootfs/etc/mtab
79
fb7460fe
DL
80 # disable selinux in debian
81 mkdir -p $rootfs/selinux
82 echo 0 > $rootfs/selinux/enforce
06388011 83
fb7460fe
DL
84 # configure the network using the dhcp
85 cat <<EOF > $rootfs/etc/network/interfaces
86auto lo
87iface lo inet loopback
06388011 88
fb7460fe
DL
89auto eth0
90iface eth0 inet dhcp
06388011 91EOF
1846e71a 92
fb7460fe
DL
93 # set the hostname
94 cat <<EOF > $rootfs/etc/hostname
95$hostname
96EOF
06388011 97
fb7460fe 98 # reconfigure some services
f1fa1a08 99 if [ -z "$LANG" ]; then
14d9c0f0
SG
100 chroot $rootfs locale-gen en_US.UTF-8 UTF-8
101 chroot $rootfs update-locale LANG=en_US.UTF-8
f1fa1a08 102 else
90ccc878 103 encoding=$(echo $LANG | cut -d. -f2)
09da66e6 104 chroot $rootfs sed -e "s/^# \(${LANG} ${encoding}\)/\1/" \
cd44154c 105 -i /etc/locale.gen 2> /dev/null
90ccc878 106 chroot $rootfs locale-gen $LANG $encoding
14d9c0f0 107 chroot $rootfs update-locale LANG=$LANG
f1fa1a08 108 fi
06388011 109
fb7460fe 110 # remove pointless services in a container
7593bdfb 111 chroot $rootfs /usr/sbin/update-rc.d -f checkroot.sh remove
fb7460fe
DL
112 chroot $rootfs /usr/sbin/update-rc.d -f umountfs remove
113 chroot $rootfs /usr/sbin/update-rc.d -f hwclock.sh remove
114 chroot $rootfs /usr/sbin/update-rc.d -f hwclockfirst.sh remove
19d618b1 115
ce68d5b4
SG
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
119#!/bin/sh
120exit 101
121EOF
122 chmod +x $rootfs/usr/sbin/policy-rc.d
123
124 if [ -f $rootfs/etc/init/ssh.conf ]; then
125 mv $rootfs/etc/init/ssh.conf $rootfs/etc/init/ssh.conf.disabled
126 fi
127
128 rm -f $rootfs/etc/ssh/ssh_host_*key*
129
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
132
133 if [ -f "$rootfs/etc/init/ssh.conf.disabled" ]; then
134 mv $rootfs/etc/init/ssh.conf.disabled $rootfs/etc/init/ssh.conf
135 fi
136
137 rm -f $rootfs/usr/sbin/policy-rc.d
138 fi
139
f7365a24 140 # set initial timezone as on host
141 if [ -f /etc/timezone ]; then
0a3673e8 142 cat /etc/timezone > $rootfs/etc/timezone
f7365a24 143 chroot $rootfs dpkg-reconfigure -f noninteractive tzdata
144 elif [ -f /etc/sysconfig/clock ]; then
17abf278 145 . /etc/sysconfig/clock
f7365a24 146 echo $ZONE > $rootfs/etc/timezone
147 chroot $rootfs dpkg-reconfigure -f noninteractive tzdata
148 else
149 echo "Timezone in container is not configured. Adjust it manually."
150 fi
151
19d618b1
DL
152 echo "root:root" | chroot $rootfs chpasswd
153 echo "Root password is 'root', please change !"
154
155 return 0
06388011 156}
157
177f2cd2
AD
158write_sourceslist()
159{
160 local rootfs="$1"; shift
161 local release="$1"; shift
162 local arch="$1"; shift
163
164 local prefix="deb"
165 if [ -n "${arch}" ]; then
166 prefix="deb [arch=${arch}]"
167 fi
168
169 cat >> "${rootfs}/etc/apt/sources.list" << EOF
5d20559c
G
170${prefix} $MIRROR ${release} main contrib non-free
171${prefix} $SECURITY_MIRROR ${release}/updates main contrib non-free
177f2cd2
AD
172EOF
173}
174
175install_packages()
176{
177 local rootfs="$1"; shift
178 local packages="$*"
179
180 chroot ${rootfs} apt-get update
181 if [ -n "${packages}" ]; then
182 chroot ${rootfs} apt-get install --force-yes -y --no-install-recommends ${packages}
183 fi
184}
185
a9bf60ba
AT
186configure_debian_systemd()
187{
188 path=$1
189 rootfs=$2
190 init="$(chroot ${rootfs} dpkg-query --search /sbin/init | cut -d : -f 1)"
191 if [ "$init" != "systemd-sysv" ]; then
192 # systemd is not PID 1
193 return
194 fi
195
196 echo 'lxc.autodev = 1' >> "$path/config"
197 echo 'lxc.kmsg = 0' >> "$path/config"
198
199 # This function has been copied and adapted from lxc-fedora
200 rm -f ${rootfs}/etc/systemd/system/default.target
201 touch ${rootfs}/etc/fstab
202 chroot ${rootfs} ln -s /dev/null /etc/systemd/system/udev.service
203 chroot ${rootfs} ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
204 # Make systemd honor SIGPWR
205 chroot ${rootfs} ln -s /lib/systemd/system/halt.target /etc/systemd/system/sigpwr.target
206 sed -e 's/^ConditionPathExists=/# ConditionPathExists=/' \
207 -e 's/After=dev-%i.device/After=/' \
208 < ${rootfs}/lib/systemd/system/getty\@.service \
209 > ${rootfs}/etc/systemd/system/getty\@.service
210 # Setup getty service on the 4 ttys we are going to allow in the
211 # default config. Number should match lxc.tty
212 ( cd ${rootfs}/etc/systemd/system/getty.target.wants
213 for i in 1 2 3 4 ; do ln -sf ../getty\@.service getty@tty${i}.service; done )
214
215 return 0
216}
217
f1ccde27
SH
218cleanup()
219{
b269b8ad
LV
220 rm -rf $cache/partial-$release-$arch
221 rm -rf $cache/rootfs-$release-$arch
f1ccde27
SH
222}
223
fb7460fe
DL
224download_debian()
225{
226 packages=\
227ifupdown,\
228locales,\
229libui-dialog-perl,\
230dialog,\
06a1e1db 231isc-dhcp-client,\
fb7460fe
DL
232netbase,\
233net-tools,\
234iproute,\
235openssh-server
236
237 cache=$1
238 arch=$2
b269b8ad 239 release=$3
fb7460fe 240
f1ccde27 241 trap cleanup EXIT SIGHUP SIGINT SIGTERM
fb7460fe 242 # check the mini debian was not already downloaded
b269b8ad 243 mkdir -p "$cache/partial-$release-$arch"
fb7460fe 244 if [ $? -ne 0 ]; then
b269b8ad 245 echo "Failed to create '$cache/partial-$release-$arch' directory"
14d9c0f0 246 return 1
81f6a40a
RT
247 fi
248
fb7460fe
DL
249 # download a mini debian into a cache
250 echo "Downloading debian minimal ..."
251 debootstrap --verbose --variant=minbase --arch=$arch \
14d9c0f0 252 --include=$packages \
b269b8ad 253 "$release" "$cache/partial-$release-$arch" $MIRROR
fb7460fe 254 if [ $? -ne 0 ]; then
14d9c0f0
SG
255 echo "Failed to download the rootfs, aborting."
256 return 1
c952d1b9 257 fi
cd830f33 258
b269b8ad 259 mv "$1/partial-$release-$arch" "$1/rootfs-$release-$arch"
fb7460fe 260 echo "Download complete."
f1ccde27
SH
261 trap EXIT
262 trap SIGINT
263 trap SIGTERM
264 trap SIGHUP
cd830f33 265
fb7460fe 266 return 0
c952d1b9 267}
bad69158 268
fb7460fe 269copy_debian()
c952d1b9 270{
fb7460fe
DL
271 cache=$1
272 arch=$2
273 rootfs=$3
b269b8ad 274 release=$4
bad69158 275
fb7460fe
DL
276 # make a local copy of the minidebian
277 echo -n "Copying rootfs to $rootfs..."
6d8ac56b 278 mkdir -p $rootfs
b269b8ad 279 rsync -Ha "$cache/rootfs-$release-$arch"/ $rootfs/ || return 1
fb7460fe 280 return 0
c952d1b9 281}
282
fb7460fe
DL
283install_debian()
284{
00fe5e1d 285 cache="$LOCALSTATEDIR/cache/lxc/debian"
fb7460fe 286 rootfs=$1
b269b8ad 287 release=$2
1927a6be 288 arch=$3
00fe5e1d 289 mkdir -p $LOCALSTATEDIR/lock/subsys/
fb7460fe 290 (
17abf278 291 flock -x 9
14d9c0f0
SG
292 if [ $? -ne 0 ]; then
293 echo "Cache repository is busy."
294 return 1
295 fi
7afc269d 296
b269b8ad
LV
297 echo "Checking cache download in $cache/rootfs-$release-$arch ... "
298 if [ ! -e "$cache/rootfs-$release-$arch" ]; then
299 download_debian $cache $arch $release
14d9c0f0
SG
300 if [ $? -ne 0 ]; then
301 echo "Failed to download 'debian base'"
302 return 1
303 fi
304 fi
c952d1b9 305
b269b8ad 306 copy_debian $cache $arch $rootfs $release
14d9c0f0
SG
307 if [ $? -ne 0 ]; then
308 echo "Failed to copy rootfs"
309 return 1
310 fi
c952d1b9 311
14d9c0f0 312 return 0
c952d1b9 313
00fe5e1d 314 ) 9>$LOCALSTATEDIR/lock/subsys/lxc-debian
85cbaa06 315
fb7460fe 316 return $?
bad69158 317}
318
fb7460fe
DL
319copy_configuration()
320{
321 path=$1
322 rootfs=$2
16501521 323 hostname=$3
1927a6be 324 arch=$4
bad69158 325
00fe5e1d
SG
326 # Generate the configuration file
327 ## Create the fstab (empty by default)
328 touch $path/fstab
329
aea1cd3c
SG
330 # if there is exactly one veth network entry, make sure it has an
331 # associated hwaddr.
332 nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
333 if [ $nics -eq 1 ]; then
334 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
335 fi
336
00fe5e1d
SG
337 ## Add all the includes
338 echo "" >> $path/config
339 echo "# Common configuration" >> $path/config
340 if [ -e "${LXC_TEMPLATE_CONFIG}/debian.common.conf" ]; then
341 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/debian.common.conf" >> $path/config
342 fi
343 if [ -e "${LXC_TEMPLATE_CONFIG}/debian.${release}.conf" ]; then
344 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/debian.${release}.conf" >> $path/config
345 fi
346
347 ## Add the container-specific config
348 echo "" >> $path/config
349 echo "# Container specific configuration" >> $path/config
cd44154c 350 grep -q "^lxc.rootfs" $path/config 2> /dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
00fe5e1d 351
fb7460fe 352 cat <<EOF >> $path/config
00fe5e1d 353lxc.mount = $path/fstab
16501521 354lxc.utsname = $hostname
00fe5e1d 355lxc.arch = $arch
fb7460fe 356EOF
bad69158 357
fb7460fe 358 if [ $? -ne 0 ]; then
14d9c0f0
SG
359 echo "Failed to add configuration"
360 return 1
bad69158 361 fi
362
363 return 0
364}
365
177f2cd2
AD
366post_process()
367{
368 local rootfs="$1"; shift
369 local release="$1"; shift
370 local arch="$1"; shift
371 local hostarch="$1"; shift
372 local packages="$*"
373
374 # Disable service startup
375 cat > ${rootfs}/usr/sbin/policy-rc.d << EOF
376#!/bin/sh
377exit 101
378EOF
379 chmod +x ${rootfs}/usr/sbin/policy-rc.d
380
381 # If the container isn't running a native architecture, setup multiarch
382 if [ "${arch}" != "${hostarch}" ]; then
383 mkdir -p ${rootfs}/etc/dpkg/dpkg.cfg.d
384 echo "foreign-architecture ${hostarch}" > ${rootfs}/etc/dpkg/dpkg.cfg.d/lxc-multiarch
385 fi
386
387 # Write a new sources.list containing both native and multiarch entries
388 > ${rootfs}/etc/apt/sources.list
389 if [ "${arch}" = "${hostarch}" ]; then
390 write_sourceslist ${rootfs} ${release} ${arch}
391 else
392 write_sourceslist ${rootfs} ${release}
393 fi
394
395 # Install Packages in container
396 if [ -n "${packages}" ]; then
397 local pack_list="`echo ${packages} | sed 's/,/ /g'`"
398 echo "Installing packages: ${pack_list}"
399 install_packages ${rootfs} ${pack_list}
400 fi
401
402 # Re-enable service startup
403 rm ${rootfs}/usr/sbin/policy-rc.d
404}
405
fb7460fe
DL
406clean()
407{
00fe5e1d 408 cache="$LOCALSTATEDIR/cache/lxc/debian"
bad69158 409
fb7460fe 410 if [ ! -e $cache ]; then
14d9c0f0 411 exit 0
bad69158 412 fi
413
414 # lock, so we won't purge while someone is creating a repository
415 (
17abf278 416 flock -x 9
14d9c0f0
SG
417 if [ $? != 0 ]; then
418 echo "Cache repository is busy."
419 exit 1
420 fi
bad69158 421
14d9c0f0
SG
422 echo -n "Purging the download cache..."
423 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
424 exit 0
bad69158 425
00fe5e1d 426 ) 9>$LOCALSTATEDIR/lock/subsys/lxc-debian
bad69158 427}
428
fb7460fe
DL
429usage()
430{
431 cat <<EOF
177f2cd2 432$1 -h|--help -p|--path=<path> [-a|--arch] [-c|--clean] [--mirror=<mirror>] [-r|--release=<release>] [--security-mirror=<security mirror>]
1927a6be 433arch: the container architecture (e.g. amd64): defaults to host arch
177f2cd2
AD
434release: the debian release (e.g. wheezy): defaults to current stable
435mirror: debain mirror to use during installation
436security mirror: debain mirror to use for security updates
437packages: list of packages to add comma separated
fb7460fe
DL
438EOF
439 return 0
440}
441
177f2cd2 442options=$(getopt -o hp:n:a:r:c -l arch:,clean,help,mirror:,name:,packages:,path:,release:,rootfs:,security-mirror: -- "$@")
fb7460fe
DL
443if [ $? -ne 0 ]; then
444 usage $(basename $0)
14d9c0f0 445 exit 1
fb7460fe
DL
446fi
447eval set -- "$options"
448
cd44154c 449if which dpkg > /dev/null 2>&1 ; then
1927a6be
LV
450 arch=$(dpkg --print-architecture)
451else
452 arch=$(uname -m)
453 if [ "$arch" = "i686" ]; then
454 arch="i386"
455 elif [ "$arch" = "x86_64" ]; then
456 arch="amd64"
457 elif [ "$arch" = "armv7l" ]; then
458 arch="armhf"
459 fi
460fi
461hostarch=$arch
462
fb7460fe
DL
463while true
464do
465 case "$1" in
177f2cd2
AD
466 -h|--help) usage $0 && exit 1;;
467 --) shift 1; break ;;
468
469 -a|--arch) arch=$2; shift 2;;
470 -c|--clean) clean=$2; shift 1;;
471 --mirror) MIRROR=$2; shift 2;;
472 -n|--name) name=$2; shift 2;;
473 --packages) packages=$2; shift 2;;
474 -p|--path) path=$2; shift 2;;
475 -r|--release) release=$2; shift 2;;
476 --rootfs) rootfs=$2; shift 2;;
477 --security-mirror) SECURITY_MIRROR=$2; shift 2;;
478 *) break ;;
fb7460fe
DL
479 esac
480done
481
482if [ ! -z "$clean" -a -z "$path" ]; then
483 clean || exit 1
484 exit 0
485fi
486
17abf278 487if [ "$arch" = "i686" ]; then
1927a6be
LV
488 arch=i386
489fi
490
17abf278 491if [ "$arch" = "x86_64" ]; then
1927a6be
LV
492 arch=amd64
493fi
494
495if [ $hostarch = "i386" -a $arch = "amd64" ]; then
496 echo "can't create $arch container on $hostarch"
497 exit 1
498fi
499
500if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \
501 [ $arch != "armhf" -a $arch != "armel" ]; then
502 echo "can't create $arch container on $hostarch"
503 exit 1
504fi
505
506if [ $hostarch = "powerpc" -a $arch != "powerpc" ]; then
507 echo "can't create $arch container on $hostarch"
508 exit 1
509fi
510
fb7460fe
DL
511type debootstrap
512if [ $? -ne 0 ]; then
513 echo "'debootstrap' command is missing"
514 exit 1
515fi
516
517if [ -z "$path" ]; then
518 echo "'path' parameter is required"
519 exit 1
520fi
521
522if [ "$(id -u)" != "0" ]; then
523 echo "This script should be run as 'root'"
524 exit 1
c01d62f2 525fi
bad69158 526
b3d3f3c6 527current_release=`wget ${MIRROR}/dists/stable/Release -O - 2> /dev/null | head |awk '/^Codename: (.*)$/ { print $2; }'`
b269b8ad
LV
528release=${release:-${current_release}}
529valid_releases=('squeeze' 'wheezy' 'jessie' 'sid')
b3d3f3c6 530if [[ ! "${valid_releases[*]}" =~ (^|[^[:alpha:]])$release([^[:alpha:]]|$) ]]; then
b269b8ad
LV
531 echo "Invalid release ${release}, valid ones are: ${valid_releases[*]}"
532 exit 1
533fi
534
1881820a
SH
535# detect rootfs
536config="$path/config"
1897e3bc 537if [ -z "$rootfs" ]; then
cd44154c 538 if grep -q '^lxc.rootfs' $config 2> /dev/null ; then
853d58fd 539 rootfs=$(awk -F= '/^lxc.rootfs =/{ print $2 }' $config)
1897e3bc
SH
540 else
541 rootfs=$path/rootfs
542 fi
1881820a
SH
543fi
544
1927a6be 545install_debian $rootfs $release $arch
fb7460fe
DL
546if [ $? -ne 0 ]; then
547 echo "failed to install debian"
548 exit 1
549fi
550
551configure_debian $rootfs $name
552if [ $? -ne 0 ]; then
553 echo "failed to configure debian for a container"
554 exit 1
555fi
556
1927a6be 557copy_configuration $path $rootfs $name $arch
fb7460fe
DL
558if [ $? -ne 0 ]; then
559 echo "failed write configuration file"
560 exit 1
561fi
562
a9bf60ba
AT
563configure_debian_systemd $path $rootfs
564
177f2cd2
AD
565post_process ${rootfs} ${release} ${arch} ${hostarch} ${packages}
566
fb7460fe
DL
567if [ ! -z $clean ]; then
568 clean || exit 1
569 exit 0
570fi