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 ################################################################################
13 if [ "$REPORT_TRAP_ERR" = "yes" ] || [ "$FAIL_TRAP_ERR" = "yes" ]; then
16 trap "error_handler" ERR
20 # shellcheck disable=SC1091
21 . /etc/debootstrap/config || exit 1
22 # shellcheck disable=SC1091
23 . /etc/debootstrap/variables || exit 1
25 [ -r /proc/1 ] || mount -t proc none /proc
26 [ -r /sys/kernel ] || mount -t sysfs none /sys
30 # use aptitude only if it's available
31 if [ -x /usr/bin/aptitude ] ; then
32 APTUPDATE="aptitude update $DPKG_OPTIONS"
33 # Debian ISOs do not contain signed Release files
34 if [ -n "$ISO" ] ; then
35 APTINSTALL="aptitude -y --allow-untrusted --without-recommends install $DPKG_OPTIONS"
36 APTUPGRADE="aptitude -y --allow-untrusted safe-upgrade $DPKG_OPTIONS"
38 APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
39 APTUPGRADE="aptitude -y safe-upgrade $DPKG_OPTIONS"
42 APTINSTALL="apt-get -y --no-install-recommends install $DPKG_OPTIONS"
43 APTUPDATE="apt-get update $DPKG_OPTIONS"
44 APTUPGRADE="apt-get -y upgrade $DPKG_OPTIONS"
47 if [ -z "$STAGES" ] ; then
48 STAGES='/etc/debootstrap/stages'
49 [ -d "$STAGES" ] || mkdir -p "$STAGES"
53 # helper functions {{{
56 echo "$2" > "$STAGES/$1"
58 elif grep -q 'done' "$STAGES/$1" 2>/dev/null ; then
59 echo " [*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
62 echo " Executing stage ${1}"
67 # read -s emulation for dash. result is in $resp.
69 [ -t 0 ] && stty -echo
76 # define chroot mirror {{{
78 if [ "$KEEP_SRC_LIST" = "yes" ] ; then
79 echo "KEEP_SRC_LIST has been enabled, skipping chrootmirror stage."
83 if [ -z "$COMPONENTS" ] ; then
86 echo "Using repository components $COMPONENTS"
88 if [ -n "$ISO" ] ; then
89 echo "Adjusting sources.list for ISO (${ISO})."
90 echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
92 if [ -n "$MIRROR" ] ; then
93 echo "Adding mirror entry (${MIRROR}) to sources.list."
94 echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
97 if [ -n "$MIRROR" ] ; then
98 echo "Adjusting sources.list for mirror (${MIRROR})."
99 echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
106 if [ -n "$MIRROR" ] ; then
107 echo "Release matching $RELEASE - enabling LTS support in sources.list"
108 echo "deb $MIRROR ${RELEASE}-lts $COMPONENTS" >> /etc/apt/sources.list
113 # add security.debian.org:
115 unstable|sid|lenny) ;; # no security pool available
116 squeeze|wheezy|jessie|stretch|buster)
117 echo "Adding security.debian.org to sources.list."
118 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
121 # bullseye and newer releases use a different repository layout, see
122 # https://lists.debian.org/debian-devel-announce/2019/07/msg00004.html
123 echo "Adding security.debian.org/debian-security to sources.list."
124 echo "deb http://security.debian.org/debian-security ${RELEASE}-security $COMPONENTS" >> /etc/apt/sources.list
130 # remove local chroot mirror {{{
131 remove_chrootmirror() {
132 if [ "$KEEP_SRC_LIST" = "yes" ] ; then
133 echo "KEEP_SRC_LIST has been enabled, skipping remove_chrootmirror stage."
137 if [ -n "$ISO" ] ; then
138 echo "Removing ISO (${ISO}) from sources.list."
139 TMP_ISO="${ISO//\//\\\/}"
140 sed -i "/deb $TMP_ISO $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
142 if [ -n "$MIRROR" ] && echo "$MIRROR" | grep -q 'file:' ; then
143 echo "Removing local mirror (${MIRROR}) from sources.list."
144 TMP_MIRROR="${MIRROR//\//\\\/}"
145 sed -i "/deb $TMP_MIRROR $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
146 echo "Adding fallback mirror entry (${FALLBACK_MIRROR}) to sources.list instead."
147 echo "deb $FALLBACK_MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
153 # set up grml repository {{{
155 if [ -n "$GRMLREPOS" ] ; then
156 # user might have provided their own apt sources.list
157 if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
158 cat >> /etc/apt/sources.list.d/grml.list << EOF
159 # grml: stable repository:
160 deb http://deb.grml.org/ grml-stable main
161 deb-src http://deb.grml.org/ grml-stable main
163 # grml: testing/development repository:
164 deb http://deb.grml.org/ grml-testing main
165 deb-src http://deb.grml.org/ grml-testing main
169 # shellcheck disable=SC2086
170 if apt-get update $DPKG_OPTIONS; then
171 # shellcheck disable=SC2086
172 apt-get -y --allow-unauthenticated install grml-debian-keyring $DPKG_OPTIONS
173 # shellcheck disable=SC2086
174 apt-get update $DPKG_OPTIONS
176 # make sure we have the keys available for aptitude
177 gpg --keyserver subkeys.pgp.net --recv-keys 709BCE51568573EBC160E590F61E2E7CECDEA787
178 gpg --export 709BCE51568573EBC160E590F61E2E7CECDEA787 | apt-key add - || true # not yet sure
179 # why it's necessary, sometimes we get an error even though it works [mika]
182 # make sure we install packages from Grml's pool only if not available
184 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
185 cat >> /etc/apt/preferences << EOF
186 // debian pool (default):
188 Pin: release o=Debian
191 // main grml-repository:
193 Pin: origin deb.grml.org
201 # feature to provide Debian backports repos {{{
203 if [ -n "$BACKPORTREPOS" ] ; then
204 cat >> /etc/apt/sources.list.d/backports.list << EOF
205 # debian backports: ${RELEASE}-backports repository:
206 deb ${MIRROR} ${RELEASE}-backports main
207 deb-src ${MIRROR} ${RELEASE}-backports main
213 # set up kernel-img.conf {{{
215 if ! [ -r /etc/kernel-img.conf ] ; then
216 echo "Setting up /etc/kernel-img.conf"
217 cat > /etc/kernel-img.conf << EOF
218 # Kernel Image management overrides
219 # See kernel-img.conf(5) for details
227 # make sure services do not start up {{{
228 install_policy_rcd() {
229 if ! [ -r /usr/sbin/policy-rc.d ] ; then
231 cat > /usr/sbin/policy-rc.d << EOF
235 chmod 775 /usr/sbin/policy-rc.d
240 # make sure we have an up2date system {{{
242 if [ "$UPGRADE_SYSTEM" = "yes" ] ; then
243 echo "Running update + upgrade"
245 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTUPGRADE
247 echo "Not running update + upgrade as \$UPDATE_AND_UPGRADE is not set to 'yes'."
252 # remove now useless apt cache {{{
254 if [ "$RM_APTCACHE" = 'yes' ] ; then
255 echo "Cleaning apt cache."
256 # shellcheck disable=SC2086
257 apt-get clean $DPKG_OPTIONS
259 echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
264 # install additional packages {{{
266 # Pre-seed the debconf database with answers. Each question will be marked
267 # as seen to prevent debconf from asking the question interactively.
268 [ -f /etc/debootstrap/debconf-selections ] && {
269 echo "Preseeding the debconf database, some lines might be skipped..."
270 debconf-set-selections < /etc/debootstrap/debconf-selections
273 if [ "$PACKAGES" = 'yes' ] ; then
274 if ! [ -r /etc/debootstrap/packages ] ; then
275 echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
279 # shellcheck disable=SC2086,SC2046
280 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
286 # install extra packages {{{
288 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
289 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
290 if [ -n "$PACKAGELIST" ]; then
291 # shellcheck disable=SC2086
293 # run apt again to resolve any deps
294 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
300 # check if the specified Debian package exists
302 output=$(apt-cache show "$1" 2>/dev/null)
308 # determine the kernel version postfix
309 get_kernel_version() {
310 # do not override $KERNEL if set via config file
311 if [ -n "$KERNEL" ] ; then
318 # shellcheck disable=SC2153
322 lenny|squeeze|wheezy) KARCH='686' ;;
323 # since jessie the linux-image-686 image doesn't exist any longer
324 *) KARCH='686-pae' ;;
331 echo "Only i386 and amd64 are currently supported" >&2
335 for KPREFIX in "" "2.6-" ; do # iterate through the kernel prefixes,
336 # currently "" and "2.6-"
337 if package_exists linux-image-${KPREFIX}${KARCH} ; then
338 echo ${KPREFIX}${KARCH}
345 # install kernel packages {{{
347 if [ -n "$NOKERNEL" ] ; then
348 echo "Skipping installation of kernel packages as requested via --nokernel"
355 kernel_version=$(get_kernel_version)
357 if [ -z "${kernel_version}" ] ; then
358 echo "Error: could not find a kernel for your system. Your system won't be able to boot itself!" >&2
362 # defaults (note: install busybox to be able to debug initramfs)
363 KERNELPACKAGES="linux-image-${kernel_version} linux-headers-${kernel_version} busybox firmware-linux-free"
365 # only add firmware-linux if we have non-free as a component
366 if expr "$COMPONENTS" : '.*non-free' >/dev/null ; then
367 KERNELPACKAGES="$KERNELPACKAGES firmware-linux"
370 # when installing into a VM using buster or newer, install only
371 # linux-image-cloud-amd64 (and no headers and firmware packages)
372 if [ -n "${VMSIZE}" ] && [ "${ARCH:-}" = "amd64" ] ; then
374 lenny|squeeze|wheezy|jessie|stretch)
377 echo "Note: installing into VM, choosing linux-image-cloud-amd64 kernel package"
378 KERNELPACKAGES="linux-image-cloud-amd64"
383 # shellcheck disable=SC2086
384 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
388 # reconfigure packages {{{
390 if [ -n "$RECONFIGURE" ] ; then
391 for package in $RECONFIGURE ; do
392 if dpkg --list "$package" >/dev/null 2>&1 | grep -q '^ii' ; then
393 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure "$package" || \
394 echo "Warning: $package does not exist, can not reconfigure it."
401 # set password of user root {{{
404 if [ -n "$NOPASSWORD" ] ; then
405 echo "Skip setting root password as requested."
409 echo "Activating shadow passwords."
413 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
417 if [ -n "$ROOTPASSWORD" ] ; then
418 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
419 export ROOTPASSWORD=''
423 echo "Setting password for user root:"
424 while [ "$a" != "$b" ] ; do
425 printf "Enter new UNIX password for user root: "
430 printf "Retype new UNIX password for user root: "
435 if [ "$a" != "$b" ] ; then
436 echo "Sorry, passwords do not match. Retry."
440 echo root:"$a" | chpasswd $CHPASSWD_OPTION
449 # set up /etc/hosts {{{
451 if ! [ -f /etc/hosts ] ; then
452 cat > /etc/hosts << EOF
454 ::1 localhost ip6-localhost ip6-loopback
456 ff02::2 ip6-allrouters
462 # set default locales {{{
464 if [ -n "$DEFAULT_LOCALES" ] ; then
465 if ! [ -x /usr/sbin/update-locale ] ; then
466 echo "Warning: update-locale executable not available (no locales package installed?)"
467 echo "Ignoring request to run update-locale for $DEFAULT_LOCALES therefore"
471 /usr/sbin/update-locale LANGUAGE="$DEFAULT_LANGUAGE" LANG="$DEFAULT_LOCALES"
476 # adjust timezone {{{
478 if [ -n "$TIMEZONE" ] ; then
479 echo "Adjusting /etc/localtime"
480 ln -sf "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime
482 echo "Setting /etc/timezone to $TIMEZONE"
483 printf '%s\n' "$TIMEZONE" > /etc/timezone
489 # helper function for fstab() {{{
491 echo "Setting up /etc/fstab"
492 cat > /etc/fstab <<EOF
493 # /etc/fstab - created by grml-debootstrap on $(date)
494 # Accessible filesystems, by reference, are maintained under '/dev/disk/'.
495 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
497 # After editing this file, run 'systemctl daemon-reload' to update systemd
498 # units generated from this file.
502 if [ -n "$TARGET_UUID" ] ; then
503 local rootfs_mount_options=",errors=remount-ro"
504 case "${FILESYSTEM}" in
506 # errors=remount-ro is unsupported, see https://github.com/grml/grml-debootstrap/issues/163
507 rootfs_mount_options=""
511 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults${rootfs_mount_options} 0 1" >> /etc/fstab
513 echo "Warning: couldn't identify target UUID for rootfs, your /etc/fstab might be incomplete."
516 if [ -n "$EFI" ] ; then
517 # shellcheck disable=SC2086
518 echo "UUID=$(blkid -o value -s UUID $EFI) /boot/efi vfat umask=0077 0 1" >> /etc/fstab
521 cat >> /etc/fstab << EOF
522 proc /proc proc defaults 0 0
523 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
524 # some other examples:
525 # /dev/sda2 none swap sw,pri=0 0 0
526 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
527 # //1.2.3.4/pub /smb/pub cifs user,noauto,uid=grml,gid=grml 0 0
528 # linux:/pub /beer nfs defaults 0 0
529 # tmpfs /tmp tmpfs size=300M 0 0
530 # /dev/sda5 none swap sw 0 0
535 # generate /etc/fstab {{{
537 # set up /etc/fstab if file is not present (cdebootstrap)
538 if [ ! -f /etc/fstab ] ; then
542 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
543 if grep -q UNCONFIGURED /etc/fstab ; then
549 # set up hostname {{{
551 if [ -n "$HOSTNAME" ] ; then
552 echo "Setting hostname to ${HOSTNAME}."
553 echo "$HOSTNAME" > /etc/hostname
555 # adjust postfix configuration
556 if [ -r /etc/postfix/main.cf ] ; then
557 # adjust hostname related options:
558 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
560 # listen on loopback interface only:
561 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
562 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
564 if [ -r /etc/mailname ] ; then
565 # adjust /etc/mailname
566 local etc_mail_domain
567 etc_mail_domain=$(/bin/dnsdomainname 2>/dev/null || echo localdomain)
570 local mailname="$HOSTNAME"
573 local mailname="${HOSTNAME}.${etc_mail_domain}"
576 echo "Setting mailname to ${mailname}"
577 echo "$mailname" > /etc/mailname
583 # generate initrd/initramfs {{{
585 # assume the first available kernel as our main kernel
586 # shellcheck disable=SC2012
587 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
588 if [ -z "$KERNELIMG" ] ; then
589 echo 'No kernel image found, skipping initrd stuff.'>&2
593 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
596 if [ -n "$INITRD" ] ; then
597 echo "Generating initrd."
598 update-initramfs -c -t -k "$KERNELVER"
604 if [ -z "$EFI" ] ; then
608 if ! dpkg --list efibootmgr 2>/dev/null | grep -q '^ii' ; then
609 echo "Notice: efi option set but no efibootmgr package, installing it therefore."
610 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL efibootmgr
614 echo "Mounting $EFI on /boot/efi"
615 mount "$EFI" /boot/efi || return 1
617 # if efivarfs kernel module is loaded, but efivars isn't,
618 # then we need to mount efivarfs for efibootmgr usage
619 if ! ls /sys/firmware/efi/efivars/* &>/dev/null ; then
620 echo "Mounting efivarfs on /sys/firmware/efi/efivars"
621 mount -t efivarfs efivarfs /sys/firmware/efi/efivars
624 echo "Invoking efibootmgr"
625 efibootmgr || return 1
627 umount /sys/firmware/efi/efivars &>/dev/null || true
631 # grub configuration/installation {{{
634 if [ -z "$GRUB" ] ; then
635 echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
639 efi_setup || return 1
641 if [ -n "$EFI" ] ; then
642 GRUB_PACKAGE=grub-efi-amd64
647 # make sure this is pre-defined so we have sane settings for automated
648 # upgrades, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=711019
649 echo "Setting ${GRUB_PACKAGE} debconf configuration for install device to $GRUB"
650 echo "${GRUB_PACKAGE} ${GRUB_PACKAGE}/install_devices multiselect $GRUB" | debconf-set-selections
652 if ! dpkg --list ${GRUB_PACKAGE} 2>/dev/null | grep -q '^ii' ; then
653 echo "Notice: grub option set but no ${GRUB_PACKAGE} package, installing it therefore."
654 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL ${GRUB_PACKAGE}
657 if ! [ -x "$(command -v grub-install)" ] ; then
658 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
662 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
663 for device in $SELECTED_PARTITIONS ; do
664 GRUB="${device%%[0-9]}"
665 echo "Installing grub on ${GRUB}:"
666 if ! grub-install --no-floppy "$GRUB" ; then
667 echo "Error: failed to execute 'grub-install --no-floppy $GRUB'." >&2
672 rm -f /boot/grub/device.map
674 echo "Installing grub on ${GRUB}:"
676 lenny|squeeze|wheezy)
678 grub_dev="$(readlink -f "${GRUB}")"
679 if ! grub-install --no-floppy "${grub_dev}" ; then
680 echo "Error: failed to execute 'grub-install --no-floppy ${grub_dev}'." >&2
683 rm -f /boot/grub/device.map
686 echo "(hd0) ${GRUB}" > /boot/grub/device.map
687 if ! grub-install "(hd0)" ; then
688 echo "Error: failed to execute 'grub-install (hd0)'." >&2
691 rm /boot/grub/device.map
696 echo "Adjusting grub configuration for use on ${GRUB}."
698 # finally install grub
699 if [ -x /usr/sbin/update-grub ] ; then
700 UPDATEGRUB='/usr/sbin/update-grub'
701 elif [ -x /sbin/update-grub ] ; then
702 UPDATEGRUB='/sbin/update-grub'
704 echo "Error: update-grub not available, can not execute it." >&2
708 if [ -n "${BOOT_APPEND}" ] ; then
709 echo "Adding BOOT_APPEND configuration ['${BOOT_APPEND}'] to /etc/default/grub."
710 sed -i "/GRUB_CMDLINE_LINUX_DEFAULT/ s#\"\$# ${BOOT_APPEND}\"#" /etc/default/grub
713 mountpoint /boot/efi &>/dev/null && umount /boot/efi
719 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
721 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
723 for script in /etc/debootstrap/chroot-scripts/* ; do
724 echo "Executing script $script"
725 $script && echo "done" || echo "failed"
730 # make sure we don't have any running processes left {{{
732 for service in ssh mdadm mdadm-raid ; do
733 if [ -x /etc/init.d/"$service" ] ; then
734 /etc/init.d/"$service" stop || true
740 # unmount /proc and make sure nothing is left {{{
742 # make sure we don't leave any sensible data
743 rm -f /etc/debootstrap/variables
745 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
747 umount /sys >/dev/null 2>/dev/null || true
748 umount /proc >/dev/null 2>/dev/null || true
755 [ -n "$1" ] && EXIT="$1" || EXIT="1"
760 # set signal handler {{{
761 trap signal_handler HUP INT QUIT TERM
764 # execute the functions {{{
766 # always execute install_policy_rcd
769 for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
770 kernel packages extrapackages reconfigure hosts \
771 default_locales timezone fstab hostname initrd grub_install passwords \
772 custom_scripts upgrade_system remove_apt_cache services \
773 remove_chrootmirror; do
775 $i && stage $i 'done' || exit 1
778 # always execute the finalize stage:
782 # finally exit the chroot {{{
783 echo "Finished chroot installation, exiting."
787 ## END OF FILE #################################################################
788 # vim: ai tw=80 expandtab foldmethod=marker