#!/bin/bash
# $Id: setup.sh,v 1.17 2009-02-07 08:04:14 tim Exp $

export LANG=C

DISKS=$( echo /dev/hd? /dev/sd? | sed "s|/dev/||g" | sed "s/..?//g" )
while true; do
    echo
    echo "What disk to you want to install to?"
    echo "Available disks are: $DISKS"

    read DISK
    if [[ -e /dev/$DISK ]]; then
        echo
        echo "Displaying partition table for /dev/$DISK"
        fdisk -l /dev/$DISK
        echo
        echo "Continuing will erase everything on this disk."
        echo "Are you sure?"
        read yn
        [[ $yn = y ]] && break
    else
        echo "That disk does not seem to exist in /dev/"
    fi
done

echo
echo "Do you want to setup RAID? (y/n)"
read yn && RAID=y && [[ $yn = y ]] || RAID=n

echo
echo "Do you want to setup LVM? (y/n)"
echo "Saying n will install with just /boot and /. There will be no swap"
read yn && LVM=y && [[ $yn = y ]] || LVM=n

echo
echo "Do you want to use ext3 journalling? (y/n)"
read yn && EXT3=y && [[ $yn = y ]] || EXT3=n

FSOPT=-j && [[ $EXT3 = y ]] || FSOPT=
FSTYPE=ext3 && [[ $EXT3 = y ]] || FSTYPE=ext2

#Create two partitions. 128Mb for /boot the rest for LVM
fdisk /dev/${DISK} <<EOF >/dev/null 2>&1
d
1
d
2
d
3
d
4
n
p
1

+128M
n
p
2


w
EOF

#make sure there is no md superblock on /dev/${DISK}
mdadm --zero-superblock /dev/${DISK}
mdadm --zero-superblock /dev/${DISK}1
mdadm --zero-superblock /dev/${DISK}2
mdadm --examine --scan /dev/hd* /dev/sd* | sed -e 's/^[[:space:]]*devices=/DEVICE /' -e 's/,/ /' >/etc/mdadm/mdadm.conf

if [[ "$RAID" = "y" ]]; then
    #set up the md array
    modprobe md_mod

    echo y | mdadm --create /dev/md0 -a md -f --level=1 --raid-devices=2 /dev/${DISK}1 missing
    echo y | mdadm --create /dev/md1 -a md -f --level=1 --raid-devices=2 /dev/${DISK}2 missing

    mke2fs ${FSOPT} /dev/md0
    BOOT_UUID="UUID=$( vol_id --uuid /dev/md0 )"

    if [[ "$LVM" = "y" ]]; then
	pvcreate -y -ff /dev/md1
	vgcreate vg0 /dev/md1
    fi
else
    mke2fs ${FSOPT} /dev/${DISK}1
    BOOT_UUID="UUID=$( vol_id --uuid /dev/${DISK}1 )"

    if [[ "$LVM" = "y" ]]; then
	pvcreate -y -ff /dev/${DISK}2
	vgcreate vg0 /dev/${DISK}2
    fi
fi

mdadm --examine --scan /dev/hd* /dev/sd* | sed -e 's/^[[:space:]]*devices=/DEVICE /' -e 's/,/ /' >/etc/mdadm/mdadm.conf

if [[ "$LVM" = "y" ]]; then

    /etc/init.d/lvm2 start

    VGS=$( vgdisplay /dev/vg0 | grep "VG Size" | awk '{ print $3 }' )

    if [[ ${VGS%.*} -ge 13 ]]; then
        MULTIPART=1
        lvcreate -L2000 -nswap vg0
        lvcreate -L500 -nroot vg0
        lvcreate -L2000 -ntmp vg0
        lvcreate -L4000 -nusr vg0
        lvcreate -L2000 -nvar vg0
        lvcreate -L2000 -nhome vg0
    else
        MULTIPART=0
        lvcreate -L500 -nswap vg0
        PE=$( vgdisplay /dev/vg0 | grep "Free  PE" | awk '{ print $5 }' )
        RPE=$( lvdisplay /dev/vg0/swap | grep "Current LE" | awk '{ print $3 }' )
        lvcreate -l$(( ${PE} - ${RPE} )) -nroot vg0
    fi

    mkswap /dev/vg0/swap
    swapon /dev/vg0/swap
    mke2fs ${FSOPT} /dev/vg0/root
    ROOT_UUID=/dev/mapper/vg0-root
else
    MULTIPART=0
    mke2fs ${FSOPT} /dev/${DISK}2
    ROOT_UUID="UUID=$( vol_id --uuid /dev/${DISK}2 )"
fi

mount ${ROOT_UUID} /mnt
cd /mnt
mkdir boot
mount ${BOOT_UUID} /mnt/boot

if [[ $MULTIPART -eq 1 ]]; then
    mke2fs ${FSOPT} /dev/vg0/tmp
    mkdir tmp
    mount /dev/vg0/tmp /mnt/tmp
    chmod 1777 tmp

    mke2fs ${FSOPT} /dev/vg0/usr
    mkdir usr
    mount /dev/vg0/usr /mnt/usr

    mke2fs ${FSOPT} /dev/vg0/var
    mkdir var
    mount /dev/vg0/var /mnt/var

    mke2fs ${FSOPT} /dev/vg0/home
    mkdir home
    mount /dev/vg0/home /mnt/home
fi

restore -r -f /cdrom/install.dmp
rm restoresymtable

cp -dp /etc/network/interfaces /mnt/etc/network/interfaces
cp -dpR /cdrom/root/.ssh /mnt/root

mkdir /mnt/cdrom
mount -o ro $(sed -n 's| /cdrom .*$||p' /proc/mounts ) /mnt/cdrom

chroot /mnt <<CHROOT

echo "deb file:/cdrom/debian stable main" >/etc/apt/sources.list
echo "deb file:/cdrom/debian stable/updates main" >>/etc/apt/sources.list
apt-get update

apt-get install -y --allow-unauthenticated udev

CHROOT

umount /mnt/cdrom
umount /mnt/boot
rm /mnt/dev/null

kill `ps -ef | grep udev[d] | awk '{print $2}'`

chroot /mnt <<CHROOT

if [[ $MULTIPART -eq 1 ]]; then
cat <<EOF >/etc/fstab
# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>               <dump>  <pass>
/dev/vg0/root   /               ${FSTYPE}    errors=remount-ro       1       1
none            /proc           proc    defaults                0       0
/dev/vg0/swap   none            swap    sw                      0       0
${BOOT_UUID} /boot ${FSTYPE} defaults   1       2
/dev/vg0/tmp    /tmp            ${FSTYPE}    defaults                0       2
/dev/vg0/usr    /usr            ${FSTYPE}    defaults                1       2
/dev/vg0/var    /var            ${FSTYPE}    defaults                1       2
/dev/vg0/home   /home           ${FSTYPE}    defaults                1       2
/dev/fd0        /floppy         auto    user,noauto             0       0
/dev/cdrom      /cdrom          iso9660 ro,user,noauto          0       0
EOF
elif [[ "$LVM" = y ]]; then
cat <<EOF >/etc/fstab
# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>               <dump>  <pass>
/dev/vg0/root   /               ${FSTYPE}    errors=remount-ro       1       1
none            /proc           proc    defaults                0       0
/dev/vg0/swap   none            swap    sw                      0       0
${BOOT_UUID} /boot ${FSTYPE} defaults   1       2
/dev/fd0        /floppy         auto    user,noauto             0       0
/dev/cdrom      /cdrom          iso9660 ro,user,noauto          0       0
EOF
else
cat <<EOF >/etc/fstab
# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>               <dump>  <pass>
${ROOT_UUID} / ${FSTYPE} errors=remount-ro 1    1
none            /proc           proc    defaults                0       0
${BOOT_UUID} /boot ${FSTYPE} defaults   1       2
/dev/fd0        /floppy         auto    user,noauto             0       0
/dev/cdrom      /cdrom          iso9660 ro,user,noauto          0       0
EOF
fi

mount /proc
mount -t sysfs none /sys

RUNLEVEL=S PREVLEVEL=N /etc/init.d/udev start

mount -o ro $(sed -n 's| /cdrom .*$||p' /proc/mounts ) /cdrom
apt-get update
apt-get install -y --allow-unauthenticated ifupdown grub ssh netbase nvi dhcp3-client console-common <<EOF
EOF

install-keymap uk

[[ "$LVM" = "y" ]] && apt-get install -y --allow-unauthenticated lvm2
[[ "$RAID" = "y" ]] && apt-get install -y --allow-unauthenticated mdadm

echo "PasswordAuthentication no" >>/etc/ssh/sshd_config
sed "s/\( *HashKnownHosts *\)yes/\1no/" /etc/ssh/ssh_config >/etc/ssh/ssh_config.tmp
mv /etc/ssh/ssh_config.tmp /etc/ssh/ssh_config

mount /boot
[[ "$LVM" = "y" ]] && /etc/init.d/lvm2 start

cat <<EOF >/etc/kernel-img.conf
link_in_boot = Yes
do_symlinks = Yes
do_initrd = Yes
relative_links = Yes
EOF

apt-get -y --allow-unauthenticated -f install linux-image-2.6-486 atl2-modules-2.6-486

mkdir -p /boot/grub

cat <<EOF >/boot/grub/menu.lst
#
# Sample boot menu configuration file
#

# Boot automatically after 30 secs.
timeout 10

# By default, boot the first entry.
default 0

# Fallback to the second entry.
fallback 1 2 3

# For booting Linux
title Linux-(hd0)
    root (hd0,0)
    kernel /vmlinuz root=${ROOT_UUID}
    initrd /initrd.img

# For booting Linux
title Linux-(hd1)
    root (hd1,0)
    kernel /vmlinuz root=${ROOT_UUID}
    initrd /initrd.img

# For booting Linux
title LinuxOLD-(hd0)
    root (hd0,0)
    kernel /vmlinuz.old root=${ROOT_UUID}
    initrd /initrd.img.old

# For booting Linux
title LinuxOLD-(hd1)
    root (hd1,0)
    kernel /vmlinuz.old root=${ROOT_UUID}
    initrd /initrd.img.old

# For installing GRUB into the hard disk
title Install GRUB into (hd0)
    root    (hd0,0)
    setup   (hd0)

# For installing GRUB into the hard disk
title Install GRUB into (hd1)
    root    (hd1,0)
    setup   (hd1)

# Change the colors.
title Change the colors
    color light-green/brown blink-red/blue
EOF

cp /usr/lib/grub/i386-pc/stage1 /boot/grub/
cp /usr/lib/grub/i386-pc/stage2 /boot/grub/
cp /usr/lib/grub/i386-pc/*_stage1_5 /boot/grub/

echo "(hd0) /dev/${DISK}" >/boot/grub/device.map
/usr/sbin/grub --batch --device-map=/boot/grub/device.map <<EOF
root (hd0,0)
setup (hd0)
quit
EOF

/etc/init.d/udev stop
[[ "$RAID" = "y" ]] && /etc/init.d/mdadm stop

umount /boot
umount /proc
umount /cdrom
umount /sys

CHROOT

if [[ $MULTIPART -eq 1 ]]; then
    umount /mnt/home
    umount /mnt/tmp
    umount /mnt/usr
    umount /mnt/var
fi
umount /mnt/dev

cd /

umount /mnt
swapoff -a

[[ "$LVM" = "y" ]] && /etc/init.d/lvm2 stop

if [[ "$RAID" = "y" ]]; then
    /etc/init.d/mdadm-raid stop
    rmmod raid1
    rmmod md_mod
fi

exit 0

