#! /bin/sh
# Copyright (c) 2000-2007 SUSE LINUX Products GmbH, Germany.
#
# Author: Marcus Schaefer <ms@suse.de>
#
# /etc/init.d/bsmd
#   and its symbolic  link
# /usr/sbin/rcbsmd
#
### BEGIN INIT INFO
# Provides:          bsmd
# Required-Start:    $time $syslog
# Required-Stop:
# Default-Start:     3 5
# Default-Stop:      0 1 2 4 6
# Description:       openSUSE build service host lvm setup (striping)
### END INIT INFO

. /etc/rc.status

#============================================
# getEmptyDisk
#--------------------------------------------
function getEmptyDisk () {
	for disk in `hwinfo --disk | grep "Device File:" |\
		cut -f2 -d: | cut -f2 -d" "`;do
		count=0;
		for i in `sfdisk -l $disk 2>/dev/null | tr -d '*' | grep ^/ |\
			sed -e s"@\s\+@:@g" | cut -f1,5,6 -d:`;do
			blocks=`echo $i | cut -f2 -d:`
			if [ $blocks = "0" ];then
				count=`expr $count + 1`
				continue
			fi
		done
		if [ $count -eq 4 ] || [ $count -eq 0 ];then
			echo $disk
		fi
	done
}

#============================================
# getRaidPartitions
#--------------------------------------------
function getRaidPartitions () {
	for i in `sfdisk -l 2>/dev/null | tr -d '*' | grep ^/ |\
		sed -e s"@\s\+@:@g" | cut -f1,5,6 -d:`;do
		device=`echo $i | cut -f1 -d:`
		blocks=`echo $i | cut -f2 -d:`
		partid=`echo $i | cut -f3 -d:`
		if [ $blocks = "0" ];then
			continue 
		fi
		if [ $partid = "8e" ];then
			echo $device
		fi
	done
}

#============================================
# getPartitions
#--------------------------------------------
function getPartitions () {
	for i in `sfdisk -l 2>/dev/null | tr -d '*' | grep ^/ |\
		sed -e s"@\s\+@:@g" | cut -f1,5,6 -d:`;do
		device=`echo $i | cut -f1 -d:`
		blocks=`echo $i | cut -f2 -d:`
		partid=`echo $i | cut -f3 -d:`
		if [ $blocks = "0" ];then
			continue
		fi
		if [ $partid = "0" ] || [ $partid = "8e" ];then
			fsystem=`getFileSystem $device`
			if [ $fsystem = "unknown" ];then
				echo $device
			fi
		fi
	done
}

#============================================
# getFileSystem
#--------------------------------------------
function getFileSystem () {
	local FSTYPE=unknown
	dd if=$1 of=/tmp/filesystem-$$ bs=128k count=1 >/dev/null 2>&1
	data=$(file /tmp/filesystem-$$) && rm -f /tmp/filesystem-$$
	case $data in
		*ext3*)     FSTYPE=ext3 ;;
		*ext2*)     FSTYPE=ext2 ;;
		*ReiserFS*) FSTYPE=reiserfs ;;
		*)
			FSTYPE=unknown
		;;
	esac
	echo $FSTYPE
}

rc_reset
case "$1" in
	start)
		echo -n "Starting buildservice raid (striping)"
		dmsetup remove_all
		#============================================
		# 1) incorporate empty disks
		#--------------------------------------------
		for disk in `getEmptyDisk`;do
			sfdisk $disk >/dev/null 2>&1 <<- EOF
				,,8e,-
			EOF
			if [ $? -ne 0 ];then
				rc_failed 3
				rc_status -v
				rc_exit
			fi
		done
		#============================================
		# 2) setup lvm raid with striping level
		#--------------------------------------------
		if [ $? -eq 0 ];then
			# no raid active create new one on md0...
			devices=0
			parties=""
			for i in `getRaidPartitions`;do
				devices=`expr $devices + 1`
				parties="$parties $i"
			done
			if [ $devices -gt 0 ];then

                                lvm vgremove abuild 2>/dev/null || true
                                lvm vgremove systemvg 2>/dev/null || true

				lvm pvcreate $parties && \
				lvm vgcreate abuild $parties

				if [ $? -ne 0 ];then
					rc_failed 3
					rc_status -v
					rc_exit
				fi
			else
				rc_status -s
				rc_exit
			fi
		else
			# raid system already running add new devices...
			#rc_status -s
			#rc_exit
			:
		fi
		#============================================
		# 3) create and mount filesystem on array
		#--------------------------------------------
		fsystem=`getFileSystem /dev/md0`
		if [ $fsystem = "unknown" ];then
			mkfs.ext3 /dev/md0 >/dev/null 2>&1
		else
			true
		fi
		if [ $? -ne 0 ];then
			rc_failed 3
		else
			mkdir -p /abuild && mount /dev/md0 /abuild &>/dev/null
			true
		fi
		rc_status -v
	;;
	stop)
		echo -n "Stopping buildservice raid (striping)"
		lvm vgremove abuild || rc_failed 3
		rc_status -v
	;;
	restart)
		$0 stop
		$0 start
		rc_status
	;;
	force-reload)
		$0 stop  &&  $0 start
		rc_status
	;;
	try-restart)
		$0 status >/dev/null &&  $0 restart
		rc_status
	;;
	reload)
		echo -n "Reload buildservice raid (striping)"
		rc_failed 3
		rc_status -v
	;;
	status)
		rc_status -v
	;;
	*)
		usage="{start|stop|status|try-restart|restart|force-reload|reload}"
		echo "Usage: $0 $usage"
		exit 1
	;;
esac
rc_exit
