#!/bin/bash
#  Copyright 2010 QLogic Corporation. All rights reserved.
#
#  Permission is granted to QLogic customers to use this software in 
#  source and binary forms, with or without modification, provided that 
#  the following conditions are met: 
#  
#  + All copies of source code must retain the above copyright notice, 
#    this list of conditions and the following disclaimer. 
#  
#  + Customer has a valid license agreement from QLogic Corporation. for the 
#    software product with which this software is originally distributed. 
#  
#  + Redistribution of original or modified versions to third parties is 
#    prohibited without specific prior written permission of QLogic
#    Corporation. 
#  
#  + If customer makes modifications to this software, and provides those 
#    modifications to QLogic, customer assigns to QLogic Corporation. 
#    ownership of the provided modifications and all intellectual property 
#    rights embodied in those modifications. 
#  
#  + The name of QLogic Corporation. may not be used to endorse or promote 
#    products derived from this software without specific prior written 
#    permission. 
#  
#  THIS SOFTWARE IS PROVIDED BY QLOGIC CORPORATION. AND ITS LICENSORS "AS IS" 
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
#  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
#  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL QLOGIC CORPORATION. OR ITS 
#  LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
#  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

# This assumes you will use the currently running version of linux, and
# that you are starting from a fully configured machine of the same type
# (hardware configuration), and BIOS settings.
#
# start with a known path, to get the system commands
PATH=/sbin:/usr/sbin:/bin:/usr/bin:$PATH

# start from a copy of the current initd image

mkdir -p /var/tmp/initrd-ib
cd /var/tmp/initrd-ib
init="initrd"
kern=$(uname -r)
if [ -e /boot/initrd-${kern}.img ]; then
	initrd=/boot/initrd-${kern}.img 
elif [ -e /boot/initramfs-${kern}.img ]; then
 	initrd=/boot/initramfs-${kern}.img 
	init="initramfs"
elif [ -e /boot/initrd ]; then
	initrd=/boot/initrd
else
	echo Unable to find initrd or initramfs, fix script and re-run
fi

cp ${initrd} ${init}-ib-${kern}.img

# Get full original listing
gunzip -dc ${init}-ib-${kern}.img | cpio -it --quiet | grep -v '^\.$' | sort -o Orig-listing

# start building modified image
rm -rf new # for retries
mkdir new
cd new

# extract previous contents
gunzip -dc ../${init}-ib-${kern}.img | cpio --quiet -id

# add infiniband modules
mkdir -p lib/ib
find /lib/modules/${kern}/updates -type f | \
 egrep '(iw_cm|ib_(mad|addr|core|sa|cm|uverbs|ucm|umad|ipoib|qib).ko|rdma_|ipoib_helper)' | \
 xargs -I '{}' cp -a  '{}' lib/ib

# Some distros have ipoib_helper, others don't require it
if [ -e lib/ib/ipoib_helper.ko ]; then
	helper_cmd='/sbin/insmod /lib/ib/ipoib_helper.ko'
fi

# On some kernels, the qib driver will require the dca module
if modinfo -F depends ib_qib | grep -q dca; then 
  cp $(find /lib/modules/$(uname -r) -name dca.ko) lib/ib
  dcacmd='/sbin/insmod /lib/ib/dca.ko'
else
  dcacmd=
fi

# IB requires loading an IPv6 module. If you do not have it in your initrd, add it
if grep -q ipv6 ../Orig-listing; then
	# already added, and presumably insmod'ed, along with any dependencies
	v6cmd=
else
	echo -e 'Adding IPv6 and related modules\n'
	cp /lib/modules/${kern}/kernel/net/ipv6/ipv6.ko lib
	IFS=' ' v6cmd='echo "Loading IPV6"
/sbin/insmod /lib/ipv6.ko'
	# Some versions of IPv6 have dependencies, add them.
	xfrm=$(modinfo -F depends ipv6)
	if [ ${xfrm} ]; then
	    cp $(find /lib/modules/$(uname -r) -name ${xfrm}.ko) lib
	    IFS=' ' v6cmd='/sbin/insmod /lib/'${xfrm}'.ko
'"$v6cmd"
	    crypto=$(modinfo -F depends $xfrm)
	    if [ ${crypto} ]; then
	      cp $(find /lib/modules/$(uname -r) -name ${crypto}.ko) lib
	    IFS=' ' v6cmd='/sbin/insmod /lib/'${crypto}'.ko
'"$v6cmd"
	    fi
	fi
fi

# we need insmod to load the modules; if not present it, copy it
mkdir -p sbin
grep -q insmod ../Orig-listing || cp /sbin/insmod sbin

echo -e 'NOTE: you will need to config ib0 in the normal way in your booted root
filesystem, in order to use it for NFS, etc.\n'

# Now build the commands to load the additional modules.  We add them just after
# the last existing insmod command, so all other dependences will be resolved
# You can change the location if desired or necessary.
# loading order is important.  You can verify the order works ahead of time
# by running "/etc/init.d/openibd stop", and then running these commands
# manually by cut and paste
# This will work on SLES, although different than the standard mechanism

cat > ../init-cmds << EOF
# Start of IB module block
$v6cmd
echo "loading IB modules"
/sbin/insmod /lib/ib/ib_addr.ko
/sbin/insmod /lib/ib/ib_core.ko
/sbin/insmod /lib/ib/ib_mad.ko
/sbin/insmod /lib/ib/ib_sa.ko
/sbin/insmod /lib/ib/ib_cm.ko
/sbin/insmod /lib/ib/ib_uverbs.ko
/sbin/insmod /lib/ib/ib_ucm.ko
/sbin/insmod /lib/ib/ib_umad.ko
/sbin/insmod /lib/ib/iw_cm.ko
/sbin/insmod /lib/ib/rdma_cm.ko
/sbin/insmod /lib/ib/rdma_ucm.ko
$dcacmd
/sbin/insmod /lib/ib/ib_qib.ko
$helper_cmd
/sbin/insmod /lib/ib/ib_ipoib.ko
echo "finished loading IB modules"
# End of IB module block
EOF

# first get line number where we append (after last insmod if any, otherwse
# at start
line=$(egrep -n insmod init | sed -n '$s/:.*//p')
if [ ! "${line}" ]; then line=1; fi
sed -e "${line}r ../init-cmds" init > init.new


# show the difference, then rename
echo -e 'Differences between original and new init command script\n'
diff init init.new
mv init.new init
chmod 700 init

# now rebuilt the initrd image
find . | cpio --quiet -H newc -o | gzip > ../${init}-${kern}.img 

cd ..

# get the file list in the new image
gunzip -dc ${init}-${kern}.img | cpio --quiet -it | grep -v '^\.$' | sort -o New-listing

# and show the differences.
echo -e "\nChanges in files in $init image\n"
diff Orig-listing New-listing

# copy the new initrd to wherever you have configure the dhcp server to look
# for it (here we assume it's /images)
mkdir -p /images
cp  ${init}-${kern}.img /images

echo -e "\nCompleted ${init} for IB"
ls -l /images/${init}-${kern}.img
