#!/bin/sh
#
# mkdiskless is a script to configure the diskless NFS share.
#   It takes one parameter containing the name of the directory where the 
#   "root" directory of the diskless environment resides.
#   If you copied the Red Hat Enterprise Linux AS OS to 
#       /diskless/i386/RHELAS/root then you would run
#       mkdiskless /diskless/i386/RHELAS
#   This script can be run multiple times and should be run everytime you 
#   update the software on this share.
#
# Copyright (C) 2003 Daniel Walsh <dwalsh@redhat.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
#

if [ $# != 1 ]; then
        echo $"Usage: $0 DISKLESSDIRECTORY"
        exit 1
fi
INSTALLDIR=/usr/share/redhat-config-netboot/diskless
base=`basename $1`
if [ $base == "root" ]; then
    wd=`dirname $1`
else
    wd=$1
fi
ROOT=$wd/root
ROOT=`dirname $ROOT`/root
if [ ! -d "$ROOT" ]; then
        echo $"$wd must point to a directory containing a root subdirectory"
        exit 1
fi
SNAPSHOT=$wd/snapshot
#
# Create snapshot directory which will contain all client specific files.
#
mkdir -p "$SNAPSHOT"
#
#  Copy the file files to the snapshot directory.
#  This file contains a  list of the non-shared files/directories 
#   on to the diskless root directory
#  Everytime the client boots it checks this file.  
#      If the client's snapshot directory does not contain a file/directory c
#      contained in files, it will copy it from the root directory.
#  The client will mount -o bind all entries from the snapshot directory over 
#      The entries in the root directory, so that they can be written too.
#
cp $INSTALLDIR/files "$SNAPSHOT"
#
#  Add the diskless service script.  Currently this script only unmounts the 
#  memory resident kernel used to boot the diskless client.
#
cp $INSTALLDIR/diskless.init "$ROOT"/etc/init.d/diskless
chmod 755 "$ROOT"/etc/init.d/diskless
(cd "$ROOT"/etc/rc3.d/; ln -sf ../init.d/diskless S01diskless)
(cd "$ROOT"/etc/rc5.d/; ln -sf ../init.d/diskless S01diskless)

###
#
#  Replace the fstab with a generic fstab that contains no local disks.
#
mv "$ROOT"/etc/fstab "$ROOT"/etc/fstab.prediskless
cp -p $INSTALLDIR/fstab "$ROOT"/etc


###
#
#  Create the .snapshot directory in the root partition.  This will be used 
#  by the client as the mount point of the snapshot directory from the server.
#
mkdir -p "$ROOT"/.snapshot
#
#  Create the .oldroot directory in the root partition.  This will be used 
#  by pivot-root to mount the memory resident root directory.  It is unmounted
#  by the diskless service script.
#
mkdir -p "$ROOT"/.oldroot

###
#
# This script will now remove all scripts that cause problems for the diskless
# environment
#
#echo cleaning up network scripts
#
# Since there is no diskless autofsck is not used
#
rm -f "$ROOT"/.autofsck
#
# Since there is disk lvm will not work
#
rm -f "$ROOT"/etc/lvmtab
#
# Since the network is brought up using the memory resident partition,
# no network scripts are needed or used
#
rm -f "$ROOT"/etc/rc.d/rc*.d/K*network
rm -f "$ROOT"/etc/rc.d/rc*.d/K*netfs
rm -f "$ROOT"/etc/sysconfig/network-scripts/ifcfg-eth*
#
# Setup all logging to go to this server
#
echo "*          @`hostname`" > "$ROOT"/etc/syslogd.conf

#
# Everytime software is installed RPM leaves the DB files around. The system
# startup scripts remove them on boot.  Since we are on a read/only file system
# attempting to remove these files creates an error on the screen.  Eliminating
# these files now eliminates this error.
#
rm -f "$ROOT"/var/lib/rpm/__db*

#
# Eliminate cron jobs that do not make sense on a Diskless environment.
# The server can run these jobs to update the man pages and locate database.
#
#echo removing makewhatis from cron
rm -f "$ROOT"/etc/cron.daily/makewhatis.cron
rm -f "$ROOT"/etc/cron.monthly/makewhatis.cron

#echo removing slocate from cron
rm -f "$ROOT"/etc/cron.daily/slocate.cron

#
# Create files that do not necessarily exist when the client is installed.
# If the client needs to change the XF86Config file it will need write access
# to this file.
#
touch "$ROOT"/etc/X11/XF86Config.backupn

#
# Eliminate the HOSTNAME entry from the /etc/sysconfig/network script.
# This will be set by the memory resident boot environment
#
grep -v "^HOSTNAME" "$ROOT"/etc/sysconfig/network > /tmp/network
cp "$ROOT"/etc/sysconfig/network "$ROOT"/etc/sysconfig/network.prediskless
cp /tmp/network "$ROOT"/etc/sysconfig/network

#
# /proc is client specific.  Creating a link from /etc/mtab -> /proc/mounts
# eliminates alot of potential problems
#

#echo Creating mtab link
mv "$ROOT"/etc/mtab "$ROOT"/etc/mtab.prediskless
ln -s /proc/mounts "$ROOT"/etc/mtab

#
# Remove all lock,run,spool,log files that may have been left over from the
# initial client installation
#
rm -f `find "$ROOT"/var/lock -type f`
rm -f `find "$ROOT"/var/run -type f`
rm -rf "$ROOT"/var/tmp/*
rm -rf "$ROOT"/tmp/*

for i in `find "$ROOT"/var/spool -type f`; do
    cat /dev/null >> $i
done
for i in `find "$ROOT"/var/log -type f`; do
    cat /dev/null >> $i
done
#
#  We need to make small changes to the following startup scripts.
#
#
#echo Applying Patches
#
# rc.sysinit Changes
#  Stop rc.sysinit from trying to remount the / file systems rw.
#     / needs to stay r/o
#  Don't create /.autofsck since this is a diskless client
#
patch -N -b -z .prediskless    "$ROOT"/etc/rc.d/rc.sysinit ${INSTALLDIR}/rc.sysinit.patch
#
#  Halt changes
#    Don't unmount /dev directory since it is mounted using NFS
#    This would cause the halt to hang
#
patch -N -b -z .prediskless    "$ROOT"/etc/init.d/halt ${INSTALLDIR}/halt.patch
#
#  netfs changes
#    Don't unmount /.snapshot directory since it is mounted using NFS
#    This would cause the halt to hang
#
patch -N -b -z .prediskless    "$ROOT"/etc/init.d/netfs ${INSTALLDIR}/netfs.patch
#
#  xfs changes:
#     xfs does a whole lot of evil stuff to the /usr directory in order
#      to maintain protections.  This patch eliminates these changes
#

patch -N -b -z .prediskless    "$ROOT"/etc/init.d/xfs ${INSTALLDIR}/xfs.patch
#echo all done

grep "$ROOT" /etc/exports 2>1 > /dev/null
if [ $? != 0 ]; then
    echo "\"$ROOT\" *(ro,no_root_squash,sync)" >> /etc/exports
    echo "\"$SNAPSHOT\" *(rw,no_root_squash,sync)" >> /etc/exports
    exportfs -r
fi
