#! /bin/sh
#----------------------------------------------------------------------------
# /linuxrc2 - installer script of opt.tgz
#
# Creation   :  2001-10-15  fm
# Last Update:  $Id$
#
# Copyright (c) 2001-2020 the eisfair team, team(at)eisfair(dot)org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------
PATH=$PATH:/usr/local/bin
export PATH
kernel=`uname -r`

change_pw ()
{
    user=$1
    passretval=x
    while [ ${passretval} != 0 ]
    do
        colecho "Setting password for user $user:" gn
        /rootfs/usr/bin/chroot /rootfs passwd $user
        passretval=${?}
    done
}

ask ()
{
    while [ 1 ]
    do
        echo -e "$1 (y/n)? \c"
        read a
        case "$a" in
        z | zes | y | yes)  a=y; break;;
                   n | no)  a=n; break;;
                        *)  colecho "Please answer y(es) or n(o)" br x br;;
        esac
    done
}

#----------------------------------------------------------------------------
# welcome
#----------------------------------------------------------------------------
base_version=`grep base /build | cut -d" " -f2`
kernel_version=`grep kernel /build | cut -d" " -f2`
cd /
echo
colecho "Welcome to \c" gn
colecho "eisfair-64 \c" br x br
colecho "installation base \c" gn
colecho "$base_version \c" br x br
colecho "kernel \c" gn
colecho "$kernel_version\c" br x br
colecho "!" gn
echo

#----------------------------------------------------------------------------
# ask for scsi
#----------------------------------------------------------------------------
ask "Load SCSI/SATA/RAID/USB-STORAGE drivers"
if [ $a = "y" ]
then
    sh /scsi.sh
fi

#----------------------------------------------------------------------------
# extract 'mdadm' from rootfs tarball
#----------------------------------------------------------------------------
echo "Extracting mdadm from rootfs.tlz ..."

# push net information from udhcpc
if [ -f /netinfo ]
then
    . /netinfo
fi

if [ "${boot_media}" = "pxe" ]
then
    /busybox tftp -l - -r ${InstFolder:-eis}/rootfs.tlz -g ${DHCP_SERVER} |
             tar --lzma -xpf - sbin/mdadm
else
    tar xpfa /mnt/rootfs.tlz sbin/mdadm
fi

#----------------------------------------------------------------------------
# search for harddisks and cdroms
#----------------------------------------------------------------------------
#echo "Boot media : ${boot_media}"
#echo "Boot device: ${boot_dev}"
#ask "show boot dev"
echo
colecho "Searching hard disks ..." gn
disk=none
cdrom=none
scsi_found=false
cpqscsi_found=false
cpqprefix=''
disk_other=''
scsi_devices="sda sdb sdc sdd sde sdf sdg"
cpq_devices="c0d0 c0d1 c0d2 c0d3 c0d4 c0d5 c0d6 c0d7"

for j in $scsi_devices
do
    grep " $j" /proc/partitions >/dev/null 2>&1
    if [ $? = 0 ]
    then
        scsi_found=true
        eval $j=disk

        if [ "$disk" = "none" ]
        then
            if [ "${j}" != "`echo ${boot_dev} | cut -c6-8 `" ]
            then
                disk=$j
            fi
        else
            disk_other=$j
        fi

        if [ "${boot_media}" = "USB-Stick" ]
        then
            if [ "${j}" = "`echo ${boot_dev} | cut -c6-8 `" ]
            then
                eval ${j}="USB-Stick"
            fi
        fi
    else
        eval $j=none
    fi
done

for cpqprefix in ida cciss
do
    for j in $cpq_devices
    do
        grep " $cpqprefix/$j$" /proc/partitions >/dev/null 2>&1
        if [ $? = 0 ]
        then
            cpqscsi_found=true
            eval $j=disk
            j=$cpqprefix/$j

            if [ $disk = none ]
            then
                disk=$j
            else
                disk_other=$j
            fi
        else
            eval $j=none
        fi
    done

    if [ $cpqscsi_found = true ]
    then
        break
    fi
done

sizeMB=''
sizeKB=''

if [ ${scsi_found} = true ]
then
    if [ "${boot_media}" = "USB-Stick" ]
    then
        new_boot_dev="`echo ${boot_dev} | cut -c6-8 `"
    fi

    header=1
    for a in $scsi_devices
    do
        eval new_disk='$'${a}
        if [ "${a}" = "${new_boot_dev}" ]
        then
            new_disk="USB-Stick"
        fi

        if [ "$new_disk" = "disk" -o "$new_disk" = "USB-Stick" ]
        then
            sizeKB=`sfdisk -s /dev/${a}`

            if [ "${new_disk}" != "USB-Stick" ]
            then
                # disk fuer RAID rausschreiben
                echo "${sizeKB} ${a}" >>/have_raid_disk
            fi

            if [ -n "$sizeKB" ]
            then
                sizeMB=`expr ${sizeKB} / 1024`
                sizeMB="($sizeMB MiB)"
            fi

            if [ ${header} -ne 0 ]
            then
                echo
                echo " SCSI-/SATA-/USB-Disk         | Device   | Type"
                echo " -----------------------------+----------+-------------------------------------"
                header=0
            fi

            echo " ${a}                          |/dev/${a}  |${new_disk} ${sizeMB}"
        fi
        sizeMB=''
        sizeKB=''
    done
    echo
fi

if [ $cpqscsi_found = true ]
then
    if [ "$cpqprefix" = "ida" ]
    then
        fill='  '
    elif [ "$cpqprefix" = "cciss" ]
    then
        fill=''
    fi

    header=1
    for a in $cpq_devices
    do
        eval new_disk='$'${a}
        if [ "$new_disk" = "disk" ]
        then
            sizeKB=`sfdisk -s /dev/${cpqprefix}/${a}`
            # disk fuer RAID rausschreiben
            echo "${sizeKB} ${cpqprefix}/${a}" >>/have_raid_disk

            if [ -n "$sizeKB" ]
            then
                sizeMB=`expr ${sizeKB} / 1024`
                sizeMB="($sizeMB MiB)"
            fi

            if [ ${header} -ne 0 ]
            then
                echo
                echo " SmartArray-Disk      | Device           | Type"
                echo " ---------------------+------------------+-------------------------------------"
                header=0
            fi

            echo " ${a}                 |/dev/$cpqprefix/${a} ${fill}  |${new_disk} ${sizeMB}"
        fi
        sizeMB=''
        sizeKB=''
    done
    echo
fi

if [ "$disk" = "none" ]
then
    colecho "No disk found, sorry. Exit." br x br
    sleep 100000
    exit 1
fi

#----------------------------------------------------------------------------
# get raid information
#----------------------------------------------------------------------------
_raid_disk_n='0'
_raid_n_disk='0'
_raid_level=''

# list all disks
if [ -f /have_raid_disk ]
then
    _raid_disk_n=$(grep -c . /have_raid_disk)
fi

get_master_disk ()
{
    while [ $# -gt 0 ]
    do
        if [ -z "${_raid_master_size}" ]
        then
            _raid_master_size="${1}"
            _raid_master_dev="${2}"
            _raid_extra_dev="/dev/${2}"
            _raid_sub_disk="${2}"
            shift; shift
        else
            if [ ${1} -lt ${_raid_master_size} ]
            then
                _raid_master_size="${1}"
                _raid_master_dev="${2}"
            fi

            _raid_extra_dev="${_raid_extra_dev},"/dev/${2}
            _raid_sub_disk="${_raid_sub_disk} "${2}
            shift; shift
        fi
    done
}

if [ ${_raid_disk_n:-0} -gt 1 -a ${_raid_disk_n:-0} -lt 8 ]
then
    _raid_disk=$(cat /have_raid_disk)
    get_master_disk ${_raid_disk}
    disk=${_raid_master_dev}
    _raid_sub_disk=$(echo ${_raid_sub_disk} | /busybox sed "s#${_raid_master_dev}##")
    _raid_n_disk="${_raid_disk_n}"

    echo
    echo "Do you want to use listed disks"
    colecho "${_raid_extra_dev}" gn

    case ${_raid_disk_n:-0} in
    2)
        ask "as software RAID1 (mirroring)?"
        if [ ${a} = y ]
        then
            _raid_level='1'
        fi
        ;;
    [3-7])
        ask "as software RAID5 (block-level striping)?"
        if [ ${a} = y ]
        then
            _raid_level='5'
        fi
        ;;
    esac
fi

echo
if [ -z "${_raid_level}" ]
then
    ask "Using $disk for installation, ok"
    if [ $a = n ]
    then
        while [ 1 ]
        do
            echo
            disk=$disk_other
            echo -e "Disk to use [$disk]\c"
            read a
            case "$a" in
            "")
                break
                ;;
            sda|sdb|sdc|sdd|sde|sdf|sdg|c0d0|c0d1|c0d2|c0d3|c0d4|c0d5|c0d6|c0d7)
                eval x='$'$a
                if [ "$x" != "disk" ]
                then
                    colecho "$a is no disk, try again!" br x br
                else
                    if [ $cpqscsi_found = true ]
                    then
                        disk=$cpqprefix/$a
                    else
                        disk=$a
                    fi
                    break
                fi
                ;;
            *)
                colecho "$disk would be a correct answer, try again!" br x br
                ;;
            esac
        done
    fi
    echo
fi

#----------------------------------------------------------------------------
# search for existing linux partitions
#----------------------------------------------------------------------------

# if sfdisk is recognizing a gpt system, write /tmp/gpt
if `sfdisk -l /dev/$disk 2>/dev/null | grep -q ' gpt$'`
then
    >/tmp/gpt
fi

do_find_partitions ()
{
    boot_idx=''
    root_idx=''
    data_idx=''
    swap_idx=''
    do_swap=''

    if [ "$1" = "auto" ]
    then
        possible_boot_idx=1

        if [ -f /tmp/gpt ]
        then
            # grep for label 'boot' from partitioning with this installer
            boot_partition=`sgdisk -p /dev/$disk | grep 'boot$' | grep ' 8300 ' | grep ' MiB ' |  cut -d' ' -f1-9 | /busybox sed 's# ##g'`
            if [ "$boot_partition" != "" ]
            then
                boot_idx=$boot_partition
            else
                boot_partition=`sgdisk -p /dev/$disk | grep " $possible_boot_idx " | grep ' 8300 ' | grep ' MiB '`
                if [ "$boot_partition" != "" ]
                then
                    boot_idx=$possible_boot_idx
                fi
            fi

            if [ -n "$boot_idx" -a "$cpqscsi_found" = "true" ]
            then
                # Partitionsnummern bei sgdisk sind auch bei ida/cciss 1, 2, ...
                # und nicht p1, p2, ... !
                # Aber formatiert wird dann /dev/cciss/c0d0p1
                boot_idx=p$boot_idx
            fi
        else
            if [ "$cpqscsi_found" = "true" ]
            then
                possible_boot_idx=p$possible_boot_idx
            fi

            boot_partition=`sfdisk -l /dev/$disk | grep /dev/$disk$possible_boot_idx 2>/dev/null | grep 'Linux filesystem' 2>/dev/null`
            if [ "$boot_partition" != "" ]
            then
                sizeKBboot=`sfdisk -s /dev/$disk$possible_boot_idx 2>/dev/null`
                if [ "$sizeKBboot" -lt 102400 ]        # kleiner als 100 MB
                then
                    boot_idx=$possible_boot_idx
                fi
            fi
        fi
    fi

    if [ "$boot_idx" = "" ]
    then
        while [ 1 ]
        do
            echo -e "Index of extra boot partition (1-15, p1-p7, 0=none) /dev/$disk\c"
            read boot_idx
            case "$boot_idx" in
            1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|p1|p2|p3|p4|p5|p6|p7)
                if [ -f /tmp/gpt ]
                then
                    sgdisk -i $boot_idx /dev/$disk | grep '(Linux filesystem)' >/dev/null
                else
                    sfdisk -l /dev/$disk | grep /dev/$disk$boot_idx | grep 'Linux filesystem' >/dev/null
                fi

                if [ "$?" != "0" ]
                then
                    colecho "/dev/$disk$boot_idx is no valid Linux partition!" br x br
                else
                    if [ -f /tmp/gpt ]
                    then
                        # Partitionsnummern bei sgdisk sind auch bei ida/cciss 1, 2, ...
                        # und nicht p1, p2, ... !
                        # Aber formatiert wird dann /dev/cciss/c0d0p1
                        if [ "$cpqscsi_found" = "true" ]
                        then
                            boot_idx=p$boot_idx
                        fi
                    fi

                    break
                fi
                ;;
            0)
                break
                ;;
            esac
        done
    fi

    if [ "$1" = "auto" ]
    then
        possible_root_idx=3
        if [ -f /tmp/gpt ]
        then
            s=`sgdisk -p /dev/$disk | grep ' 8200 '`
        else
            s=`sfdisk -l /dev/$disk 2>/dev/null | grep 'Linux swap'`
        fi

        if [ "$s" = "" ]
        then
            possible_root_idx=2
        fi

        if [ -f /tmp/gpt ]
        then
            # grep for label 'root' from partitioning with this installer
            root_partition=`sgdisk -p /dev/$disk | grep 'root$' | grep ' 8300 ' | cut -d' ' -f1-9 | /busybox sed 's# ##g'`
            if [ "$root_partition" != "" ]
            then
                root_idx=$root_partition
            else
                root_partition=`sgdisk -p /dev/$disk | grep " $possible_root_idx " | grep ' 8300 ' 2>/dev/null`
                if [ "$root_partition" != "" ]
                then
                    root_idx=$possible_root_idx
                fi
            fi

            if [ -n "$root_idx" -a "$cpqscsi_found" = "true" ]
            then
                # Partitionsnummern bei sgdisk sind auch bei ida/cciss 1, 2, ...
                # und nicht p1, p2, ... !
                # Aber formatiert wird dann /dev/cciss/c0d0p2
                root_idx=p$root_idx
            fi
        else
            if [ "$cpqscsi_found" = "true" ]
            then
                possible_root_idx=p$possible_root_idx
            fi

            root_partition=`sfdisk -l /dev/$disk | grep /dev/$disk$possible_root_idx 2>/dev/null | grep 'Linux filesystem' 2>/dev/null`
            if [ "$root_partition" != "" ]
            then
                sizeKBroot=`sfdisk -s /dev/$disk$possible_root_idx 2>/dev/null`
                if [ "$sizeKBroot" -gt 153600 ]        # groesser als 150 MB
                then
                    root_idx=$possible_root_idx
                fi
            fi
        fi
    fi

    if [ "$root_idx" = "" ]
    then
        while [ 1 ]
        do
            echo -e "Index of root partition (1-15, p1-p7) /dev/$disk\c"
            read root_idx
            case "$root_idx" in
            1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|p1|p2|p3|p4|p5|p6|p7)
                if [ -f /tmp/gpt ]
                then
                    sgdisk -i $root_idx /dev/$disk | grep '(Linux filesystem)' >/dev/null
                else
                    sfdisk -l /dev/$disk | grep /dev/$disk$root_idx | grep 'Linux filesystem' >/dev/null
                fi

                if [ "$?" != "0" ]
                then
                    colecho "/dev/$disk$root_idx is no valid Linux partition!" br x br
                else
                    if [ -f /tmp/gpt ]
                    then
                        # Partitionsnummern bei sgdisk sind auch bei ida/cciss 1, 2, ...
                        # und nicht p1, p2, ... !
                        # Aber formatiert wird dann /dev/cciss/c0d0p1
                        if [ "$cpqscsi_found" = "true" ]
                        then
                            root_idx=p$root_idx
                        fi
                    fi

                    break
                fi
                ;;
            esac
        done
    fi

    if [ $boot_idx = $root_idx ]
    then
        echo "Ok, you want to use the same partition for boot/root, using /dev/$disk$root_idx"
        boot_idx=0
    fi

    if [ "$1" = "auto" ]
    then
        possible_data_idx=4
        if [ -f /tmp/gpt ]
        then
            s=`sgdisk -p /dev/$disk | grep ' 8200 '`
        else
            s=`sfdisk -l /dev/$disk 2>/dev/null | grep 'Linux swap'`
        fi

        if [ "$s" = "" ]
        then
            possible_data_idx=3
        fi

        if [ -f /tmp/gpt ]
        then
            # grep for label 'data' from partitioning with this installer
            data_partition=`sgdisk -p /dev/$disk | grep 'data$' | grep ' 8300 ' | cut -d' ' -f1-9 | /busybox sed 's# ##g'`
            if [ "$data_partition" != "" ]
            then
                data_idx=$data_partition
            else
                data_partition=`sgdisk -p /dev/$disk | grep " $possible_data_idx " | grep ' 8300 ' | grep ' GiB ' 2>/dev/null`
                if [ "$data_partition" != "" ]
                then
                    data_idx=$possible_data_idx
                fi
            fi

            if [ -n "$data_idx" -a "$cpqscsi_found" = "true" ]
            then
                # Partitionsnummern bei sgdisk sind auch bei ida/cciss 1, 2, ...
                # und nicht p1, p2, ... !
                # Aber formatiert wird dann /dev/cciss/c0d0p4
                data_idx=p$data_idx
            fi
        else
            if [ "$cpqscsi_found" = "true" ]
            then
                possible_data_idx=p$possible_data_idx
            fi

            data_partition=`sfdisk -l /dev/$disk | grep /dev/$disk$possible_data_idx 2>/dev/null | grep 'Linux filesystem' 2>/dev/null`
            if [ "$data_partition" != "" ]
            then
                sizeKBdata=`sfdisk -s /dev/$disk$possible_data_idx 2>/dev/null`
                if [ "$sizeKBdata" -gt 204800 ]        # groesser als 200 MB
                then
                    data_idx=$possible_data_idx
                fi
            fi
        fi
    fi

    if [ "$data_idx" = "" ]
    then
        while [ 1 ]
        do
            echo -e "Index of extra data partition (1-15, p1-p7, 0=none) /dev/$disk\c"
            read data_idx
            case "$data_idx" in
            1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|p1|p2|p3|p4|p5|p6|p7)
                if [ -f /tmp/gpt ]
                then
                    sgdisk -i $data_idx /dev/$disk | grep '(Linux filesystem)' >/dev/null
                else
                    sfdisk -l /dev/$disk | grep /dev/$disk$data_idx | grep 'Linux filesystem' >/dev/null
                fi

                if [ "$?" != "0" ]
                then
                    colecho "/dev/$disk$data_idx is no valid Linux partition!" br x br
                else
                    if [ -f /tmp/gpt ]
                    then
                        # Partitionsnummern bei sgdisk sind auch bei ida/cciss 1, 2, ...
                        # und nicht p1, p2, ... !
                        # Aber formatiert wird dann /dev/cciss/c0d0p1
                        if [ "$cpqscsi_found" = "true" ]
                        then
                            data_idx=p$data_idx
                        fi
                    fi

                    if [ $data_idx = $boot_idx ]
                    then
                        colecho "/dev/$disk$data_idx is already reserved for boot partition!" br x br
                    else
                        if [ $data_idx = $root_idx ]
                        then
                            colecho "/dev/$disk$data_idx is already reserved for root partition!" br x br
                        else
                            data_format='no'
                            break
                        fi
                    fi
                fi
                ;;
            0)
                break
                ;;
            esac
        done
    fi

    if [ -f /tmp/gpt ]
    then
        s=`sgdisk -p /dev/$disk | grep ' 8200 '`
    else
        s=`sfdisk -l /dev/$disk 2>/dev/null | grep 'Linux swap'`
    fi

    if [ "$s" != "" ]
    then
        if [ "$1" = "selfdefined" ]
        then
            echo
            echo "Found Linux swap partition(s) on /dev/$disk:"
            if [ -f /tmp/gpt ]
            then
                sgdisk -p /dev/$disk | grep 'Number'
                sgdisk -p /dev/$disk | grep ' 8200 '
            else
                sfdisk -l /dev/$disk | grep 'Linux swap'
            fi

            echo
            ask "Use existing swap partition on /dev/$disk"
            if [ "$a" = "y" ]
            then
                do_swap='yes'
            else
                swap_idx=0
            fi
        else
            if [ -f /tmp/gpt ]
            then
                # grep for label 'swap' from partitioning with this installer
                swap_partition=`sgdisk -p /dev/$disk | grep 'swap$' | grep -m 1 ' 8200 ' | cut -d' ' -f1-9 | /busybox sed 's# ##g'`
                if [ "$swap_partition" != "" ]
                then
                    swap_idx=$swap_partition
                else
                    swap_partition=`sgdisk -p /dev/$disk | grep -m 1 ' 8200 ' | cut -d' ' -f1-9 | /busybox sed 's# ##g'`
                    if [ "$swap_partition" != "" ]
                    then
                        swap_idx=$swap_partition
                    fi
                fi

                if [ -n "$swap_idx" -a "$cpqscsi_found" = "true" ]
                then
                    # Partitionsnummern bei sgdisk sind auch bei ida/cciss 1, 2, ...
                    # und nicht p1, p2, ... !
                    # Aber formatiert wird dann /dev/cciss/c0d0p2
                    swap_idx=p$swap_idx
                fi
            else
                swap_partition=`sfdisk -l /dev/$disk | grep -m 1 'Linux swap' | cut -f1 -d" " | /busybox sed "s#/dev/$disk##g"`
                if [ "$swap_partition" != "" ]
                then
                    swap_idx=$swap_partition
                fi
            fi
        fi

        if [ "$do_swap" = "yes" -o "$swap_idx" = "" ]
        then
            while [ 1 ]
            do
                echo -e "Index of swap partition (1-15, p1-p7) /dev/$disk\c"
                read swap_idx
                case "$swap_idx" in
                1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|p1|p2|p3|p4|p5|p6|p7)
                    if [ -f /tmp/gpt ]
                    then
                        sgdisk -i $swap_idx /dev/$disk | grep '(Linux swap)' >/dev/null
                    else
                        sfdisk -l /dev/$disk | grep /dev/$disk$swap_idx | grep 'Linux swap' >/dev/null
                    fi

                    if [ "$?" != "0" ]
                    then
                        colecho "/dev/$disk$swap_idx is no valid swap partition!" br x br
                        echo -e "Index of swap partition (1-15, p1-p7) /dev/$disk\c"
                        read swap_idx
                    else
                        if [ -f /tmp/gpt ]
                        then
                            # Partitionsnummern bei sgdisk sind auch bei ida/cciss 1, 2, ...
                            # und nicht p1, p2, ... !
                            # Aber formatiert wird dann /dev/cciss/c0d0p1
                            if [ "$cpqscsi_found" = "true" ]
                            then
                                swap_idx=p$swap_idx
                            fi
                        fi

                        break
                    fi
                    ;;
                esac
            done
        fi
    else
        swap_idx=0
    fi

    echo
    colecho "Summary:" gn
    if [ $boot_idx = 0 ]
    then
        echo "  boot partition: none (use root partition for boot)"
    else
        echo "  boot partition: /dev/$disk$boot_idx (will be erased)"
    fi

    echo "  root partition: /dev/$disk$root_idx (will be erased)"

    if [ $data_idx = 0 ]
    then
        echo "  data partition: none"
    else
        if [ "$data_format" = "yes" ]
        then
            echo "  data partition: /dev/$disk$data_idx (will be erased)"
        else
            echo "  data partition: /dev/$disk$data_idx (will be left untouched)"
        fi
    fi

    if [ $swap_idx = 0 ]
    then
        echo "  swap partition: none"
    else
        echo "  swap partition: /dev/$disk$swap_idx (will be erased)"
    fi
    echo
}

do_clean_disks ()
{
    # force stop automount all /dev/md* partitions
    for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    do
        mdadm -r /dev/md${i} >/dev/null 2>&1
        mdadm --stop /dev/md${i} >/dev/null 2>&1
    done

    # Reste eines eventuell vorhanden alten RAID loeschen
    mdadm --zero-superblock /dev/${disk} >/dev/null 2>&1
    for y in 1 2 3 4 5 6 7 8 9 10 p1 p2 p3 p4 p5 p6 p7
    do
        mdadm --zero-superblock /dev/${disk}${y} >/dev/null 2>&1
    done
}

# quiet kernel messages, md sucks
echo "0" >/proc/sys/kernel/printk

if [ -z "${_raid_level}" ]
then
    if [ -f /tmp/gpt ]
    then
        l=`sgdisk -p /dev/$disk | grep ' 8300 '`
    else
        l=`sfdisk -l /dev/$disk 2>/dev/null | grep 'Linux filesystem'`
    fi
fi

if [ -n "$l" ]
then
    echo "Found Linux partition(s) on /dev/$disk:"
    if [ -f /tmp/gpt ]
    then
        sgdisk -p /dev/$disk | grep 'Number'
        sgdisk -p /dev/$disk | grep ' 8300 '
    else
        sfdisk -l /dev/$disk | grep 'Linux filesystem'
    fi

    if [ -f /tmp/gpt ]
    then
        s=`sgdisk -p /dev/$disk | grep ' 8200 '`
    else
        s=`sfdisk -l /dev/$disk 2>/dev/null | grep 'Linux swap'`
    fi

    if [ "$s" != "" ]
    then
        echo
        echo "Found Linux swap partition(s) on /dev/$disk:"
        if [ -f /tmp/gpt ]
        then
            sgdisk -p /dev/$disk | grep 'Number'
            sgdisk -p /dev/$disk | grep ' 8200 '
        else
            sfdisk -l /dev/$disk | grep 'Linux swap'
        fi
    fi

    echo
    ask "Use existing Linux partition(s)"
    if [ "$a" = "y" ]
    then
        do_find_partitions "auto"

        colecho "WARNING: If you answer yes, COMPLETE DATA ON LISTED PARTITIONS WILL BE ERASED!" br x br
        echo
        ask "Partition setup ok"
        if [ "$a" != "y" ]
        then
            echo "Trying again with your choices ..."
            echo
            echo "Found Linux partition(s) on /dev/$disk:"
            if [ -f /tmp/gpt ]
            then
                sgdisk -p /dev/$disk | grep 'Number'
                sgdisk -p /dev/$disk | grep ' 8300 '
            else
                sfdisk -l /dev/$disk | grep 'Linux filesystem'
            fi

            echo
            do_find_partitions "selfdefined"

            colecho "WARNING: If you answer yes, COMPLETE DATA ON LISTED PARTITIONS WILL BE ERASED!" br x br
            echo
            ask "Partition setup ok"
            if [ "$a" != "y" ]
            then
                echo
                colecho "Sorry. Exit." br x br
                sleep 100000
                exit 1
            fi
        fi
    else
        do_clean_disks
        . /setup.part
    fi
else
    if [ -z "${_raid_level}" ]
    then
        ask "eisfair installation on complete /dev/$disk, ok"
    else
        ask "eisfair installation on ${_raid_extra_dev}, ok"
    fi

    if [ "$a" != "y" ]
    then
        colecho "Sorry. Exit." br x br
        sleep 100000
        exit 1
    fi

    do_clean_disks
    . /setup.part
fi

# boot_idx, root_idx, swap_idx, data_idx sind
# 0 1 2 3 4 oder p0 p1 p2 p3 p4

#----------------------------------------------------------------------------
# setup raid devices
#----------------------------------------------------------------------------
if [ -n "${_raid_level}" ]
then
    # Partitions-Id auf 'fd' aendern, Linux raid autodetect
    for i in 1 2 3 4
    do
        if [ -f /tmp/gpt ]
        then
            _disk_id=`sgdisk -i ${i} /dev/${disk} | grep 'code:' | cut -d'(' -f2 | cut -d')' -f1`
            if [ -n "${_disk_id}" ]
            then
                if [ "${_disk_id}" != "Linux RAID" ]
                then
                    sgdisk -t ${i}:fd00 /dev/${disk} >/dev/null 2>&1
                    if [ $? -ne 0 ]
                    then
                        colecho "Running" rd
                        colecho "sgdisk -t ${i}:fd00 /dev/${disk} >/dev/null 2>&1" br x br
                        colecho "failed!" rd
                    fi
                fi
            fi
        else
            #_disk_id=$(sfdisk -c /dev/${disk} ${i})
            #if [ "${_disk_id}" != "0" ]
            #then
            #    sfdisk --change-id /dev/${disk} ${i} fd >/dev/null 2>&1
            #fi

            _disk_id=$(sfdisk --part-type /dev/${disk} ${i} 2>/dev/null)
            if [ -n "${_disk_id}" ]
            then
                sfdisk --part-type /dev/${disk} ${i} fd >/dev/null 2>&1
            fi
        fi
    done

    do_clean_disks

    # Platte clonen auf alle RAID Platten
    for i in ${_raid_sub_disk}
    do
        # Reste eines eventuell vorhanden alten RAID loeschen
        mdadm --zero-superblock /dev/${i} >/dev/null 2>&1
        for y in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 p1 p2 p3 p4 p5 p6 p7
        do
            mdadm --zero-superblock /dev/${i}${y} >/dev/null 2>&1
        done

        sgdisk -Z /dev/${i} >/dev/null 2>&1

        echo "Now running cloning of partition table of /dev/${disk} to /dev/${i}:"

        if [ -f /tmp/gpt ]
        then
            colecho "sgdisk -R=/dev/${i} /dev/${disk} >/dev/null 2>&1" gn
            sgdisk -R=/dev/${i} /dev/${disk} >/dev/null 2>&1
            if [ $? -ne 0 ]
            then
                colecho "Running" rd
                colecho "sgdisk -R=/dev/${i} /dev/${disk} >/dev/null 2>&1" br x br
                colecho "failed!" rd
            fi

            # The second MBR partition slot were not cloned. The command below will
            # overwrite the second MBR partition slot and add a bootable partition
            # there of type 0 (i.e. unused), covering only the first sector of the
            # device. It will not interfere with the GPT or with the first MBR
            # partition entry which normally contains a protective MBR partition.

            printf '\200\0\0\0\0\0\0\0\0\0\0\0\001\0\0\0' | dd of=/dev/${i} bs=1 seek=462 2>/dev/null

            echo "Now running randomizing disk GUIDs and partition GUIDs of /dev/${i}:"
            colecho "sgdisk -G /dev/${i} >/dev/null 2>&1" gn
            sgdisk -G /dev/${i} >/dev/null 2>&1
            if [ $? -ne 0 ]
            then
                colecho "Running" rd
                colecho "sgdisk -G /dev/${i} >/dev/null 2>&1" br x br
                colecho "failed!" rd
            fi
        else
            dd if=/dev/zero of=/dev/${i} bs=512 count=1 2>/dev/null
            colecho "sfdisk -d /dev/${disk} | sfdisk -f /dev/${i} >/dev/null 2>&1" gn
            sfdisk -d /dev/${disk} | sfdisk -f /dev/${i} >/dev/null 2>&1
            if [ $? -ne 0 ]
            then
                colecho "Running" rd
                colecho "sfdisk -d /dev/${disk} | sfdisk -f /dev/${i} >/dev/null 2>&1" br x br
                colecho "failed!" rd
            fi
        fi
    done

    disk_idx=1
    rdisk=''
    while [ $disk_idx -le $_raid_disk_n ]
    do
        rdisk="`echo ${_raid_extra_dev} | cut -d ',' -f $disk_idx`"
        eval _raid_disk${disk_idx}='$rdisk'

        if [ -z "$raiddev" ]
        then
            raiddev="$rdisk"
        else
            raiddev="$raiddev $rdisk"
        fi

        disk_idx=`expr $disk_idx + 1`
    done

    if [ "$boot_idx" != "0" ]
    then
        for a in $raiddev
        do
            if [ -z "$raiddevlineboot" ]
            then
                raiddevlineboot="$a${boot_idx}"
            else
                raiddevlineboot="$raiddevlineboot $a${boot_idx}"
            fi
        done

        echo "Now running creation of boot raid array:"
        colecho "echo 'yes' | mdadm --quiet --create /dev/md${boot_mdx} --bitmap=internal --level=1 --metadata=0.90 --raid-devices=${_raid_n_disk} --force $raiddevlineboot >/dev/null 2>&1" gn
        echo 'yes' | mdadm --quiet --create /dev/md${boot_mdx} --bitmap=internal --level=1 --metadata=0.90 --raid-devices=${_raid_n_disk} --force $raiddevlineboot >/dev/null 2>&1
    fi

    if [ "$swap_idx" != "0" ]
    then
        for a in $raiddev
        do
            if [ -z "$raiddevlineswap" ]
            then
                raiddevlineswap="$a${swap_idx}"
            else
                raiddevlineswap="$raiddevlineswap $a${swap_idx}"
            fi
        done

        echo "Now running creation of swap raid array:"
        colecho "echo 'yes' | mdadm --quiet --create /dev/md${swap_mdx} --bitmap=internal --level=${_raid_level} --raid-devices=${_raid_n_disk} --force $raiddevlineswap >/dev/null 2>&1" gn
        echo 'yes' | mdadm --quiet --create /dev/md${swap_mdx} --bitmap=internal --level=${_raid_level} --raid-devices=${_raid_n_disk} --force $raiddevlineswap >/dev/null 2>&1
    fi

    if [ "$root_idx" != "0" ]
    then
        for a in $raiddev
        do
            if [ -z "$raiddevlineroot" ]
            then
                raiddevlineroot="$a${root_idx}"
            else
                raiddevlineroot="$raiddevlineroot $a${root_idx}"
            fi
        done

        echo "Now running creation of root raid array:"
        colecho "echo 'yes' | mdadm --quiet --create /dev/md${root_mdx} --bitmap=internal --level=${_raid_level} --raid-devices=${_raid_n_disk} --force $raiddevlineroot >/dev/null 2>&1" gn
        echo 'yes' | mdadm --quiet --create /dev/md${root_mdx} --bitmap=internal --level=${_raid_level} --raid-devices=${_raid_n_disk} --force $raiddevlineroot >/dev/null 2>&1
    fi

    if [ "$data_idx" != "0" -a "$data_format" = "yes" ]
    then
        for a in $raiddev
        do
            if [ -z "$raiddevlinedata" ]
            then
                raiddevlinedata="$a${data_idx}"
            else
                raiddevlinedata="$raiddevlinedata $a${data_idx}"
            fi
        done

        echo "Now running creation of data raid array:"
        colecho "echo 'yes' | mdadm --quiet --create /dev/md${data_mdx} --bitmap=internal --level=${_raid_level} --raid-devices=${_raid_n_disk} --force $raiddevlinedata >/dev/null 2>&1" gn
        echo 'yes' | mdadm --quiet --create /dev/md${data_mdx} --bitmap=internal --level=${_raid_level} --raid-devices=${_raid_n_disk} --force $raiddevlinedata >/dev/null 2>&1
    fi

    # safe and change $disk to md
    old_disk="${disk}"
    disk="md"
fi

#----------------------------------------------------------------------------
# always use ext4
#----------------------------------------------------------------------------
filesystem='ext4'

#----------------------------------------------------------------------------
# setup boot filesystem
#----------------------------------------------------------------------------
if [ "$boot_idx" != "0" ]
then
    if [ -n "${_raid_level}" ]
    then
        colecho "Formatting /dev/$disk$boot_mdx ..." gn
        mkfs.${filesystem} -qF /dev/$disk$boot_mdx >/dev/null 2>&1
    else
        colecho "Formatting /dev/$disk$boot_idx ..." gn
        mkfs.${filesystem} -qF /dev/$disk$boot_idx >/dev/null 2>&1
    fi
fi

#----------------------------------------------------------------------------
# setup data filesystem
#----------------------------------------------------------------------------
if [ "$data_idx" != "0" -a "$data_format" = "yes" ]
then
    data_filesystem=${filesystem}
    if [ -n "${_raid_level}" ]
    then
        colecho "Formatting /dev/$disk$data_mdx ..." gn
        mkfs.${filesystem} -qF /dev/$disk$data_mdx >/dev/null 2>&1
    else
        colecho "Formatting /dev/$disk$data_idx ..." gn
        mkfs.${filesystem} -qF /dev/$disk$data_idx >/dev/null 2>&1
    fi
elif [ "$data_idx" != "0" -a "${data_format:-no}" = "no" ]
then
    # get the filesystem from existing partition
    if [ -n "${_raid_level}" ]
    then
        data_filesystem=`blkid -s TYPE -o value /dev/$disk$data_mdx`
    else
        data_filesystem=`blkid -s TYPE -o value /dev/$disk$data_idx`
    fi
fi

#----------------------------------------------------------------------------
# setup swap space
#----------------------------------------------------------------------------
if [ "$swap_idx" != "0" ]
then
    if [ -n "${_raid_level}" ]
    then
        colecho "Setting up swap space on /dev/$disk$swap_mdx ..." gn
        mkswap /dev/$disk$swap_mdx >/dev/null 2>&1
        swapon /dev/$disk$swap_mdx
    else
        colecho "Setting up swap space on /dev/$disk$swap_idx ..." gn
        mkswap /dev/$disk$swap_idx >/dev/null 2>&1
        swapon /dev/$disk$swap_idx
    fi
fi

#----------------------------------------------------------------------------
# setup root filesystem
#----------------------------------------------------------------------------
if [ -n "${_raid_level}" ]
then
    colecho "Formatting /dev/$disk$root_mdx ..." gn
    mkfs.${filesystem} -qF /dev/$disk$root_mdx >/dev/null 2>&1
else
    colecho "Formatting /dev/$disk$root_idx ..." gn
    mkfs.${filesystem} -qF /dev/$disk$root_idx >/dev/null 2>&1
fi

#----------------------------------------------------------------------------
# install root filesystem
#----------------------------------------------------------------------------
colecho "Installing root filesystem ..." gn
mkdir /rootfs /boot

if [ -n "${_raid_level}" ]
then
    mount /dev/$disk$root_mdx /rootfs
else
    mount /dev/$disk$root_idx /rootfs
fi

cd /rootfs
mkdir tmp proc mnt media boot data
chmod 1777 tmp

if [ "${boot_media}" = "pxe" ]
then
    /busybox tftp -l - -r ${InstFolder:-eis}/rootfs.tlz -g ${DHCP_SERVER} |
        tar --lzma -xpf -
    rootfs_ret=${?}
else
    tar xpfa /mnt/rootfs.tlz
    rootfs_ret=${?}
fi

if [ "${rootfs_ret}" != "0" ]
then
    echo "Something went wrong"
    echo "untar rootfs failed"
    cd /
    if [ "${boot_media}" = "pxe" ]
    then
        umount /rootfs
    else
        umount /mnt
        umount /rootfs
    fi
    exit 1
fi

#----------------------------------------------------------------------------
# configuring lilo bootloader
#----------------------------------------------------------------------------
# create temporary file for lilo for installation and first boot
write_lilo_tmp_file ()
{
    lilo_tmp_file='/tmp/lilo.conf.tmp'
    {
    echo "lba32"
#    echo "large-memory"

    SD_DEVICES="a:8:0:4
                b:8:16:4
                c:8:32:4
                d:8:48:4
                e:8:64:4
                f:8:70:4
                g:8:86:4
                h:8:102:4"

    for sd in ${SD_DEVICES}
    do
        sd1=`echo "${sd}" | /rootfs/usr/bin/cut -d':' -f1`
        sd2=`echo "${sd}" | /rootfs/usr/bin/cut -d':' -f2`
        sd3=`echo "${sd}" | /rootfs/usr/bin/cut -d':' -f3`
        sd4=`echo "${sd}" | /rootfs/usr/bin/cut -d':' -f4`

        if [ ! -b /rootfs/dev/sd${sd1} ]
        then
            /rootfs/usr/bin/mknod /rootfs/dev/sd${sd1} b ${sd2} ${sd3}
        fi

        idx=1
        idy=`/rootfs/usr/bin/expr ${sd3} + 1`
        while [ ${idx} -le ${sd4} ]
        do
            if [ ! -b /rootfs/dev/sd${sd1}${idx} ]
            then
                /rootfs/usr/bin/mknod /rootfs/dev/sd${sd1}${idx} b ${sd2} ${idy}
            fi

            idx=`/rootfs/usr/bin/expr ${idx} + 1`
            idy=`/rootfs/usr/bin/expr ${idy} + 1`
        done
    done

    if [ -n "${_raid_level}" ]
    then
        echo "disk = ${_raid_disk1}"
        echo "  bios = 0x80"
        echo "boot = /dev/${disk}${boot_mdx}"
        echo "raid-extra-boot = ${_raid_extra_dev}"

        MD_DEVICES="0:9:0
                    1:9:1
                    2:9:2
                    3:9:3
                    4:9:4"

        for md in ${MD_DEVICES}
        do
            md1=`echo "${md}" | cut -d':' -f1`
            md2=`echo "${md}" | cut -d':' -f2`
            md3=`echo "${md}" | cut -d':' -f3`

            if [ ! -b /rootfs/dev/md${md1} ]
            then
                /rootfs/usr/bin/mknod /rootfs/dev/md${md1} b ${md2} ${md3}
            fi
        done
    else
        echo "disk = /dev/$disk"
        echo "  bios = 0x80"
        echo "boot = /dev/$disk"
    fi

    echo "read-only"
    echo "prompt"
    echo "timeout = 50"
    echo "vga = normal"
    echo "image = /boot/kernel"

    if [ "${boot_media}" = "USB-Stick" ]
    then
        case ${disk} in
        sdb)  disk=sda  ;;
        sdc)  disk=sdb  ;;
        sdd)  disk=sdc  ;;
        sde)  disk=sdd  ;;
        sdf)  disk=sde  ;;
        sdg)  disk=sdf  ;;
        sdh)  disk=sdg  ;;
        esac
    fi

    if [ -f /usb_install ]
    then
        __disk=${disk}
        disk=sda
    fi

    if [ -n "${_raid_level}" ]
    then
        echo "root = /dev/$disk$root_mdx"
    else
        echo "root = /dev/$disk$root_idx"
    fi

    echo "label = eis"
    echo "initrd = /boot/initrd.gz"

    if [ -f /usb_install ]
    then
        disk=${__disk}
    fi

    if [ "${boot_media}" = "USB-Stick" ]
    then
        case ${disk} in
            sda)  disk=sdb  ;;
            sdb)  disk=sdc  ;;
            sdc)  disk=sdd  ;;
            sdd)  disk=sde  ;;
            sde)  disk=sdf  ;;
            sdf)  disk=sdg  ;;
            sdg)  disk=sdh  ;;
        esac
    fi

    echo "append = \"raid=noautodetect\""
    } >/rootfs${lilo_tmp_file}

    {
    echo "/sbin/lilo"
    echo "rm -f ${lilo_tmp_file}"
    echo "rm -f \${0}"
    } >/rootfs/etc/rc2.d/S99liloonce
    chmod +x /rootfs/etc/rc2.d/S99liloonce
}

if [ "$boot_idx" != "0" ]           # /boot
then
    if [ "${boot_media}" = "USB-Stick" ]
    then
        case ${disk} in
        md*)
            write_lilo_tmp_file
            bootdir=/rootfs/boot
            ;;
        sd*)
            write_lilo_tmp_file
            bootdir=/rootfs/boot
            ;;
          *)
            bootdir=/boot
            ;;
        esac

        if [ -f /usb_install ]
        then
            write_lilo_tmp_file
            bootdir=/rootfs/boot
        fi

        if [ -n "${_raid_level}" ]
        then
            mount /dev/$disk$boot_mdx $bootdir
        else
            mount /dev/$disk$boot_idx $bootdir
        fi
    else
        bootdir=/boot
        if [ -f /usb_install ]
        then
            write_lilo_tmp_file
            bootdir=/rootfs/boot
        fi

        if [ -n "${_raid_level}" ]
        then
            mount /dev/$disk$boot_mdx $bootdir
        else
            mount /dev/$disk$boot_idx $bootdir
        fi
    fi
else
    bootdir=/rootfs/boot
    if [ "${boot_media}" = "USB-Stick" ]
    then
        case ${disk} in
        md*)
            write_lilo_tmp_file
            ;;
        sd*)
            write_lilo_tmp_file
            ;;
        esac
    fi

    if [ -f /usb_install ]
    then
        write_lilo_tmp_file
    fi
fi

cd $bootdir
if [ "${boot_media}" = "pxe" ]
then
    /busybox tftp -l - -r ${InstFolder:-eis}/boot.tlz -g ${DHCP_SERVER} |
        tar --lzma -xpf -
else
    tar xpfa /mnt/boot.tlz
fi

# write normal lilo conf file
cd /
colecho "Setting up lilo.conf ..." gn

{
echo "lba32"
#echo "large-memory"

if [ "${boot_media}" = "USB-Stick" ]
then
    case ${disk} in
    sdb)  disk=sda  ;;
    sdc)  disk=sdb  ;;
    sdd)  disk=sdc  ;;
    sde)  disk=sdd  ;;
    sdf)  disk=sde  ;;
    sdg)  disk=sdf  ;;
    sdh)  disk=sdg  ;;
    esac
fi

if [ -f /usb_install ]
then
    __disk=${disk}
    disk=sda
fi

if [ -n "${_raid_level}" ]
then
    echo "boot = /dev/${disk}${boot_mdx}"
    echo "raid-extra-boot = mbr"
else
    echo "boot = /dev/$disk"
fi

echo "read-only"
echo "prompt"
echo "timeout = 50"
echo "vga = normal"
echo "image = /boot/kernel"

if [ -n "${_raid_level}" ]
then
    echo "root = /dev/$disk$root_mdx"
else
    echo "root = /dev/$disk$root_idx"
fi

echo "label = eis"
echo "initrd = /boot/initrd.gz"

if [ -f /usb_install ]
then
    disk=${__disk}
fi

if [ "${boot_media}" = "USB-Stick" ]
then
    case ${disk} in
    sda)  disk=sdb  ;;
    sdb)  disk=sdc  ;;
    sdc)  disk=sdd  ;;
    sdd)  disk=sde  ;;
    sde)  disk=sdf  ;;
    sdf)  disk=sdg  ;;
    sdg)  disk=sdh  ;;
    esac
fi

echo "append = \"raid=noautodetect\""
} >/etc/lilo.conf

#----------------------------------------------------------------------------
# Create initramfs
#----------------------------------------------------------------------------
colecho "Creating initramfs ..." gn

# no space left, remove now unneeded stuff
/rootfs/usr/bin/rm /bin/dd
/rootfs/usr/bin/rm /bin/mknod
/rootfs/usr/bin/rm /bin/mount
/rootfs/usr/bin/rm /bin/sleep
/rootfs/usr/bin/rm /bin/uname
/rootfs/usr/bin/rm /bin/xz
/rootfs/usr/bin/rm /bin/zcat
/rootfs/usr/bin/rm /build
/rootfs/usr/bin/rm /have_raid_disk
/rootfs/usr/bin/rm /kernel_version
/rootfs/usr/bin/rm /lib64/libe2p.so.2
/rootfs/usr/bin/rm /lib64/libext2fs.so.2
/rootfs/usr/bin/rm /lib64/libfdisk.so.1
/rootfs/usr/bin/rm /lib64/libgcc_s.so.1
/rootfs/usr/bin/rm /lib64/libstdc++.so.6
/rootfs/usr/bin/rm /lib64/libsmartcols.so.1
/rootfs/usr/bin/rm /init
/rootfs/usr/bin/rm /netinfo
/rootfs/usr/bin/rm /sbin/blkid
/rootfs/usr/bin/rm /sbin/insmod
/rootfs/usr/bin/rm /sbin/kmod
/rootfs/usr/bin/rm /sbin/mke2fs
/rootfs/usr/bin/rm /sbin/mkfs.ext3
/rootfs/usr/bin/rm /sbin/mkfs.ext4
/rootfs/usr/bin/rm /sbin/mkswap
/rootfs/usr/bin/rm /sbin/rmmod
/rootfs/usr/bin/rm /sbin/sfdisk
/rootfs/usr/bin/rm /sbin/sgdisk
/rootfs/usr/bin/rm /sbin/swapon
/rootfs/usr/bin/rm /scsi.list
/rootfs/usr/bin/rm -f /scsi-modules # existiert nur wenn scsi.sh ausgefuehrt wird
/rootfs/usr/bin/rm /scsi.sh
/rootfs/usr/bin/rm /setup.part
/rootfs/usr/bin/rm /usr/bin/expr
/rootfs/usr/bin/rm /usr/bin/printf
# tty is needed for colecho
#/rootfs/usr/bin/rm /usr/bin/tty

initrd_mount='/initramfs'
mkdir -p $initrd_mount
cd $initrd_mount
tar -xpzf /rootfs/var/install/initrd/initramfs_udev.tar.gz

# entpacke die initrd.gz vom bootmedium mit allen notwendigen dateien
# sie wird beim kernelupdate in der virtuellen maschine vom kernel-update
# erzeugt und beinhaltet nur kein mdadm - aber mdadm wird bei bedarf kopiert
cp $bootdir/initrd.gz /tmp
gzip -d /tmp/initrd.gz
/rootfs/usr/bin/cpio --quiet -u -i < /tmp/initrd
/rootfs/usr/bin/rm /tmp/initrd

# no space left, remove now unneeded stuff
/rootfs/usr/bin/rm /bin/gunzip
/rootfs/usr/bin/rm /bin/tar

/rootfs/usr/bin/rm -f ${initrd_mount}/etc/modprobe.d/blacklist.conf
if [ ! -f /usb_install ]
then
    message="Blacklisting usb-storage ..."
    echo "$message"
    echo "blacklist uas" > ${initrd_mount}/etc/modprobe.d/blacklist.conf
    echo "blacklist usb-storage" >> ${initrd_mount}/etc/modprobe.d/blacklist.conf
fi

# add init header
cat $initrd_mount/initheader >$initrd_mount/init
/rootfs/usr/bin/rm $initrd_mount/initheader

# add loading message
echo '/bin/echo -e "\033[32m\033[49mLoading Input/USB/SCSI/SATA/PATA drivers ...\033[0m"' >>$initrd_mount/init

mods='psmouse.ko usb-common.ko usbcore.ko ehci-hcd.ko ehci-pci.ko uhci-hcd.ko ohci-hcd.ko ohci-pci.ko xhci-hcd.ko xhci-pci.ko hid.ko usbhid.ko hid-generic.ko hid-cherry.ko'
for mod in ${mods}
do
    # Mit den neuen modutils steht nur noch ein relativer Pfad in modules.dep:
    # kernel/drivers/input/mouse/psmouse.ko
    driver_with_path_relative=`grep "/$mod:" /rootfs/lib/modules/$kernel/modules.dep | cut -d: -f1`
    driver_with_path="/lib/modules/$kernel/$driver_with_path_relative"
    #echo "Writing \"insmod $driver_with_path\" to $initrd_mount/init."
    echo "insmod $driver_with_path" >>$initrd_mount/init
done

if [ -s /modules ]
then
    #/busybox cat /modules
    for j in `cat /modules 2>/dev/null`
    do
        modname=`basename $j`
        #echo "Writing \"insmod $j\" to $initrd_mount/init."
        echo "insmod $j" >>$initrd_mount/init

        if [ "$modname" = "usb-storage.ko" ]
        then
            # delay_use von usb-storage ist 1 sec, warte insges. 2 sec auf das Device
            {
            echo '/bin/echo -e "\033[32m\033[49mWaiting for usb-storage device coming up ...\033[0m"'
            echo '/bin/sleep 2'
            } >>$initrd_mount/init
        fi
    done

    # always waiting for devices coming up
    {
    echo '/bin/echo -e "\033[32m\033[49mWaiting for SCSI/SATA/PATA devices coming up ...\033[0m"'
    echo "/bin/sleep 1"
    } >>$initrd_mount/init
fi

# add initpartone
cat $initrd_mount/initpartone >>$initrd_mount/init

# no space left, remove now unneeded stuff
/rootfs/usr/bin/rm -f /modules # existiert nur wenn scsi.sh ausgefuehrt wird
/rootfs/usr/bin/rm $initrd_mount/initpartone

if [ -n "${_raid_level}" ]
then
    cp /sbin/mdadm $initrd_mount/sbin

    echo "DEVICE /dev/[sh]d*[0-9]" >$initrd_mount/etc/mdadm.conf
    mdadm --examine --scan --config=none >>$initrd_mount/etc/mdadm.conf
    message="/etc/mdadm.conf for initramfs:"
    echo "$message"
    message=`cat ${initrd_mount}/etc/mdadm.conf`
    echo "$message"

    echo 'echo "Executing \"mdadm --assemble --scan\" ..."' >>$initrd_mount/init
    echo "mdadm --assemble --scan" >>$initrd_mount/init
    echo 'echo "Executing \"udevadm settle\" ..."' >>$initrd_mount/init
    echo "udevadm settle" >>$initrd_mount/init
    echo 'echo "Executing \"mdadm --incremental --run --scan\" ..."' >>$initrd_mount/init
    echo "mdadm --incremental --run --scan" >>$initrd_mount/init
    echo "sleep 1" >>$initrd_mount/init
    message="Added mdadm for software raid to init of initramfs."
    echo "$message"
fi

# no space left, remove now unneeded stuff
/rootfs/usr/bin/rm /sbin/mdadm
#/rootfs/usr/bin/rm /bin/grep
/rootfs/usr/bin/rm /usr/bin/basename
/rootfs/usr/bin/rm /usr/bin/cut

# add initparttwo
cat $initrd_mount/initparttwo >>$initrd_mount/init
/rootfs/usr/bin/rm $initrd_mount/initparttwo
chmod +x $initrd_mount/init

# no space left, remove now unneeded stuff
/rootfs/usr/bin/rm /bin/cat
/rootfs/usr/bin/rm /bin/chmod

cd $initrd_mount
/rootfs/usr/bin/find . | /rootfs/usr/bin/cpio -o -H newc --quiet | gzip -9 > $bootdir/initrd.gz

# no space left, remove now unneeded stuff
/rootfs/usr/bin/rm /bin/gzip

cd /
/rootfs/usr/bin/rm -r $initrd_mount

if [ -f /usb_install ]
then
    echo "Do not remove me!" >$bootdir/usb-drive
fi

#----------------------------------------------------------------------------
# Install lilo bootloader
#----------------------------------------------------------------------------
colecho "Installing lilo bootloader ..." gn
cp /etc/lilo.conf /rootfs/etc

if [ "$boot_idx" != "0" ]          # /boot
then
    if [ "${boot_media}" = "USB-Stick" ]
    then
        case ${disk} in
        md*)
            /rootfs/usr/bin/chroot /rootfs /sbin/lilo -C ${lilo_tmp_file}
            umount ${bootdir}
            ;;
        sd*)
            /rootfs/usr/bin/chroot /rootfs /sbin/lilo -C ${lilo_tmp_file}
            umount ${bootdir}
            ;;
        *)
            /rootfs/sbin/lilo
            umount ${bootdir}
            ;;
        esac
    else
        if [ -f /usb_install ]
        then
            /rootfs/usr/bin/chroot /rootfs /sbin/lilo -C ${lilo_tmp_file}
            umount ${bootdir}
        else
            /rootfs/sbin/lilo
            umount ${bootdir}
        fi
    fi
else
    if [ "${boot_media}" = "USB-Stick" ]
    then
        case ${disk} in
        md*)
            /rootfs/usr/bin/chroot /rootfs /sbin/lilo -C ${lilo_tmp_file}
            ;;
        sd*)
            /rootfs/usr/bin/chroot /rootfs /sbin/lilo -C ${lilo_tmp_file}
            ;;
          *)
            /rootfs/usr/bin/chroot /rootfs /sbin/lilo
            ;;
        esac
    else
        if [ -f /usb_install ]
        then
            /rootfs/usr/bin/chroot /rootfs /sbin/lilo -C ${lilo_tmp_file}
            umount ${bootdir}
        else
            /rootfs/usr/bin/chroot /rootfs /sbin/lilo
        fi
    fi
fi

#----------------------------------------------------------------------------
# setup mountable devices
#----------------------------------------------------------------------------
colecho "Setting up mountable devices ..." gn

if [ "$cdrom" != "none" ]
then
    {
    echo "# /etc/udev/rules.d/81-atapi-cdrom.rules: Set ATAPI CD-ROM"
    echo "SUBSYSTEM==\"block\", KERNEL==\"${cdrom}\", SYMLINK+=\"cdrom\", GROUP=\"cdrom\""
    }>/rootfs/etc/udev/rules.d/81-atapi-cdrom.rules
fi

{
if [ -n "${_raid_level}" ]
then
    r_uuid=$(/rootfs/usr/sbin/blkid -o export /dev/$disk$root_mdx | grep '^UUID')
    echo "${r_uuid} / ${filesystem} defaults,errors=remount-ro 0 1"
else
    r_uuid=$(/rootfs/usr/sbin/blkid -o export /dev/$disk$root_idx | grep '^UUID')
    echo "${r_uuid} / ${filesystem} defaults,errors=remount-ro 0 1"
fi

if [ "$boot_idx" != "0" ]
then
    if [ -n "${_raid_level}" ]
    then
        b_uuid=$(/rootfs/usr/sbin/blkid -o export /dev/$disk$boot_mdx | grep '^UUID')
        echo "${b_uuid} /boot ${filesystem} defaults,errors=remount-ro 0 1"
    else
        b_uuid=$(/rootfs/usr/sbin/blkid -o export /dev/$disk$boot_idx | grep '^UUID')
        echo "${b_uuid} /boot ${filesystem} defaults,errors=remount-ro 0 1"
    fi
fi

if [ "$swap_idx" != "0" ]
then
    if [ -n "${_raid_level}" ]
    then
        s_uuid=$(/rootfs/usr/sbin/blkid -o export /dev/$disk$swap_mdx | grep '^UUID')
        echo "${s_uuid} none swap sw 0 0"
    else
        s_uuid=$(/rootfs/usr/sbin/blkid -o export /dev/$disk$swap_idx | grep '^UUID')
        echo "${s_uuid} none swap sw 0 0"
    fi
fi

if [ "$data_idx" != "0" ]
then
    if [ -n "${_raid_level}" ]
    then
        d_uuid=$(/rootfs/usr/sbin/blkid -o export /dev/$disk$data_mdx | grep '^UUID')
        echo "${d_uuid} /data ${data_filesystem} defaults 0 2"
    else
        d_uuid=$(/rootfs/usr/sbin/blkid -o export /dev/$disk$data_idx | grep '^UUID')
        echo "${d_uuid} /data ${data_filesystem} defaults 0 2"
    fi
fi

echo "proc        /proc          proc      defaults                 0 0"
echo "/dev/fd0    /media/floppy  auto      defaults,user,noauto     0 0"
echo "/dev/cdrom  /media/cdrom   iso9660   defaults,ro,user,noauto  0 0"
echo "devpts      /dev/pts       devpts    defaults,gid=5,mode=620  0 0"
echo "/sys        /sys           sysfs     defaults                 0 0"
echo "tmpfs       /dev/shm       tmpfs     rw,nosuid,nodev          0 0"
echo "tmpfs       /run           tmpfs     defaults                 0 0"
echo "devtmpfs    /dev           devtmpfs  mode=0755,nosuid         0 0"
} >/rootfs/etc/fstab

LD_LIBRARY_PATH=/lib:/lib64:/usr/lib:/usr/lib64:/rootfs/lib:/rootfs/lib64:/rootfs/usr/lib:/rootfs/usr/lib64
export LD_LIBRARY_PATH
change_pw root
change_pw eis
change_pw halt
change_pw reboot

#----------------------------------------------------------------------------
# write net driver to base config
#----------------------------------------------------------------------------
if [ "${net_driver}" != "" ]
then
#    /rootfs/usr/bin/sed -i "s/^\(ETH_DRV_1\)=.*#\(.*\)/\1='${net_driver}'                     #\2/" /rootfs/etc/config.d/base

    if [ "${IPADDR}" != "" ]
    then
        # set name and macaddr
        macaddr=`LANG=C /rootfs/usr/sbin/ip addr show | /rootfs/usr/bin/gawk '/ether/ {print $2}' | /rootfs/usr/bin/head -1`

        # write udev rule
        udev_folder='/etc/udev/rules.d'
        udev_file="${udev_folder}/70-persistent-net.rules"
        echo 'SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="'${macaddr}'", ATTR{dev_id}=="0x0", ATTR{type}=="1", NAME="net0"' >/rootfs${udev_file}

        /rootfs/usr/bin/sed -i "s/^\(IP_ETH_1_NAME\)=.*#\(.*\)/\1='net0'                   #\2/; \
                                s/^\(IP_ETH_1_IPADDR\)=.*#\(.*\)/\1='${IPADDR}'         #\2/; \
                                s/^\(IP_ETH_1_NETWORK\)=.*#\(.*\)/\1='${NETWORK}'         #\2/; \
                                s/^\(IP_ETH_1_NETMASK\)=.*#\(.*\)/\1='${NETMASK}'       #\2/; \
                                s/^\(IP_ETH_1_MACADDR\)=.*#\(.*\)/\1='${macaddr}'   #\2/; \
                                s/^\(IP_DEFAULT_GATEWAY\)=.*#\(.*\)/\1='${GATEWAY}'       #\2/" /rootfs/etc/config.d/base
    fi

    if [ "${DOMAIN_NAME}" != "" ]
    then
        /rootfs/usr/bin/sed -i "s/^\(DOMAIN_NAME\)=.*#\(.*\)/\1='${DOMAIN_NAME}'           #\2/" /rootfs/etc/config.d/base
    fi

    if [ "${DNS_SERVER}" != "" ]
    then
        /rootfs/usr/bin/sed -i "s/^\(DNS_SERVER\)=.*#\(.*\)/\1='${DNS_SERVER}'          #\2/" /rootfs/etc/config.d/base
    fi
fi

#----------------------------------------------------------------------------
# umount devices
#----------------------------------------------------------------------------
cd /
colecho "Unmounting root file system ..." gn

/rootfs/usr/bin/rm -f /rootfs/dev/sd*
/rootfs/usr/bin/rm -f /rootfs/dev/md*

if [ "${boot_media}" = "pxe" ]
then
    umount /rootfs
else
    umount /mnt
    umount /rootfs
fi

#----------------------------------------------------------------------------
# ok, we are ready
#----------------------------------------------------------------------------
# enable kernel messages again
#echo "6" >/proc/sys/kernel/printk

swapoff -a #2>/dev/null
echo

if [ -e /dev/cdrom ]
then
    /bin/eject
fi

colecho "Ready to go." gn
colecho "Remove installation medium and reboot your computer." br x br
colecho "Edit the base configuration after reboot to adjust hostname" br x br
colecho "and network settings!" br x br
colecho "Good luck." gn
echo
ask "Do you want to reboot now"
if [ "$a" = "y" ]
then
    /sbin/shutdown -rn now 2>/dev/null
else
    echo "Remove installation medium and reboot your computer."
    while [ 1 ]
    do
        read dummy
        echo "Remove installation medium and reboot your computer."
    done
fi

exit 0