1、环境介绍
SCSI(Small Computer System Interface)是块数据传输协议,在存储行业广泛应用,是存储设备最基本的标准协议。从根本上说,iSCSI协议是一种利用IP网络来传输潜伏时间短的SCSI数据块的方法,ISCSI使用以太网协议传送SCSI命令、响应和数据。ISCSI可以用我们已经熟悉和每天都在使用的以太网来构建IP存储局域网。通过这种方法,ISCSI克服了直接连接存储的局限性,使我们可以跨不同服务器共享存储资源,并可以在不停机状态下扩充存储容量。
所需软件与软件结构
CentOS 将 tgt 的软件名称定义为 scsi-target-utils ,因此你得要使用 yum 去安装他才行。至于用来作为 initiator 的软件则是使用 linux-iscsi 的项目,该项目所提供的软件名称则为 iscsi-initiator-utils 。所以,总的来说,你需要的软件有:
scsi-target-utils:用来将 Linux 系统仿真成为 iSCSI target 的功能;
iscsi-initiator-utils:挂载来自 target 的磁盘到 Linux 本机上。
那么 scsi-target-utils 主要提供哪些档案呢?基本上有底下几个比较重要需要注意的:
/etc/tgt/targets.conf:主要配置文件,设定要分享的磁盘格式与哪几颗;
/usr/sbin/tgt-admin:在线查询、删除 target 等功能的设定工具;
/usr/sbin/tgt-setup-lun:建立 target 以及设定分享的磁盘与可使用的
客户端等工具软件。
/usr/sbin/tgtadm:手动直接管理的管理员工具 (可使用配置文件取代);
/usr/sbin/tgtd:主要提供 iSCSI target 服务的主程序;
/usr/sbin/tgtimg:建置预计分享的映像文件装置的工具 (以映像文件仿真磁盘);
这次的实验结构
(sdx1、sdy1是物理的磁盘通过lun连接到target虚拟的共享块,在客户端挂载这个块。这里我们添加一块硬盘sdb,创建sdb1(400m)和sdb2(500m))
2、server端配置
添加一块磁盘后
复查一下
[root@100 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 59.5G 0 part
├─rhel-swap 253:0 0 2G 0 lvm [SWAP]
├─rhel-root 253:1 0 38.6G 0 lvm /
└─rhel-home 253:2 0 18.9G 0 lvm /home
sdb 8:16 0 10G 0 disk
├─sdb1 8:17 0 400M 0 part
└─sdb2 8:18 0 500M 0 part
sr0 11:0 1 3.6G 0 rom /dvd
安装target并加入开机自启动
yum install -y target*
systemctl start target
systemctl enable target
配置target的ctl将sdb1和sdb2共享出去
(一)、block关联磁盘
[root@100 ~]# targetcli
targetcli shell version 2.1.fb37
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.
/> /backstores/block create block1 /dev/sdb
/dev/sdb /dev/sdb1 /dev/sdb2
...................................dev
/> /backstores/block create block1 /dev/sdb1
Created block storage object block1 using /dev/sdb1.
/> /backstores/block create block2 /dev/sdb2
Created block storage object block2 using /dev/sdb2.
/> ls
o- / ..................................................................... [...]
o- backstores .......................................................... [...]
| o- block .............................................. [Storage Objects: 2]
| | o- block1 .................. [/dev/sdb1 (400.0MiB) write-thru deactivated]#新建的block
| | o- block2 .................. [/dev/sdb2 (500.0MiB) write-thru deactivated]#新建的block
| o- fileio ............................................. [Storage Objects: 0]
| o- pscsi .............................................. [Storage Objects: 0]
| o- ramdisk ............................................ [Storage Objects: 0]
o- iscsi ........................................................ [Targets: 0]
o- loopback ..................................................... [Targets: 0]
/>
(二)、创建target
/> /iscsi create iqn.2018-10.20.com:disk
Created target iqn.2018-10.20.com:disk.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.
/> ls
o- / ..................................................................... [...]
o- backstores .......................................................... [...]
| o- block .............................................. [Storage Objects: 2]
| | o- block1 .................. [/dev/sdb1 (400.0MiB) write-thru deactivated]
| | o- block2 .................. [/dev/sdb2 (500.0MiB) write-thru deactivated]
| o- fileio ............................................. [Storage Objects: 0]
| o- pscsi .............................................. [Storage Objects: 0]
| o- ramdisk ............................................ [Storage Objects: 0]
o- iscsi ........................................................ [Targets: 1] #这里是创建tartet的目录
| o- iqn.2018-10.20.com:disk ....................................... [TPGs: 1]
| o- tpg1 ........................................... [no-gen-acls, no-auth]
| o- acls ...................................................... [ACLs: 0]
| o- luns ...................................................... [LUNs: 0]
| o- portals ................................................ [Portals: 1]
| o- 0.0.0.0:3260 ................................................. [OK]
o- loopback ..................................................... [Targets: 0]
(三)、创建lun关联block和target
/> ls
o- / ..................................................................... [...]
o- backstores .......................................................... [...]
| o- block .............................................. [Storage Objects: 2]
| | o- block1 .................. [/dev/sdb1 (400.0MiB) write-thru deactivated]
| | o- block2 .................. [/dev/sdb2 (500.0MiB) write-thru deactivated]
| o- fileio ............................................. [Storage Objects: 0]
| o- pscsi .............................................. [Storage Objects: 0]
| o- ramdisk ............................................ [Storage Objects: 0]
o- iscsi ........................................................ [Targets: 1]
| o- iqn.2018-10.20.com:disk ....................................... [TPGs: 1]
| o- tpg1 ........................................... [no-gen-acls, no-auth]
| o- acls ...................................................... [ACLs: 0]
| o- luns ...................................................... [LUNs: 0]
| o- portals ................................................ [Portals: 1]
| o- 0.0.0.0:3260 ................................................. [OK]
o- loopback ..................................................... [Targets: 0]
/> iscsi/iqn.2018-10.20.com:disk/tpg1/acls create iqn.2018-10.20.com:xx
Created Node ACL for iqn.2018-10.20.com:xx
/> ls
o- / ..................................................................... [...]
o- backstores .......................................................... [...]
| o- block .............................................. [Storage Objects: 2]
| | o- block1 .................. [/dev/sdb1 (400.0MiB) write-thru deactivated]
| | o- block2 .................. [/dev/sdb2 (500.0MiB) write-thru deactivated]
| o- fileio ............................................. [Storage Objects: 0]
| o- pscsi .............................................. [Storage Objects: 0]
| o- ramdisk ............................................ [Storage Objects: 0]
o- iscsi ........................................................ [Targets: 1]
| o- iqn.2018-10.20.com:disk ....................................... [TPGs: 1]
| o- tpg1 ........................................... [no-gen-acls, no-auth]
| o- acls ...................................................... [ACLs: 1]
| | o- iqn.2018-10.20.com:xx ............................ [Mapped LUNs: 0]
| o- luns ...................................................... [LUNs: 0]
| o- portals ................................................ [Portals: 1]
| o- 0.0.0.0:3260 ................................................. [OK]
o- loopback ..................................................... [Targets: 0]
/> iscsi/iqn.2018-10.20.com:disk/tpg1/luns create /
/backstores/block/block1 /lib/ /sbin/
/backstores/block/block2 /lib64/ /srv/
/bin/ /media/ /sys/
/boot/ /mnt/ /tmp/
/dev/ /opt/ /usr/
/dvd/ /proc/ /var/
/etc/ /root/
/home/ /run/
..................................................storage_object
/> iscsi/iqn.2018-10.20.com:disk/tpg1/luns create /backstores/block/block
/backstores/block/block1 /backstores/block/block2
..................................................storage_object
/> iscsi/iqn.2018-10.20.com:disk/tpg1/luns create /backstores/block/block1
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.2018-10.20.com:xx
/> iscsi/iqn.2018-10.20.com:disk/tpg1/luns create /backstores/block/block2
Created LUN 1.
Created LUN 1->1 mapping in node ACL iqn.2018-10.20.com:xx
/> ls
o- / ..................................................................... [...]
o- backstores .......................................................... [...]
| o- block .............................................. [Storage Objects: 2]
| | o- block1 .................... [/dev/sdb1 (400.0MiB) write-thru activated]
| | o- block2 .................... [/dev/sdb2 (500.0MiB) write-thru activated]
| o- fileio ............................................. [Storage Objects: 0]
| o- pscsi .............................................. [Storage Objects: 0]
| o- ramdisk ............................................ [Storage Objects: 0]
o- iscsi ........................................................ [Targets: 1]
| o- iqn.2018-10.20.com:disk ....................................... [TPGs: 1]
| o- tpg1 ........................................... [no-gen-acls, no-auth]
| o- acls ...................................................... [ACLs: 1] #添加了一条认证
| | o- iqn.2018-10.20.com:xx ............................ [Mapped LUNs: 2]
| | o- mapped_lun0 ............................ [lun0 block/block1 (rw)] #关联lun
| | o- mapped_lun1 ............................ [lun1 block/block2 (rw)] #关联lun
| o- luns ...................................................... [LUNs: 2]
| | o- lun0 ................................... [block/block1 (/dev/sdb1)]
| | o- lun1 ................................... [block/block2 (/dev/sdb2)]
| o- portals ................................................ [Portals: 1]
| o- 0.0.0.0:3260 ................................................. [OK]
o- loopback ..................................................... [Targets: 0]
/> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
至此服务器端就配置完了,详细的配置可以到/etc/target/saveconfig.json修改
3、client端配置
(一)、安装iscsi*
软件包 iscsi-initiator-utils-6.2.0.873-29.el7.x86_64
软件包 iscsi-initiator-utils-iscsiuio-6.2.0.873-29.el7.x86_64
[root@200 ~]# yum install -y iscsi*
(二)、加入开机自启
[root@200 ~]# systemctl restart iscsid.service
[root@200 ~]# systemctl enable iscsid.service
(三)、做一个发现的操作,发现服务器共享的target名字是什么
[root@200 ~]# iscsiadm -m discovery -t st -p 192.168.137.100
192.168.137.100:3260,1 iqn.2018-10.20.com:disk
m模式mode-discovery, discoverydb.
t类型type must be sendtargets (or abbreviated as st), slp, isns or fw. Currently only sendtargets, fw, and iSNS is supported, see the DISCOVERY TYPES section.
p是IP:port
(四)、查看发现的条目
[root@200 ~]# iscsiadm -m discovery -p 192.168.137.100
# BEGIN RECORD 6.2.0.873-28
discovery.startup = manual
discovery.type = sendtargets #-t st
discovery.sendtargets.address = 192.168.137.100 #target服务端
discovery.sendtargets.port = 3260 #端口
discovery.sendtargets.auth.authmethod = None #认证没有
discovery.sendtargets.auth.username = <empty>
discovery.sendtargets.auth.password = <empty>
discovery.sendtargets.auth.username_in = <empty>
discovery.sendtargets.auth.password_in = <empty>
discovery.sendtargets.timeo.login_timeout = 15 #登录超时时间
discovery.sendtargets.use_discoveryd = No #没有使用这个发现
discovery.sendtargets.discoveryd_poll_inval = 30
discovery.sendtargets.reopen_max = 5
discovery.sendtargets.timeo.auth_timeout = 45
discovery.sendtargets.timeo.active_timeout = 30
discovery.sendtargets.iscsi.MaxRecvDataSegmentLength = 32768
# END RECORD
(五)、修改客户端的acl并登录
[root@200 ~]# iscsiadm -m node -T iqn.2018-10.20.com:disk -p 192.168.137.100 -l
Logging in to [iface: default, target: iqn.2018-10.20.com:disk, portal: 192.168.137.100,3260] (multiple)
Login to [iface: default, target: iqn.2018-10.20.com:disk, portal: 192.168.137.100,3260] successful.
[root@200 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 59.5G 0 part
├─rhel-swap 253:0 0 2G 0 lvm [SWAP]
├─rhel-root 253:1 0 38.6G 0 lvm /
└─rhel-home 253:2 0 18.9G 0 lvm /home
sdb 8:16 0 400M 0 disk
sdc 8:32 0 500M 0 disk
sr0 11:0 1 3.6G 0 rom /dvd
[root@200 ~]# cat /etc/iscsi/initiatorname.iscsi
#InitiatorName=iqn.1994-05.com.redhat:949f18f9ce84
InitiatorName=iqn.2018-10.20.com:xx ##刚才设置的acl条目
注:这个自动加载过来的磁盘重启之后是自动加载过来的
4、客户端挂载iscsi的盘
格式化sdb和sdc
[root@200 ~]# mkfs.xfs /dev/sdb
meta-data=/dev/sdb isize=256 agcount=8, agsize=12800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=102400, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@200 ~]# mkfs.xfs /dev/sdc
meta-data=/dev/sdc isize=256 agcount=8, agsize=16000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=128000, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
创建挂载点并挂载
[root@200 ~]# mkdir /sdb
[root@200 ~]# mkdir /sdc
注:如果需要实时同步的话需要使用gfs集群文件系统
[root@200 ~]# cat /etc/fstab |tail -n 4
/dev/mapper/rhel-swap swap swap defaults 0 0
/dev/cdrom /dvd iso9660 defaults 0 0
/dev/sdb /sdb xfs _netdev 0 0 #添加的条目
/dev/sdc /sdc xfs _netdev 0 0 #添加的条目
[root@200 ~]# # mount -a
[root@200 ~]# mount |tail -n 4
gvfsd-fuse on /run/user/0/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=0,group_id=0)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/sdb on /sdb type xfs (rw,relatime,seclabel,attr2,inode64,noquota,_netdev)
/dev/sdc on /sdc type xfs (rw,relatime,seclabel,attr2,inode64,noquota,_netdev)
[root@200 ~]#
5、总结
(一)、增加iscsi存储
(1)发现iscsi存储:iscsiadm
-m discovery -t st -p ISCSI_IP
(2)查看iscsi发现记录:iscsiadm
-m node
(3)登录iscsi存储:iscsiadm
-m node -T LUN_NAME -p ISCSI_IP -l
(4)开机自动:
iscsiadm -m node –T LUN_NAME -p ISCSI_IP --op
update -n node.startup -v
automatic
(二)、删除iscsi存储
(1)登出iscsi存储
iscsiadm -m node -T LUN_NAME -p ISCSI_IP -u
(2)对出iscsi所有登录
iscsiadm -m node --logoutall=all
(3)删除iscsi发现记录:iscsiadm
-m node -o delete -T LUN_NAME -p ISCSI_IP