#!/bin/sh

# Copyright C 2002-2003 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
#
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
#
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems.
#
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

# Description: Hot adds a drive to a node. Drive does not need to be
# preformated.

# New and improved preformat script. This one doesn't have the crazy command
# line args, prompting or excessive error checking of the old preformat.py.

PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/bin/isi_hwtools"
PATH="${PATH}:/usr/local/bin"

usage() {
        echo "Usage: $0 <drive>"
	echo "  Remove <drive> from imdd array. <drive> can be specified as:"
	echo "  /dev/twedN, twedN, portN, pN, unitN, uN, lnumN, lN, bayN, bN, N[bay]"
	exit
}

preformat() {
	TWED=$( isi_drivenum -t ${BAY} | tr -d '[:space:]' )

	# Clear old disklabels ...
	dd if=/dev/zero of=${TWED} bs=1m count=1

	# ... then repair the disklabel and prepare imdd slices ...
	/etc/rc.d/isi_imdd_start

	# ... then try to preformat the IFS slice.
	echo yes | newfs_efs -p ${TWED}s1e
	if [ $? -ne 0 ] ; then
		echo "ERROR: Couldn't preformat drive."
		exit 1
	fi
	echo "Drive is now preformated."
}

if [ "$#" -ne 1 -o "$1" = "-h" -o "$1" = "-H" ]; then
	usage
fi

# Get the bay and verify that "$1" is both a valid specifier for a drive and is
# the drive the user intended to preformat.
BAY=b`isi_drivenum -b -- $1`
if [ "$?" -ne 0 ]; then
        echo "No such drive \"$1\"."
	echo ""
	usage
fi
# trim off final spaces from BAY (for cleanliness, I assume)
BAY=`echo $BAY`

echo ""
echo "You have specified the following drive to be added to the system."
echo "If this is not your intention, press ^C now, otherwise, press ENTER."
echo "WARNING: if there is no drive in bay ${BAY}, you should quit now."
isi_drivenum ${BAY}
read a b

# Check to see if the drive has already been added to the driver. This will be
# the case if the system has been rebooted since we added this drive. It
# doesn't hurt to try to add the drive multiple times, but this way we can
# avoid printing scary messages.
#
# Using both isi_tweconfig and isi_drivenum let's us handle all of the
# following cases:
# ad0:           isi_tweconfig != 0, isi_drivenum == 0
# active:        isi_tweconfig == 0, isi_drivenum == 0
# empty:         isi_tweconfig == 0, isi_drivenum != 0
# unconfigured:  isi_tweconfig == 0, isi_drivenum != 0
if isi_tweconfig -q ${BAY} ; then
	if isi_drivenum -t ${BAY} ; then
		echo "Drive already added to twed driver. OK."
	else
		echo "Adding drive to twed driver."
		if isi_tweconfig -a ${BAY} && isi_drivenum -t ${BAY} ; then
			echo "Drive added successfully."
		else
			echo "ERROR: Failed to add drive in bay ${BAY}."
			exit 1
		fi
	fi
else
	echo "ERROR: Failed to query drive in bay ${BAY}."
	exit 1
fi

# Check the current state of this drive. We don't want to preformat a drive
# which is currently active. If the drive state is anything other than
# 'n/a' then we've seen it before and we need take measures to deal with that.
STATE=$( isi_drivenum -L ${BAY} | tr A-Z a-z | tr -d '[:space:]' )
GUID=$(  isi_drivenum -g ${BAY} | tr A-Z a-z | tr -d '[:space:]' )

if [ ${GUID} = "n/a" ] ; then
	echo "Drive is not preformated."
	preformat
else
	echo "This drive already has an IFS filesystem on it."
	echo "Checking to see if it is a known drive..."

	if [ ${STATE} = "n/a" ] ; then
		echo "Drive is OK. Continuing."
	elif [ ${STATE} = "active" ] ; then
		echo ""
		echo "ERROR: This drive is currently in use by the system."
		exit 1
	elif [ ${STATE} = "dead" -o ${STATE} = "gone" ] ; then
		echo ""
		echo "WARNING:"
		echo "This drive has a known GUID and has likely been used in this system before."
		if [ ${STATE} = "dead" ] ; then
			echo ""
			echo "This drive may currently still be referenced by some files on"
			echo "this cluster. You may want to allow the restriper to complete"
			echo "the FlexProtect phase before re-adding this drive."
		fi
		echo "To continue adding drive, press ENTER; otherwise, press ^C now."
		echo ""
		read a b
		preformat
	else
		echo ""
		echo "ERROR: Unknown drive state \"${STATE}\" (GUID: \"${GUID}\""
		exit 1
	fi
fi

# Add the drive to imdd. If we had to preformat the drive earlier, then this
# script will already have been run. Shouldn't hurt anything to run it again.
/etc/rc.d/isi_imdd_start

echo ""
echo "You may either reboot or remount /ifs to incorporate drive."
