2 # Filename: /etc/debootstrap/chroot-script
3 # Purpose: script executed in chroot when installing Debian via grml-debootstrap
4 # Authors: grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports: see https://grml.org/bugs/
6 # License: This file is licensed under the GPL v2.
7 ################################################################################
8 # GRML_CHROOT_SCRIPT_MARKER - do not remove this line unless you want to keep
9 # this script as /bin/chroot-script on your new installed system
10 ################################################################################
11 # shellcheck disable=SC2317 # shellcheck has trouble understanding the code flow in this file
17 trap "error_handler" ERR
20 bash -n /etc/debootstrap/config
21 # shellcheck source=config
22 . /etc/debootstrap/config || exit 1
23 bash -n /etc/debootstrap/variables
24 # shellcheck source=tests/shellcheck-stub-debootstrap-variables
25 . /etc/debootstrap/variables || exit 1
27 [ -r /proc/1 ] || mount -t proc none /proc
28 [ -r /sys/kernel ] || mount -t sysfs none /sys
32 # use aptitude only if it's available
33 if [ -x /usr/bin/aptitude ] ; then
34 APTUPDATE="aptitude update $DPKG_OPTIONS"
35 # Debian ISOs do not contain signed Release files
36 if [ -n "$ISO" ] ; then
37 APTINSTALL="aptitude -y --allow-untrusted --without-recommends install $DPKG_OPTIONS"
38 APTUPGRADE="aptitude -y --allow-untrusted safe-upgrade $DPKG_OPTIONS"
40 APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
41 APTUPGRADE="aptitude -y safe-upgrade $DPKG_OPTIONS"
44 APTINSTALL="apt-get -y --no-install-recommends install $DPKG_OPTIONS"
45 APTUPDATE="apt-get update $DPKG_OPTIONS"
46 APTUPGRADE="apt-get -y upgrade $DPKG_OPTIONS"
49 if [ -z "$STAGES" ] ; then
50 STAGES='/etc/debootstrap/stages'
51 [ -d "$STAGES" ] || mkdir -p "$STAGES"
55 # helper functions {{{
58 echo "$2" > "$STAGES/$1"
60 elif grep -q 'done' "$STAGES/$1" 2>/dev/null ; then
61 echo " [*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
64 echo " Executing stage ${1}"
69 # read -s emulation for dash. result is in $resp.
71 [ -t 0 ] && stty -echo
78 # define chroot mirror {{{
80 if [ "$KEEP_SRC_LIST" = "yes" ] ; then
81 echo "KEEP_SRC_LIST has been enabled, skipping chrootmirror stage."
85 if [ -z "$COMPONENTS" ] ; then
88 echo "Using repository components $COMPONENTS"
90 if [ -n "$ISO" ] ; then
91 echo "Adjusting sources.list for ISO (${ISO})."
92 echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
94 if [ -n "$MIRROR" ] ; then
95 echo "Adding mirror entry (${MIRROR}) to sources.list."
96 echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
99 if [ -n "$MIRROR" ] ; then
100 echo "Adjusting sources.list for mirror (${MIRROR})."
101 echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
105 # add security.debian.org:
107 unstable|sid|stretch) ;; # no security pool available
109 echo "Adding security.debian.org to sources.list."
110 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
113 # bullseye and newer releases use a different repository layout, see
114 # https://lists.debian.org/debian-devel-announce/2019/07/msg00004.html
115 echo "Adding security.debian.org/debian-security to sources.list."
116 echo "deb http://security.debian.org/debian-security ${RELEASE}-security $COMPONENTS" >> /etc/apt/sources.list
122 # remove local chroot mirror {{{
123 remove_chrootmirror() {
124 if [ "$KEEP_SRC_LIST" = "yes" ] ; then
125 echo "KEEP_SRC_LIST has been enabled, skipping remove_chrootmirror stage."
129 if [ -n "$ISO" ] ; then
130 echo "Removing ISO (${ISO}) from sources.list."
131 TMP_ISO="${ISO//\//\\\/}"
132 sed -i "/deb $TMP_ISO $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
134 if [ -n "$MIRROR" ] && echo "$MIRROR" | grep -q 'file:' ; then
135 echo "Removing local mirror (${MIRROR}) from sources.list."
136 TMP_MIRROR="${MIRROR//\//\\\/}"
137 sed -i "/deb $TMP_MIRROR $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
138 echo "Adding fallback mirror entry (${FALLBACK_MIRROR}) to sources.list instead."
139 echo "deb $FALLBACK_MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
145 # set up grml repository {{{
147 if [ -z "$GRMLREPOS" ] ; then
151 # user might have provided their own apt sources configuration
152 if [ -r /etc/apt/sources.list.d/grml.list ] ; then
153 echo "File /etc/apt/sources.list.d/grml.list exists already, not modifying."
155 echo "Setting up /etc/apt/sources.list.d/grml.list."
156 cat > /etc/apt/sources.list.d/grml.list << EOF
157 # grml: stable repository:
158 deb [signed-by=/usr/share/keyrings/grml-archive-keyring.gpg] http://deb.grml.org/ grml-stable main
159 deb-src [signed-by=/usr/share/keyrings/grml-archive-keyring.gpg] http://deb.grml.org/ grml-stable main
161 # grml: testing/development repository:
162 deb [signed-by=/usr/share/keyrings/grml-archive-keyring.gpg] http://deb.grml.org/ grml-testing main
163 deb-src [signed-by=/usr/share/keyrings/grml-archive-keyring.gpg] http://deb.grml.org/ grml-testing main
167 # make sure we install packages from Grml's pool only if not available from Debian
168 if [ -r /etc/apt/preferences.d/grml.pref ] ; then
169 echo "File /etc/apt/preferences.d/grml.pref exists already, not modifying."
171 echo "Setting up /etc/apt/preferences.d/grml.pref."
172 cat > /etc/apt/preferences.d/grml.pref << EOF
173 Explanation: use Grml repository only after Debian ones
175 Pin: origin deb.grml.org
180 apt-get update -o Acquire::AllowInsecureRepositories=1
181 apt-get -y --allow-unauthenticated install grml-debian-keyring
184 if [ "$(dpkg-query -f "\${db:Status-Status} \${db:Status-Eflag}" -W grml-debian-keyring 2>/dev/null)" != 'installed ok' ]; then
185 echo "Error: installation of grml-debian-keyring failed." >&2
191 # feature to provide Debian backports repos {{{
193 if [ -n "$BACKPORTREPOS" ] ; then
194 cat >> /etc/apt/sources.list.d/backports.list << EOF
195 # debian backports: ${RELEASE}-backports repository:
196 deb ${MIRROR} ${RELEASE}-backports main
197 deb-src ${MIRROR} ${RELEASE}-backports main
203 # set up kernel-img.conf {{{
205 if ! [ -r /etc/kernel-img.conf ] ; then
206 echo "Setting up /etc/kernel-img.conf"
207 cat > /etc/kernel-img.conf << EOF
208 # Kernel Image management overrides
209 # See kernel-img.conf(5) for details
217 # make sure services do not start up {{{
218 install_policy_rcd() {
219 if ! [ -r /usr/sbin/policy-rc.d ] ; then
221 cat > /usr/sbin/policy-rc.d << EOF
225 chmod 775 /usr/sbin/policy-rc.d
230 # make sure we have an up2date system {{{
232 if [ "$UPGRADE_SYSTEM" = "yes" ] ; then
233 echo "Running update + upgrade"
235 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTUPGRADE
237 echo "Not running update + upgrade as \$UPDATE_AND_UPGRADE is not set to 'yes'."
242 # remove now useless apt cache {{{
244 if [ "$RM_APTCACHE" = 'yes' ] ; then
245 echo "Cleaning apt cache."
246 # shellcheck disable=SC2086
247 apt-get clean $DPKG_OPTIONS
249 echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
254 # install additional packages {{{
256 # Pre-seed the debconf database with answers. Each question will be marked
257 # as seen to prevent debconf from asking the question interactively.
258 [ -f /etc/debootstrap/debconf-selections ] && {
259 echo "Preseeding the debconf database, some lines might be skipped..."
260 debconf-set-selections < /etc/debootstrap/debconf-selections
263 if [ "$PACKAGES" = 'yes' ] ; then
264 PACKAGES_FILE="/etc/debootstrap/packages"
266 if [ "$ARCH" = 'arm64' ]; then
267 PACKAGES_FILE="/etc/debootstrap/packages-arm64"
270 if ! [ -r "${PACKAGES_FILE}" ] ; then
271 echo "Error: ${PACKAGES_FILE} (inside chroot) not found, exiting." >&2
276 # shellcheck disable=SC2086,SC2046
277 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' "${PACKAGES_FILE}") $GRMLPACKAGES
283 # install extra packages {{{
285 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
286 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
287 if [ -n "$PACKAGELIST" ]; then
288 # shellcheck disable=SC2086
290 # run apt again to resolve any deps
291 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
297 # check if the specified Debian package exists
299 output=$(apt-cache show "$1" 2>/dev/null)
305 # determine the kernel version postfix
306 get_kernel_version() {
307 # do not override $KERNEL if set via config file
308 if [ -n "$KERNEL" ] ; then
315 # shellcheck disable=SC2153
327 echo "Only i386, amd64 and arm64 are currently supported" >&2
332 KPACKAGE=linux-image-"${KPREFIX}${KARCH}"
333 if package_exists "$KPACKAGE"; then
334 echo "${KPREFIX}${KARCH}"
338 echo "Expected kernel package $KPACKAGE not found" >&2
342 # install kernel packages {{{
344 if [ -n "$NOKERNEL" ] ; then
345 echo "Skipping installation of kernel packages as requested via --nokernel"
350 KVER=$(get_kernel_version)
351 if [ -n "$KVER" ] ; then
354 echo "Installing busybox on Debian/$RELEASE as it's essential for the initramfs"
355 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL busybox
359 KERNELPACKAGES="linux-image-$KVER linux-headers-$KVER firmware-linux-free $INITRD_GENERATOR"
360 # only add firmware-linux if we have non-free as a component
361 if expr "$COMPONENTS" : '.*non-free' >/dev/null ; then
362 KERNELPACKAGES="$KERNELPACKAGES firmware-linux"
364 # shellcheck disable=SC2086
365 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
367 echo "Warning: Could not find a kernel for your system. Your system won't be able to boot itself!"
372 # reconfigure packages {{{
374 if [ -n "$RECONFIGURE" ] ; then
375 for package in $RECONFIGURE ; do
376 if dpkg --list "$package" >/dev/null 2>&1 | grep -q '^ii' ; then
377 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure "$package" || \
378 echo "Warning: $package does not exist, can not reconfigure it."
385 # set password of user root {{{
388 if [ -n "$NOPASSWORD" ] ; then
389 echo "Skip setting root password as requested."
394 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
398 if [ -n "$ROOTPASSWORD" ] ; then
399 # shellcheck disable=SC2086
400 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
401 export ROOTPASSWORD=''
405 echo "Setting password for user root:"
406 while [ "$a" != "$b" ] ; do
407 printf "Enter new UNIX password for user root: "
412 printf "Retype new UNIX password for user root: "
417 if [ "$a" != "$b" ] ; then
418 echo "Sorry, passwords do not match. Retry."
422 # shellcheck disable=SC2086
423 echo root:"$a" | chpasswd $CHPASSWD_OPTION
432 # set up /etc/hosts {{{
434 if ! [ -f /etc/hosts ] ; then
435 cat > /etc/hosts << EOF
437 ::1 localhost ip6-localhost ip6-loopback
439 ff02::2 ip6-allrouters
445 # set default locales {{{
447 if [ -n "$DEFAULT_LOCALES" ] ; then
448 if ! [ -x /usr/sbin/update-locale ] ; then
449 echo "Warning: update-locale executable not available (no locales package installed?)"
450 echo "Ignoring request to run update-locale for $DEFAULT_LOCALES therefore"
454 /usr/sbin/update-locale LANGUAGE="$DEFAULT_LANGUAGE" LANG="$DEFAULT_LOCALES"
459 # adjust timezone {{{
461 if [ -n "$TIMEZONE" ] ; then
462 echo "Adjusting /etc/localtime"
463 ln -sf "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime
465 echo "Setting /etc/timezone to $TIMEZONE"
466 printf '%s\n' "$TIMEZONE" > /etc/timezone
472 # helper function for fstab() {{{
474 echo "Setting up /etc/fstab"
475 cat > /etc/fstab <<EOF
476 # /etc/fstab - created by grml-debootstrap on $(date)
477 # Accessible filesystems, by reference, are maintained under '/dev/disk/'.
478 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
480 # After editing this file, run 'systemctl daemon-reload' to update systemd
481 # units generated from this file.
485 if [ -n "$TARGET_UUID" ] ; then
486 local rootfs_mount_options=""
488 if [ -z "${FILESYSTEM}" ] ; then
489 FILESYSTEM="$(blkid -o value -s TYPE /dev/disk/by-uuid/"${TARGET_UUID}")" || true
492 case "${FILESYSTEM}" in
493 # errors=remount-ro is supported only by a few file systems
494 ext*|exfat|fat|jfs|nilfs2|vfat)
495 rootfs_mount_options=",errors=remount-ro"
499 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults${rootfs_mount_options} 0 1" >> /etc/fstab
501 echo "Warning: couldn't identify target UUID for rootfs, your /etc/fstab might be incomplete."
504 if [ -n "$EFI" ] ; then
505 UUID_EFI="$(blkid -o value -s UUID "$EFI")"
506 echo "UUID=$UUID_EFI /boot/efi vfat umask=0077 0 1" >> /etc/fstab
509 cat >> /etc/fstab << EOF
510 proc /proc proc defaults 0 0
511 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
512 # some other examples:
513 # /dev/sda2 none swap sw,pri=0 0 0
514 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
515 # //1.2.3.4/pub /smb/pub cifs user,noauto,uid=grml,gid=grml 0 0
516 # linux:/pub /beer nfs defaults 0 0
517 # tmpfs /tmp tmpfs size=300M 0 0
518 # /dev/sda5 none swap sw 0 0
523 # generate /etc/fstab {{{
525 # set up /etc/fstab if file is not present (cdebootstrap)
526 if [ ! -f /etc/fstab ] ; then
530 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
531 if grep -q UNCONFIGURED /etc/fstab ; then
537 # ensure we have according filesystem tools available {{{
541 # note: this is supposed to be coming either via command lines'
542 # $_opt_filesystem or via createfstab()
543 case "${FILESYSTEM}" in
552 if [ -n "${pkg:-}" ] && ! dpkg --list "${pkg}" 2>/dev/null | grep -q '^ii' ; then
553 echo "Filesystem package ${pkg} not present, installing now"
554 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL "${pkg}"
559 # set up hostname {{{
561 if [ -n "$HOSTNAME" ] ; then
562 echo "Setting hostname to ${HOSTNAME}."
563 echo "$HOSTNAME" > /etc/hostname
565 # adjust postfix configuration
566 if [ -r /etc/postfix/main.cf ] ; then
567 # adjust hostname related options:
568 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
570 # listen on loopback interface only:
571 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
572 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
574 if [ -r /etc/mailname ] ; then
575 # adjust /etc/mailname
576 local etc_mail_domain
577 etc_mail_domain=$(/bin/dnsdomainname 2>/dev/null || echo localdomain)
580 local mailname="$HOSTNAME"
583 local mailname="${HOSTNAME}.${etc_mail_domain}"
586 echo "Setting mailname to ${mailname}"
587 echo "$mailname" > /etc/mailname
593 # generate initrd/initramfs {{{
595 # assume the first available kernel as our main kernel
596 # shellcheck disable=SC2012
597 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
598 if [ -z "$KERNELIMG" ] ; then
599 echo 'No kernel image found, skipping initrd stuff.'>&2
603 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
606 if [ -n "$INITRD" ] ; then
607 echo "Generating initrd."
608 if [ "$INITRD_GENERATOR" = 'dracut' ] ; then
609 # shellcheck disable=SC2086
610 dracut --no-hostonly --kver "$KERNELVER" --fstab --add-fstab /etc/fstab --force --reproducible $INITRD_GENERATOR_OPTS
612 # shellcheck disable=SC2086
613 update-initramfs -c -t -k "$KERNELVER" $INITRD_GENERATOR_OPTS
620 if [ -z "$EFI" ] ; then
624 if ! dpkg --list efibootmgr 2>/dev/null | grep -q '^ii' ; then
625 echo "Notice: efi option set but no efibootmgr package, installing it therefore."
626 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL efibootmgr
630 echo "Mounting $EFI on /boot/efi"
631 mount "$EFI" /boot/efi
633 # if efivarfs kernel module is loaded, but efivars isn't,
634 # then we need to mount efivarfs for efibootmgr usage
635 if ! ls /sys/firmware/efi/efivars/* &>/dev/null ; then
636 echo "Mounting efivarfs on /sys/firmware/efi/efivars"
637 mount -t efivarfs efivarfs /sys/firmware/efi/efivars
640 echo "Invoking efibootmgr"
644 # grub configuration/installation {{{
646 # helper function to get relevant /dev/disk/by-id/* entries,
647 # based on GRUB's postinst script
651 [ -d /dev/disk/by-id ] || return
653 for path in /dev/disk/by-id/*; do
654 [ -e "${path}" ] || continue
655 printf '%s %s\n' "${path}" "$(readlink -f "${path}")"
656 done | sort -k2 -s -u | cut -d' ' -f1
661 # helper function to report corresponding /dev/disk/by-id/ for a given device name,
662 # based on GRUB's postinst script
666 for id in $(available_ids); do
667 if [ "$(readlink -f "${id}")" = "$(readlink -f "$1")" ]; then
673 # Fall back to the plain device name if there's no by-id link for it.
683 if [ -z "$GRUB" ] ; then
684 echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
690 if [ -n "$EFI" ] ; then
691 GRUB_PACKAGE=grub-efi-amd64
696 # make sure this is pre-defined so we have sane settings for automated
697 # upgrades, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=711019
699 grub_device=$(device_to_id "${GRUB}")
700 if [ -z "${grub_device:-}" ] ; then
701 echo "Warning: Could not identify /dev/disk/by-id/... for '${GRUB}', falling back to '${GRUB}'"
702 grub_device="${GRUB}"
705 echo "Setting ${GRUB_PACKAGE} debconf configuration for install device to $GRUB"
706 echo "${GRUB_PACKAGE} ${GRUB_PACKAGE}/install_devices multiselect ${grub_device}" | debconf-set-selections
708 if ! dpkg --list "${GRUB_PACKAGE}" 2>/dev/null | grep -q '^ii' ; then
709 echo "Notice: grub option set but no ${GRUB_PACKAGE} package, installing it therefore."
710 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL "${GRUB_PACKAGE}"
713 if ! [ -x "$(command -v grub-install)" ] ; then
714 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
717 if ! [ -x "$(command -v update-grub)" ] ; then
718 echo "Error: update-grub not available. (Error while installing grub package?)" >&2
722 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
723 for device in $SELECTED_PARTITIONS ; do
724 GRUB="${device%%[0-9]}"
725 echo "Installing grub on ${GRUB}:"
726 if ! grub-install --no-floppy "$GRUB" ; then
727 echo "Error: failed to execute 'grub-install --no-floppy $GRUB'." >&2
732 rm -f /boot/grub/device.map
734 echo "Installing grub on ${GRUB}:"
735 echo "(hd0) ${GRUB}" > /boot/grub/device.map
736 if ! grub-install "(hd0)" ; then
737 echo "Error: failed to execute 'grub-install (hd0)'." >&2
740 rm /boot/grub/device.map
743 echo "Adjusting grub configuration for use on ${GRUB}."
745 if [ -n "${BOOT_APPEND}" ] ; then
746 echo "Adding BOOT_APPEND configuration ['${BOOT_APPEND}'] to /etc/default/grub."
747 sed -i "/GRUB_CMDLINE_LINUX_DEFAULT/ s#\"\$# ${BOOT_APPEND}\"#" /etc/default/grub
750 mountpoint /boot/efi &>/dev/null && umount /boot/efi
752 # finally install grub. Existence of update-grub is checked above.
757 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
759 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
761 for script in /etc/debootstrap/chroot-scripts/* ; do
762 echo "Executing script $script"
763 $script && echo "done" || echo "failed"
768 # make sure we don't have any running processes left {{{
770 for service in ssh mdadm mdadm-raid ; do
771 if [ -x /etc/init.d/"$service" ] ; then
772 /etc/init.d/"$service" stop || true
778 # unmount /proc and make sure nothing is left {{{
780 # make sure we don't leave any sensible data
781 rm -f /etc/debootstrap/variables
783 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
785 umount /sys/firmware/efi/efivars &>/dev/null || true
787 umount /sys >/dev/null 2>/dev/null || true
788 umount /proc >/dev/null 2>/dev/null || true
795 [ -n "$1" ] && EXIT="$1" || EXIT="1"
800 # set signal handler {{{
801 trap signal_handler HUP INT QUIT TERM
804 # execute the functions {{{
806 # always execute install_policy_rcd
809 for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
810 kernel packages extrapackages reconfigure hosts \
811 default_locales timezone fstab install_fs_tools hostname \
812 initrd grub_install passwords \
813 custom_scripts upgrade_system remove_apt_cache services \
814 remove_chrootmirror; do
820 # always execute the finalize stage:
824 # finally exit the chroot {{{
825 echo "Finished chroot installation, exiting."
829 ## END OF FILE #################################################################
830 # vim: ai tw=80 expandtab foldmethod=marker