+# }}}
+
+# keyring setupt {{{
+KEYRING=""
+if [ "$SECURE" = 'yes' ] ; then
+ if [ -e '/etc/apt/trusted.gpg' ] ; then
+ KEYRING="--keyring /etc/apt/trusted.gpg"
+ else
+ eerror "Could not find /etc/apt/trusted.gpg."
+ fi
+else
+ ewarn "Not checking Release signatures!"
+fi
+# }}}
+
+# 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%%/}
+# }}}
+
+# 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 leave anything (/proc /proc is intended)
+ for ARG in /sys /proc /proc ; do
+ [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1 || true
+ done
+ umount "$MNTPOINT"/dev >/dev/null 2>&1 || true
+
+ [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1 || true
+
+ if [ -n "$DIRECTORY" ] ; then
+ einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
+ else
+ einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
+ 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
+ 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
+}
+# }}}
+
+# create filesystem {{{
+mkfs() {
+ if [ -n "$DIRECTORY" ] ; then
+ einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
+ else
+ if grep -q "$TARGET" /proc/mounts ; then
+ eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
+ exit 1
+ fi
+
+ if [ -n "$MKFS" ] ; then
+ einfo "Running $MKFS on $TARGET"
+ $MKFS $TARGET ; RC=$?
+
+ # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
+ # will fail to detect the uuid in the chroot
+ if echo "$TARGET" | grep -q "/dev/md" ; then
+ blockdev --rereadpt "${TARGET}"
+ else
+ blockdev --rereadpt "${TARGET%%[0-9]*}"
+ fi
+ # give the system 2 seconds, otherwise we might run into
+ # race conditions :-/
+ sleep 2
+
+ eval $(blkid -o udev $TARGET 2>/dev/null)
+ [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
+
+ eend $RC
+ fi
+
+ fi
+}
+# }}}
+
+# modify filesystem settings {{{
+tunefs() {
+ if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; 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
+ ewarn "$TARGET already mounted, continuing anyway." ; eend 0
+ 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
+}
+# }}}
+
+# install main chroot {{{
+debootstrap_system() {
+ if [ "$_opt_nodebootstrap" ]; then
+ einfo "Skipping debootstrap as requested."
+ return
+ fi
+
+ if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
+ einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
+ if [ -n "$MIRROR" ] ; then
+ einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
+ $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
+ else
+ einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
+ $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
+ fi
+ eend $?
+ else
+ eerror "Error: $MNTPOINT not mounted, can not continue."
+ eend 1
+ fi
+}
+# }}}
+
+# prepare chroot via chroot-script {{{
+preparechroot() {
+ einfo "Preparing chroot system"