#!/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

processExit() {
  script_put_lock
  exit $1
}

raid_path=$1
raid_name=$(basename $raid_path)
TOS_RAID_UUID=$(GetDeviceUUID $raid_path)
#设备序号
DEVSORT=$(GetVolumeSort $raid_path)
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
    processExit 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-json | 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:$TOS_RAID_UUID:3:failed" >$debugfile
            echo $usage
            processExit 3
        fi
        vgchange -an $TOS_VG_NAME >/dev/null 2>&1
    fi
fi

process=10
echo "stopRaid:$raid_path:$TOS_RAID_UUID: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 -S $blk >/dev/null 2>&1
    sleep 1
    [ -b "$blk" ] && mdadm -S $blk >/dev/null 2>&1
    sleep 1
    #判断是否停止成功...
    if [ ! -e "/sys/block/$name" ]; then
        #remove volume group config
        iniparse -d -f /etc/volume/storage.conf -s "$TOS_RAID_UUID"
        process=60
        echo "stopRaid:$raid_path:$TOS_RAID_UUID:0:$process" >$debugfile
        for item in $slaves; do
            [ -z "$item" ] && continue
            mdadm --zero-superblock $item >/dev/null
        done
        rm -f $blk
    else
        echo "stopmd:$raid_path:$TOS_RAID_UUID:1: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:$TOS_RAID_UUID: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:$TOS_RAID_UUID: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:$TOS_RAID_UUID:0:$process" >$debugfile
    umountAll "$MNTPOINT"

    fs=$(blkid $raid_path -s TYPE -o value)
    if [ "$fs" = "crypto_LUKS" ]; then
        cryptblk=/dev/mapper/$raid_name
        [ -b $cryptblk ] && cryptsetup close $cryptblk
    fi

    process=50
    echo "stopRaid:$raid_path:$TOS_RAID_UUID: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
	    ter_service $svrname start >/dev/null 2>&1
        done
        for svrname in $(ls /etc/sc.d/); do
            [ "$svrname" = "nginx" ] && continue
            in_Array "$svrname"
            [ $? -eq 0 ] && continue

            if [ "$svrname" = "msg" ]; then
                [ -e "/root/.msgcenter.label"] && rm -r /root/.msgcenter.label
            fi
            ter_service $svrname start >/dev/null 2>&1
        done
    }
else
    process=50
    echo "stopRaid:$raid_path:$TOS_RAID_UUID:0:$process" >$debugfile
    removeRaid $raid_path >/dev/null 2>&1
fi

process=100
echo "stopRaid:$raid_path:$TOS_RAID_UUID:0:$process" >$debugfile
iniparse -d -f $TOS_VOLUE_CONFIG -s "$TOS_RAID_UUID"
iniparse -d -f $TOS_STORAGE_CONFIG -s "$TOS_RAID_UUID"

processExit 0
