#!/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: Removes a drive from the runtime [in-memory] imdd
# sets.  This is essentially a quick wrapper to isi_imdd_config to
# figure out what are the right sets and then remove from them.
#
# This script is useful for stopping all accesses to a disk so that
# the disk can be safely removed [or debugged].
#
# Note in contrast isi_imdd_clear, which clears out the on-disk imdd
# information so that the disk won't 'infect' a new node if moved
# between nodes.

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
}

if [ "$#" -ge 1 -a "$1" = "-f" ]; then
	#Force the argument to be used directly as a TWED entry
	FORCETWED="YES"
	shift
fi

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

if [ "$FORCETWED" = "YES" ]; then
	TWED="$1"
else
	TWED=`isi_drivenum -t -- $1`
fi

if [ "$?" -ne 0 ]; then
	echo "No such drive \"$1\"."
	echo ""
	usage
fi

# Remove the imdd partitions from any active imdd groups
for IMDD in `isi_imdd_config -g | grep ${TWED} | cut -f 1` ; do
	isi_imdd_config -x $IMDD `isi_imdd_config -i $IMDD | tail -1 | tr -s "\t " "\n" | grep ${TWED}`
done
