#! /bin/sh

# $Id: make-fai-nfsroot,v 1.97 2002/04/15 17:00:53 lange Exp $
#*********************************************************************
#
# make-fai-nfsroot -- create nfsroot directory and add additional packages
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 2000-2002 by Thomas Lange, lange@informatik.uni-koeln.de
# Universitaet zu Koeln
#
#*********************************************************************
# 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.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# A copy of the GNU General Public License is available as
# `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html.  You
# can also obtain it by writing to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#*********************************************************************

# Packages that are install to nfsroot by default Additional packages are
# defined with $NFSROOT_PACKAGES in fai.conf
packages="portmap dhcp-client file rdate cfengine bootpc wget rsh-client less dump
ext2resize strace hdparm parted dnsutils grub ntpdate psmisc 
dosfstools sysutils dialog libdetect0 discover mdetect read-edid kudzu hwtools"

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

if [ "$UID" -ne 0 ]; then
    echo "Run this program as root."
    exit 9
fi

while getopts rv opt ; do
    case "$opt" in
        v) verbose=1 ;;
        r) recover=1 ;;
    esac
done

set -e
. /etc/fai/fai.conf

ROOTCMD="chroot $NFSROOT"

RUNDIR=/var/run/fai/make-nfs-root
[ ! -d $RUNDIR ] && mkdir -p $RUNDIR
[ ! "$recover" ] && rm -rf $RUNDIR/*

LIBFAI=/usr/lib/fai
SHAREFAI=/usr/share/fai
conffile=$NFSROOT/etc/rcS_fai.conf
export DEBIAN_FRONTEND=noninteractive

trap "umount_dirs" EXIT
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
die() {

    echo "$@"
    exit 99
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call_with_stamp() {

    local func=$1
    local stamp=$RUNDIR/$func
    # call subroutine
    [ "$recover" -a -f $stamp ] && return 0 
    "$@"
    > $stamp
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call_verbose() {

    if [ "$verbose" ]; then
        "$@"
    else
        "$@" > /dev/null
    fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
install_kernel_nfsroot() {

    rm -rf $NFSROOT/boot/*-$KERNELVERSION $NFSROOT/lib/modules/$KERNELVERSION
    # since woody we can install the kernel using dpkg -i
    echo "do_boot_enable=no" > $NFSROOT/etc/kernel-img.conf
    dpkg -x $KERNELPACKAGE $NFSROOT
    # if $NFROOT/proc/modules exists, then update-modules calls depmod -a without
    # these special flags; so umount first
    [ -e $NFSROOT/proc/modules ] && umount $NFSROOT/proc
    chroot $NFSROOT update-modules
    chroot $NFSROOT depmod -a -F /boot/System.map-$KERNELVERSION $KERNELVERSION
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
setup_ssh() {

    # nothing to do if no ssh is available in nfsroot
    [ -f $NFSROOT/var/lib/dpkg/info/ssh.list ] || return 0
    mkdir -p -m 700 $NFSROOT/root/.ssh
    [ -f /etc/ssh/ssh_known_hosts ] && cp /etc/ssh/ssh_known_hosts $NFSROOT/root/.ssh/known_hosts
    if [ -n "$LOGUSER" ] ; then
        loguserhome=`eval "cd ~$LOGUSER 2>/dev/null && pwd;true"`
    # is copying of *.pub important ?
        [ -d $loguserhome/.ssh ] &&
	  cp -p $loguserhome/.ssh/identity* $loguserhome/.ssh/*.pub $NFSROOT/root/.ssh/
    fi

    # enable root login
    perl -pi -e 's/PermitRootLogin no/PermitRootLogin yes/' $NFSROOT/etc/ssh/sshd_config
    if [ -f "$SSH_IDENTITY" ]; then
        cp -p $SSH_IDENTITY $NFSROOT/root/.ssh/authorized_keys
        echo You can log into install clients without password using $SSH_IDENTITY
    fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
copy_fai_files() {

    # copy to nfsroot
    perl -pi -e "s#^root::#root:${FAI_ROOTPW}:#" etc/passwd
    mkdir -p $NFSROOT/$FAI/fai_config $NFSROOT/$SHAREFAI $NFSROOT/etc/fai
    cd $NFSROOT
    cp -p $LIBFAI/sbin/dhclient-script $SHAREFAI/etc/dhclient.conf etc/
    cp -Rp /etc/fai etc/
    [ -f /etc/fai/.cvspass ] && cp -p /etc/fai/.cvspass .cvspass
    cp -p $LIBFAI/sbin/* sbin/
    cp -p $SHAREFAI/etc/fai_modules_off etc/modutils/

    cp -p $SHAREFAI/subroutines* usr/share/fai
    cp -p $SHAREFAI/etc/apt.conf etc/apt
    cp -p /usr/share/perl5/Debian/Fai.pm usr/share/perl5/Debian/
    echo $NFSROOT_ETC_HOSTS >> etc/hosts
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call_debootstrap() {
    
    echo "Creating nfsroot for $1 using debootstrap"
    [ "$verbose" ] && echo "calling debootstrap $1 $NFSROOT $2"
    yes '' | debootstrap $FAI_DEBOOTSTRAP_OPTS $1 $NFSROOT $2 || true
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
create_base() {

    if [ "$FAI_DEBOOTSTRAP" ]; then
        call_verbose call_with_stamp call_debootstrap $FAI_DEBOOTSTRAP
        $ROOTCMD apt-get clean
        echo "Creating base.tgz"
        tar -C $NFSROOT -cf - . | gzip > $NFSROOT/../base.tgz
        mv $NFSROOT/../base.tgz $NFSROOT/var/tmp/base.tgz
    else
        # old method for potato
        call_with_stamp get_basetgz
        echo "Unpacking base2_2.tgz"
        zcat /tmp/base2_2.tgz | tar -C $NFSROOT -xpf -
        cp -p /tmp/base2_2.tgz $NFSROOT/var/tmp/base.tgz
    fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
create_nfsroot() {

    mkdir -p $NFSROOT/$FAI
    cd $NFSROOT || die "Error: Can't cd to $NFSROOT"

    call_with_stamp create_base

    [ ! -c dev/boot255 ] && mknod dev/boot255 c 0 255

    if [ "$FAI_DEBMIRROR" ]; then
        mkdir -p $NFSROOT/$MNTPOINT
        mount -o ro,rsize=8192 $FAI_DEBMIRROR $NFSROOT/$MNTPOINT || \
                die "Can't mount $FAI_DEBMIRROR"
        fi

    # hoaks some packages
    # liloconfig, dump and raidtool2 needs these files
    echo "# UNCONFIGURED FSTAB FOR BASE SYSTEM" > etc/fstab
    > etc/raidtab
    mkdir -p lib/modules/$KERNELVERSION           # dirty trick to hoax lvm
    >  lib/modules/$KERNELVERSION/modules.dep  # dirty trick to hoax lvm
    mkdir -p etc/ssh
    echo 'NTPSERVERS=""' > etc/default/ntp-servers

    # potato only
    > etc/ssh/NOSERVER
    # woody uses debconf

    if [ "$FAI_SOURCES_LIST" ]; then
        echo "$FAI_SOURCES_LIST" > etc/apt/sources.list
    else
        cp /etc/apt/sources.list etc/apt/sources.list
    fi
    [ -f /etc/apt/preferences ] && cp /etc/apt/preferences etc/apt
    echo "Upgrading $NFSROOT"
    call_verbose call_with_stamp upgrade_nfsroot  
    echo "Adding additional packages to $NFSROOT:"
    echo "$packages"
    call_verbose call_with_stamp add_packages_nfsroot
    call_with_stamp copy_fai_files

    # set timezone
    rm -f etc/localtime
    cp -d /etc/localtime /etc/timezone etc

    # make little changes to nfsroot, because nfsroot is
    # read only for the install clients 
    rm -rf etc/mtab var/run etc/sysconfig
    mv etc/init.d/rcS etc/init.d/rcS.orig
    ln -s /proc/mounts etc/mtab
    ln -s /tmp/var/run var/run 
    ln -sf /tmp/etc/resolv.conf etc/resolv.conf
    ln -sf /tmp etc/sysconfig
    ln -s /sbin/rcS_fai etc/init.d/rcS
    ln -s /dev/null etc/network/ifstate
    # for nis
    [ -d var/yp ] && ln -s /tmp/binding var/yp/binding

    # turn off logging of loading kernel modules
    [ -d var/log/ksymoops/ ] && rmdir var/log/ksymoops/
    ln -s /dev/null var/log/ksymoops

    # definition for loopback device
    echo "iface lo inet loopback" > etc/network/interfaces

    echo "*.* /tmp/syslog.log" > etc/syslog.conf
    cat >> root/.profile <<-EOF
	. $SHAREFAI/subroutines
	. $SHAREFAI/subroutines-$OS_TYPE
	set -a
	. /etc/fai/fai.conf
	. /tmp/rcsfai.var
EOF

    call_verbose call_with_stamp setup_ssh

    cat >$NFSROOT/etc/rc2.d/S01fai_abort <<-EOF
        #!/bin/sh
        echo FAI: installation aborted.
        echo reboot with: faireboot
        echo or after a logout
        sh
        cd /
        umount -ar
        reboot -dfi
EOF
    chmod a+rx $NFSROOT/etc/rc2.d/S01fai_abort
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
upgrade_nfsroot() {

    cp /etc/resolv.conf $NFSROOT/etc
    $ROOTCMD apt-get update
    $ROOTCMD apt-get check
    rm -rf $NFSROOT/etc/apm
    mount -t proc /proc $NFSROOT/proc
    $ROOTCMD apt-get --purge -y install debconf </dev/null

    # fake start-stop-dameon
    $ROOTCMD dpkg-divert --quiet --package fai --add --rename /sbin/start-stop-daemon
    cp $LIBFAI/sbin/start-stop-daemon $NFSROOT/sbin
    $ROOTCMD apt-get -y upgrade
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
add_packages_nfsroot() {

    $ROOTCMD apt-get -y --fix-missing install $packages </dev/null
    if [ -n "$NFSROOT_PACKAGES" ] ; then
        $ROOTCMD apt-get -y --fix-missing install $NFSROOT_PACKAGES </dev/null
    fi
    $ROOTCMD apt-get clean
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
get_basetgz() {

    [ -f /tmp/base2_2.tgz ] && return
    [ "$FAI_BASETGZ" ] || die "No /tmp/base2_2.tgz found and FAI_BASETGZ not defined."
    case $FAI_BASETGZ in
        ftp:*|http:*)
            echo "Fetching $FAI_BASETGZ via wget. This may take some time."
            TMPBDIR=`mktemp /tmp/FAI-wget-XXXXXX` || exit 1
            rm $TMPBDIR; mkdir $TMPBDIR || exit
            wget -P$TMPBDIR $FAI_BASETGZ
            mv $TMPBDIR/base2_2.tgz /tmp
            rm -rf $TMPBDIR
            ;;
        /*/base2_2.tgz)
            rm -f /tmp/base2_2.tgz
            test -r $FAI_BASETGZ || die "Can't read $FAI_BASETGZ. Check FAI_BASETGZ in fai.conf."
            ln -s $FAI_BASETGZ /tmp
            ;;
        *)
            die "FAI_BASETGZ in fai.conf is $FAI_BASETGZ and looks very strange."
            ;;
    esac
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
umount_dirs() {

    cd /
    sleep 2
    umount $NFSROOT/proc 1>/dev/null 2>&1 || true
    umount $NFSROOT/dev/pts 1>/dev/null 2>&1 ||true
    if [ "$FAI_DEBMIRROR" ]; then
        test -d $NFSROOT/$MNTPOINT && umount $NFSROOT/$MNTPOINT || true
    fi
    # show directories still mounted on nfsroot
    mount | grep " on $NFSROOT " || true
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
get_kernel_version() {

    local package=$1
    KERNELVERSION=`dpkg --info $1 | grep "Package: kernel-image" | sed -e 's/.*kernel-image-'//`
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
setup_bootp(){

    mknbi-linux --verbose $NFSROOT/boot/vmlinuz-$KERNELVERSION /boot/fai/installimage
    # imggen is free software from 3com
    # it converts netboot image to images, that are bootable by 3com network cards
    imggen=`which imggen || true`
    if [ -x "$imggen" ]; then
        imggen -a /boot/fai/installimage /boot/fai/installimage_3com
    fi
    echo "BOOTP environment prepared."
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
setup_dhcp(){

    # pxe and dhcp environment
    cp -p $NFSROOT/boot/vmlinuz-$KERNELVERSION /boot/fai/installkernel
    rdev /boot/fai/installkernel $NFSROOT/dev/boot255
    [ -f /usr/lib/syslinux/pxelinux.bin ] && cp /usr/lib/syslinux/pxelinux.bin /boot/fai
    [ -d /boot/fai/pxelinux.cfg ] || mkdir /boot/fai/pxelinux.cfg || true
    cp -p /usr/share/fai/etc/pxelinux.cfg /boot/fai/pxelinux.cfg/default
    echo "DHCP environment prepared. Now enable dhcpd and the special tftp daemon"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# main routine

# Kill the directory if not in recover mode
if [ -d $NFSROOT/$FAI -a ! "$recover" ]
then
    echo $NFSROOT already exists. Removing $NFSROOT
    umount $NFSROOT/dev/pts 1>/dev/null 2>&1 || true
    rm -rf $NFSROOT/.??* $NFSROOT/*
    # also remove files $NFSROOT/.? but not . and ..
    find $NFSROOT ! -type d -xdev -maxdepth 1 | xargs -r rm -f
fi

# Create a new nfsroot
call_with_stamp create_nfsroot

# Install the kernel package
if [ -f $KERNELPACKAGE ]; then
    # determine kernel version
    get_kernel_version $KERNELPACKAGE

    # create tftp boot images
    call_with_stamp install_kernel_nfsroot

    grep -q _dhcp_ $NFSROOT/boot/System.map-$KERNELVERSION && TYPE=DHCP
    # only BOOTP need a netboot image, DHCP can do with raw kernels
    if [ "$TYPE" = DHCP ]; then
        call_with_stamp setup_dhcp
    else
        call_with_stamp setup_bootp
    fi
else
    echo "Kernel package $KERNELPACKAGE not found."
    echo "No install kernel installed in /boot/fai."
    echo "No kernel modules available in nfsroot."
fi

echo make-fai-nfsroot finished.
exit 0
