All pastes #1877561 Raw Edit

loomsen

public shellscript v1 · immutable
#1877561 ·published 2010-06-04 23:33 UTC
rendered paste body
#!/bin/bashset +x## global constants	PROGNAME=$(basename $0)	VERSION="1.0"## global variablesMY_DISKS="SATA_WDC_WD800AAJS-0_WD-WCAS25853716SATA_Hitachi_HDP7250_GEA534RF2A2W5A"## functions used in this scripterror_exit() {	echo "you're not root"	exit 1}## check permissions/executing user id## we need root for this script to work properly[[ ! $EUID -eq 0 ]] && error_exit  my_umount(){## for every disk in MY_DISKS, do following	for i in $MY_DISKS; do		## check if device is present		#if [ -L "$MY_PATH"*"$i" -a -b "$MY_PATH"*"$i" ]; then		if [ "$(grep $i <(echo /dev/disk/by-id/*) 2>/dev/null)" ]; then				echo "Disk $i is present."			## unmount all mounted partitions			for foo in /dev/disk/by-id/*${i}-* ; do 				## find out the sdXY name of the partition, #=> MY_HELPER0=sdXY				MY_HELPER0="$(readlink -f "$foo")"				[[ "$(grep $MY_HELPER0 /proc/mounts 2>/dev/null)" ]] && umount "$MY_HELPER0" || :			done			sync			echo "all partitions unmounted and synced, you may safely remove your device now"			echo "press any key to continue..."			read CONTINUE_KEY			# trigger a re-scan of the bus			echo "0 0 0" > /sys/class/scsi_host/host4/scan		fi	done}my_mount(){## for every disk in MY_DISKS, do following	for i in $MY_DISKS; do		## check if device is present		if [ ! "$(grep $i <(echo /dev/disk/by-id/*) 2>/dev/null)" ]; then			echo "Disk $i is not present, please plug it in and hit any key to continue"			read CONTINUE_KEY	        echo "SCSI Bus re-scan now."			echo "0 0 0" > /sys/class/scsi_host/host4/scan			sync			## mount all available partitions			for foo in /dev/disk/by-id/*${i}-part* ; do 				IFS=_ read -r _ MY_VENDOR _ <<< "$foo"				MY_PARTNR="${foo##*-}"				[[ ! -d /mnt/"$MY_VENDOR"-"$MY_PARTNR" ]] && mkdir -p /mnt/"$MY_VENDOR"-"$MY_PARTNR"				mount "$foo" /mnt/"$MY_VENDOR"-"$MY_PARTNR"			done		fi	done} usage() { echo -e "Usage: ${PROGNAME} [-m | -u]  |  [-h | --help | -? ] ";}helptext(){	local tab=$(echo -en "\t\t")	cat <<- -EOF-	${PROGNAME} ver. ${VERSION}	Use this script to manage hot-swappable hard-disks.	$(usage)	Options:	-m              	mount newly plugged disk	-u              	unmount mounted disk(s)	-h, --help		Display this help message and exit.-EOF-}## main program starts here ### process cli arguments...if [ "$1" = "--help" -o "$1" = "-h" -o "$1" = "-?" ]; then	helptext	exit 0elif [ "$1" = "" ]; then	usage	exit 0fiwhile getopts ":hum" opt; do	case $opt in		u | --umount | --remove | -r )			my_umount			exit $?			;;		m | --mount | --insert | -i )			echo "0 0 0" > /sys/class/scsi_host/host4/scan			my_mount			exit $?			;;		*)			helptext			exit 0			;;	esacdone