+ # 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
+
+ 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 >/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 {{{