#!/bin/bash

#############################
#
# error code:
# 1: parameter error
# 2: block device isn't exist
# 3: used by lv_dm_path
# 4: raid remove failed
#
#############################
# get parameter list
source /etc/profile
source /etc/tos/scripts/scripts

usage="Usage:`basename $0` [/dev/md0]"
if [ -z "$1" ]; then
    echo $usage
    exit 1
fi
script_get_lock > /dev/null
[ $? -ne 0 ] && exit 1

raid_path=$1
raid_name=`basename $raid_path`
DEVSORT=${raid_name:2} #设备序号
debugfile=/tmp/volume-$raid_name
process=0
rm -f $debugfile
# raid isn't exists
if [ ! -b "$raid_path" ]; then
    echo "stopRaid:$raid_path:2:failed" > $debugfile
    echo $usage
    exit 2
fi
TOS_VG_NAME=`pvs -o vg_name $raid_path --noheadings 2>/dev/null |sed s/[[:space:]]//g`
if [ ! -z "$TOS_VG_NAME" ]; then    
    TOS_LV_PATHS=`vgs -o lv_dm_path $TOS_VG_NAME --noheadings 2>/dev/null |sed s/[[:space:]]//g`
    if [ ! -z "$TOS_LV_PATHS" ]; then
        lv_used=0
        for lv_dm_path in $TOS_LV_PATHS; do
            df | grep "$lv_dm_path" > /dev/null
            if [ $? -eq 0 ]; then
                lv_used=1
                break
            fi
        done
        # used by lv_dm_path
        if [ $lv_used -eq 1 ]; then
            echo "stopRaid:$raid_path:3:failed" > $debugfile
            echo $usage
            exit 3
        fi
        vgchange -an $TOS_VG_NAME > /dev/null 2>&1
    fi
fi

process=10
echo "stopRaid:$raid_path:0:$process" > $debugfile

# remove single raid
removeRaid(){
	local blk=$1
	local name=$(basename $blk)
	local slaves=""
	[ ! -b "$blk" ] && return 0

	if [ -e /sys/block/$name/slaves/ ]; then
		for subdisk in `ls /sys/block/$name/slaves/`; do
			slaves="${slaves}/dev/${subdisk} "
		done
	fi
	sysctl -w dev.raid.speed_limit_max=0 #200000
	sysctl -w dev.raid.speed_limit_min=0 #100000
	#重复2次去尝试停掉阵列...
	[ -b "$blk" ] && mdadm --manage $blk --stop >/dev/null
	sleep 1
	[ -b "$blk" ] && mdadm --manage $blk --stop >/dev/null
	sleep 1
	#判断是否停止成功...
	if [ ! -e "$blk" ]; then
        process=60
        echo "stopRaid:$raid_path:0:$process" > $debugfile
		for item in $slaves; do
			[ -z "$item" ] && continue
			mdadm --zero-superblock $item >/dev/null
		done
	else
		echo "stopmd:null:$DEVNAME:failed" > $debugfile
		return 1
	fi
	echo `cat /etc/mdadm.conf | grep -v $blk` > /etc/mdadm.conf
	# wait sync
	sleep 1
	mdadm -Ds > /etc/mdadm.conf
    sysctl -w dev.raid.speed_limit_max=200000
    sysctl -w dev.raid.speed_limit_min=100000
    return 0
}

#取得挂载点
MNTPOINT=$(getmntpoint "$raid_path")

process=20
echo "stopRaid:$raid_path:0:$process" > $debugfile
# remove the flashcache
DEVFLAC=/dev/mapper/fc${DEVSORT}
[ -b $DEVFLAC ] && /etc/tos/scripts/flashcache remove $raid_path > /dev/null

process=30
echo "stopRaid:$raid_path:0:$process" > $debugfile

if [ ! -z "$MNTPOINT" ]; then
    check_main_raid "$MNTPOINT"
    if [ $? -eq 0 ]; then
        main_service_stop
    fi
    service_stop
    
    process=40
    echo "stopRaid:$raid_path:0:$process" > $debugfile
    umountAll "$MNTPOINT"
    
    process=50
    echo "stopRaid:$raid_path:0:$process" > $debugfile
    #删除raid
    removeRaid $raid_path >/dev/null 2>&1
    
    /etc/tos/scripts/mntvfs >/dev/null 2>&1
    public_dir=`readlink -f /mnt/public`
    [ ! -z "$public_dir" ] && {
        for svrname in $MUSTAPP; do
            svrpath=/etc/init.d/$svrname
            [ -e "$svrpath"] && $svrpath start >/dev/null 2>&1
        done
    }
else
    process=50
    echo "stopRaid:$raid_path:0:$process" > $debugfile
    removeRaid $raid_path >/dev/null 2>&1
fi

process=100
echo "stopRaid:$raid_path:0:$process" > $debugfile

script_put_lock > /dev/null
exit 0


