+# }}}
+
+# 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%%/}
+# }}}
+
+# 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}"
+ elif ! [ -n "$VIRTUAL" ] ; then
+ 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
+ if ! [ -d "${MNTPOINT}" ] ; then
+ [ -n "$VIRTUAL" ] || mkdir -p "${MNTPOINT}"
+ fi
+ einfo "Mounting $TARGET to $MNTPOINT"
+ mkdir -p "$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
+}
+# }}}
+
+# prepare VM image for usage with debootstrap {{{
+prepare_vm() {
+ if [ -z "$VIRTUAL" ] ; then
+ return 0 # be quite by intention
+ fi
+
+ if [ -b "$TARGET" ] ; then
+ eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
+ eend 1
+ exit 1
+ fi
+
+ ORIG_TARGET="$TARGET" # store for later reuse
+
+ qemu-img create -f raw "${TARGET}" "${VMSIZE}"
+ echo 4 66 | /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
+ dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
+ parted -s "${TARGET}" 'mkpart primary ext3 2M -1'
+
+ DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
+ if [ -z "${DEVINFO}" ] ; then
+ eerror "Error setting up loopback device." ; eend 1
+ exit 1
+ fi
+
+ # hopefully this always works as expected
+ LOOP=$(echo ${DEVINFO} | sed 's/.* linear //; s/ [[:digit:]]*//') # '/dev/loop1'
+ BLOCKDEV=$(echo "${DEVINFO}" | sed -e 's/.* (\(.*:.*\)).*/\1/') # '253:0'
+ LOOP_PART="$(echo ${DEVINFO##add map } | sed 's/ .*//')" # '/dev/loop1p1'
+ export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
+
+ blockdev --rereadpt "${LOOP}"
+
+ if [ -z "$TARGET" ] ; then
+ eerror "Error: target could not be set to according /dev/mapper/* device." ; eend 1
+ exit 1
+ fi
+}
+# }}}
+
+# make VM image bootable and unmount it {{{
+finalize_vm() {
+ if [ -z "${VIRTUAL}" ] ; then
+ return 0
+ fi
+
+ if ! mount "${TARGET}" "${MNTPOINT}" ; then
+ eerror "Error: Mounting ${TARGET} failed, can not continue." ; eend 1
+ exit 1
+ fi
+
+ einfo "Installing Grub as bootloader."
+ mount -t proc none "${MNTPOINT}"/proc
+ mount -t sysfs none "${MNTPOINT}"/sys
+ mount --bind /dev "${MNTPOINT}"/dev
+
+ mkdir -p "${MNTPOINT}/boot/grub"
+ if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
+ eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
+ exit 1
+ fi
+
+ cp "${MNTPOINT}"/usr/lib/grub/i386-pc/* "${MNTPOINT}/boot/grub/"
+ chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
+ dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
+ rm -f "${MNTPOINT}/tmp/core.img"