+# make sure we have what we need {{{
+check4progs debootstrap dialog || bailout 1
+# }}}
+
+# source main configuration file {{{
+if [ -r /etc/debootstrap/config ] ; then
+ . /etc/debootstrap/config
+fi
+# }}}
+
+# cmdline handling {{{
+# source external command line parameter-processing script
+if [ -r ./cmdlineopts.clp ] ; then
+ . ./cmdlineopts.clp
+elif [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
+ . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
+else
+ eerror "Error: cmdline function file not found, exiting."
+ eend 1
+ bailout 1
+fi
+
+# == business-logic of command line parameter-processing
+
+# source configuration file in <confdir> if supplied. {{{
+[ "$_opt_confdir" ] && {
+ CONFFILES=$_opt_confdir
+ einfo "Using config files under $CONFFILES/."
+ if ! [ -r "$CONFFILES/config" ] ; then
+ eerror "Error: config file $CONFFILES/config not found."; eend 1; bailout 1
+ fi
+ if ! . "$CONFFILES/config" ; then
+ eerror "Error reading config file $CONFFILES/config" ; eend 1 ; bailout 1
+ fi
+ # restore the command line parameter value
+ CONFFILES=$_opt_confdir
+}
+# }}}
+
+[ "$_opt_mirror" ] && MIRROR=$_opt_mirror
+[ "$_opt_iso" ] && ISO=$_opt_iso
+[ "$_opt_release" ] && RELEASE=$_opt_release
+[ "$_opt_target" ] && TARGET=$_opt_target
+[ "$_opt_vmfile" ] && VIRTUAL=1
+[ "$_opt_vmsize" ] && VMSIZE=$_opt_vmsize
+[ "$_opt_mntpoint" ] && MNTPOINT=$_opt_mntpoint
+[ "$_opt_debopt" ] && DEBOOTSTRAP_OPT=$_opt_debopt
+[ "$_opt_interactive" ] && INTERACTIVE=1
+[ "$_opt_config" ] && CONFIGFILE=$_opt_config
+[ "$_opt_filesystem" ] && MKFS="mkfs.$_opt_filesystem"
+[ "$_opt_packages_set" ] && PACKAGES='yes'
+[ "$_opt_nopackages" ] && PACKAGES=''
+[ "$_opt_debconf_set" ] && DEBCONF='yes'
+[ "$_opt_scripts_set" ] && SCRIPTS='yes'
+[ "$_opt_pre_scripts_set" ] && PRE_SCRIPTS='yes'
+[ "$_opt_chroot_scripts_set" ] && CHROOT_SCRIPTS='yes'
+[ "$_opt_keep_src_list" ] && KEEP_SRC_LIST='yes'
+[ "$_opt_grmlrepos" ] && GRMLREPOS='yes'
+[ "$_opt_hostname" ] && HOSTNAME=$_opt_hostname
+[ "$_opt_password" ] && ROOTPASSWORD=$_opt_password
+[ "$_opt_nopassword" ] && NOPASSWORD='yes'
+[ "$_opt_bootappend" ] && BOOT_APPEND=$_opt_bootappend
+[ "$_opt_grub" ] && GRUB=$_opt_grub
+[ "$_opt_arch" ] && ARCH=$_opt_arch
+[ "$_opt_insecure" ] && SECURE='false'
+[ "$_opt_force" ] && FORCE=$_opt_force
+[ "$_opt_verbose" ] && VERBOSE="-v"
+
+[ "$_opt_help" ] && {
+ usage ; eend 0
+ eend 0
+ exit 0
+}
+
+[ "$_opt_version" ] && {
+ einfo "$PN - version $VERSION"
+ einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
+ eend 0
+ exit 0
+}
+# }}}
+
+# check for root permissions {{{
+if ! check4root ; then
+ echo "For usage instructions please execute '$PN --help'."
+ bailout 1
+fi
+# }}}
+
+# make sure we have what we need {{{
+if [ -n "$VIRTUAL" ] ; then
+ check4progs kpartx mksh qemu-img || bailout 1
+fi
+# }}}
+
+# source specified configuration file {{{
+if [ -n "$CONFIGFILE" ] ; then
+ einfo "Reading specified config file $CONFIGFILE."
+ if ! . "$CONFIGFILE" ; then
+ eerror "Error reading config file $CONFIGFILE" ; eend 1 ; bailout 1
+ fi
+fi
+# }}}
+
+# backwards compability checks {{{
+if [ -n "$GROOT" ] ; then
+ eerror "Error: you seem to have \$GROOT configured."
+ eerror "This variable is no longer supported, please visit the"
+ eerror "grml-debootstrap documentation for details."
+ eend 1
+ bailout 1
+fi
+
+if echo "$GRUB" | grep -q '^hd' ; then
+ eerror "Error: this syntax for the grub configuration variable is no longer supported."
+ eerror "Please do not use hd... any longer but /dev/sdX instead."
+ eend 1
+ bailout 1
+fi
+# }}}
+
+# welcome screen {{{
+welcome_dialog()
+{
+ dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
+Do you want to continue installing Debian using this frontend?" 0 0 || bailout 0
+}
+# }}}
+
+# ask for target {{{
+prompt_for_target()
+{
+ AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
+ sed 's/*//' | \
+ grep -v 'Extended$' | \
+ gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}'; ls /dev/md* 2>/dev/null || true);
+
+ if [ -z "$AVAILABLE_PARTITIONS" ] ; then
+ dialog --title "$PN" --trim \
+ --msgbox "Sorry, no partitions found. Please configure your
+ harddisks (see /proc/partitions) using a tool like fdisk,
+ cfdisk, gpart, gparted,..." 0 0
+ bailout 1
+ fi
+
+ PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
+ echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet])"
+ done)
+
+ TARGET=$(dialog --title "$PN" --single-quoted --stdout \
+ --menu "Please select the target partition:" 0 0 0 \
+ $PARTITION_LIST)
+ [ $? -eq 0 ] || bailout 1
+}
+# }}}
+
+# ask for bootmanager {{{
+prompt_for_bootmanager()
+{
+ ADDITIONAL_PARAMS=""
+
+ if echo "$TARGET" | grep -q "/dev/md" ; then
+ MBRPART="all disks of Software RAID $TARGET"
+ else
+ # figure out whole disk device
+ found=
+ for device in /dev/disk/by-id/*
+ do
+ [ $(readlink -f $device) = ${TARGET} ] || continue
+ found=1
+ break
+ done
+ [ -n "$found" ] && MBRDISK=$(echo ${device}|sed -e 's/-part[0-9][0-9]*$//')
+ if [ -e "$MBRDISK" ]; then
+ MBRDISK=$(readlink -f $MBRDISK)
+ else
+ # fall back to old behaviour
+ MBRDISK=$(echo ${TARGET} | sed -e 's/[0-9][0-9]*$//')
+ fi
+
+ MBRPART="MBR of $MBRDISK"
+ fi
+
+ for device in cciss/c0d0 sda hda; do
+ if [ /dev/$device != ${MBRDISK} ]; then
+ grep -q $device /proc/partitions && \
+ ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS:$device:install bootmanager grub into MBR of /dev/$device"
+ fi
+ done
+ ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
+
+ OIFS="$IFS"; IFS=:
+
+ GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
+ --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
+ mbr "install bootmanager into $MBRPART" \
+ nowhere "do not install bootmanager at all" \
+ ${ADDITIONAL_PARAMS})
+ [ $? -eq 0 ] || bailout 3
+ IFS="$OIFS"
+
+ case "$GETMBR" in
+ mbr)
+ # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
+ if echo "$TARGET" | grep -q "/dev/md" ; then
+ # using sw-raid:
+ if [ -n "$SELECTED_PARTITIONS" ] ; then
+ GRUB=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
+ else
+ GRUB="$TARGET"
+ fi
+ else
+ GRUB="$MBRDISK"
+ fi
+ ;;
+ hda)
+ GRUB="/dev/hda"
+ ;;
+ sda)
+ GRUB="/dev/sda"
+ ;;
+ nowhere)
+ GRUB=''
+ ;;
+ esac
+}
+# }}}
+
+# ask for Debian release {{{
+prompt_for_release()
+{
+ [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='squeeze'
+ RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
+ "Please enter the Debian release you would like to use for installation:" \
+ 0 50 4 \
+ lenny Debian/old-stable \
+ squeeze Debian/stable \
+ wheezy Debian/testing \
+ sid Debian/unstable)"
+ [ $? -eq 0 ] || bailout
+}
+# }}}
+
+# ask for hostname {{{
+prompt_for_hostname()
+{
+ HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
+ "Please enter the hostname you would like to use for installation:" \
+ 0 0 $HOSTNAME)"
+ [ $? -eq 0 ] || bailout
+}
+# }}}
+
+# ask for password {{{
+prompt_for_password()
+{
+ if [ "$_opt_nopassword" ] ; then
+ einfo "Skip asking for root password as requested."
+ return 0
+ fi
+
+ ROOTPW1='PW1'
+ ROOTPW2='PW2'
+ while [ "$ROOTPW1" != "$ROOTPW2" ]; do
+ ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
+ "Please enter the password for the root account:" 10 60)
+ [ $? -eq 0 ] || bailout
+ ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
+ "Please enter the password for the root account again for \
+ confirmation:" 10 60)
+ [ $? -eq 0 ] || bailout
+
+ if [ "$ROOTPW1" != "$ROOTPW2" ]; then
+ $(dialog --stdout --title "${PN}" --ok-label \
+ "Retry" --msgbox "Passwords do not match!" 10 60)
+ fi
+ done
+ ROOTPASSWORD="$ROOTPW1"
+}
+# }}}
+
+# ask for Debian mirror {{{
+prompt_for_mirror()
+{
+ [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
+
+ CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
+ --menu "Where do you want to install from?" 0 0 0 \
+ net "install via network (downloading from mirror)" \
+ local "install from local directory/mirror"
+ )
+ [ $? -eq 0 ] || bailout
+
+ if [ "$CHOOSE_MIRROR" = 'net' ] ; then
+ [ -n "$MIRROR" ] || MIRROR='http://cdn.debian.net/debian'
+ MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
+ "Please enter Debian mirror you would like to use for installing packages." \
+ 0 0 $MIRROR)"
+ [ $? -eq 0 ] || bailout
+ else # CHOOSE_MIRROR == local
+ [ -n "$ISO" ] || ISO='/mnt/mirror'
+ ISO="$(dialog --stdout --title "${PN}" --inputbox \
+ "Please enter directory name you would like to use for installing packages." \
+ 0 0 $ISO)"
+ [ $? -eq 0 ] || bailout
+ fi
+}
+# }}}
+
+# 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 $(blkid -s TYPE -o value $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
+[ $? -eq 0 ] || bailout
+RETVAL=$?
+SELECTED_PARTITIONS="$(cat $TMPFILE)"
+
+NUM_PARTITIONS=0
+for i in $(cat $TMPFILE) ; do
+ NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
+done
+
+# force metadata version 0.90 for lenny so old grub can boot from this array.
+METADATA_VERSION=""
+if [ $RELEASE = "lenny" ]; then
+ METADATA_VERSION="-e0"
+fi
+
+ERRORFILE=$(mktemp)
+yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
+ --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
+RC=$?
+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"
+ bailout 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
+}
+# }}}
+