#!/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 remove a drive from a node.

PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/bin/isi_hwtools"
# while testing
PATH="$PATH:/usr/local/bin"

usage() {
        echo "Usage: $0 [ -f ] <drive>"
	echo "  Remove <drive> from imdd array. <drive> can be specified as:"
	echo "  /dev/daN, daN, portN, pN, unitN, uN, lnumN, lN, bayN, bayXY, N[bay], XY[bay]"
	echo ""
	echo " -f : Force drive to be kicked out even if /ifs is still mounted."
	exit
}

if [ "$1" = "-f" ] ; then
	FORCE="YES"
	shift
else
	FORCE="NO"
fi

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

DRIVE=b`isi_drivenum -b -- $1`
if [ $? -ne 0 ] ; then
	echo "Invalid drive \"$1\""
	echo ""
	usage
fi

isi_imdd_remove ${DRIVE}

mount -t efs | grep -q '^/'
if [ $? -ne 0 ] ; then
	IFSMOUNTED="NO"
else
	IFSMOUNTED="YES"
fi

if [ "$FORCE" = "YES" ] ; then
	echo "Forcing drive deactivation. This will cause the restriper to be run."
	echo "You do not have to wait for it to run to completion."
	isi_drive_deactivate ${DRIVE}
elif [ "$IFSMOUNTED" = "YES" ] ; then
	echo "The following IFS filesystems /ifs are currently mounted."
	echo "Either run with -f or unmount these filesytems manually."
	mount -t efs
	exit 1
else # FORCE = NO and IFSMOUNTED = NO
	echo "No IFS filesystems are mounted and you did not force deactivation."
	echo "If you remove the drive now, it will not be kicked out of IFS"
	echo "until the next time you mount /ifs."
fi

isi_tweconfig -r ${DRIVE}
isi_tweconfig -a ${DRIVE}
drive_blink ${DRIVE}

# This line may not be reached. Not fully tested.
isi_tweconfig -r ${DRIVE}
