#! /bin/bash

# $Id: subroutines-linux,v 1.38 2004/11/08 13:56:48 lange Exp $
#
# subroutine definitions for linux
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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)
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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() {

    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
    define_fai_flags
    [ "$syslogd" ] && {
      klogd -c7 -f $LOGDIR/kernel.log
      syslogd -m 0 -p /tmp/etc/syslogsocket
    }

    create_resolv_conf
    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 > $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_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 
    mount --bind $FAI $FAI_ROOT/fai
    if [ "$debug" ]; then
	prepare_apt | tee -a $LOGDIR/updatebase.log 2>&1
    else
	prepare_apt  > $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
    local sd
    for sd in $SWAPLIST; do
       swapoff $sd && [ "$verbose" ] && echo "Disable swap device $sd"
    done
    umount -n $FAI_ROOT/proc
    # undo fake of all programs made by fai
    fai-divert -R
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_chboot() {

    local frsh
    local doexit=0
    local hostname
    hostname=`hostname`
    frsh="$FAI_REMOTESH -l $LOGUSER ${SERVER}"

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

    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 $?
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
