+# early helper functions {{{
+# skip colors when running within a dumb terminal
+if [ "${TERM}" = "dumb" ] ; then
+ GOOD=
+ BAD=
+ WARN=
+ NORMAL=
+else
+ GOOD='\e[32;01m'
+ BAD='\e[31;01m'
+ WARN='\e[33;01m'
+ NORMAL='\e[0m'
+fi
+
+einfo() {
+ einfon "$1\\n"
+ return 0
+}
+
+einfon() {
+ [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
+ printf " %s*%s $*" "${GOOD}" "${NORMAL}"
+ LAST_E_CMD=einfon
+ return 0
+}
+
+ewarn() {
+ printf " %s*%s $*\\n" "${WARN}" "${NORMAL}"
+ return 0
+}
+
+eerror() {
+ [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
+ printf " %s*%s $*\\n" "${BAD}" "${NORMAL}" >&2
+ LAST_E_CMD=eerror
+ return 0
+}
+
+eend() {
+ local retval="${1:-0}"
+ shift
+ if [ "$retval" -gt 0 ]; then
+ printf " %s-> Failed (rc=%s)%s\\n" "${BAD}" "${retval}" "${NORMAL}"
+ fi
+ return "$retval"
+}
+
+check4root(){
+ if [ "$(id -u 2>/dev/null)" != 0 ] ; then
+ echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
+ fi
+}
+
+check4progs(){
+ local RC=''
+ for arg in "$@" ; do
+ command -v "$arg" >/dev/null 2>&1 || RC="$arg"
+ done
+ if [ -n "$RC" ] ; then
+ echo "$RC not installed"
+ return 1
+ fi
+}
+# }}}
+
+# unmount mountpoint {{{
+try_umount() {
+ local tries=$1
+ local mountpoint="$2"
+
+ if ! mountpoint "$mountpoint" &>/dev/null ; then
+ return 0
+ fi
+
+ for (( try=1; try<=tries; try++ )); do
+ if [[ ${try} -eq ${tries} ]]; then
+ # Last time, show errors this time
+ umount "${mountpoint}" && return 0
+ else
+ # Not last time, hide errors until fatal
+ if umount "${mountpoint}" 2>/dev/null ; then
+ return 0
+ else
+ sleep 1
+ fi
+ fi
+ done
+ return 1 # Tried enough
+}
+# }}}
+
+# helper functions {{{
+cleanup() {
+ if [ -n "$CHROOT_VARIABLES" ] ; then
+ einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" || eend $?
+ fi
+
+ if [ -n "$STAGES" ] ; then
+ einfo "Removing ${STAGES}" ; rmdir "$STAGES" || eend $?
+ fi
+
+ try_umount 3 "${MNTPOINT}"/boot/efi
+
+ # Remove temporary mountpoint again
+ if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
+ if [ -d "$MNTPOINT" ] ; then
+ rmdir "$MNTPOINT" || true
+ fi
+ fi
+
+ # make sure $TARGET is not mounted when exiting grml-debootstrap
+ if [ -n "$MNTPOINT" ] ; then
+ if grep -q "$MNTPOINT" /proc/mounts ; then
+ # make sure nothing is left inside chroot so we can unmount it
+ for service in ssh mdadm ; do
+ if [ -x "${MNTPOINT}/etc/init.d/${service}" ] ; then
+ chroot "$MNTPOINT" "/etc/init.d/${service}" stop
+ fi
+ done
+
+ [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a >/dev/null 2>&1 || true
+
+ # ugly, but make sure we really don't leave anything (/proc /proc and
+ # /dev /dev are intended, trying to work around timing issues, see #657023)
+ for ARG in /run/udev /sys /proc /proc /dev/pts /dev/pts /dev /dev ; do
+ [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1 || true
+ umount "$MNTPOINT"/$ARG >/dev/null 2>&1 || true
+ done
+
+ if [ -n "$ISODIR" ] ; then
+ try_umount 3 "$MNTPOINT/$ISODIR" >/dev/null 2>&1 || true
+ fi
+
+ if [ -n "$DIRECTORY" ] ; then
+ einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice."
+ else
+ einfo "Unmounting $MNTPOINT"
+ try_umount 3 "$MNTPOINT"
+ fi
+
+ if [ -n "$STAGES" ] ; then
+ echo -n "Removing stages directory ${STAGES}: "
+ rm -rf "$STAGES" && echo 'done'
+ fi
+
+ # remove directory only if we used the default with process id inside the name
+ if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
+ if [ -d "$MNTPOINT" ] ; then
+ einfo "Removing directory ${MNTPOINT}"
+ rmdir "$MNTPOINT" || eend $?
+ fi
+ fi
+ fi
+ fi
+
+ if [ -n "${ORIG_TARGET}" ] ; then
+ einfo "Removing loopback mount of file ${ORIG_TARGET}."
+ kpartx -d "${ORIG_TARGET}" || eend $?
+ # Workaround for a bug in kpartx which doesn't clean up properly,
+ # see Debian Bug #891077 and Github-PR grml/grml-debootstrap#112
+ if dmsetup ls | grep -q "^${LOOP_PART} "; then
+ kpartx -d "/dev/${LOOP_DISK}" >/dev/null || eend $?
+ fi
+ fi
+}
+
+# we want to exit smoothly and clean:
+bailout(){
+ eend "$1" || true
+
+ cleanup
+
+ [ -n "$1" ] && EXIT="$1" || EXIT="1"
+ [ -n "$2" ] && einfo "Notice: remove $STAGES/$2 to reexecute the stage"
+
+ exit "$EXIT"
+}
+trap bailout HUP INT QUIT TERM
+
+# we want to execute all the functions only once, simple check for it:
+stage() {
+ if [ -n "$2" ] ; then
+ echo "$2" > "${STAGES}/${1}"
+ return 0
+ elif grep -q 'done' "${STAGES}/${1}" 2>/dev/null ; then
+ ewarn "Notice: stage $1 has been executed already, skipping execution therefore."
+ ewarn " To reexecute it clean up the according directory inside $STAGES"
+ return 1
+ fi
+}