#!/bin/bash
read -p "请根据本系统选择安装Centos源(7-8): [7] " input1
echo -e "\e[0m"

if [[ ! -n ${input1} || ${input1} != "8" ]];then
echo -e "\033[31m 现在开始安装Centos7 阿里源。。。 ！ \033[0m"
sleep 3s
rpm -ivh http://154.92.99.55/localhost/script/wget-1.14-18.el7_6.1.x86_64.rpm
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

else

echo -e "\033[31m 现在开始安装Centos8 阿里源。。。 ！ \033[0m"
sleep 3s
cd /etc/yum.repos.d/
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
yum install wget -y
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo

fi

read -p "选择连接到那个网盘关网10.100.66.14、10.100.66.12；请输入IP: " ip
ping -c5 $ip

read -p "确认存储IP网络是否正常？继续安装吗?(yes/no): [no] " input1 
echo -e "\e[0m"

if [[ ! -n ${input1} || ${input1} != "yes" ]];then
echo -e "\033[31m 请检查网卡2内网IP配置是否正确！ \033[0m"
        exit 0
fi

yum -y install iscsi-initiator-utils
sed -i 's/InitiatorName=.*/InitiatorName=iqn.2001-07.com.ceph:1670307748843/' /etc/iscsi/initiatorname.iscsi
sed -i 's/#node.session.auth.username =.*/node.session.auth.username = 4910Thyec48843/'  /etc/iscsi/iscsid.conf
sed -i 's/#node.session.auth.password =.*/node.session.auth.password = 48843abcdewhyec4/' /etc/iscsi/iscsid.conf
sed -i 's/#node.session.auth.authmethod = CHAP*/node.session.auth.authmethod = CHAP/' /etc/iscsi/iscsid.conf

systemctl  enable iscsid
systemctl restart iscsid
systemctl restart iscsi

iscsiadm -m discovery -t sendtargets -p $ip:3260  #发现存储
iscsiadm --mode node --portal $ip:3260 --login     #登陆存储
# iscsiadm -m node --logoutall=all 登出
sleep 3s
lsblk

echo -e "\033[31m  请问是否对硬盘/dev/sdb进行分区格式化！命令./parted-ext4.sh  /dev/sdb1!\033[0m"
echo -e "\033[33m "
read -p "    是否继续安装?(Passwd/no): [no] " input1
echo -e "\e[0m"

if [[ ! -n ${input1} || ${input1} != "wlsd@1998" ]];then
        exit 0
fi

wget http://154.92.99.55/localhost/iscsi/parted-ext4.sh && chmod +x parted-ext4.sh 
sleep 3s
./parted-ext4.sh  /dev/sdb
echo -e "\033[31m 设置开机挂入磁盘目录/wwwroot！ \033[0m"
mkdir /wwwroot
mount -o discard /dev/sdb1 /wwwroot
blkid | grep /dev/sdb
echo -e "\033[31m  echo UUID=   /wwwroot  ext4 defaults,_netdev,discard        0 0 >>  /etc/fstab \033[0m"
echo -e "\033[33m "
rm -fr parted-ext4.sh
rm -fr init_netdisk_ok.sh
