iscsi脚本

服务端

#!/bin/bash
#测试主机是否可以联网
echo -e "\033[31m即将测试主机是否可以联网\033[0m"
ping -c 4 www.baidu.com
if [ $? -eq 0 ];then
		echo -e "\033[31m主机可以联网继续脚本\033[0m"
else
		echo -e "\033[31m主机不可联网,即将退出脚本\033[0m"
        exit
fi
#编写服务端与客户端IP地址
echo -e "\033[31m即将编写服务端与客户端IP地址\033[0m"
sleep 3s
rm -rf ./ip
cat >> ./ip <<EOF
#在后面写IP地址,ip1为服务端,ip2为客户端
export ip1=
export ip2=
EOF
vi ./ip
source ./ip

echo -e "\033[31m即将关闭firewalld与selinux\033[0m"
sleep 1s
# firewalld config
systemctl disable firewalld
systemctl stop firewalld

#关闭selinux
setenforce 0

echo -e "\033[31m即将配置yum仓库文件\033[0m"
sleep 1s
#创建本地仓库文件
mkdir /mnt/cdrom
mount /dev/sr0 /mnt/cdrom

cat >> /etc/yum.repos.d/a.repo <<EOF
[a]
name=a
baseurl=file:///mnt/cdrom
gpgcheck=0
EOF


yum install -y wget
#下载epel7仓库文件
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

yum repolist

sleep 3s

echo -e "\033[31m\n即将安装targetcli\033[0m"
sleep 1s
yum -y install targetcli

echo -e "\033[31m\n即将配置targetcli\033[0m"
sleep 1s
#创建共享文件夹存档点
mkdir /iscsi_disks
chmod 777 /iscsi_disks

>/etc/target/saveconfig.json
cat >> /etc/target/saveconfig.json <<EOF
{
  "fabric_modules": [], 
  "storage_objects": [
    {
      "attributes": {
        "block_size": 512, 
        "emulate_dpo": 0, 
        "emulate_fua_read": 0, 
        "emulate_fua_write": 1, 
        "emulate_model_alias": 1, 
        "emulate_rest_reord": 0, 
        "emulate_tas": 1, 
        "emulate_tpu": 0, 
        "emulate_tpws": 0, 
        "emulate_ua_intlck_ctrl": 0, 
        "emulate_write_cache": 1, 
        "enforce_pr_isids": 1, 
        "fabric_max_sectors": 8192, 
        "is_nonrot": 0, 
        "max_unmap_block_desc_count": 1, 
        "max_unmap_lba_count": 8192, 
        "max_write_same_len": 4096, 
        "optimal_sectors": 8192, 
        "queue_depth": 128, 
        "unmap_granularity": 1, 
        "unmap_granularity_alignment": 0
      }, 
      "dev": "/iscsi_disks/disk01.img", 
      "name": "disk01", 
      "plugin": "fileio", 
      "size": 2147483648, 
      "write_back": true, 
      "wwn": "3267f7a8-b56b-448e-a0dd-b9f65906e96e"
    }
  ], 
  "targets": [
    {
      "fabric": "iscsi", 
      "tpgs": [
        {
          "attributes": {
            "authentication": 0, 
            "cache_dynamic_acls": 0, 
            "default_cmdsn_depth": 16, 
            "demo_mode_write_protect": 1, 
            "generate_node_acls": 0, 
            "login_timeout": 15, 
            "netif_timeout": 2, 
            "prod_mode_write_protect": 0
          }, 
          "enable": true, 
          "luns": [
            {
              "index": 0, 
              "storage_object": "/backstores/fileio/disk01"
            }
          ], 
          "node_acls": [
            {
              "attributes": {
                "dataout_timeout": 3, 
                "dataout_timeout_retries": 5, 
                "default_erl": 0, 
                "nopin_response_timeout": 30, 
                "nopin_timeout": 15, 
                "random_datain_pdu_offsets": 0, 
                "random_datain_seq_offsets": 0, 
                "random_r2t_offsets": 0
              }, 
              "chap_password": "password", 
              "chap_userid": "username", 
              "mapped_luns": [
                {
                  "index": 0, 
                  "tpg_lun": 0, 
                  "write_protect": false
                }
              ], 
              "node_wwn": "iqn.2016-10.vdevops.org:vdevops.org"
            }
          ], 
          "parameters": {
            "AuthMethod": "CHAP,None", 
            "DataDigest": "CRC32C,None", 
            "DataPDUInOrder": "Yes", 
            "DataSequenceInOrder": "Yes", 
            "DefaultTime2Retain": "20", 
            "DefaultTime2Wait": "2", 
            "ErrorRecoveryLevel": "0", 
            "FirstBurstLength": "65536", 
            "HeaderDigest": "CRC32C,None", 
            "IFMarkInt": "2048~65535", 
            "IFMarker": "No", 
            "ImmediateData": "Yes", 
            "InitialR2T": "Yes", 
            "MaxBurstLength": "262144", 
            "MaxConnections": "1", 
            "MaxOutstandingR2T": "1", 
            "MaxRecvDataSegmentLength": "8192", 
            "MaxXmitDataSegmentLength": "262144", 
            "OFMarkInt": "2048~65535", 
            "OFMarker": "No", 
            "TargetAlias": "LIO Target"
          }, 
          "portals": [
            {
              "ip_address": "0.0.0.0", 
              "iser": false, 
              "port": 3260
            }
          ], 
          "tag": 1
        }
      ], 
      "wwn": "iqn.2016-10.vdevops.org:storage.target00"
    }
  ]
}

EOF

echo -e "\033[31m\n查看3260端口是否打开\033[0m"
sleep 1s
ss -napt | grep 3260
sleep 3s

systemctl enable target

echo -e "\033[31m即将使用EPEL源安装scsi-target-utils并配置\033[0m"
sleep 3s
# 使用EPEL源安装scsi-target-utils
yum --enablerepo=epel -y install scsi-target-utils 
# 创建硬盘镜像
dd if=/dev/zero of=/iscsi_disks/disk01.img count=0 bs=1 seek=2G

cat >> /etc/tgt/targets.conf  <<EOF
<target iqn.2016-10.vdevops.org:target00>
backing-store /iscsi_disks/disk01.img
initiator-address $ip2
incominguser username password
</target>
EOF

#开启并自启服务
systemctl start tgtd
systemctl status tgtd |grep Active:
systemctl enable tgtd
sleep 3s

#查看状态
echo -e "\033[31m查看服务状态\033[0m"
sleep 3s
tgtadm --mode target --op show

客服端

#!/bin/bash
lsblk >lsblk1
#测试主机是否可以联网
echo -e "\033[31m即将测试主机是否可以联网\033[0m"
if [ $? -eq 0 ];then
		echo -e "\033[31m主机可以联网继续脚本\033[0m"
else
		echo -e "\033[31m主机不可联网,即将退出脚本\033[0m"
        exit
fi
#编写服务端与客户端IP地址
echo -e "\033[31m\n即将编写服务端与客户端IP地址\033[0m"
sleep 3s
rm -rf ./ip
cat >> ./ip <<EOF
#在后面写IP地址,ip1为服务端,ip2为客户端
export ip1=
export ip2=
EOF
vi ./ip
source ./ip

echo -e "\033[31m\n即将关闭firewalld与selinux\033[0m"
sleep 1s
# firewalld config
systemctl disable firewalld
systemctl stop firewalld
#关闭selinux
setenforce 0

echo -e "\033[31m即将配置yum仓库文件\033[0m"
sleep 1s
#创建本地仓库文件
mkdir /mnt/cdrom
mount /dev/sr0 /mnt/cdrom

cat >> /etc/yum.repos.d/a.repo <<EOF
[a]
name=a
baseurl=file:///mnt/cdrom
gpgcheck=0
EOF


yum install -y wget vim
#下载epel7仓库文件
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

yum repolist

sleep 3s

echo -e "\033[31m即将使用EPEL安装iscsi-initiator-utils并配置\033[0m"
sleep 1s
#使用EPEL安装iscsi-initiator-utils
yum -y install iscsi-initiator-utils

echo "InitiatorName=iqn.2016-10.vdevops.org:vdevops.org" >/etc/iscsi/initiatorname.iscsi

echo "node.session.auth.authmethod = CHAP" >>/etc/iscsi/iscsid.conf
echo "node.session.auth.username = username" >>/etc/iscsi/iscsid.conf
echo "node.session.auth.password = password" >>/etc/iscsi/iscsid.conf

# 发现target
iscsiadm -m discovery -t sendtargets -p $ip1
sleep 5s
iscsiadm -m node -o show
#登录target
iscsiadm -m node --login
#确认连接状态
iscsiadm -m session -o show
#确认分区状况
echo -e "\033[35m等待5s对比看是否多出一块disk磁盘\033[0m"
sleep 5s
lsblk >lsblk2
vim -O lsblk1 lsblk2


posted @ 2019-06-28 17:25  kklinux  阅读(479)  评论(0编辑  收藏  举报