#!/bin/bash
# Workaround for not having keyboard & mouse if the system has too many LUNs
#  this is intended for use with RHEL type 6.x systems
#
# Includes some weak error checking
#
zip -r ~/no_qla-boot.bak.zip /boot /etc/grub.conf /etc/rc.local
if [ -f ~/no_qla-boot.bak.zip ]  # check to see if the zip file exists
then
	echo "Adding: 'blacklist qla2xxx' to /etc/modprobe.d/blacklist-qlogic.conf"
	echo "blacklist qla2xxx" > /etc/modprobe.d/blacklist-qlogic.conf
	if [ -f /boot/initramfs-$(uname -r)-no-qla.img ]
	then
		echo "RAMDisk '/boot/initramfs-$(uname -r)-no-qla.img' already exists"
	else
		echo "Building new initramfs image file... this will take some time"
		dracut /boot/initramfs-$(uname -r)-no-qla.img $(uname -r)
	fi
	if [ -f /etc/grub.conf ]  # Check to see if the file exists
	then
		if [[ -z `grep "initrd /boot/initramfs-$(uname -r)-no-qla.img" /etc/grub.conf` ]]
		then
			echo "Replacing the grub.conf line to call new RAMDisk"
			editedGrubArray=( "$(grep -vi initramfs-$(uname -r) /etc/grub.conf)" )
			echo "${editedGrubArray[@]}" > /etc/grub.conf
			echo "initrd /boot/initramfs-$(uname -r)-no-qla.img" >> /etc/grub.conf
		else
			echo "Seems like /etc/grub.conf already contains 'initrd /boot/initramfs-$(uname -r)-no-qla.img'"
		fi
	else
		echo "file /etc/grub.conf is missing?"
		echo "you may need to restore the file from ~/no_qla-boot.bak.zip"
	fi
	if [[ -z `grep "modprobe qla2xxx" /etc/rc.local` ]] && [[ -z `grep "service cvfs start" /etc/rc.local` ]] # be sure the lines don't already exist
	then
		echo "Adding the lines to launch the qla driver and start cvfs"
		for i in "modprobe qla2xxx" "service cvfs start"; do echo $i >> /etc/rc.local; done
	else
		echo "Looks like /etc/rc.local already contains either 'modprobe qla2xxx' or 'service cvfs start'"
	fi
	echo "Also, running chkconfig cvfs off so it won't try and start too soon"
	chkconfig cvfs off
else
	echo "the expected zip file ~/no_qla-boot.bak.zip is missing? nothing done"
fi
