+# }}}
+
+# make sure we have the right syntax when using an iso image {{{
+if [ -n "$ISO" ] ; then
+ case $ISO in
+ file*) # do nothing
+ ;;
+ *)
+ ISO=file:$1
+ ;;
+ esac
+fi
+ISODIR=${ISO##file:}
+ISODIR=${ISODIR%%/}
+# }}}
+
+# provide variables to chroot system {{{
+CHROOT_VARIABLES="/etc/debootstrap/variables_${SHORT_TARGET}"
+touch $CHROOT_VARIABLES
+chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
+[ -n "$ARCH" ] && echo "ARCH=$ARCH" > $CHROOT_VARIABLES
+[ -n "$GRUB" ] && echo "GRUB=$GRUB" >> $CHROOT_VARIABLES
+[ -n "$GROOT" ] && echo "GROOT=$GROOT" >> $CHROOT_VARIABLES
+[ -n "$TARGET" ] && echo "TARGET=$TARGET" >> $CHROOT_VARIABLES
+[ -n "$ISO" ] && echo "ISO=$ISO" >> $CHROOT_VARIABLES
+[ -n "$ISODIR" ] && echo "ISODIR=$ISO" >> $CHROOT_VARIABLES
+[ -n "$MIRROR" ] && echo "MIRROR=$MIRROR" >> $CHROOT_VARIABLES
+[ -n "$MIRROR" ] && echo "CHROOTMIRROR=$MIRROR" >> $CHROOT_VARIABLES
+[ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
+# }}}
+
+# helper functions {{{
+# we want to exit smoothly and clean:
+bailout(){
+ # 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
+ [ -x "$MNTPOINT"/etc/init.d/ssh ] && "$MNTPOINT"/etc/init.d/ssh stop
+ [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
+ # ugly, but make sure we really don't leav anything
+ [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /sys 1>/dev/null 2>&1
+ [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a 1>/dev/null 2>&1
+ [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
+ [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
+ [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" 1>/dev/null 2>&1
+ einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
+
+ # remove directory only if we used the default with process id inside the name
+ if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
+ einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
+ fi
+ fi
+ fi
+
+ [ -n "$1" ] && EXIT="$1" || EXIT="1"
+ [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 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." ; eend 0
+ eindent
+ ewarn "To reexecute it clean up the according directory inside $STAGES" ; eend 0
+ eoutdent
+ return 1
+ fi
+}
+# }}}
+
+# user should recheck his configuration {{{
+# support full automatic installation:
+checkforrun() {
+ dialog --timeout 10 --title "$PN" \
+ --yesno "Do you want to stop at this stage?
+
+Notice: you are running ${PN} in non-interactive mode.
+${PN} will install Debian ${RELEASE} on ${TARGET}.
+Last chance to quit. Timeout of 10 seconds running....
+
+Do you want to stop now?" 0 0 2>/dev/null
+}
+
+# create filesystem {{{
+mkfs() {
+ if [ -n "$MKFS" ] ; then
+ einfo "Running $MKFS on $TARGET"
+ $MKFS $TARGET
+ eend $?
+ fi
+}
+# }}}
+
+# modify filesystem settings {{{
+tunefs() {
+ if [ -n "$TUNE2FS" ] ; then
+ einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
+ $TUNE2FS $TARGET
+ eend $?
+ fi
+}
+# }}}
+
+# mount the new partition or if it's a directory do nothing at all {{{
+mount_target() {
+ if [ -n "$DIRECTORY" ] ; then
+ einfo "Running grml-debootstrap on a directory, nothing to mount."
+ else
+ if grep -q $TARGET /proc/mounts ; then
+ eerror "$TARGET already mounted, exiting."
+ else
+ [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
+ einfo "Mounting $TARGET to $MNTPOINT"
+ mount -o rw,suid,dev $TARGET $MNTPOINT
+ eend $?
+ fi
+ fi
+ if [ -n "$ISODIR" ] ; then
+ einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
+ mkdir -p "$MNTPOINT/$ISODIR"
+ mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
+ eend $?
+ fi
+}
+# }}}