#!/bin/bash

if [ -z "$1" ];then
    exit
fi

#stop server
systemctl stop minio smb nfs-server tgtd minio-colony glusterd

#if device number
modedata=($(/opt/tool/saturn-get-mode.sh))
num=${modedata[0]}
disks=${modedata[@]:1:${num}}
level=${1}
dn=""
mds=$(cat /proc/mdstat | egrep -o "md[0-9]{1,3}")


if [ -z "$mds" ];then    
    if [ $num -eq 4 ] || [ $num -eq 8 ];then
       for d in ${disks[*]};do
           umount /dev/${d}1
           parted /dev/$d set 1 raid on
           dn="${dn}/dev/${d}1 "
       done
       echo 'yes' |mdadm -C -v /dev/md5 -a yes -l${level} -n${num} $dn

       sleep 1

       read_ok=$(cat /proc/mdstat | egrep "md5 : active ")
   
       if [ -n "$read_ok" ];then
           mdadm -Ds > /etc/mdadm.conf
           mkfs.ext4 -F /dev/md5
	   sleep 1
           /opt/tool/saturn-mount.py --raid
           if [ -n "$(df -Th | grep /dev/md5)" ];then
               echo "success"
           else
               echo "Error:/dev/md5 mount failed"
           fi	   
       else
           echo "Error:/dev/md5 is not activated"
       fi

    else
       echo "Error:disk_not_found"
    fi
else
    echo "Error:Raid ${mds} already exists"
fi
