#!/bin/bash

set -x

host=128.200.34.153
#host=esmfgw.nac.uci.edu
vg=test_vg3

function digit_to_letter
{
	echo "$1" | tr '[0-9]' '[a-j]'
}

case "$1" in
	stop)
		killall -USR1 -v enbd-client
		sleep 1
		killall -USR2 -v enbd-client
		rmmod enbd
		;;
	start)
		#rhel 3 doesn't need a full path.  FC2 does.  The module had to be
		#compiled for both
		insmod /lib/modules/`uname -r`/kernel/drivers/block/enbd/enbd.ko
		# nb is it. nd aint.
		#/usr/local/sbin/enbd-client "$host" 1100 /dev/nda &
		#/usr/local/sbin/enbd-client "$host" 1101 /dev/ndb &
		#/usr/local/sbin/enbd-client "$host" 1102 /dev/ndc &
		#for i in `seq 0 2`
		for i in `seq 0 2`
		do
			/usr/local/sbin/enbd-client \
				"$host":"$(expr 1100 + $i)" \
				-i X$(digit_to_letter $i) \
				-n 1 \
				-b 4096 \
				/dev/nd$(digit_to_letter $i) 
			sleep 2
			#/usr/local/sbin/enbd-client "$host" $(expr 1100 + $i) /dev/nbc &
		done
		;;
	restart)
		$0 stop
		$0 start
		;;
	init)
		# This likes to create new volume groups,
		# even if the old ones are dead.  Maybe someday I'll look into how
		# to remove one.  :)
		#
		# wipe the partition table, create the uuid, 
		if [ 1 = 1 ]
		then
			# just for testing. Normally we don't want to remove these very
			# often.  :)
			vgremove "$vg"
		fi
		for digit in `seq 0 2`
		do
			letter="$(digit_to_letter $digit)"
			df /dev/nd$letter
			dd if=/dev/zero of=/dev/nd$letter bs=1024 count=1
			pvcreate /dev/nd$letter
		done
		vgcreate "$vg" /dev/nda /dev/ndb /dev/ndc
		lvcreate -i 3 -I 8 -L 2.7T "$vg"
		ls -l /dev/$vg/lvol0
		lvdisplay /dev/$vg/lvol0
		;;
	mkfs)
		dev=/dev/"$vg"/lvol0
		case jfs in
			ext3)
				# takes forever.  With nbd, this crashed before it was done
				mkfs.ext3 "$dev"
				;;
			jfs)
				# errored out with nbd almost immediately.  How about enbd?
				#
				# jfs on 64 bit is supposed to be able to store 2^52 bytes,
				# which should more than enough for ESMF
				#
				# jfs stores block numbers in 40 bits.  As it
				# only support 4K blocks, the file size is limited to 2^52
				# bytes, which is pretty large.
				# 
				# jfs appears to be handling my 2.7T filesystem well.  It is
				# not (yet) suffering from the strange delays that reiserfs
				# had from time to time.
				#
				# Also jfs is about 2 megabits/second faster than reiserfs
				# over 100BaseT and my diminutive packetpasser script (~12
				# Mbps vs ~10 Mbps)
				mkfs.jfs "$dev"
				;;
			xfs)
				# this completed quickly with nbd, and gave the best results
				# of the filesystems I tried with nbd.
				#
				# this does Not complete that quickly with enbd, but at
				# least I can see plenty of network traffic going by in
				# tethereal, so I know it's progressing...  However,
				# although the mkfs.xfs completed without error, the mount
				# command errored out with:
				# ioctl(3, BLKGETSIZE, 0xbfffef70)    = -1 EFBIG (File to large)
				#
				# From the xfs mailing list, Apr 2004:
				# We've tested XFS on a petabyte, on Altix - thats a
				# modified version
				# of Redhat (most Redhat versions that have XFS are modified
				# versions)
				# and its a 64 bit machine - very important to get past the
				# small TB
				# numbers.  The theoretical maximum for a 64 bit box is some
				# number
				# of exabytes (can't remember off the top of my head), or
				# something
				# ridiculous like that.
				# 
				# But for 32 bit machines, I'd test extensively before going
				# past the
				# 1 or 2 TB range, as others have said.
				#
				#
				# From a slashdot thread on large filesystems:
				# xfs can do 26^3 = 9 x 10^18 = 9 exabytes
				mkfs.xfs "$dev"
				;;
			xfs+)
				# this too errors on:
				# ioctl(3, BLKGETSIZE, 0xbfffef70) = -1 EFBIG (File too large)
				# ...when trying to mount -t xfs
				mkfs.xfs -b size=65536 -s size=32768 "$dev"
				;;
			reiserfs)
				# Trying this with enbd...  It mkreiserfs'd very quickly.
				# Apparently this has a 16 Terrabyte limit on 2.6.x
				# (reiserfs 3.6)
				#
				# with reiserfs the mount takes a long time, and generates lots
				# of network traffic.  Actually, it appears to be working pretty
				# well.  However, the maximum filesystem size is only 16
				# terrabytes in reiserfs 3.6 (included with Linus' 2.6.7)
				# according to the reiserfs FAQ.
				# 
				# performance is choppy, actually.  Sometimes response time
				# is great, other times you end up waiting too long for
				# simple operations - like listing a directory.  JFS does
				# not appear to suffer from this problem.
				mkreiserfs "$dev"
				;;
		esac
		;;
	test)
		if [ 1 = 1 ]
		then
			if ! mount -t jfs /dev/$vg/lvol0 /mnt/big
			then
				echo Mount failed 1>&2
				exit 1
			fi
		fi
		df /mnt/big
		set +x
		for i in `seq 1 260`; \
		do
			echo
			echo $i; \
			(cd / && /bin/tar cflS - .) | \
				reblock -t $(expr 1024 \* 1024) 120 | \
				(cd /mnt/big && mkdir $i && cd $i && /bin/tar xfp -); \
		done
		;;
esac

