+cat >> /etc/fstab << EOF
+proc /proc proc defaults 0 0
+/dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
+# some other examples:
+# /dev/sda2 none swap sw,pri=0 0 0
+# /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
+# //1.2.3.4/pub /smb/pub cifs user,noauto,uid=grml,gid=grml 0 0
+# linux:/pub /beer nfs defaults 0 0
+# tmpfs /tmp tmpfs size=300M 0 0
+# /dev/sda5 none swap sw 0 0
+EOF
+}
+# }}}
+
+# generate /etc/fstab {{{
+fstab() {
+ # set up /etc/fstab if file is not present (cdebootstrap)
+ if [ ! -f /etc/fstab ] ; then
+ createfstab
+ fi
+
+ # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
+ if grep -q UNCONFIGURED /etc/fstab ; then
+ createfstab
+ fi
+}
+# }}}
+
+# ensure we have according filesystem tools available {{{
+install_fs_tools() {
+ local pkg=""
+
+ # note: this is supposed to be coming either via command lines'
+ # $_opt_filesystem or via createfstab()
+ case "${FILESYSTEM}" in
+ jfs)
+ pkg="jfsutils"
+ ;;
+ xfs)
+ pkg="xfsprogs"
+ ;;
+ esac
+
+ if [ -n "${pkg:-}" ] && ! dpkg --list "${pkg}" 2>/dev/null | grep -q '^ii' ; then
+ echo "Filesystem package ${pkg} not present, installing now"
+ DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL "${pkg}"
+ fi
+}
+# }}}
+
+# set up hostname {{{
+hostname() {
+ if [ -n "$HOSTNAME" ] ; then
+ echo "Setting hostname to ${HOSTNAME}."
+ echo "$HOSTNAME" > /etc/hostname
+
+ # adjust postfix configuration
+ if [ -r /etc/postfix/main.cf ] ; then
+ # adjust hostname related options:
+ sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
+
+ # listen on loopback interface only:
+ sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
+ grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
+ fi
+ if [ -r /etc/mailname ] ; then
+ # adjust /etc/mailname
+ local etc_mail_domain
+ etc_mail_domain=$(/bin/dnsdomainname 2>/dev/null || echo localdomain)
+ case "$HOSTNAME" in
+ *.*)
+ local mailname="$HOSTNAME"
+ ;;
+ *)
+ local mailname="${HOSTNAME}.${etc_mail_domain}"
+ ;;
+ esac
+ echo "Setting mailname to ${mailname}"
+ echo "$mailname" > /etc/mailname
+ fi
+ fi
+}
+# }}}
+
+# generate initrd/initramfs {{{
+initrd() {
+ # assume the first available kernel as our main kernel
+ # shellcheck disable=SC2012
+ KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
+ if [ -z "$KERNELIMG" ] ; then
+ echo 'No kernel image found, skipping initrd stuff.'>&2
+ return
+ fi
+
+ KERNELVER=${KERNELIMG#/boot/vmlinuz-}
+
+ # generate initrd
+ if [ -n "$INITRD" ] ; then
+ echo "Generating initrd."
+ update-initramfs -c -t -k "$KERNELVER"
+ fi
+}
+# }}}
+
+efi_setup() {
+ if [ -z "$EFI" ] ; then
+ return 0
+ fi
+
+ if ! dpkg --list efibootmgr 2>/dev/null | grep -q '^ii' ; then
+ echo "Notice: efi option set but no efibootmgr package, installing it therefore."
+ DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL efibootmgr
+ fi
+
+ mkdir -p /boot/efi
+ echo "Mounting $EFI on /boot/efi"
+ mount "$EFI" /boot/efi || return 1
+
+ # if efivarfs kernel module is loaded, but efivars isn't,
+ # then we need to mount efivarfs for efibootmgr usage
+ if ! ls /sys/firmware/efi/efivars/* &>/dev/null ; then
+ echo "Mounting efivarfs on /sys/firmware/efi/efivars"
+ mount -t efivarfs efivarfs /sys/firmware/efi/efivars
+ fi
+
+ echo "Invoking efibootmgr"
+ efibootmgr || return 1
+}
+
+# grub configuration/installation {{{
+grub_install() {
+
+ if [ -z "$GRUB" ] ; then
+ echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
+ return 0
+ fi
+
+ efi_setup || return 1
+
+ if [ -n "$EFI" ] ; then
+ GRUB_PACKAGE=grub-efi-amd64
+ else
+ GRUB_PACKAGE=grub-pc
+ fi
+
+ # make sure this is pre-defined so we have sane settings for automated
+ # upgrades, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=711019
+ echo "Setting ${GRUB_PACKAGE} debconf configuration for install device to $GRUB"
+ echo "${GRUB_PACKAGE} ${GRUB_PACKAGE}/install_devices multiselect $GRUB" | debconf-set-selections
+
+ if ! dpkg --list ${GRUB_PACKAGE} 2>/dev/null | grep -q '^ii' ; then
+ echo "Notice: grub option set but no ${GRUB_PACKAGE} package, installing it therefore."
+ DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL ${GRUB_PACKAGE}
+ fi
+
+ if ! [ -x "$(command -v grub-install)" ] ; then
+ echo "Error: grub-install not available. (Error while installing grub package?)" >&2
+ return 1
+ fi
+
+ if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
+ for device in $SELECTED_PARTITIONS ; do
+ GRUB="${device%%[0-9]}"
+ echo "Installing grub on ${GRUB}:"
+ if ! grub-install --no-floppy "$GRUB" ; then
+ echo "Error: failed to execute 'grub-install --no-floppy $GRUB'." >&2
+ exit 1
+ fi
+
+ done
+ rm -f /boot/grub/device.map
+ else
+ echo "Installing grub on ${GRUB}:"
+ case "$RELEASE" in
+ lenny|squeeze|wheezy)
+ local grub_dev
+ grub_dev="$(readlink -f "${GRUB}")"
+ if ! grub-install --no-floppy "${grub_dev}" ; then
+ echo "Error: failed to execute 'grub-install --no-floppy ${grub_dev}'." >&2
+ exit 1
+ fi
+ rm -f /boot/grub/device.map
+ ;;
+ *)
+ echo "(hd0) ${GRUB}" > /boot/grub/device.map
+ if ! grub-install "(hd0)" ; then
+ echo "Error: failed to execute 'grub-install (hd0)'." >&2
+ exit 1
+ fi
+ rm /boot/grub/device.map
+ ;;
+ esac
+ fi
+
+ echo "Adjusting grub configuration for use on ${GRUB}."
+
+ # finally install grub
+ if [ -x /usr/sbin/update-grub ] ; then
+ UPDATEGRUB='/usr/sbin/update-grub'
+ elif [ -x /sbin/update-grub ] ; then
+ UPDATEGRUB='/sbin/update-grub'
+ else
+ echo "Error: update-grub not available, can not execute it." >&2
+ return 1
+ fi
+
+ if [ -n "${BOOT_APPEND}" ] ; then
+ echo "Adding BOOT_APPEND configuration ['${BOOT_APPEND}'] to /etc/default/grub."
+ sed -i "/GRUB_CMDLINE_LINUX_DEFAULT/ s#\"\$# ${BOOT_APPEND}\"#" /etc/default/grub
+ fi
+
+ mountpoint /boot/efi &>/dev/null && umount /boot/efi
+
+ $UPDATEGRUB
+}
+# }}}
+
+# execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
+custom_scripts() {
+ [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
+
+ for script in /etc/debootstrap/chroot-scripts/* ; do
+ echo "Executing script $script"
+ $script && echo "done" || echo "failed"
+ done
+}
+# }}}
+
+# make sure we don't have any running processes left {{{
+services() {
+ for service in ssh mdadm mdadm-raid ; do
+ if [ -x /etc/init.d/"$service" ] ; then
+ /etc/init.d/"$service" stop || true
+ fi
+ done
+}
+# }}}
+
+# unmount /proc and make sure nothing is left {{{
+finalize() {
+ # make sure we don't leave any sensible data
+ rm -f /etc/debootstrap/variables
+
+ [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
+
+ umount /sys/firmware/efi/efivars &>/dev/null || true
+
+ umount /sys >/dev/null 2>/dev/null || true
+ umount /proc >/dev/null 2>/dev/null || true
+}
+# }}}
+
+# signal handler {{{
+signal_handler() {
+ finalize
+ [ -n "$1" ] && EXIT="$1" || EXIT="1"
+ exit "$EXIT"
+}
+# }}}
+
+# set signal handler {{{
+trap signal_handler HUP INT QUIT TERM
+# }}}
+
+# execute the functions {{{
+
+ # always execute install_policy_rcd
+ install_policy_rcd
+
+ for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
+ kernel packages extrapackages reconfigure hosts \
+ default_locales timezone fstab install_fs_tools hostname \
+ initrd grub_install passwords \
+ custom_scripts upgrade_system remove_apt_cache services \
+ remove_chrootmirror; do
+ if stage $i ; then
+ $i && stage $i 'done' || exit 1
+ fi
+ done
+ # always execute the finalize stage:
+ finalize
+# }}}