Debian Codegraph @

templates/lxc-debian.in

raw ◀ ▶
3 Debian patches touch this file
1 #!/bin/bash
2
3 #
4 # lxc: linux Container library
5
6 # Authors:
7 # Daniel Lezcano <[email protected]>
8 # Greg Olsen <[email protected]> (lxc-devuan)
9 # Tanya <[email protected]>
10
11 # This library is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU Lesser General Public
13 # License as published by the Free Software Foundation; either
14 # version 2.1 of the License, or (at your option) any later version.
15
16 # This library is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 # Lesser General Public License for more details.
20
21 # You should have received a copy of the GNU Lesser General Public
22 # License along with this library; if not, write to the Free Software
23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24
25 # Detect use under userns (unsupported)
26 for arg in "$@"; do
27 [ "$arg" = "--" ] && break
28 if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
29 echo "This template can't be used for unprivileged containers." 1>&2
30 echo "You may want to try the \"download\" template instead." 1>&2
•31 echo "You can also use mmdebstrap --mode=unshare, and an example is found at" 1>&2
32 echo "https://wiki.debian.org/LXC#Unprivileged_Debian_container_by_mmdebstrap_--mode.3Dunshare " 1>&2
33 echo "or in /usr/share/doc/lxc-templates/README.Debian." 1>&2
34 exit 1
35 fi
36 done
37
38 # Make sure the usual locations are in PATH
39 export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
40 export GREP_OPTIONS=""
41
42 DEBIAN_MIRROR=http://deb.debian.org/debian
43 DEBIAN_PORTS_MIRROR=${MIRROR:-http://deb.debian.org/debian-ports}
44 MIRROR=${MIRROR:-${DEBIAN_MIRROR}}
•45 SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.debian.org/debian-security}
46 LOCALSTATEDIR="@LOCALSTATEDIR@"
47 LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
48 # Allows the lxc-cache directory to be set by environment variable
49 LXC_CACHE_PATH=${LXC_CACHE_PATH:-"$LOCALSTATEDIR/cache/lxc"}
50 DEBIAN_ARCHIVE_KEYRING=/usr/share/keyrings/debian-archive-keyring.gpg
51 DEBIAN_PORTS_ARCHIVE_KEYRING=/usr/share/keyrings/debian-ports-archive-keyring.gpg
52 KEYRING=${KEYRING:-${DEBIAN_ARCHIVE_KEYRING}}
53 DOWNLOAD_KEYRING=${DOWNLOAD_KEYRING:-1}
54
55 find_interpreter()
56 {
57 given_interpreter=$(basename "$1")
58
59 if [ ! -d /proc/sys/fs/binfmt_misc/ ] ; then
60 return 1
61 fi
62 for file in /proc/sys/fs/binfmt_misc/* ; do
63 if [ "$file" = "/proc/sys/fs/binfmt_misc/register" -o \
64 "$file" = "/proc/sys/fs/binfmt_misc/status" ] ; then
65 continue
66 fi
67 interpreter_path=$(sed -n "/^interpreter/s/interpreter \([^[:space:]]*\)/\1/p" "$file")
68 interpreter=$(basename "$interpreter_path")
69
70 if [ "$given_interpreter" = "$interpreter" ] ; then
71 echo "$interpreter_path"
72 return 0
73 fi
74 done
75 return 1
76 }
77
78 release_info() {
79 if wget "${1}/dists/${release}/Release" -O /dev/null --quiet 2> /dev/null; then
80 local release_file=$(wget "${1}/dists/${release}/Release" -O - --quiet 2> /dev/null)
81 distro_suite=${release_file#*Suite: }
82 distro_suite=${distro_suite%%$'\n'*}
83 distro_codename=${release_file#*Codename: }
84 distro_codename=${distro_codename%%$'\n'*}
85 return 0
86 fi
87 return 1
88 }
89
90 configure_debian()
91 {
92 rootfs=$1
93 hostname=$2
94 num_tty=$3
95
96 # squeeze only has /dev/tty and /dev/tty0 by default,
97 # therefore creating missing device nodes for tty1-4.
98 for tty in $(seq 1 "$num_tty"); do
99 if [ ! -e "$rootfs/dev/tty$tty" ]; then
100 mknod "$rootfs/dev/tty$tty" c 4 "$tty"
101 fi
102 done
103
104 # configure the inittab
105 cat <<EOF > $rootfs/etc/inittab
106 id:3:initdefault:
107 si::sysinit:/etc/init.d/rcS
108 l0:0:wait:/etc/init.d/rc 0
109 l1:1:wait:/etc/init.d/rc 1
110 l2:2:wait:/etc/init.d/rc 2
111 l3:3:wait:/etc/init.d/rc 3
112 l4:4:wait:/etc/init.d/rc 4
113 l5:5:wait:/etc/init.d/rc 5
114 l6:6:wait:/etc/init.d/rc 6
115 # Normally not reached, but fallthrough in case of emergency.
116 z6:6:respawn:/sbin/sulogin
117 1:2345:respawn:/sbin/getty 38400 console
118 $(for tty in $(seq 1 "$num_tty"); do echo "c${tty}:12345:respawn:/sbin/getty 38400 tty${tty} linux" ; done;)
119 p6::ctrlaltdel:/sbin/init 6
120 p0::powerfail:/sbin/init 0
121 EOF
122
123 # symlink mtab
124 [ -e "$rootfs/etc/mtab" ] && rm "$rootfs/etc/mtab"
125 ln -s /proc/self/mounts "$rootfs/etc/mtab"
126
127 # disable selinux in debian
128 mkdir -p "$rootfs/selinux"
129 echo 0 > "$rootfs/selinux/enforce"
130
131 # configure the network using the dhcp
132 cat <<EOF > $rootfs/etc/network/interfaces
133 auto lo
134 iface lo inet loopback
135
136 auto eth0
137 iface eth0 inet dhcp
138 EOF
139
140 # set the hostname
141 cat <<EOF > $rootfs/etc/hostname
142 $hostname
143 EOF
144
145 # set minimal hosts
146 cat <<EOF > $rootfs/etc/hosts
147 127.0.0.1 localhost
148 127.0.1.1 $hostname
149
150 # The following lines are desirable for IPv6 capable hosts
151 ::1 localhost ip6-localhost ip6-loopback
152 ff02::1 ip6-allnodes
153 ff02::2 ip6-allrouters
154 EOF
155
156 # reconfigure some services
157
158 # but first reconfigure locales - so we get no noisy perl-warnings
159 if [ -z "$LANG" ] || echo $LANG | grep -E -q "^C(\..+)*$"; then
160 cat >> "$rootfs/etc/locale.gen" << EOF
161 en_US.UTF-8 UTF-8
162 EOF
163 chroot "$rootfs" locale-gen en_US.UTF-8 UTF-8
164 chroot "$rootfs" update-locale LANG=en_US.UTF-8
165 else
166 encoding=$(locale charmap)
167 [ -z "${encoding}" ] && encoding="UTF-8"
168 chroot "$rootfs" sed -e "s/^# \(${LANG} ${encoding}\)/\1/" \
169 -i /etc/locale.gen 2> /dev/null
170 cat >> "$rootfs/etc/locale.gen" << EOF
171 $LANG $encoding
172 EOF
173 chroot "$rootfs" locale-gen "$LANG" "$encoding"
174 chroot "$rootfs" update-locale LANG="$LANG"
175 fi
176
177 # remove pointless services in a container
178 chroot "$rootfs" /usr/sbin/update-rc.d -f checkroot.sh disable
179 chroot "$rootfs" /usr/sbin/update-rc.d -f umountfs disable
180 chroot "$rootfs" /usr/sbin/update-rc.d -f hwclock.sh disable
181 chroot "$rootfs" /usr/sbin/update-rc.d -f hwclockfirst.sh disable
182
183 # generate new SSH keys
184 if [ -x "$rootfs/var/lib/dpkg/info/openssh-server.postinst" ]; then
185 cat > "$rootfs/usr/sbin/policy-rc.d" << EOF
186 #!/bin/sh
187 exit 101
188 EOF
189 chmod +x "$rootfs/usr/sbin/policy-rc.d"
190
191 if [ -f "$rootfs/etc/init/ssh.conf" ]; then
192 mv "$rootfs/etc/init/ssh.conf" "$rootfs/etc/init/ssh.conf.disabled"
193 fi
194
195 rm -f "$rootfs/etc/ssh/"ssh_host_*key*
196
197 DPKG_MAINTSCRIPT_PACKAGE=openssh DPKG_MAINTSCRIPT_NAME=postinst chroot "$rootfs" /var/lib/dpkg/info/openssh-server.postinst configure
198 sed -i "s/root@$(hostname)/root@$hostname/g" "$rootfs/etc/ssh/"ssh_host_*.pub
199
200 if [ -f "$rootfs/etc/init/ssh.conf.disabled" ]; then
201 mv "$rootfs/etc/init/ssh.conf.disabled" "$rootfs/etc/init/ssh.conf"
202 fi
203
204 rm -f "$rootfs/usr/sbin/policy-rc.d"
205 fi
206
207 # set initial timezone as on host
208 if [ -f /etc/timezone ]; then
209 cat /etc/timezone > "$rootfs/etc/timezone"
210 rm -f "$rootfs/etc/localtime"
211 chroot "$rootfs" dpkg-reconfigure -f noninteractive tzdata
212 elif [ -f /etc/sysconfig/clock ]; then
213 . /etc/sysconfig/clock
214 echo "$ZONE" > "$rootfs/etc/timezone"
215 rm -f "$rootfs/etc/localtime"
216 chroot "$rootfs" dpkg-reconfigure -f noninteractive tzdata
217 else
218 echo "Timezone in container is not configured. Adjust it manually."
219 fi
220
221 if [ -n "$authkey" ]; then
222 local ssh_dir_path="${rootfs}/root/.ssh"
223 mkdir -p "$ssh_dir_path"
224 cp "$authkey" "${ssh_dir_path}/authorized_keys"
225 chmod 700 "$ssh_dir_path"
226 echo "Inserted SSH public key from '$authkey' into /root/.ssh/authorized_keys"
227 fi
228
229 return 0
•230 }
231
232 get_security_repo() {
233 local release="${1}"; shift
234 local -i release_code=0
235
236 # oldoldstable is too old, let's ignore that case.
237 if [[ "${release}" == "oldoldstable" ]]; then
238 echo "${release}/updates"
239 return 0
240 fi
241
242 # Unstable has no security repo
243 if [[ "${release}" == "unstable" || "${release}" == "sid" || "${release}" == "experimental" ]]; then
244 echo ""
245 return 0
246 fi
247
248 # Extract the release code
249 if [[ "$release" == *"stable" || "$release" == "testing" ]]; then
250 release_code="$(debian-distro-info -r --"${release}")"
251 else
252 release_code="$(debian-distro-info -r --series="${release}")"
253 fi
254
255 # From the release code, get the security repository distro
256 if [ "${release_code}" -ge 11 ]; then
257 echo "${release}-security"
258 else
259 echo "${release}/updates"
260 fi
261
262 return 0
263 }
264
265 write_sourceslist()
266 {
267 local rootfs="$1"; shift
•268 local release="$1"; shift
269 local arch="$1"; shift
270 local sec_release
271
272 local prefix="deb"
273 if [ -n "${arch}" ]; then
274 prefix="deb [arch=${arch}]"
275 fi
276
277 if [ "$mainonly" = 1 ]; then
278 non_main=''
279 else
280 case "$distro_codename" in
281 wheezy|jessie|stretch|buster|bullseye)
282 non_main=' contrib non-free'
283 ;;
284 *)
285 non_main=' contrib non-free non-free-firmware'
286 ;;
287 esac
288 fi
289
290 cat >> "${rootfs}/etc/apt/sources.list" << EOF
291 ${prefix} $MIRROR ${release} main${non_main}
•292 EOF
293
294 sec_release="$(get_security_repo "${release}")"
295
•296 if [ -n "${sec_release}" ]; then
297 cat >> "${rootfs}/etc/apt/sources.list" << EOF
298 ${prefix} $SECURITY_MIRROR ${sec_release} main${non_main}
299 EOF
300 fi
301 }
302
303 apt_update_packages(){
304 local apt_update_args="update"
305 local lpath="${rootfs}/var/lib/apt/lists/$(echo "$MIRROR" | sed 's#.*://##;s#/#_#g')_dists_${release}_Release"
306 if [ -f "$lpath" ] && [ ! -z "$distro_suite" ]; then
307 local lsuite=$(cat "$lpath" | grep 'Suite:')
308 [ "${lsuite#Suite: }" != "$distro_suite" ] && apt_update_args="--allow-releaseinfo-change $apt_update_args"
309 fi
310 chroot "${rootfs}" apt-get $apt_update_args
311 return $?
312 }
313
314 install_packages()
315 {
316 local rootfs="$1"; shift
317 local packages="$*"
318
319 if [ -n "${packages}" ] && apt_update_packages; then
320 chroot "${rootfs}" apt-get install -y --no-install-recommends ${packages}
321 elif [ -n "${packages}" ]; then
322 echo "Apt update failed, not downloading packages"
323 fi
324 }
325
326 configure_debian_systemd()
327 {
328 path=$1
329 rootfs=$2
330 config=$3
331 num_tty=$4
332
333 # just in case systemd is not installed
334 mkdir -p "${rootfs}/lib/systemd/system"
335 mkdir -p "${rootfs}/etc/systemd/system/getty.target.wants"
336
337 # Fix getty-static-service as debootstrap does not install dbus
338 if [ -e "$rootfs//lib/systemd/system/getty-static.service" ] ; then
339 local tty_services
340 tty_services=$(for i in $(seq 2 "$num_tty"); do echo -n "getty@tty${i}.service "; done; )
341 sed 's/ getty@tty.*/'" $tty_services "'/g' \
342 "$rootfs/lib/systemd/system/getty-static.service" | \
343 sed 's/\(tty2-tty\)[5-9]/\1'"${num_tty}"'/g' > "$rootfs/etc/systemd/system/getty-static.service"
344 fi
345
346 # This function has been copied and adapted from lxc-fedora
347 rm -f "${rootfs}/etc/systemd/system/default.target"
348 chroot "${rootfs}" ln -s /dev/null /etc/systemd/system/udev.service
349 chroot "${rootfs}" ln -s /dev/null /etc/systemd/system/systemd-udevd.service
350 chroot "${rootfs}" ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
351 # Setup getty service on the ttys we are going to allow in the
352 # default config. Number should match lxc.tty.max
353 ( cd "${rootfs}/etc/systemd/system/getty.target.wants"
354 for i in $(seq 1 "$num_tty") ; do ln -sf ../getty\@.service getty@tty"${i}".service; done )
355
356 # Since we use static-getty.target; we need to mask [email protected] generated by
357 # container-getty-generator, so we don't get multiple instances of agetty running.
358 # See https://github.com/lxc/lxc/issues/520 and https://github.com/lxc/lxc/issues/484
359 ( cd "${rootfs}/etc/systemd/system/getty.target.wants"
360 for i in $(seq 0 "$num_tty"); do ln -sf /dev/null container-getty\@"${i}".service; done )
361
362 return 0
363 }
364
365 # Check if given path is in a btrfs partition
366 is_btrfs()
367 {
368 [ -e "$1" -a "$(stat -f -c '%T' "$1")" = "btrfs" ]
369 }
370
371 # Check if given path is the root of a btrfs subvolume
372 is_btrfs_subvolume()
373 {
374 [ -d "$1" -a "$(stat -f -c '%T' "$1")" = "btrfs" -a "$(stat -c '%i' "$1")" -eq 256 ]
375 }
376
377 try_mksubvolume()
378 {
379 path=$1
380 [ -d "$path" ] && return 0
381 mkdir -p "$(dirname "$path")"
382 if which btrfs >/dev/null 2>&1 && is_btrfs "$(dirname "$path")"; then
383 btrfs subvolume create "$path"
384 else
385 mkdir -p "$path"
386 fi
387 }
388
389 try_rmsubvolume()
390 {
391 path=$1
392 [ -d "$path" ] || return 0
393 if which btrfs >/dev/null 2>&1 && is_btrfs_subvolume "$path"; then
394 btrfs subvolume delete "$path"
395 else
396 rm -rf "$path"
397 fi
398 }
399
400 cleanup()
401 {
402 try_rmsubvolume "$cache/partial-$release-$arch"
403 try_rmsubvolume "$cache/rootfs-$release-$arch"
404 }
405
406 download_debian()
407 {
408 case "$release" in
409 wheezy)
410 init=sysvinit
411 iproute=iproute
412 ;;
413 *)
414 init=init
415 iproute=iproute2
416 ;;
417 esac
418 # Check by codename as stable and testing are moving targets
419 case "$distro_codename" in
420 wheezy|jessie|stretch|buster|bullseye|bookworm|trixie)
421 dhcpclient=isc-dhcp-client
422 ;;
423 *)
424 dhcpclient=dhcpcd-base
425 ;;
426 esac
427 packages=\
428 $init,\
429 ifupdown,\
430 locales,\
431 ca-certificates,\
432 dialog,\
433 $dhcpclient,\
434 netbase,\
435 net-tools,\
436 $iproute,\
437 openssh-server,\
438 apt-transport-https
439
440 cache=$1
441 arch=$2
442 release=$3
443 interpreter="$4"
444 interpreter_path="$5"
445
446 trap cleanup EXIT SIGHUP SIGINT SIGTERM
447
448 # Create the cache
449 mkdir -p "$cache"
450
451 # If debian-archive-keyring isn't installed, fetch GPG keys directly
452 releasekeyring=${KEYRING}
453 lreleasekeyring=/etc/apt/trusted.gpg.d/debian-archive-$release-stable.gpg
454 if [ -f "$releasekeyring" ]; then
455 apt_gpg_opt="--keyring=${releasekeyring}"
456 elif [ -f "$lreleasekeyring" ]; then
457 apt_gpg_opt="--keyring=${lreleasekeyring}"
458 elif [ ${releasekeyring}" = "${DEBIAN_ARCHIVE_KEYRING} -a "$DOWNLOAD_KEYRING" = 1 ]; then
459 [ ! -d "/etc/apt/trusted.gpg.d" ] && lreleasekeyring="$cache/archive-key.gpg"
460 if [[ "$(id -u)" == "0" ]]; then
461 case $release in
462 "wheezy")
463 gpgkeyname="archive-key-7.0"
464 ;;
465 *)
466 gpgkeyname="archive-key-8"
467 ;;
468 esac
469 wget https://ftp-master.debian.org/keys/${gpgkeyname}.asc -O - --quiet \
470 | gpg --import --no-default-keyring --keyring="${lreleasekeyring}"
471 apt_gpg_opt="--keyring=${lreleasekeyring}"
472 else
473 echo "Must be root (sudo) to save $lreleasekeyring"
474 fi
475 fi
476 if [ -z "$apt_gpg_opt" ]; then
477 echo "WARNING: No GPG check"
478 apt_gpg_opt='--no-check-gpg'
479 fi
480 # check the mini debian was not already downloaded
481 try_mksubvolume "$cache/partial-$release-$arch"
482 if [ $? -ne 0 ]; then
483 echo "Failed to create '$cache/partial-$release-$arch' directory"
484 return 1
485 fi
486
487 # download a mini debian into a cache
488 echo "Downloading debian minimal ..."
489 if [ "$interpreter" = "" ] ; then
490 debootstrap --verbose --variant=minbase --arch="$arch" \
491 --include=$packages "${apt_gpg_opt}" \
492 "$release" "$cache/partial-$release-$arch" "$MIRROR"
493 if [ $? -ne 0 ]; then
494 echo "Failed to download the rootfs, aborting."
495 return 1
496 fi
497 else
498 debootstrap --foreign --verbose --variant=minbase --arch="$arch" \
499 --include=$packages "${apt_gpg_opt}" \
500 "$release" "$cache/partial-$release-$arch" "$MIRROR"
501 if [ $? -ne 0 ]; then
502 echo "Failed to download the rootfs, aborting."
503 return 1
504 fi
505 mkdir -p "$(dirname "$cache/partial-$release-$arch/$interpreter_path")"
506 cp "$interpreter" "$cache/partial-$release-$arch/$interpreter_path"
507 if [ $? -ne 0 ]; then
508 echo "failed to copy $interpreter to $cache/partial-$release-$arch/$interpreter_path"
509 return 1
510 fi
511 chroot "$cache/partial-$release-$arch" debootstrap/debootstrap --second-stage
512 if [ $? -ne 0 ]; then
513 echo "failed to update the rootfs, aborting"
514 return 1
515 fi
516 fi
517
518 mv "$1/partial-$release-$arch" "$1/rootfs-$release-$arch"
519 echo "Download complete."
520 trap EXIT
521 trap SIGINT
522 trap SIGTERM
523 trap SIGHUP
524
525 return 0
526 }
527
528 copy_debian()
529 {
530 cache=$1
531 arch=$2
532 rootfs=$3
533 release=$4
534
535 # make a local copy of the minidebian
536 echo -n "Copying rootfs to $rootfs..."
537 try_mksubvolume "$rootfs"
538 if which btrfs >/dev/null 2>&1 && \
539 is_btrfs_subvolume "$cache/rootfs-$release-$arch" && \
540 is_btrfs_subvolume "$rootfs"; then
541 realrootfs="$(dirname "$config")"/rootfs
542 [ "$rootfs" = "$realrootfs" ] || umount "$rootfs" || return 1
543 btrfs subvolume delete "$realrootfs" || return 1
544 btrfs subvolume snapshot "$cache/rootfs-$release-$arch" "$realrootfs" || return 1
545 [ "$rootfs" = "$realrootfs" ] || mount --bind "$realrootfs" "$rootfs" || return 1
546 else
547 rsync -SHaAX "$cache/rootfs-$release-$arch"/ $rootfs/ || return 1
548 fi
549 return 0
550 }
551
552 install_debian()
553 {
554 rootfs=$1
555 release=$2
556 arch=$3
557 cache="$4/debian"
558 interpreter="$5"
559 interpreter_path="$6"
560 flushcache=$7
561 mkdir -p $LOCALSTATEDIR/lock/subsys/
562 (
563 flock -x 9
564 if [ $? -ne 0 ]; then
565 echo "Cache repository is busy."
566 return 1
567 fi
568
569 if [ "$flushcache" -eq 1 ]; then
570 echo "Flushing cache..."
571 cleanup
572 fi
573
574 echo "Checking cache download in $cache/rootfs-$release-$arch ... "
575 if [ ! -e "$cache/rootfs-$release-$arch" ]; then
576 download_debian "$cache" "$arch" "$release" "$interpreter" "$interpreter_path"
577 if [ $? -ne 0 ]; then
578 echo "Failed to download 'debian base'"
579 return 1
580 fi
581 fi
582
583 copy_debian "$cache" "$arch" "$rootfs" "$release"
584 if [ $? -ne 0 ]; then
585 echo "Failed to copy rootfs"
586 return 1
587 fi
588
589 return 0
590
591 ) 9>$LOCALSTATEDIR/lock/subsys/lxc-debian
592
593 return $?
594 }
595
596 copy_configuration()
597 {
598 path=$1
599 rootfs=$2
600 hostname=$3
601 arch=$4
602 num_tty=$5
603
604 # Generate the configuration file
605 # if there is exactly one veth network entry, make sure it has an
606 # associated hwaddr.
607 nics=$(grep -ce '^lxc\.net\.0\.type[ \t]*=[ \t]*veth' "$path/config")
608 if [ "$nics" -eq 1 ]; then
609 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"
610 fi
611
612 ## Add all the includes
613 echo "" >> "$path/config"
614 echo "# Common configuration" >> "$path/config"
615 if [ -e "${LXC_TEMPLATE_CONFIG}/debian.common.conf" ]; then
616 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/debian.common.conf" >> "$path/config"
617 fi
618 if [ -e "${LXC_TEMPLATE_CONFIG}/debian.${release}.conf" ]; then
619 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/debian.${release}.conf" >> "$path/config"
620 fi
621
622 ## Add the container-specific config
623 echo "" >> "$path/config"
624 echo "# Container specific configuration" >> "$path/config"
625 grep -q "^lxc.rootfs.path" "$path/config" 2> /dev/null || echo "lxc.rootfs.path = $rootfs" >> "$path/config"
626
627 cat <<EOF >> $path/config
628 lxc.tty.max = $num_tty
629 lxc.uts.name = $hostname
630 lxc.arch = $arch
631 lxc.pty.max = 1024
632 EOF
633
634 if [ $? -ne 0 ]; then
635 echo "Failed to add configuration"
636 return 1
637 fi
638
639 return 0
640 }
641
642 post_process()
643 {
644 local rootfs="$1"; shift
645 local release="$1"; shift
646 local arch="$1"; shift
647 local hostarch="$1"; shift
648 local interpreter="$1"; shift
649 local packages="$*"
650
651 # Disable service startup
652 cat > "${rootfs}/usr/sbin/policy-rc.d" << EOF
653 #!/bin/sh
654 exit 101
655 EOF
656 chmod +x "${rootfs}/usr/sbin/policy-rc.d"
657
658 # If the container isn't running a native architecture, setup multiarch
659 if [ "$interpreter" = "" -a "${arch}" != "${hostarch}" ]; then
660 # Test if dpkg supports multiarch
661 if ! chroot "$rootfs" dpkg --print-foreign-architectures 2>&1; then
662 chroot "$rootfs" dpkg --add-architecture "${hostarch}"
663 fi
664 fi
665
666 # Write a new sources.list containing both native and multiarch entries
667 > "${rootfs}/etc/apt/sources.list"
668 if [ "$interpreter" != "" -a "${arch}" = "${hostarch}" ]; then
669 write_sourceslist "${rootfs}" "${release}" "${arch}"
670 else
671 write_sourceslist "${rootfs}" "${release}"
672 fi
673
674 # Copy non-standard keyring inside rootfs
675 local extra_keyring
676 extra_keyring="${rootfs}/etc/apt/trusted.gpg.d/lxc-create-$(basename "${KEYRING}")"
677 if [ "${KEYRING}" != "${DEBIAN_ARCHIVE_KEYRING}" ]; then
678 cp "${KEYRING}" "${extra_keyring}"
679 fi
680
681 # Install Packages in container
682 if [ -n "${packages}" ]; then
683 local pack_list
684 pack_list="${packages//,/ }"
685 echo "Installing packages: ${pack_list}"
686 install_packages "${rootfs}" "${pack_list}"
687 fi
688
689 # Re-enable service startup
690 rm "${rootfs}/usr/sbin/policy-rc.d"
691
692 # Fix by Greg Olsen, avoid warning 'stdin not a tty' in some cases
693 if [[ -f "${rootfs}/root/.profile" ]]; then
694 sed -i -e 's/^mesg n/test -t 0 \&\& mesg n/g' ${rootfs}/root/.profile
695 fi
696
697 # end
698 }
699
700 clean()
701 {
702 cache=${LXC_CACHE_PATH:-"$LOCALSTATEDIR/cache/lxc/debian"}
703
704 if [ ! -e "$cache" ]; then
705 exit 0
706 fi
707
708 # lock, so we won't purge while someone is creating a repository
709 (
710 flock -x 9
711 if [ $? != 0 ]; then
712 echo "Cache repository is busy."
713 exit 1
714 fi
715
716 echo -n "Purging the download cache..."
717 rm --preserve-root --one-file-system -rf "$cache" && echo "Done." || exit 1
718 exit 0
719
720 ) 9>$LOCALSTATEDIR/lock/subsys/lxc-debian
721 }
722
723 usage()
724 {
725 cat <<EOF
•726 Template specific options can be passed to lxc-create after a '--' like this:
727
728 lxc-create --name=NAME [-lxc-create-options] -- [-template-options]
729
730 Usage: $1 -h|--help -p|--path=<path> [-c|--clean] [-a|--arch=<arch>] [-r|--release=<release>]
731 [--mirror=<mirror>] [--security-mirror=<security mirror>]
732 [--keyring=<keyring>]
733 [--package=<package_name1,package_name2,...>]
734 [-I|--interpreter-path=<interpreter path>]
•735 [-F | --flush-cache] [-S|--auth-key=<keyfile>]
736
737 Options:
738
739 -h, --help print this help text
740 -p, --path=PATH directory where config and rootfs of this VM will be kept
741 -S, --auth-key=KEYFILE SSH public key to inject into the container as the root user.
742 -a, --arch=ARCH The container architecture. Can be one of: i686, x86_64,
743 amd64, armhf, armel, powerpc. Defaults to host arch.
744 -r, --release=RELEASE Debian release. Can be one of: buster, bullseye, bookworm, trixie,
745 testing, sid. Defaults to current stable.
746 For archived releases, see the section below.
747 --mirror=MIRROR Debian mirror to use during installation. Overrides the MIRROR
748 environment variable (see below).
749 --keyring=KEYRING GPG keyring to authenticate the repository against.
750 Overrides the KEYRING environment variable (see below).
•751 --security-mirror=SECURITY_MIRROR
752 Debian mirror to use for security updates. Overrides the
753 SECURITY_MIRROR environment variable (see below).
754 --packages=PACKAGE_NAME1,PACKAGE_NAME2,...
755 List of additional packages to install. Comma separated, without space.
756 -c, --clean only clean up the cache and terminate
757 --enable-non-free include also Debian's contrib, non-free and non-free-firmware repositories.
758 -I|--interpreter-path=INTERPRETER-PATH
759 Path of the binfmt interpreter to copy to the rootfs
760 -F | --flush-cache Flush the debian release cache
761
762 Archived releases:
763
764 It is possible to create containers for some archived Debian releases. As of February 2024,
765 stretch works and jessie will create a container but doesn't automatically obtain an IP address.
766
767 wheezy and earlier releases do NOT work.
768
769 You must manually specify the --mirror and --keyring options like so:
770
771 lxc-create -t debian -n stretch -- -r stretch \
772 --mirror=http://archive.debian.org/debian \
773 --keyring=/usr/share/keyrings/debian-archive-removed-keys.gpg
774
775 Environment variables:
776
777 MIRROR The Debian package mirror to use. See also the --mirror switch above.
778 Defaults to '$MIRROR'
779 SECURITY_MIRROR The Debian package security mirror to use. See also the --security-mirror switch above.
780 Defaults to '$SECURITY_MIRROR'
781 KEYRING Defaults to '$DEBIAN_ARCHIVE_KEYRING'
782 DOWNLOAD_KEYRING Sets whether to download keyring when missing or ignore keyring checks
783 Defaults to 1
784
785 EOF
786 return 0
787 }
788
789 options=$(getopt -o hp:n:a:r:cI:FS: -l arch:,auth-key:,clean,help,enable-non-free,keyring:,mirror:,name:,packages:,path:,release:,rootfs:,security-mirror:,interpreter-path:,flush-cache -- "$@")
790 if [ $? -ne 0 ]; then
791 usage "$(basename "$0")"
792 exit 1
793 fi
794 eval set -- "$options"
795
796 littleendian=$(lscpu | grep '^Byte Order' | grep -q Little && echo yes)
797
798 arch=$(uname -m)
799 if [ "$arch" = "i686" ]; then
800 arch="i386"
801 elif [ "$arch" = "x86_64" ]; then
802 arch="amd64"
803 elif [ "$arch" = "armv7l" ]; then
804 arch="armhf"
805 elif [ "$arch" = "aarch64" ]; then
806 arch="arm64"
807 elif [ "$arch" = "ppc" ]; then
808 arch="powerpc"
809 elif [ "$arch" = "ppc64le" ]; then
810 arch="ppc64el"
811 elif [ "$arch" = "mips" -a "$littleendian" = "yes" ]; then
812 arch="mipsel"
813 elif [ "$arch" = "mips64" -a "$littleendian" = "yes" ]; then
814 arch="mips64el"
815 fi
816 hostarch=$arch
817 packages=
818 mainonly=1
819 flushcache=0
820
821 while true
822 do
823 case "$1" in
824 -h|--help) usage "$0" && exit 1;;
825 --) shift 1; break ;;
826
827 -a|--arch) arch=$2; shift 2;;
828 -S|--auth-key) authkey=$2; shift 2;;
829 -I|--interpreter-path)
830 interpreter="$2"; shift 2;;
831 -c|--clean) clean=1; shift 1;;
832 --enable-non-free) mainonly=0; shift 1;;
833 --mirror) MIRROR=$2; shift 2;;
834 --keyring) KEYRING=$2; shift 2;;
835 -n|--name) name=$2; shift 2;;
836 --packages) packages=$2; shift 2;;
837 -p|--path) path=$2; shift 2;;
838 -r|--release) release=$2; shift 2;;
839 --rootfs) rootfs=$2; shift 2;;
840 --security-mirror) SECURITY_MIRROR=$2; shift 2;;
841 -F|--flush-cache) flushcache=1; shift 1;;
842 *) break ;;
843 esac
844 done
845
846 if [ ! -z "$clean" -a -z "$path" ]; then
847 clean || exit 1
848 exit 0
849 fi
850
851 if [ "$arch" = "i686" ]; then
852 arch=i386
853 fi
854
855 if [ "$arch" = "x86_64" ]; then
856 arch=amd64
857 fi
858
859
860 release_file=${MIRROR}/dists/${release}/main/binary-${arch}/Release
861 if ! wget -q -O /dev/null "${release_file}"; then
862 if [ "${release}" = unstable ] || [ "${release}" = sid ]; then
863 echo "${arch}/${release} not available on mirror, trying debian ports"
864 # non-release architecture; assume debian-ports architecture
865 if [ "${MIRROR}" = "${DEBIAN_MIRROR}" ]; then
866 MIRROR="${DEBIAN_PORTS_MIRROR}"
867 fi
868 if [ "${KEYRING}" = "${DEBIAN_ARCHIVE_KEYRING}" ]; then
869 KEYRING="${DEBIAN_PORTS_ARCHIVE_KEYRING}"
870 fi
871 packages="debian-ports-archive-keyring,${packages}"
872 else
873 echo "Error: ${arch}/${release} not available on mirror ${MIRROR}"
874 exit 1
875 fi
876 fi
877
878 if [ "$interpreter" = "" ] ; then
879 if [ $hostarch = "i386" -a $arch = "amd64" ]; then
880 echo "can't create $arch container on $hostarch"
881 exit 1
882 fi
883
884 if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \
885 [ $arch != "armhf" -a $arch != "armel" ]; then
886 echo "can't create $arch container on $hostarch"
887 exit 1
888 fi
889
890 if [ $hostarch = "powerpc" -a $arch != "powerpc" ]; then
891 echo "can't create $arch container on $hostarch"
892 exit 1
893 fi
894
895 if [ $hostarch = "mips" -a $arch != "mips" ] || \
896 [ $hostarch = "mipsel" -a $arch != "mipsel" ] || \
897 [ $hostarch = "mips64" -a $arch != "mips" -a $arch != "mips64" ] || \
898 [ $hostarch = "mips64el" -a $arch != "mipsel" -a $arch != "mips64el" ]; then
899 echo "can't create $arch container on $hostarch"
900 exit 1
901 fi
902 else
903 if ! file -b "${interpreter}" |grep -q "statically linked" ; then
904 echo "'${interpreter}' must be statically linked" 1>&2
905 exit 1
906 fi
907 interpreter_path=$(find_interpreter "$interpreter")
908 if [ $? -ne 0 ] ; then
909 echo "no binfmt interpreter using $(basename "$interpreter")" 1>&2
910 exit 1
911 fi
912 fi
913
914 type debootstrap
915 if [ $? -ne 0 ]; then
916 echo "'debootstrap' command is missing"
917 exit 1
918 fi
919
920 if [ -z "$path" ]; then
921 echo "'path' parameter is required"
922 exit 1
923 fi
924
925 if [ "$(id -u)" != "0" ]; then
926 echo "This script should be run as 'root'"
927 exit 1
928 fi
929
930 if [ -n "$authkey" ]; then
931 if [ ! -f "$authkey" ]; then
932 echo "SSH keyfile '$authkey' not found"
933 exit 1
934 fi
935 # This is mostly to prevent accidental uage of the private key instead
936 # of the public key.
937 if [ "${authkey: -4}" != ".pub" ]; then
938 echo "SSH keyfile '$authkey' does not end with '.pub'"
939 exit 1
940 fi
941 fi
942
943 release=${release:-stable}
944 permanent_releases=('oldoldstable' 'oldstable' 'stable' 'testing' 'sid' 'unstable')
945 if [[ ! "${permanent_releases[*]}" =~ (^|[^[:alpha:]])$release([^[:alpha:]]|$) ]]; then
946 if ! release_info "${MIRROR}"; then
947 echo "Invalid release ${release} (not found in mirror)"
948 exit 1
949 fi
950 else
951 release_info "${MIRROR}"
952 fi
953
954 # detect rootfs
955 config="$path/config"
956 if [ -z "$rootfs" ]; then
957 if grep -q '^lxc.rootfs.path' "$config" 2> /dev/null ; then
958 rootfs=$(awk -F= '/^lxc.rootfs.path[ \t]+=/{ print $2 }' "$config")
959 else
960 rootfs=$path/rootfs
961 fi
962 fi
963
964 # determine the number of ttys - default is 4
965 if grep -q '^lxc.tty.max' "$config" 2> /dev/null ; then
966 num_tty=$(awk -F= '/^lxc.tty.max[ \t]+=/{ print $2 }' "$config")
967 else
968 num_tty=4
969 fi
970
971 install_debian "$rootfs" "$release" "$arch" "$LXC_CACHE_PATH" "$interpreter" "$interpreter_path" "$flushcache"
972 if [ $? -ne 0 ]; then
973 echo "failed to install debian"
974 exit 1
975 fi
976
977 configure_debian "$rootfs" "$name" $num_tty
978 if [ $? -ne 0 ]; then
979 echo "failed to configure debian for a container"
980 exit 1
981 fi
982
983 copy_configuration "$path" "$rootfs" "$name" $arch $num_tty
984 if [ $? -ne 0 ]; then
985 echo "failed write configuration file"
986 exit 1
987 fi
988
989 configure_debian_systemd "$path" "$rootfs" "$config" $num_tty
990
991 post_process "${rootfs}" "${release}" ${arch} ${hostarch} "${interpreter}" "${packages}"
992
993 if [ ! -z "$clean" ]; then
994 clean || exit 1
995 exit 0
996 fi