#!/bin/sh
#    mkiso.sh
# builds an ISO image incorporating the PDK tools in the Anaconda
# installer the ISO created will use a kickstart file as the default
# menu option the kickstart file does the following:
#   1. in the %pre install section the PDK scripts are invoked that 
#      load drivers to support PDK utilities
#   2. formats the first hard driver and installs Fedora 6
#   3. in the %post section the PDK SystemInfo.xml and SystemLog.xml 
#      files along with all the other PDK files are copied to the hard
#      disk.
#
# Caution: A CDROM burned with this ISO will boot and perform an
#          unattended install after the menu timeout formatting the
#          hard drive.
#
# assumptions:
#   RHEL5.2-Server-20080430.0-i386-DVD.iso and Intel_PDK-1.2.zip are in the current working
#   directory
#
# Customization:
#   1. this script can be customized for any Linux distribution
#      using Anaconda as the installer
#   2. the kickstart file is normally customized for a given
#      installation
#   3. the Python script 'bin/customDeployment' is intended to be 
#      customized to meet the deployment objectives by using the
#      various PDK tools to configure the Intel platform.
#      Note: the example 'bin/customDeployment' file is a Python script
#      but any scripting language can be used as long as the
#      interpreter is available.
#      'bin/customDeployment' can be a shell script#

PDK_VER=Intel_PDK-1.2
WORKING=`pwd`
ISO_MNT=$WORKING/iso
umount $ISO_MNT
mkdir $ISO_MNT
mount -o loop -t iso9660 RHEL5.2-Server-20080430.0-i386-DVD.iso $ISO_MNT
#Install some requires RPMs - others maybe needed to satisfy dependencies
KERNEL_V=2.6.18-92.el5
KERNEL_V3=$KERNEL_V.i386
ARCH=i686
KERNEL_VER=$KERNEL_V.$ARCH
KERNEL_VERSION=$KERNEL_V-$ARCH
rpm -i $ISO_MNT/Server/glibc-headers-2.5-24.i386.rpm
rpm -i $ISO_MNT/Server/glibc-devel-2.5-24.i386.rpm
rpm -i $ISO_MNT/Server/libgomp-4.1.2-42.el5.i386.rpm
rpm -i $ISO_MNT/Server/gcc-4.1.2-42.el5.i386.rpm
rpm -i $ISO_MNT/Server/kernel-headers-$KERNEL_V3.rpm
rpm -e kernel-devel-$KERNEL_V
rpm -i $ISO_MNT/Server/kernel-devel-$KERNEL_VER.rpm
rpm -i $ISO_MNT/Server/squashfs-tools-3.0-4.i386.rpm
rpm -i $ISO_MNT/Server/mkisofs-2.01-10.i386.rpm
# prepare the kernel source tree for the building of kernel drivers
rm -f /lib/modules/$KERNEL_V/build
ln -s ../../../usr/src/kernels/$KERNEL_VERSION /lib/modules/$KERNEL_V/build
cd /usr/src/kernels/$KERNEL_VERSION
make oldconfig && make prepare
ln -s /usr/src/kernels/$KERNEL_VERSION /usr/src/linux
touch /usr/src/linux/include/linux/config.h
# install the PDK in the working directory
cd $WORKING
rm -rf $PDK_VER
unzip $PDK_VER.zip
if [ $? -ne 0 ]
then
	echo "PDK unzip failed"
	exit 1
fi
# build the drivers needed for the PDK tools
# Note: the build of megaide and megasr will ONLY work for RedHat releases
# Ignore the build errors for the megaide and megasr, they are only
# needed when a software RAID is configured in the BIOS.
cd $PDK_VER/Linux/drivers
tar -xzf pdk-driver-src.tar.gz
#remove exit from smi/build.sh
sed -i -e "/exit 1/d" smi/build.sh
#handle RH point release number in megasr/build.sh
sed -i -e "s/ print \$7 / printf \"%1.0f\",\$7 /" megasr/build.sh
#add extern statement to megasr/build.sh
sed -i -e "s/BOOLEAN/extern VOID mega_check_drive_insertion_removal(HW_DEVICE_EXTENSION *);\nBOOLEAN/" megaide/megaide/megaide_ata.c
#define 'IMG' in megaide/build.sh
sed -i -e "s/cp -f  megaide.\$IMG/if [ \"\$KBASE\" = \"2.4\" ]; then IMG=o; else IMG=ko; fi;cp -f  megaide.\$IMG/" megaide/build.sh

sh build.sh
# remove the source files and build scripts that are no longer needed
rm -rf dmiedit imbdrv iqv megaide megasr ofu smi
# smidrv and imbdrv scripts are needed to install the driver
tar -xzf drivers-slax6.tar.gz ./smidrv
tar -xzf drivers-slax6.tar.gz ./imbdrv
sed -i -e "s/bash/sh/" imbdrv
rm -f *.sh *.txt *.tar.gz
#create a tar file of the newly built drivers
tar -czf drivers-el5u2.tar.gz smidrv imbdrv amifldrv_mod.o amifldrv_mod.o-dmiedit imb.ko iqvlinux.ko sg.ko smi.ko megaide.ko megasr.ko
if [ $? -ne 0 ]
then
	echo "PDK driver build failed"
	exit 1
fi
#
# add a Python dictionary entry to the OSdrivers dictionary in the
# driverMap.py file. The dictionary entry format is:
# 'unique_label':['uname-r_string','tar_filename_of_device_drivers']
# e.g.    'fc6':['2.6.18-1','drivers-fc6.tar.gz'],
sed -i -e "s/}/'el5u2':['2.6.18-92','drivers-el5u2.tar.gz'],\n    }/" ../../Python/driverMap.py
#
# create driver disk image for RAID driver
# A driver disk on the CDROM/DVD does not work in the current RH releases
#
rm -f $WORKING/drvblock.img
dd if=/dev/zero of=$WORKING/drvblock.img bs=1440K count=1
mkfs -t vfat $WORKING/drvblock.img
mkdir $WORKING/disk.img
mount -o loop -t vfat $WORKING/drvblock.img $WORKING/disk.img
mkdir $WORKING/megasr
mount -o loop -t vfat $WORKING/megasr-10.21.0507.2008-1-rhel50-u1-all.img $WORKING/megasr
cp -dr $WORKING/megasr/* $WORKING/disk.img
umount $WORKING/megasr
rmdir $WORKING/megasr
rm -f $WORKING/disk.img/modules.cgz
mkdir -p $WORKING/tmp/$KERNEL_V/$ARCH
cp -f megasr.ko megaide.ko $WORKING/tmp/$KERNEL_V/$ARCH
cd $WORKING/tmp
find $KERNEL_V -print | cpio -o -H crc | gzip -9 > $WORKING/disk.img/modules.cgz
cd $WORKING
rm -rf $WORKING/tmp
umount $WORKING/disk.img
rm -rf $WORKING/disk.img
#
# build stage2.img and minstg2.img with the PDK tools, scripts, and
# drivers included.
#
# The script 'bin/customDeployment' is intended to be customized
# to meet the deployment objectives by configuring the Intel platform.
# make script changes to this file and add additional scripts as needed
# Note: the example 'bin/customDeployment' file is a Python script
#       but any scripting language can be used as long as the
#       interpreter is available.
#       'bin/customDeployment' can be a shell script
#
#add files for the demo
cp -f $PDK_VER/Demos/Linux/Anaconda/customshell/customDeployment $PDK_VER/bin
# C++ lib needed for ofu/flashupdt
cp -f /usr/lib/libstdc++.so.5.0.7 $PDK_VER/Linux/libs/libstdc++.so.5
# ethtool not in BusyBox
cp -f /sbin/ethtool $PDK_VER/Linux
# remove unneeded Windows .cmd files
rm -f $PDK_VER/bin/*.cmd
# IMPORTANT: add execute permissions to scripts
chmod -R 755 $PDK_VER/bin $PDK_VER/Linux $PDK_VER/doc
#
# build new stage2 images with PDK scripts and binaries included
#
rm -rf stage2 minstg2
mkdir stage2 minstg2
cd stage2
cp $ISO_MNT/images/stage2.img stage2.orig.img
unsquashfs stage2.orig.img
mkdir -p squashfs-root/opt/deploy
cp -r $WORKING/$PDK_VER/bin squashfs-root/opt/deploy
cp -r $WORKING/$PDK_VER/Python squashfs-root/opt/deploy
cp -r $WORKING/$PDK_VER/Linux squashfs-root/opt/deploy
cp -r $WORKING/$PDK_VER/xml squashfs-root/opt/deploy
cp -r $WORKING/$PDK_VER/doc squashfs-root/opt/deploy
cp -r $WORKING/$PDK_VER/Demos/Linux/Anaconda/customshell squashfs-root/opt/deploy/doc
mksquashfs squashfs-root stage2.img
cd ../minstg2
cp $ISO_MNT/images/minstg2.img minstg2.orig.img
unsquashfs minstg2.orig.img
mkdir -p squashfs-root/opt/deploy
cp -r $WORKING/$PDK_VER/bin squashfs-root/opt/deploy
cp -r $WORKING/$PDK_VER/Python squashfs-root/opt/deploy
cp -r $WORKING/$PDK_VER/Linux squashfs-root/opt/deploy
cp -r $WORKING/$PDK_VER/xml squashfs-root/opt/deploy
cp -r $WORKING/$PDK_VER/doc squashfs-root/opt/deploy
cp -r $WORKING/$PDK_VER/Demos/Linux/Anaconda/customshell squashfs-root/opt/deploy/doc
mksquashfs squashfs-root minstg2.img
#
# build the new ISO by replacing the stage 2 images and preparing the
# isolinux directory
#
cd $WORKING
rm -rf pdkiso
mkdir pdkiso
cp -r $ISO_MNT/* pdkiso
cp $ISO_MNT/.* pdkiso
cp minstg2/minstg2.img pdkiso/images  
cp stage2/stage2.img pdkiso/images
#
# copy update zips to the PDK ISO image
#
cp -r $WORKING/updates pdkiso
#
# Anaconda bug alert: in EL5u2 driver disk files work from
# floppy and USB devices but NOT from a CDROM/DVD. Anaconda attempts
# to copy all files to /tmp/drivers before looking for driver disk files. 
#
cp $WORKING/drvblock.img pdkiso/images
mkdir disk.img
mount -o loop -t vfat $WORKING/drvblock.img $WORKING/disk.img
mkdir pdkiso/images/drivers
cp disk.img/* pdkiso/images/drivers
umount $WORKING/disk.img
rm -rf $WORKING/disk.img

cp $WORKING/$PDK_VER/Demos/Linux/Anaconda/customshell/ksCDROMel5u2.cfg pdkiso/isolinux/ks.cfg
#sed -i -e "s/cdrom/cdrom\ndriverdisk hda --type=iso9660/" pdkiso/isolinux/ks.cfg
#sed -i -e "s/sda/sdb/" pdkiso/isolinux/ks.cfg
#sed -i -e "s/cdrom/cdrom\ndriverdisk sda1 --type=vfat/" pdkiso/isolinux/ks.cfg
#
# wire-up the isolinux.cfg file with the kickstart
#
sed -i -e "s/default linux/default ks/" pdkiso/isolinux/isolinux.cfg
sed -i -e "s/append ks /append ks=cdrom:\/isolinux\/ks.cfg /" pdkiso/isolinux/isolinux.cfg
chmod a+w pdkiso/isolinux/isolinux.bin
mkisofs -r -T -J \
	-V "EL5u2 PDK DVD" \
	-b isolinux/isolinux.bin \
	-c isolinux/boot.catalog \
	-no-emul-boot -boot-load-size 4 -boot-info-table \
	-o PDK-EL5u2-install.iso \
	pdkiso
