+ # create device.map
+ if ! [ -f "$device_map" ] ; then
+ echo 'quit' | grub --device-map="$device_map" 1>/dev/null 2>&1
+ fi
+
+ # based on code from d-i's trunk/packages/arch/i386/grub-installer/grub-installer:
+ tmp_disk=`echo "$device" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \
+ -e 's%\(fd[0-9]*\)$%\1%' \
+ -e 's%/part[0-9]*$%/disc%' \
+ -e 's%\(c[0-7]d[0-9]*\).*$%\1%'`
+ tmp_part=`echo "$device" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' \
+ -e 's%.*/fd[0-9]*$%%' \
+ -e 's%.*/floppy/[0-9]*$%%' \
+ -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
+ -e 's%.*c[0-7]d[0-9]*p*%%'`
+ tmp_drive=$(grep -v '^#' $device_map | grep "$tmp_disk *$" | sed 's%.*\([hf]d[0-9][a-g0-9,]*\).*%\1%')
+
+ case $1 in
+ /dev/[sh]d[a-z]) # we expect something like 'hd0'
+ echo "$tmp_drive"
+ ;;
+ *) # we expect something like 'hd0,0'
+ echo "$tmp_drive" | sed "s%$%,`expr $tmp_part - 1`%" # FIXME => md0
+ ;;
+ esac
+}
+# }}}
+
+# software raid setup {{{
+config_swraid_setup()
+{
+TMPFILE=$(mktemp)
+
+# Currently we support only raid1:
+RAIDLEVEL='raid1'
+
+#RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
+# --menu "Which RAID level do you want to use?" 0 0 0 \
+# raid1 "Software RAID level 1" \
+# raid5 "Software RAID level 5")
+#[ $? -eq 0 ] || bailout 20
+
+MD_LIST=$(for i in $(seq 0 9) ; do
+ awk '{print $4}' /proc/partitions | grep -q md$i || \
+ echo "/dev/md$i /dev/md$i"
+ done)
+
+TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
+--menu "Which device do you want to use for ${RAIDLEVEL}?
+
+Notice: activated devices will not be listed for security reasons. Anyway, please make sure the selected device is not in use already!" 0 0 0 \
+$MD_LIST)
+[ $? -eq 0 ] || bailout 20
+
+AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
+ sed 's/*//' | \
+ grep -v 'Extended$' | \
+ gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
+[ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
+PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
+ echo "$i $(vol_id --type $i 2>/dev/null || echo [no_filesystem_yet]) off"
+ done)
+
+dialog --title "$PN" --separate-output \
+ --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
+ $PARTITION_LIST 2>$TMPFILE
+RETVAL=$?
+SELECTED_PARTITIONS="$(cat $TMPFILE)"
+
+NUM_PARTITIONS=0
+for i in $(cat $TMPFILE) ; do
+ NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
+done
+
+ERRORFILE=$(mktemp)
+set +e
+# TODO: better error handling?
+yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
+ --raid-devices="${NUM_PARTITIONS}" ${SELECTED_PARTITIONS} 1>/dev/null 2>$ERRORFILE
+RC=$?
+set -e
+
+if [ "$RC" = 0 ] ; then
+ dialog --title "$PN" --msgbox \
+ "Creating $TARGET was successful." 0 0
+ rm -f "$TMPFILE" "$ERRORFILE"
+else
+ dialog --title "$PN" --msgbox \
+ "There was an error setting up $TARGET:
+
+$(cat $ERRORFILE)
+
+Exiting." 0 0
+ rm -f "$TMPFILE" "$ERRORFILE"
+ exit 1
+fi
+
+}
+
+prompt_for_swraid()
+{
+if dialog --stdout --title "$PN" \
+ --defaultno --yesno "Do you want to configure Software RAID?
+
+Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
+ config_swraid_setup
+fi
+}
+# }}}
+
+# make sure the user is aware of the used configuration {{{
+checkconfiguration()
+{
+if [ -n "$AUTOINSTALL" ] ; then
+ if checkforrun ; then
+ eerror "Exiting as requested" ; eend 0
+ exit 1
+ fi
+elif [ -n "$INTERACTIVE" ] ; then
+
+ INFOTEXT="Please recheck configuration before execution:
+ "
+ [ -n "$TARGET" ] && INFOTEXT="$INFOTEXT
+ Target: $TARGET"
+ [ -n "$GRUB" ] && INFOTEXT="$INFOTEXT
+ Install grub: $GRUB"
+ [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
+ Using release: $RELEASE"
+ [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
+ Using hostname $HOSTNAME"
+ [ -n "$MIRROR" ] && INFOTEXT="$INFOTEXT
+ Using mirror: $MIRROR"
+
+ INFOTEXT="$INFOTEXT
+
+Is this ok for you? Notice: selecting 'No' will exit ${PN}."
+
+ dialog --title "$PN" --no-collapse \
+ --yesno "$INFOTEXT" 0 0
+
+else # if not running automatic installation display configuration and prompt for execution:
+ einfo "$PN - Please recheck configuration before execution:"
+ echo
+ echo " Target: $TARGET"
+ case "$MNTPOINT" in "$TARGET") ;; *) echo " Mount point: $MNTPOINT" ;; esac
+ [ -n "$GRUB" ] && echo " Install grub: $GRUB" || echo " Install grub: no"
+ [ -n "$RELEASE" ] && echo " Using release: $RELEASE"
+ [ -n "$MIRROR" ] && echo " Using mirror: $MIRROR"
+ [ -n "$ISO" ] && echo " Using iso: $ISO"
+ case "$MNTPOINT" in "$TARGET") ;; *) echo " Important! Continuing will delete all data from ${TARGET}!" ;; esac
+ echo
+ einfon "Is this ok for you? [y/N] "
+ read a
+ if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
+ eerror "Exiting as requested." ; eend 1
+ exit 1
+ fi
+fi
+}
+# }}}
+
+# interactive mode {{{
+interactive_mode()
+{
+ welcome_dialog
+
+ prompt_for_swraid
+
+ # do not prompt for partition dialog if swraid has been configured already
+ [ -n "$TARGET" ] || prompt_for_target
+
+ prompt_for_bootmanager
+
+ prompt_for_release
+
+ prompt_for_hostname
+
+ prompt_for_password
+
+ # use first disk of sw-raid for grub by default, install grub on
+ # all involved disks later on
+ if echo "$TARGET" | grep -q '/dev/md' ; then
+ if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
+ # use hdX and not hdX,Y for $GRUB
+ TMPDEVICE=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
+ GRUB="$(grubdevice ${TMPDEVICE%%[0-9]})" # like: hd0
+ GROOT="$(grubdevice ${TMPDEVICE})" # like: hd0,0
+ echo "debug: GRUB = $GRUB - GROOT = $GROOT" >/tmp/debug # FIXME
+ fi
+ else
+ [ -n "$BOOT_PARTITION" ] && GRUB="$(grubdevice $BOOT_PARTITION)"
+ [ -n "$TARGET" ] && GROOT="$(grubdevice $TARGET)"
+ fi
+
+ prompt_for_mirror
+
+ checkconfiguration
+}
+
+# run interactive mode if we didn't get the according configuration yet
+if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
+ # only target might be unset, so make sure the INTERACTIVE flag is set as well
+ INTERACTIVE=1
+ interactive_mode
+fi
+# }}}
+
+# finally make sure at least $TARGET is set [the partition for the new system] {{{
+if [ -n "$TARGET" ] ; then
+ SHORT_TARGET="${TARGET##*/}"
+else
+ eerror "Please adjust /etc/debootstrap/config or..."
+ eerror "... use the interactive version for configuration before running ${0}" ; eend 1
+ exit 1
+fi
+# }}}
+
+# stages setup {{{