#!/bin/sh 
#
# updatediskless is a script to configure the diskless boot image.
#   It takes three parameters:
#     1. The name of the directory where the "root" directory of the diskless 
#        environment resides.
#     2. The kernel version of the kernel to be used on the diskless clients.
#     3. The destination directory for the image file and kernel.
#
#   This script can be run multiple times and should be run everytime you 
#   update the kernel 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
#
#
TOP_DIR=`pwd`
if [ $# != 3 ]; then
	echo Usage: `basename $0` disklessDir kernelVersion destinationDir 
	exit 1 
fi
ROOT=$1
VERSION=$2
DESTDIR=$3
IMAGEFILE=$DESTDIR/initrd.img
MNTPOINT=/mnt/$$
KERNEL=$ROOT/boot/vmlinuz-$VERSION 
MODULES=$ROOT/lib/modules/$VERSION
INSTALLDIR=/usr/share/redhat-config-netboot/diskless
if [ -e "$KERNEL" ]; then
    if [ ! -e "$MODULES"  ]; then
	echo could not find modules in $MODULES
	exit -1
    fi
else
    echo Could not find kernel $KERNEL
    exit -1
fi
#
#  Create a new image file
#

imagesize=10000
uncompressedimage=/var/tmp/image$$
dd if=/dev/zero of=$uncompressedimage bs=1k count=$imagesize 2> /dev/null || exit 1
LODEV=$(echo findlodev ext2.o | /sbin/nash --quiet)

if [ -z "$LODEV" ]; then
    rm -rf $MNTPOINT $uncompressedimage
    echo "All of your loopback devices are in use." >&2
    exit 1
fi
/sbin/losetup ${LODEV} $uncompressedimage || exit 1

# We have to "echo y |" so that it doesn't complain about $IMAGE not
# being a block device
echo y | /sbin/mke2fs $LODEV >/dev/null 2>/dev/null
/sbin/tune2fs -i0 $LODEV >/dev/null

mkdir -p $MNTPOINT
mount -t ext2 $LODEV $MNTPOINT

#
#  Copy the diskless init script onto the image 
#
cp $INSTALLDIR/disklessrc $MNTPOINT/disklessrc
#
#  create required directories on the image 
#
mkdir -p $MNTPOINT/proc/
mkdir -p $MNTPOINT/sbin/
mkdir -p $MNTPOINT/mnt
mkdir -p $MNTPOINT/lib
mkdir -p $MNTPOINT/tmp
mkdir -p $MNTPOINT/dev
mkdir -p $MNTPOINT/var/lib/nfs
mkdir -p $MNTPOINT/var/lib/dhcp
mkdir -p $MNTPOINT/usr/share/hwdata/
mkdir -p $MNTPOINT/etc/rc.d/init.d/
mkdir -p $MNTPOINT/etc/sysconfig/network-scripts
ln -s sbin $MNTPOINT/bin

# We don't need this directory, so let's save space
rm -rf $MNTPOINT/lost+found
# mknod'ing the devices instead of copying them works both with and
# without devfs...
mknod $MNTPOINT/dev/console c 5 1
mknod $MNTPOINT/dev/null c 1 3
mknod $MNTPOINT/dev/ram b 1 1
mknod $MNTPOINT/dev/systty c 4 0
for i in 1 2 3 4; do
    mknod $MNTPOINT/dev/tty$i c 4 $i
done

#
#  copy required files on the image 
#
cp "$ROOT"/etc/rc.d/init.d/functions $MNTPOINT/etc/rc.d/init.d/
cp "$ROOT"/etc/sysconfig/network-scripts/network-functions $MNTPOINT/etc/sysconfig/network-scripts/
for ((i=0;i<10;i=i+1)); do
cat << __EOF > $MNTPOINT/etc/sysconfig/network-scripts/ifcfg-eth$i
DEVICE=eth$i
BOOTPROTO=dhcp
ONBOOT=yes
TYPE=Ethernet
__EOF
done
cp "$ROOT"/usr/share/hwdata/pcitable $MNTPOINT/usr/share/hwdata/
cp "$ROOT"/sbin/insmod $MNTPOINT/sbin/
cp "$ROOT"/sbin/dhclient $MNTPOINT/sbin/
cp "$ROOT"/sbin/dhclient-script $MNTPOINT/sbin/
cp "$ROOT"/bin/bash $MNTPOINT/sbin
cp "$ROOT"/bin/mount $MNTPOINT/sbin
cp "$ROOT"/sbin/route $MNTPOINT/sbin
cp "$ROOT"/usr/bin/expr $MNTPOINT/sbin
cp "$ROOT"/sbin/lspci $MNTPOINT/sbin
cp "$ROOT"/sbin/ifconfig $MNTPOINT/sbin
cp "$ROOT"/sbin/consoletype $MNTPOINT/sbin
cp "$ROOT"/sbin/pivot_root $MNTPOINT/sbin
#
#  Copy busybox.anaconda onto the image and
#  create all links for commands used during client boot.
#
cp "$ROOT"/sbin/busybox.anaconda $MNTPOINT/sbin/busybox
cd $MNTPOINT/sbin/
ln -sf insmod modprobe
ln -sf insmod lsmod
ln -sf insmod rmmod
ln -sf busybox awk
ln -sf busybox basename
ln -sf busybox cat
ln -sf busybox cut
ln -sf busybox df
ln -sf busybox grep
ln -sf busybox ln
ln -sf busybox ls
ln -sf busybox more
ln -sf busybox rm
ln -sf busybox sed
ln -sf busybox sleep
ln -sf busybox umount
ln -sf busybox uname
ln -sf bash ash
cd ../etc
touch fstab
ln -sf /proc/mounts mtab
cd ../lib
CVERS=2.3.2
cp "$ROOT"/lib/ld-${CVERS}.so .
ln -sf ld-${CVERS}.so ld-linux.so.2
cp "$ROOT"/lib/libc-${CVERS}.so .
ln -sf libc-${CVERS}.so libc.so.6
cp "$ROOT"/lib/libdl-${CVERS}.so .
ln -sf libdl-${CVERS}.so libdl.so.2
cp "$ROOT"/lib/libm-${CVERS}.so .
ln -sf libm-${CVERS}.so libm.so.6
cp "$ROOT"/lib/libtermcap.so.2 .
cp "$ROOT"/usr/lib/libz.so.1.1.4 .
ln -sf libz.so.1.1.4 libz.so
ln -sf libz.so.1.1.4 libz.so.1
cd $TOP_DIR

NEWMODDIR=$MNTPOINT/lib/modules/$VERSION
mkdir -p $NEWMODDIR
cp "$MODULES"/modules.* $NEWMODDIR
mkdir -p $NEWMODDIR/kernel/drivers/
cp -r "$MODULES"/kernel/drivers/net $NEWMODDIR/kernel/drivers
cp -r "$MODULES"/kernel/drivers/net $NEWMODDIR/kernel/drivers
mkdir -p $NEWMODDIR/kernel/fs/
cp -r "$MODULES"/kernel/fs/nfs $NEWMODDIR/kernel/fs/
cp -r "$MODULES"/kernel/fs/lockd $NEWMODDIR/kernel/fs/
mkdir -p $NEWMODDIR/kernel/net/
cp -r "$MODULES"/kernel/net/sunrpc $NEWMODDIR/kernel/net/
# 
#  Unmount and compress image file to be ready to boot
#
umount $MNTPOINT
/sbin/losetup -d $LODEV
rmdir  $MNTPOINT

mkdir -p "$DESTDIR"
if [ -e "$IMAGEFILE" ]; then
    mv "$IMAGEFILE" "$IMAGEFILE".backup
fi

gzip -c -9 $uncompressedimage > "$IMAGEFILE"
rm -f $uncompressedimage 
cp -a "$KERNEL" "$DESTDIR"/vmlinuz
