]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-debian.in
typofixes - https://github.com/vlajos/misspell_fixer
[mirror_lxc.git] / templates / lxc-debian.in
1 #!/bin/bash
2
3 #
4 # lxc: linux Container library
5
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
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
18
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
22
23 # Detect use under userns (unsupported)
24 for arg in "$@"; do
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
29 exit 1
30 fi
31 done
32
33 # Make sure the usual locations are in PATH
34 export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
35
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@"
40
41 configure_debian()
42 {
43 rootfs=$1
44 hostname=$2
45
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
51 fi
52 done
53
54 # configure the inittab
55 cat <<EOF > $rootfs/etc/inittab
56 id:3:initdefault:
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
74 EOF
75
76 # symlink mtab
77 [ -e "$rootfs/etc/mtab" ] && rm $rootfs/etc/mtab
78 ln -s /proc/self/mounts $rootfs/etc/mtab
79
80 # disable selinux in debian
81 mkdir -p $rootfs/selinux
82 echo 0 > $rootfs/selinux/enforce
83
84 # configure the network using the dhcp
85 cat <<EOF > $rootfs/etc/network/interfaces
86 auto lo
87 iface lo inet loopback
88
89 auto eth0
90 iface eth0 inet dhcp
91 EOF
92
93 # set the hostname
94 cat <<EOF > $rootfs/etc/hostname
95 $hostname
96 EOF
97
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
102 else
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
108 fi
109
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
115
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
120 exit 101
121 EOF
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
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
148 else
149 echo "Timezone in container is not configured. Adjust it manually."
150 fi
151
152 echo "root:root" | chroot $rootfs chpasswd
153 echo "Root password is 'root', please change !"
154
155 return 0
156 }
157
158 write_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
170 ${prefix} $MIRROR ${release} main contrib non-free
171 ${prefix} $SECURITY_MIRROR ${release}/updates main contrib non-free
172 EOF
173 }
174
175 install_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
186 configure_debian_systemd()
187 {
188 path=$1
189 rootfs=$2
190
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"
196 fi
197
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
204 fi
205
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
209
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 )
222
223 return 0
224 }
225
226 cleanup()
227 {
228 rm -rf $cache/partial-$release-$arch
229 rm -rf $cache/rootfs-$release-$arch
230 }
231
232 download_debian()
233 {
234 packages=\
235 ifupdown,\
236 locales,\
237 libui-dialog-perl,\
238 dialog,\
239 isc-dhcp-client,\
240 netbase,\
241 net-tools,\
242 iproute,\
243 openssh-server
244
245 cache=$1
246 arch=$2
247 release=$3
248
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"
254 return 1
255 fi
256
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."
264 return 1
265 fi
266
267 mv "$1/partial-$release-$arch" "$1/rootfs-$release-$arch"
268 echo "Download complete."
269 trap EXIT
270 trap SIGINT
271 trap SIGTERM
272 trap SIGHUP
273
274 return 0
275 }
276
277 copy_debian()
278 {
279 cache=$1
280 arch=$2
281 rootfs=$3
282 release=$4
283
284 # make a local copy of the minidebian
285 echo -n "Copying rootfs to $rootfs..."
286 mkdir -p $rootfs
287 rsync -Ha "$cache/rootfs-$release-$arch"/ $rootfs/ || return 1
288 return 0
289 }
290
291 install_debian()
292 {
293 cache="$LOCALSTATEDIR/cache/lxc/debian"
294 rootfs=$1
295 release=$2
296 arch=$3
297 mkdir -p $LOCALSTATEDIR/lock/subsys/
298 (
299 flock -x 9
300 if [ $? -ne 0 ]; then
301 echo "Cache repository is busy."
302 return 1
303 fi
304
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'"
310 return 1
311 fi
312 fi
313
314 copy_debian $cache $arch $rootfs $release
315 if [ $? -ne 0 ]; then
316 echo "Failed to copy rootfs"
317 return 1
318 fi
319
320 return 0
321
322 ) 9>$LOCALSTATEDIR/lock/subsys/lxc-debian
323
324 return $?
325 }
326
327 copy_configuration()
328 {
329 path=$1
330 rootfs=$2
331 hostname=$3
332 arch=$4
333
334 # Generate the configuration file
335 ## Create the fstab (empty by default)
336 touch $path/fstab
337
338 # if there is exactly one veth network entry, make sure it has an
339 # associated hwaddr.
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
343 fi
344
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
350 fi
351 if [ -e "${LXC_TEMPLATE_CONFIG}/debian.${release}.conf" ]; then
352 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/debian.${release}.conf" >> $path/config
353 fi
354
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
359
360 cat <<EOF >> $path/config
361 lxc.mount = $path/fstab
362 lxc.utsname = $hostname
363 lxc.arch = $arch
364 EOF
365
366 if [ $? -ne 0 ]; then
367 echo "Failed to add configuration"
368 return 1
369 fi
370
371 return 0
372 }
373
374 post_process()
375 {
376 local rootfs="$1"; shift
377 local release="$1"; shift
378 local arch="$1"; shift
379 local hostarch="$1"; shift
380 local packages="$*"
381
382 # Disable service startup
383 cat > ${rootfs}/usr/sbin/policy-rc.d << EOF
384 #!/bin/sh
385 exit 101
386 EOF
387 chmod +x ${rootfs}/usr/sbin/policy-rc.d
388
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
393 fi
394
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}
399 else
400 write_sourceslist ${rootfs} ${release}
401 fi
402
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}
408 fi
409
410 # Re-enable service startup
411 rm ${rootfs}/usr/sbin/policy-rc.d
412 }
413
414 clean()
415 {
416 cache="$LOCALSTATEDIR/cache/lxc/debian"
417
418 if [ ! -e $cache ]; then
419 exit 0
420 fi
421
422 # lock, so we won't purge while someone is creating a repository
423 (
424 flock -x 9
425 if [ $? != 0 ]; then
426 echo "Cache repository is busy."
427 exit 1
428 fi
429
430 echo -n "Purging the download cache..."
431 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
432 exit 0
433
434 ) 9>$LOCALSTATEDIR/lock/subsys/lxc-debian
435 }
436
437 usage()
438 {
439 cat <<EOF
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
446 EOF
447 return 0
448 }
449
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
452 usage $(basename $0)
453 exit 1
454 fi
455 eval set -- "$options"
456
457 if which dpkg > /dev/null 2>&1 ; then
458 arch=$(dpkg --print-architecture)
459 else
460 arch=$(uname -m)
461 if [ "$arch" = "i686" ]; then
462 arch="i386"
463 elif [ "$arch" = "x86_64" ]; then
464 arch="amd64"
465 elif [ "$arch" = "armv7l" ]; then
466 arch="armhf"
467 fi
468 fi
469 hostarch=$arch
470
471 while true
472 do
473 case "$1" in
474 -h|--help) usage $0 && exit 1;;
475 --) shift 1; break ;;
476
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;;
486 *) break ;;
487 esac
488 done
489
490 if [ ! -z "$clean" -a -z "$path" ]; then
491 clean || exit 1
492 exit 0
493 fi
494
495 if [ "$arch" = "i686" ]; then
496 arch=i386
497 fi
498
499 if [ "$arch" = "x86_64" ]; then
500 arch=amd64
501 fi
502
503 if [ $hostarch = "i386" -a $arch = "amd64" ]; then
504 echo "can't create $arch container on $hostarch"
505 exit 1
506 fi
507
508 if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \
509 [ $arch != "armhf" -a $arch != "armel" ]; then
510 echo "can't create $arch container on $hostarch"
511 exit 1
512 fi
513
514 if [ $hostarch = "powerpc" -a $arch != "powerpc" ]; then
515 echo "can't create $arch container on $hostarch"
516 exit 1
517 fi
518
519 type debootstrap
520 if [ $? -ne 0 ]; then
521 echo "'debootstrap' command is missing"
522 exit 1
523 fi
524
525 if [ -z "$path" ]; then
526 echo "'path' parameter is required"
527 exit 1
528 fi
529
530 if [ "$(id -u)" != "0" ]; then
531 echo "This script should be run as 'root'"
532 exit 1
533 fi
534
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[*]}"
540 exit 1
541 fi
542
543 # detect rootfs
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)
548 else
549 rootfs=$path/rootfs
550 fi
551 fi
552
553 install_debian $rootfs $release $arch
554 if [ $? -ne 0 ]; then
555 echo "failed to install debian"
556 exit 1
557 fi
558
559 configure_debian $rootfs $name
560 if [ $? -ne 0 ]; then
561 echo "failed to configure debian for a container"
562 exit 1
563 fi
564
565 copy_configuration $path $rootfs $name $arch
566 if [ $? -ne 0 ]; then
567 echo "failed write configuration file"
568 exit 1
569 fi
570
571 configure_debian_systemd $path $rootfs
572
573 post_process ${rootfs} ${release} ${arch} ${hostarch} ${packages}
574
575 if [ ! -z $clean ]; then
576 clean || exit 1
577 exit 0
578 fi