#! /bin/bash

# $Id: subroutines-linux 3027 2005-11-10 22:37:16Z lange $
#
# subroutine definitions for linux
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 2005 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

        # define flags to see if syslogd should be started
        # this routine is called again, so you can use a hook to redefine flags
        echo 6 > /proc/sys/kernel/printk
    fi
    define_fai_flags
    if [ $DO_INIT_TASKS -eq 1 ] ; then
        [ "$syslogd" ] && {
          klogd -c7 -f $LOGDIR/kernel.log
          syslogd -m 0 -p /tmp/etc/syslogsocket
        }

        create_resolv_conf
    fi
    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 daemon on $monserver. 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
    # extract the tar file which was the result of debootstrap
    echo "Unpacking Debian base archive"
    gzip -dc /var/tmp/base.tgz | 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
    echo -e "$IPADDR\t$HOSTNAME.$DOMAIN $HOSTNAME" >>$FAI_ROOT/etc/hosts
    echo $HOSTNAME >$FAI_ROOT/etc/hostname
    cp -r /etc/apt/* $FAI_ROOT/etc/apt/
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_updatebase() {

    # maybe the base system is not up to date
    echo "Updating base"
    # try to mount the config space, since it can also contain Debian Packages
    # in /fai/files/packages 
    [ "$FAI_ROOT" = "/" ] || mount --bind $FAI $FAI_ROOT/$FAI
    if [ "$debug" ]; then
	updatebase | tee -a $LOGDIR/updatebase.log 2>&1
    else
	updatebase  > $LOGDIR/updatebase.log 2>&1
    fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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() {

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

    local frsh
    local doexit=0
    local hostname=$(hostname)
    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 "Error: $frsh failed."
	else
	    # remove pxe config, so host will use default and boot from local disk
	    $frsh /usr/sbin/fai-chboot -r $hostname
	fi
    fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sndmon() {

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