#! /bin/bash

# $Id: subroutines-linux 3433 2006-04-19 12:07:35Z lange $
#
# subroutine definitions for linux
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 2005-2006 by Thomas Lange, lange@informatik.uni-koeln.de
# Universitaet zu Koeln
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set_disk_info() {

    # the variable holds a space separated list of devices and their block size
    device_size=$(disk-info)

    # a list of all local disks, without size
    disklist=$(list_disks $device_size | sort)
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
jobsrunning() {

    # test if jobs are running
    ps r | egrep -qv "ps r|TIME COMMAND"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
eval_cmdline() {

    # parse kernel parameters and define variables
    local word

    echo -n "Kernel parameters: "; cat /proc/cmdline
    for word in $(cat /proc/cmdline) ; do
	case $word in
	    [a-zA-Z]*=*)
		eval $word
		;;
	esac
    done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_confdir() {

    if [ $DO_INIT_TASKS -eq 1 ] ; then
        local bootlog
        eval_cmdline

        bootlog=$LOGDIR/boot.log
        get-boot-info
        echo "Reading $bootlog"
        . $bootlog
        unset T170 T171 T172 ROOT_PATH BOOTFILE

        echo 6 > /proc/sys/kernel/printk
	klogd -c7 -f $LOGDIR/kernel.log
	syslogd -m 0
        create_resolv_conf
    fi
    define_fai_flags
    get_fai_dir

    # check if monitor server is available
    [ -z "$monserver" ] && monserver=$SERVER
    faimond=1
    sndhostname=$HOSTNAME # save current hostname
    if sndmon check; then
	echo "Monitoring to server $monserver enabled."
    else
	faimond=0
	echo "Can't connect to monserver on $monserver port 4711. Monitoring disabled."
    fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_partition() {

    echo "Partitioning local harddisks"
    [ ! -s $diskvar ] && setup_harddisks -d -X | tee $LOGDIR/format.log 2>&1
    # setup_harddisks must create $diskvar file
    if [ ! -s $diskvar ]; then
	cat $LOGDIR/format.log
	sndmon "TASKERROR partition 21"
	die "setup_harddisks did not create $diskvar file."
    fi
    # now define variable for root and boot partition and boot device
    . $diskvar
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_extrbase() {

    local fs=$FAI_ROOT/etc/$fstab
    local basefile=/var/tmp/base.tgz
    [ $DO_INIT_TASKS -eq 0 ] && basefile=$NFSROOT/var/tmp/base.tgz
    # extract the tar file which was the result of debootstrap
    echo "Unpacking Debian base archive"
    gzip -dc $basefile | tar -C $FAI_ROOT -xpf -
    # now we can copy fstab
    [ -f $fs ] && mv $fs $fs.old
    cp -p $LOGDIR/$fstab $fs
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_mirror() {

    # mount debian mirror directory
    [ "$FAI_DEBMIRROR" ] || return   # nothing to do
    mkdir -p $FAI_ROOT/$MNTPOINT
    if mount $romountopt $FAI_DEBMIRROR $FAI_ROOT/$MNTPOINT; then
      [ "$debug" ] && echo "Mirror mounted from $FAI_DEBMIRROR to $FAI_ROOT/$MNTPOINT"
    else
      sndmon "TASKERROR mirror $?"
      die "Can't mount $FAI_DEBMIRROR"
    fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_debconf () {

    if [ ! -d $FAI/debconf ]; then
	echo "Can't find debconf directory $FAI/debconf. Skipping preseeding."
	terror=2
	return
    fi
    fai-debconf $FAI/debconf
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_prepareapt () {

    # ftp and http needs resolv.conf in chroot environment, /etc/hosts is useful
    # think about using fcopy for these two files
    [ -f /tmp/etc/resolv.conf ] && cp /tmp/etc/resolv.conf $FAI_ROOT/etc
    [ -f /etc/hosts ] && cp /etc/hosts $FAI_ROOT/etc
    # set hostname in $FAI_ROOT
    if [ -f /var/run/fai//FAI_INSTALLATION_IN_PROGRESS ]; then
      echo -e "$IPADDR\t$HOSTNAME.$DOMAIN $HOSTNAME" >>$FAI_ROOT/etc/hosts
      echo $HOSTNAME >$FAI_ROOT/etc/hostname
    fi
    [ -d /etc/apt ] && cp -r /etc/apt/* $FAI_ROOT/etc/apt/
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_updatebase() {

    # maybe the base system is not up to date
    echo "Updating base"
    updatebase  > $LOGDIR/updatebase.log 2>&1
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_instsoft() {

    echo "Installing software may take a while"
    if [ "$debug" ]; then
	install_packages | tee -a $LOGDIR/software.log
    elif [ "$verbose" ]; then
	install_packages </dev/null 2>&1 | tee -a $LOGDIR/software.log
    else
	install_packages </dev/null >> $LOGDIR/software.log 2>&1
    fi
    # This almost indicates an error
    egrep "^E:" $LOGDIR/software.log && terror=1
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_finish() {

    # show some local information
    ifconfig; df
    # umount swap space
    swapoff -a
    # undo fake of all programs made by fai
    fai-divert -R
    date
    echo "The installation took $(cut -d . -f 1 /proc/uptime) seconds."
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_chboot() {

    # the whole subroutine may be an externel script

    [ -z "$LOGUSER" ] && return # silently return from subroutine

    local frsh
    local doexit=0
    local hostname=$(hostname)
    local ipaddr=$(cat $LOGDIR/boot.log | grep IPADDR | cut -d\' -f2)
    frsh="$FAI_REMOTESH -l $LOGUSER ${SERVER}"

    if [ -z "$SERVER" ] ; then
	echo "SERVER not defined. Can't change network boot configuration"
	terror=2
	doexit=1
    fi
    [ $doexit -eq 1 ] && return

    if dmesg | grep -q "Sending BOOTP requests"; then
        # change boot device (local disk or network) when using bootp
	[ "$LOGUSER" -a "$TFTPLINK" ] &&
	    $frsh "cd /boot/fai; rm -f $hostname; ln -s $TFTPLINK $hostname"
    else
        # change boot device (local disk or network) when using PXE
        # first test if rsh to server works
	$frsh true >/dev/null 2>&1
	if [ $? -ne 0 ]; then
	    terror=3
	    echo "WARNING: $frsh failed. Can't call fai-chboot on the install server."
	else
	    # remove pxe config, so host will use default and boot from local disk
	    $frsh /usr/sbin/fai-chboot -r $ipaddr
	fi
    fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sndmon() {

    # send message to monitor daemon
    [ "$faimond" -eq 0 ] && return 0
    echo "$sndhostname $*" | nc $monserver 4711 2>/dev/null
    return $?
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
