#!/bin/bash
# this shell script create soft raid for ccdb
source /OSM/script/os_load_base.sh
source /OSM/script/ccdb_get_ld_device.sh

if [ $# -ne 9 ]; then
    OS_echo "usage:"$0" device_name dir_name product_model equipment_model engine_id linked_num node_id isMdCanPrint isReadSysDiskFirst"
    exit 1
fi

device_name=$1
dir_name=$2
product_model=$3
equipment_model=$4
engine_id=$5
linked_num=$6
node_id=$7
isMdCanPrint=$8
isReadSysDiskFirst=$9
let raid_devices=4
RAID_ERROR_LOG=/OSM/data/Infrastructure/ccdb/tmp/ccdb_raid_assemble_$$
HEALTHY_DEVICES_LIST_NAME="/OSM/data/Infrastructure/ccdb/tmp/healthy_devices.list"
CCDB_RAID_RUN_FILE="/OSM/data/Infrastructure/ccdb/tmp/ccdb_raid_run_file"
SLOW_DEVICES_LIST="/OSM/data/Infrastructure/ccdb/tmp/slow_devices_info.list"

#18800 raid devices is 2
if [ $linked_num -eq 4 ]; then
    let raid_devices=2
fi

#mdadm print limit 
if [ $isMdCanPrint -eq 1 ]; then
    PRINT_LIMIT="/dev/kmsg"
    verbose="--verbose"
else
    PRINT_LIMIT="/dev/null"
    verbose=""
fi

READY_DEVICE=$(/OSM/script/ccdb_get_disk_device.sh $product_model $equipment_model $engine_id $linked_num $node_id)
if [ "X$READY_DEVICE" == "X" ]; then
    OS_echo ""$0" device is not ready now."
    exit 1
fi

if [ ! -f "$HEALTHY_DEVICES_LIST_NAME" ]; then
    OS_echo "${HEALTHY_DEVICES_LIST_NAME} is not exit"
    for device in ${READY_DEVICE[@]}
    do
        ASSEMBLE_DEVICE_LIST="${ASSEMBLE_DEVICE_LIST} ${device}a"
    done
else
    while read LINE
    do
        ASSEMBLE_DEVICE_LIST="${ASSEMBLE_DEVICE_LIST} ${LINE}"
    done < $HEALTHY_DEVICES_LIST_NAME
fi

if [ -f "$SLOW_DEVICES_LIST" ]; then
    for device in ${ASSEMBLE_DEVICE_LIST[@]}
    do
        disk_id=`echo $device | grep -o '[0-9]\+'`
        disk_info=(`awk '/^'${disk_id}' / {print $1, $2}' $SLOW_DEVICES_LIST`)
        if [[ -n "${disk_info}" ]] && [[ ${disk_info[1]} -ge 3 ]]; then
            OS_echo ""$0" ccdb check the disk is slow, ${device}."
        else
            ASSEMBLE_DEVICE_TEMP_LIST="${ASSEMBLE_DEVICE_TEMP_LIST} ${device}"
        fi
    done
    ASSEMBLE_DEVICE_LIST=$ASSEMBLE_DEVICE_TEMP_LIST
fi

OS_echo ""$0" CCDB begin to create mdadm."

# 3. check whether the md device has been created
if [ "X$READY_DEVICE" == "X/dev/ld12a" ]; then
    device_name=$READY_DEVICE
    # check whether the ext3 filesystem has been created on md device
    result=$(file -s $device_name | grep "ext3 filesystem")
    if [ "X$result" == "X" ]; then
        mkfs.ext3 $device_name 1>/dev/null 2>/dev/kmsg
        if [ $? -ne 0 ]; then
            OS_echo ""$0" the ext3 filesystem create failed."
            exit 1
        fi
    fi
else
    mdadm --detail $device_name 1>/dev/null 2>/dev/kmsg
    if [ $? -ne 0 ]; then
        CREATE_ARRAY=($READY_DEVICE)
        COFFER_DISK_DEVICE=$(CCDB_get_ready_coffer_disk $product_model $engine_id $linked_num $node_id)
        NVRAM_DEVICE_ARRAY=$(CCDB_get_ready_nvRAM_disk $product_model $engine_id $linked_num $node_id)
        cofferuuid=""
        for device in ${COFFER_DISK_DEVICE[@]}
        do
            mdadm -E ${device%?}? 1>/dev/null 2>/dev/kmsg
            if [ $? -ne 0 ]; then
                continue
            else
                cofferuuid=$(mdadm -E ${device%?}?| grep "Array UUID" | awk -F" : " '{ print $2}')
                break
            fi
        done
        if [ "X$cofferuuid" != "X" ]; then
            for device in ${NVRAM_DEVICE_ARRAY[@]}
            do
                for ADD_DEVICE in ${ASSEMBLE_DEVICE_LIST[@]}
                do
                    if [ "${ADD_DEVICE%?}" == "${device%?}" ]; then
                        nvramuuid=$(mdadm -E ${device%?}?| grep "Array UUID" | awk -F" : " '{ print $2}')
                        if [[ ${nvramuuid} != ${cofferuuid} ]]; then
                            OS_echo ""$0" zero superblock ${device}"
                            mdadm --misc --zero-superblock ${device%?}? 
                            rm -fr /OSM/coffer_data/ccdb/bitmap.bin 1>/dev/null 2>&1
                        fi
                    fi
                done
            done
        fi

        OS_echo ""$0" CCDB begin to assemble mdadm dev $device_name ${ASSEMBLE_DEVICE_LIST} $isMdCanPrint $PRINT_LIMIT $verbose."
        mdadm --assemble $verbose --name=CCDB_RAID_DEVICE --metadata=1.2 --update=homehost --force --run $device_name ${ASSEMBLE_DEVICE_LIST} 1>${PRINT_LIMIT} 2>${RAID_ERROR_LOG}
        if [ $? -ne 0 ]; then
            # check whether the first create raid array or not.
            raid_assemble_error=$(cat ${RAID_ERROR_LOG} | grep -E "Not enough devices to start the array|doesn't match others|failed to RUN_ARRAY" 2>/dev/kmsg)
            rm -fr ${RAID_ERROR_LOG} 2>/dev/null
            if [ -n "${raid_assemble_error}" ]; then
                mdadm --stop $device_name
                OS_echo ""$0" mdadm assemble soft raid device failed, ${raid_assemble_error}."
                #query disk raid info
                if [ $isMdCanPrint -eq 1 ]; then
                    for device in ${ASSEMBLE_DEVICE_LIST[@]}
                    do
                        mdadm -E $device 1>/dev/kmsg 2>/dev/kmsg
                    done
                fi
                exit 1
            fi
            
            # raid is exited,not need to create
            if [ -f "$CCDB_RAID_RUN_FILE" ]; then
                mdadm --stop $device_name
                OS_echo ""$0" mdadm assemble soft raid device failed,but raid is exited,not need to create."
                exit 1
            fi
            # first create raid, create raid md200 for ccdb
            MISSING_DEVICE=""
            length=${#CREATE_ARRAY[@]}
            for((i=length;i<$raid_devices;i++));do MISSING_DEVICE="${MISSING_DEVICE} missing";done
            OS_echo ""$0" CCDB begin to create mdadm dev $device_name $READY_DEVICE $MISSING_DEVICE $isMdCanPrint $PRINT_LIMIT $verbose."
            if [ $isReadSysDiskFirst -eq 1 ]; then
                mdadm --create $verbose $device_name --name=CCDB_RAID_DEVICE --metadata=1.2 --bitmap=internal --bitmap-chunk=256K --delay=60 --assume-clean --force --run --level=1 --raid-devices=$raid_devices $NVRAM_DEVICE_ARRAY --write-mostly $COFFER_DISK_DEVICE $MISSING_DEVICE 1>${PRINT_LIMIT} 2>${PRINT_LIMIT}
            else
                mdadm --create $verbose $device_name --name=CCDB_RAID_DEVICE --metadata=1.2 --bitmap=internal --bitmap-chunk=256K --delay=60 --assume-clean --force --run --level=1 --raid-devices=$raid_devices $COFFER_DISK_DEVICE  --write-mostly $NVRAM_DEVICE_ARRAY $MISSING_DEVICE 1>${PRINT_LIMIT} 2>${PRINT_LIMIT}
            fi
            if [ $? -ne 0 ]; then
                mdadm --stop $device_name
                OS_echo ""$0" mdadm create soft raid device failed."
                exit 1
            fi
	    
            if [ ! -f "$CCDB_RAID_RUN_FILE" ]; then
                touch $CCDB_RAID_RUN_FILE
            fi
	    
            OS_echo ""$0" CCDB begin to make file system."
            mkfs.ext3 $device_name 1>/dev/null 2>/dev/kmsg
            if [ $? -ne 0 ]; then
                OS_echo ""$0" the ext3 filesystem create failed."
                exit 1
            fi
            OS_echo ""$0" CCDB end to mk file system."
        else
            # maybe some raid device which is out of date could not be accepted by Raid Array, 
            # we try add these device into Raid Array again
	    
            if [ ! -f "$CCDB_RAID_RUN_FILE" ]; then
                touch $CCDB_RAID_RUN_FILE
            fi
	    
            rm -fr ${RAID_ERROR_LOG} 2>/dev/null
            for device in ${NVRAM_DEVICE_ARRAY[@]}
            do
                for ADD_DEVICE in ${ASSEMBLE_DEVICE_LIST[@]}
                do
                    if [ "${ADD_DEVICE%?}" == "${device%?}" ]; then
                        #OS_echo ""$0" CCDB begin to manager $device."
                        if [ $isReadSysDiskFirst -eq 1 ]; then
                            mdadm --manage $device_name --add $ADD_DEVICE 1>/dev/null 2>${PRINT_LIMIT}
                        else
                            mdadm --manage $device_name --add --write-mostly $ADD_DEVICE 1>/dev/null 2>${PRINT_LIMIT}						
                        fi
                        #OS_echo ""$0" CCDB end to manager $device."
                        continue
                    fi
                done
            done

            if [ $equipment_model -eq 0 ]; then
                for device in ${COFFER_DISK_DEVICE[@]}
                do
                    for ADD_DEVICE in ${ASSEMBLE_DEVICE_LIST[@]}
                    do
                        if [ "${ADD_DEVICE%?}" == "${device%?}" ]; then
                            #OS_echo ""$0" CCDB begin to manager $device."
                            if [ $isReadSysDiskFirst -eq 1 ]; then
                                mdadm --manage $device_name --add --write-mostly $ADD_DEVICE 1>/dev/null 2>${PRINT_LIMIT}
                            else
                                mdadm --manage $device_name --add  $ADD_DEVICE 1>/dev/null 2>${PRINT_LIMIT}						
                            fi
                            #OS_echo ""$0" CCDB end to manager $device."
                            continue
                        fi
                    done
                done
            fi
            OS_echo ""$0" CCDB raid start to check raid md200 array."
        fi
    fi 
fi

OS_echo ""$0" CCDB end to handle madam."

exit 0

