#!/bin/sh
#
# linuxrc file for initrd (Initial Ram Disk) for LTSP
#
# Copyright (C) 2001 James A. McQuillan <jam@McQuil.Com>
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# 04/19/2003 -jam- Changed from dhclient to dhcpcd, to work better with w2k
#                  dhcp servers.  This change is thanks to
#                  Paul Whittaker <paw@si1.dod.gov.au>
#
# 04/11/2003 -jam- Added DPORT as a kernel commandline arg, to cause the
#                  dhclient program to use an alternate port. Default is 67.
#
# 03/04/2002 -jam- Changed to use modprobe instead of insmod, so that the module
#                  dependencies would work properly.
# 02/28/2002 -jam- Added IRQ as a kernel cmdline arg, to be passed to the
#                  NIC module.  Also made IO so that it would always be passed
#                  to the NIC module, even if not required by that module.
# 12/11/2001 -jam- Added loading of 8390.o when using e2100, smc-ultra and wd
# 10/18/2001 -jam- Added support for Linux Progress Patch (LPP
# 09/22/2001 -jam- Added auto-probing to determine what type of NIC
# 09/18/2001 -jam- Added loading of the 8390.o module when either
#                  3c503, hp-plus, hp, ne or ne2k-pci are used.
#
# 09/02/2001 -jam- Initial writing of this script
#

PATH="/sbin:/bin:/usr/sbin:/usr/bin"
echo ""
echo 
echo "Bienvenue - Welcome - Bienvenido - Willkommen - Benvenuto - Boa vinda  "
echo ""

mkdir /sys
mkdir /proc
mkdir /tmp
mkdir -p /var/lock
mount -t sysfs sysfs /sys
mount -t proc proc /proc

# Determine if the script should be quiet. 
QUIET=false
for x in $(cat /proc/cmdline); do
        case $x in
        quiet)
                QUIET=true
                ;;
        esac
done

silent()
{
	if [ $QUIET = "true" ]; then
		if test -c /dev/null; then
			"$@" >/dev/null 2>&1
		else
			"$@" 2>&1 | read dummyvar
		fi
	else 
		"$@"
	fi
}

# Sleepy
_sleep() {
[ "${SLEEP}" -gt 0 ] && sleep ${SLEEP}
}

silent echo "==============================================================================="
silent echo "Running /linuxrc"


silent echo "Setting hostname"
silent hostname Xterm

silent echo "Detecting network card"

if [ -z "${NIC}" ]; then
    NIC=`/sbin/pci_scan /etc/niclist`
    if [ -z "${NIC}" ]; then
        echo
        echo "ERROR!  Could not automatically detect the network card."
        echo "        PCI cards should be detected automatically."
        echo "        ISA cards cannot be detected, so they require"
        echo "        the nic driver to be specified in a 'NIC=' parameter"
        echo "        to be passed on the kernel command line,"
        echo "        usually specified in option-129, in the"
        echo "        /etc/dhcpd.conf file.  See the LTSP docs for more info."
        echo
        exit 1
    fi
fi

IOADDR=""

#
# See if we need to pass an IO argument to the driver
#
case ${NIC} in
    3c503 | hp_plus | hp | ne | wd)
        if [ -z "${IO}" ]; then
            echo
            echo "ERROR!  No IO specified."
            echo "        The NIC requires an IO=0xNNN entry on the kernel command line."
            echo "        This is setup in the /etc/dhcpd.conf file using option-129"
            echo
            exit 1
        fi
        ;;
esac

if [ -n "${IO}" ]; then
    IOADDR="io=${IO}"
fi

if [ -n "${IRQ}" ]; then
    IRQOPT="irq=${IRQ}"
fi

silent echo "Loading network driver"
silent echo "linuxrc: Installing ${NIC} driver"
silent echo "/sbin/modprobe ${NIC} ${IOADDR} ${IRQOPT} ${NICOPT}"

silent /sbin/modprobe -v ${NIC} ${IOADDR} ${IRQOPT} ${NICOPT}

if [ $? -ne 0 ]; then
    echo
    echo "ERROR!  Failed to install the NIC driver module!"
    echo "        This could be caused by the wrong module for your particular"
    echo "        network interface card.  Double check the 'option-129' entry"
    echo "        in your /etc/dhcpd.conf file."
    echo
    exit 1
fi


#
# Ready to briging up lo interface
#

silent ifconfig lo 127.0.0.1 

#
# dhcpcd runs as a daemon.  Once it acquires the info from the
# server, it will automatically configure the interface.  A script called
# /etc/dhcpc/dhcpcd.exe is used to perform additional configuration.
#
silent echo "Sending DHCP request"

if [ -z "${DPORT}" ]; then
    DPORT=67
fi

# Loading needed modules 
silent /sbin/modprobe af_packet
silent mkdir /etc/dhcpc
silent udhcpc -i eth0 -n -s /bin/udhcpc-post -P ${DPORT} -R rootpath >/tmp/dhcpcd.out 2>&1
if [ $? -ne 0 ]; then
    echo
    cat /tmp/dhcpcd.out
    echo
    echo "ERROR! dhcpcd failed!"
    echo
    /sbin/getty 38400 tty1
fi

#
# The dhcpcd program deposited a bunch of important
# information in the /tmp/dhcpcd-eth0.info file.
# Sourcing the file will set a bunch of environment variables.
#

# 
# On a slow machine, the dhclient program might fork and return
# control to the /linuxrc script, before the info file gets written.
# 

COUNT=5
while [ $COUNT -gt 0 -a ! -f /etc/dhcpc/dhcpcd-eth0.info ]; do
    sleep 1
    COUNT=`expr $COUNT - 1`
done

if [ ! -f /etc/dhcpc/dhcpcd-eth0.info ]; then
    echo
    echo "ERROR!  No dhcpcd-eth0.info file."
    echo "        This usually indicates that dhcpcd did not get"
    echo "        a response from the DHCP server, or the NIC driver"
    echo "        isn't working properly with the network card"
    echo
    exit 1
fi

. /etc/dhcpc/dhcpcd-eth0.info

#Give some diagnostic info
silent ifconfig eth0|grep HWaddr|cut -f2- -d:
silent echo "IP: $IPADDR, NETMASK: $NETMASK GATEWAY: $GATEWAY"
silent echo "DHCP SERVER: $DHCPSID"

#SLP and others don't work without a default gateway
if [ -z $GATEWAY ];then 
    echo "No gateway! Adding myself as my own gateway."
    route add default gw $IPADDR
fi

# Check if there's a nfsroot option supplied to the kernel.  This way, we can
# create grub or pxe entries for other servers that are not in DHCP (test
# servers, different distro for certain users, etc).

[ -n "${nfsroot}" ] && ROOTPATH=$nfsroot

#
# Check for a root-path from the dhcp server
#
if [ -z "${ROOTPATH}" ]; then
    echo
    echo "ERROR!  No root-path.  Check your DHCP configuration, to make"
    echo "        sure that the 'option root-path' is specified, or"
    echo "        configure a SLP server for mille-xterm."
fi


# Return to standard LTSP

NFS_IP=` echo ${ROOTPATH} | cut -d : -f 1`
NFS_DIR=`echo ${ROOTPATH} | cut -d : -f 2`

if [ "${NFS_IP}" = "${NFS_DIR}" ]; then
    #
    # Only the root directory is specified, then we have to use the
    # default IP address.  For now, we'll use the DHCP server address
    #
    NFS_IP=${DHCPSERVER}
fi

silent echo "Mounting root filesystem" 

# Loading needed modules 
NEWROOT="/newroot"
NEWROOT_RO="/xtermroot"
NEWROOT_RW="/tmpfs"
silent /bin/mkdir $NEWROOT
silent /bin/mkdir $NEWROOT_RO
silent /bin/mkdir $NEWROOT_RW

if ! /sbin/modprobe nfs 2>1 > /dev/null; then
  echo "FATAL: nfs module can't be loaded."
  exit 1
fi

if ! /sbin/modprobe unionfs 2>1 > /dev/null; then
  echo "FATAL: unionfs module can't be loaded."
  exit 1
fi

# Resolv hostname to mount the nfsroot with an IP address
REAL_IP=`nslookup $NFS_IP  | tail -1 | awk {'print $2'}`
if [ "$REAL_IP" = "" -o "$REAL_IP" = "Unknown" ]; then
	REAL_IP=$NFS_IP
fi

silent echo "Mounting root filesystem: ${NFS_DIR} from: ${REAL_IP}"

silent mount -n -o nolock,ro ${REAL_IP}:${NFS_DIR} $NEWROOT_RO
if [ $? -ne 0 ]; then
    echo
    echo "ERROR!  Failed to mount the root directory via NFS!"
    echo "        Possible reasons include:"
    echo
    echo "        1) NFS services may not be running on the server"
    echo "        2) Workstation IP does not map to a hostname, either"
    echo "           in /etc/hosts, or in DNS"
    echo "        3) Wrong address for NFS server in the DHCP config file"
    echo "        4) Wrong pathname for root directory in the DHCP config file"
    echo
    exit 1
fi

silent echo "Crate ramdisk"
if ! /bin/mount -n -o size=2048k,mode=0755 -t tmpfs none $NEWROOT_RW; then
  echo "FATAL: creating ramdisk failed"
  exit 1
fi

silent echo "Crate unionfs root"
if ! /bin/mount -t unionfs -o dirs=$NEWROOT_RW=rw:$NEWROOT_RO=ro none $NEWROOT ; then
  echo "FATAL: can't mount the unionfs"
  exit 1
fi

silent echo "Unmounting kernel file systems"
umount /sys
umount /proc

silent echo "Doing the pivot_root"

cd $NEWROOT
if ! /sbin/pivot_root . oldroot; then
  echo "FATAL: pivot_root failed"
  exec /sbin/getty 38400 tty1
  exit 1
fi
    
cd /

silent echo "Remounting kernel file systems"
mount -t sysfs sysfs /sys
mount -t proc proc /proc

silent echo "Mounting a tmpfs over /dev..."
tmpfs_size="10M"
if ! mount -n -o size=$tmpfs_size,mode=0755 -t tmpfs none /dev; then
  echo "FATAL: udev requires tmpfs support, not started."
  exit 1
fi

silent echo "Mounting devpts over /dev/pts..."
mkdir -p /dev/pts
if ! mount -n -t devpts -o mode=620 none /dev/pts; then
  echo "FATAL: error while mouting devpts over /dev/pts"
  exit 1
fi
	
silent echo "Mounting a tmpfs over /dev/shm..."
mkdir -p /dev/shm
if ! mount -n -t tmpfs  none /dev/shm; then
  echo "FATAL: error while mounting a tmpfs over /dev/shm."
  exit 1
fi
			  
silent echo "Make initial devices"
udevstart
silent echo "Starting udevd deamon"
# use udev for all hotplug events
silent echo /sbin/udevsend > /proc/sys/kernel/hotplug
udevd -d
		
silent echo "Running /sbin/init"
exec /sbin/init
