#!/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) [vg0]"
if [ -z "$1" ]; then
    echo $usage
    exit 1
fi
script_get_lock >/dev/null
[ $? -ne 0 ] && exit 1
DMSETUP=$(which dmsetup)
TOS_POOL_ROOT=/tmp/storage
[ ! -e $TOS_POOL_ROOT ] && mkdir $TOS_POOL_ROOT

processExit() {
  script_put_lock
  exit $1
}

vg_name=$1
VGSORT=${vg_name:2}
VGUUID=$(vgs -o vg_tags $vg_name --noheadings 2>/dev/null | sed s/[[:space:]]//g)
if [ -z "$VGUUID" ]; then
    echo $usage
    processExit 1
fi
debugfile=$TOS_POOL_ROOT/vg${VGSORT}
process=0
echo "stopVolumeGroup:$vg_name:$VGUUID:0:$process" >$debugfile
sleep 2

# volumeGroup isn't exists
lv_paths=$(vgs -o lv_dm_path $vg_name --noheadings 2>/dev/null | sed s/[[:space:]]//g)
if [ $? -ne 0 ]; then
    echo "stopVolumeGroup:$vg_name:$VGUUID:2:failed" >$debugfile
    echo $usage
    processExit 2
fi

get_lvpath_status() {
    local lv_path=$1
    local CACHEBLK=$(ter_flashcache -blk $lv_path -opt cache_target)
    local result=0
    if [ ! -z "$CACHEBLK" -a -b "$CACHEBLK" ]; then
        df-json | grep "$CACHEBLK" >/dev/null
        if [ $? -eq 0 ]; then
            result=1
        else
            DestroyCache "$(basename $CACHEBLK)"
        fi
    else
        df-json | grep "$lv_path" >/dev/null
        [ $? -eq 0 ] && result=1
    fi
    return $result
}

lv_used=0
if [ ! -z "$lv_paths" ]; then
    for lv_path in $lv_paths; do
        [ -z "$lv_path" ] && continue
        get_lvpath_status $lv_path
        if [ $? -eq 1 ]; then
            lv_used=1
            break
        fi
    done
fi
# used by lv_dm_path
if [ $lv_used -eq 1 ]; then
    echo "stopVolumeGroup:$vg_name:$VGUUID:3:failed" >$debugfile
    echo $usage
    processExit 3
fi

# 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 3
    [ -b "$blk" ] && mdadm -S $blk >/dev/null 2>&1
    sleep 5
    #判断是否停止成功...
    if [ ! -e "/sys/block/$name" ]; then
        for item in $slaves; do
            [ -z "$item" ] && continue
            mdadm --zero-superblock $item >/dev/null
        done
        rm -f $blk
    else
        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
}
removePhysical() {
    local raid=$1
    pvremove -ff $raid >/dev/null 2>&1
    sleep 1
    removeRaid $raid
    [ $? -eq 1 ] && {
        echo "stopVolumeGroup:$vg_name:$VGUUID:4:failed" >$debugfile
        processExit 4
    }
    # 80 raid removed
    process=80
    echo "stopVolumeGroup:$vg_name:$VGUUID:0:$process" >$debugfile
}

process=10
echo "stopVolumeGroup:$vg_name:$VGUUID:0:$process" >$debugfile
sleep 1

pv_names=$(vgs -o pv_name $vg_name --noheadings 2>/dev/null | sed s/[[:space:]]//g)
vgremove -f -y $vg_name >/dev/null 2>&1
# 50 vg removed
process=50
echo "stopVolumeGroup:$vg_name:$VGUUID:0:$process" >$debugfile

for pv_name in $pv_names; do
    isDelay=$(mdadm -D $pv_name | grep "DELAYED")
    if [ -n "$isDelay" ]; then
        removePhysical $pv_name
        sleep 1
        unset pv_names[$pv_name]
    fi
done

for pv_name in $pv_names; do
    lsblk $pv_name >/dev/null
    [ $? -ne 0 ] && continue
    removePhysical $pv_name
done

#remove volume group config
iniparse -d -f /etc/volume/storage.conf -s "$VGUUID"

# 100 finished
process=100
echo "stopVolumeGroup:$vg_name:$VGUUID:0:$process" >$debugfile

script_put_lock >/dev/null
# all process over
