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"
353 KVER=$(get_kernel_version)
354 if [ -n "$KVER" ] ; then
355 # note: install busybox to be able to debug initramfs
356 KERNELPACKAGES="linux-image-$KVER linux-headers-$KVER busybox firmware-linux-free"
357 # only add firmware-linux if we have non-free as a component
358 if expr "$COMPONENTS" : '.*non-free' >/dev/null ; then
359 KERNELPACKAGES="$KERNELPACKAGES firmware-linux"
361 # shellcheck disable=SC2086
362 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
364 echo "Warning: Could not find a kernel for your system. Your system won't be able to boot itself!"
369 # reconfigure packages {{{
371 if [ -n "$RECONFIGURE" ] ; then
372 for package in $RECONFIGURE ; do
373 if dpkg --list "$package" >/dev/null 2>&1 | grep -q '^ii' ; then
374 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure "$package" || \
375 echo "Warning: $package does not exist, can not reconfigure it."
382 # set password of user root {{{
385 if [ -n "$NOPASSWORD" ] ; then
386 echo "Skip setting root password as requested."
390 echo "Activating shadow passwords."
394 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
398 if [ -n "$ROOTPASSWORD" ] ; then
399 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
400 export ROOTPASSWORD=''
404 echo "Setting password for user root:"
405 while [ "$a" != "$b" ] ; do
406 printf "Enter new UNIX password for user root: "
411 printf "Retype new UNIX password for user root: "
416 if [ "$a" != "$b" ] ; then
417 echo "Sorry, passwords do not match. Retry."
421 echo root:"$a" | chpasswd $CHPASSWD_OPTION
430 # set up /etc/hosts {{{
432 if ! [ -f /etc/hosts ] ; then
433 cat > /etc/hosts << EOF
435 ::1 localhost ip6-localhost ip6-loopback
437 ff02::2 ip6-allrouters
443 # set default locales {{{
445 if [ -n "$DEFAULT_LOCALES" ] ; then
446 if ! [ -x /usr/sbin/update-locale ] ; then
447 echo "Warning: update-locale executable not available (no locales package installed?)"
448 echo "Ignoring request to run update-locale for $DEFAULT_LOCALES therefore"
452 /usr/sbin/update-locale LANGUAGE="$DEFAULT_LANGUAGE" LANG="$DEFAULT_LOCALES"
457 # adjust timezone {{{
459 if [ -n "$TIMEZONE" ] ; then
460 echo "Adjusting /etc/localtime"
461 ln -sf "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime
463 echo "Setting /etc/timezone to $TIMEZONE"
464 printf '%s\n' "$TIMEZONE" > /etc/timezone
470 # helper function for fstab() {{{
472 echo "Setting up /etc/fstab"
473 cat > /etc/fstab <<EOF
474 # /etc/fstab - created by grml-debootstrap on $(date)
475 # Accessible filesystems, by reference, are maintained under '/dev/disk/'.
476 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
478 # After editing this file, run 'systemctl daemon-reload' to update systemd
479 # units generated from this file.
483 if [ -n "$TARGET_UUID" ] ; then
484 local rootfs_mount_options=",errors=remount-ro"
485 case "${FILESYSTEM}" in
487 # errors=remount-ro is unsupported, see https://github.com/grml/grml-debootstrap/issues/163
488 rootfs_mount_options=""
492 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults${rootfs_mount_options} 0 1" >> /etc/fstab
494 echo "Warning: couldn't identify target UUID for rootfs, your /etc/fstab might be incomplete."
497 if [ -n "$EFI" ] ; then
498 # shellcheck disable=SC2086
499 echo "UUID=$(blkid -o value -s UUID $EFI) /boot/efi vfat umask=0077 0 1" >> /etc/fstab
502 cat >> /etc/fstab << EOF
503 proc /proc proc defaults 0 0
504 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
505 # some other examples:
506 # /dev/sda2 none swap sw,pri=0 0 0
507 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
508 # //1.2.3.4/pub /smb/pub cifs user,noauto,uid=grml,gid=grml 0 0
509 # linux:/pub /beer nfs defaults 0 0
510 # tmpfs /tmp tmpfs size=300M 0 0
511 # /dev/sda5 none swap sw 0 0
516 # generate /etc/fstab {{{
518 # set up /etc/fstab if file is not present (cdebootstrap)
519 if [ ! -f /etc/fstab ] ; then
523 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
524 if grep -q UNCONFIGURED /etc/fstab ; then
530 # set up hostname {{{
532 if [ -n "$HOSTNAME" ] ; then
533 echo "Setting hostname to ${HOSTNAME}."
534 echo "$HOSTNAME" > /etc/hostname
536 # adjust postfix configuration
537 if [ -r /etc/postfix/main.cf ] ; then
538 # adjust hostname related options:
539 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
541 # listen on loopback interface only:
542 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
543 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
545 if [ -r /etc/mailname ] ; then
546 # adjust /etc/mailname
547 local etc_mail_domain
548 etc_mail_domain=$(/bin/dnsdomainname 2>/dev/null || echo localdomain)
551 local mailname="$HOSTNAME"
554 local mailname="${HOSTNAME}.${etc_mail_domain}"
557 echo "Setting mailname to ${mailname}"
558 echo "$mailname" > /etc/mailname
564 # generate initrd/initramfs {{{
566 # assume the first available kernel as our main kernel
567 # shellcheck disable=SC2012
568 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
569 if [ -z "$KERNELIMG" ] ; then
570 echo 'No kernel image found, skipping initrd stuff.'>&2
574 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
577 if [ -n "$INITRD" ] ; then
578 echo "Generating initrd."
579 update-initramfs -c -t -k "$KERNELVER"
585 if [ -z "$EFI" ] ; then
589 if ! dpkg --list efibootmgr 2>/dev/null | grep -q '^ii' ; then
590 echo "Notice: efi option set but no efibootmgr package, installing it therefore."
591 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL efibootmgr
595 echo "Mounting $EFI on /boot/efi"
596 mount "$EFI" /boot/efi || return 1
598 echo "Invoking efibootmgr"
599 efibootmgr || return 1
602 # grub configuration/installation {{{
605 if [ -z "$GRUB" ] ; then
606 echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
610 efi_setup || return 1
612 if [ -n "$EFI" ] ; then
613 GRUB_PACKAGE=grub-efi-amd64
618 # make sure this is pre-defined so we have sane settings for automated
619 # upgrades, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=711019
620 echo "Setting ${GRUB_PACKAGE} debconf configuration for install device to $GRUB"
621 echo "${GRUB_PACKAGE} ${GRUB_PACKAGE}/install_devices multiselect $GRUB" | debconf-set-selections
623 if ! dpkg --list ${GRUB_PACKAGE} 2>/dev/null | grep -q '^ii' ; then
624 echo "Notice: grub option set but no ${GRUB_PACKAGE} package, installing it therefore."
625 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL ${GRUB_PACKAGE}
628 if ! [ -x "$(command -v grub-install)" ] ; then
629 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
633 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
634 for device in $SELECTED_PARTITIONS ; do
635 GRUB="${device%%[0-9]}"
636 echo "Installing grub on ${GRUB}:"
637 if ! grub-install --no-floppy "$GRUB" ; then
638 echo "Error: failed to execute 'grub-install --no-floppy $GRUB'." >&2
643 rm -f /boot/grub/device.map
645 echo "Installing grub on ${GRUB}:"
647 lenny|squeeze|wheezy)
649 grub_dev="$(readlink -f "${GRUB}")"
650 if ! grub-install --no-floppy "${grub_dev}" ; then
651 echo "Error: failed to execute 'grub-install --no-floppy ${grub_dev}'." >&2
654 rm -f /boot/grub/device.map
657 echo "(hd0) ${GRUB}" > /boot/grub/device.map
658 if ! grub-install "(hd0)" ; then
659 echo "Error: failed to execute 'grub-install (hd0)'." >&2
662 rm /boot/grub/device.map
667 echo "Adjusting grub configuration for use on ${GRUB}."
669 # finally install grub
670 if [ -x /usr/sbin/update-grub ] ; then
671 UPDATEGRUB='/usr/sbin/update-grub'
672 elif [ -x /sbin/update-grub ] ; then
673 UPDATEGRUB='/sbin/update-grub'
675 echo "Error: update-grub not available, can not execute it." >&2
679 if [ -n "${BOOT_APPEND}" ] ; then
680 echo "Adding BOOT_APPEND configuration ['${BOOT_APPEND}'] to /etc/default/grub."
681 sed -i "/GRUB_CMDLINE_LINUX_DEFAULT/ s#\"\$# ${BOOT_APPEND}\"#" /etc/default/grub
684 mountpoint /boot/efi &>/dev/null && umount /boot/efi
690 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
692 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
694 for script in /etc/debootstrap/chroot-scripts/* ; do
695 echo "Executing script $script"
696 $script && echo "done" || echo "failed"
701 # make sure we don't have any running processes left {{{
703 for service in ssh mdadm mdadm-raid ; do
704 if [ -x /etc/init.d/"$service" ] ; then
705 /etc/init.d/"$service" stop || true
711 # unmount /proc and make sure nothing is left {{{
713 # make sure we don't leave any sensible data
714 rm -f /etc/debootstrap/variables
716 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
718 umount /sys >/dev/null 2>/dev/null || true
719 umount /proc >/dev/null 2>/dev/null || true
726 [ -n "$1" ] && EXIT="$1" || EXIT="1"
731 # set signal handler {{{
732 trap signal_handler HUP INT QUIT TERM
735 # execute the functions {{{
737 # always execute install_policy_rcd
740 for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
741 kernel packages extrapackages reconfigure hosts \
742 default_locales timezone fstab hostname initrd grub_install passwords \
743 custom_scripts upgrade_system remove_apt_cache services \
744 remove_chrootmirror; do
746 $i && stage $i 'done' || exit 1
749 # always execute the finalize stage:
753 # finally exit the chroot {{{
754 echo "Finished chroot installation, exiting."
758 ## END OF FILE #################################################################
759 # vim: ai tw=80 expandtab foldmethod=marker