iSCSI( Internet Small Computer System Interface 互联网小型计算机系统接口)是由IBM 下属的两大研发机构一一加利福尼亚AImaden和以色列Haifa研究中心共同开发的,是一个供硬件设备使用的、可在IP协议上层运行的SCSI指令集,是一种开放的基于IP协议的工业技术标准。该协议可以用TCP/IP对SCSI指令进行封装,使得这些指令能够通过基于P网络进行传输,从而实现SCSI 和TCP/IP协议的连接。对于局域网环境中的用户来说,采用该标准只需要不多的投资就可以方便、快捷地对信息和数据进行交互式传输及管理。 [root@server ~]# fdisk -l Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors

[root@server ~]# fdisk /dev/vdb #新建分区 Command (m for help): n Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +5G Command (m for help): wq [root@server ~]# partprobe [root@server ~]# yum install targetcli [root@server ~]# systemctl start target [root@server ~]# targetcli /> /backstores/block create westos:storage1 /dev/vdb1 #/dev/vdb1在软件里起的名字叫 westos:storage1 Created block storage object westos:storage1 using /dev/vdb1. /> /iscsi create iqn.2017-12.com.example:storage1 #起的设备的名字storage1 Created target iqn.2017-12.com.example:storage1. #iqn:全局唯一名称标示 Created TPG 1. /> /iscsi/iqn.2017-12.com.example:storage1/tpg1/acls create iqn.2017-12.com.example:westoskey #生成key Created Node ACL for iqn.2017-12.com.example:westoskey /> /iscsi/iqn.2017-12.com.example:storage1/tpg1/luns create /backstores/block/westos:storage1 #key和设备关联 Created LUN 0. Created LUN 0->0 mapping in node ACL iqn.2017-12.com.example:westoskey /> iscsi/iqn.2017-12.com.example:storage1/tpg1/portals create 172.25.254.125 # ip开机启动,设备识别 Using default IP port 3260 Created network portal 172.25.254.125:3260. #打开此ip端口 /> exit Global pref auto_save_on_exit=true Last 10 configs saved in /etc/target/backup. #文件 Configuration saved to /etc/target/saveconfig.json #所写内容保存在此文件

[root@server ~]# firewall-cmd --permanent --add-port=3260/tcp #开启端口 success [root@server ~]# firewall-cmd --reload success [root@server ~]# cat /etc/target/saveconfig.json #targetcli中所写内容 客户端: [root@client ~]# yum search iscsi iscsi-initiator-utils.x86_64 [root@client ~]# yum install iscsi-initiator-utils.x86_64 [root@client ~]# systemctl start iscsi [root@client ~]# vim /etc/iscsi/initiatorname.iscsi #key文件 InitiatorName=iqn.2017-12.com.example:westoskey [root@client ~]# systemctl restart iscsi #重启 [root@client ~]# iscsiadm -m discovery -t st -p 172.25.254.125 172.25.254.125:3260,1 iqn.2017-12.com.example:storage1 m:动作 t:type p:ip #发现设备 [root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.125 -l Logging in to [iface: default, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.125,3260] (multiple) Login to [iface: default, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.125,3260] successful. node:节点 T:共享的设备的名字 l:登陆 [root@client ~]# fdisk -l

Disk /dev/sda: 5368 MB, 5368709120 bytes, 10485760 sectors #共享的磁盘/dev/sda [root@client ~]# fdisk /dev/sda #投入使用 Command (m for help): n Using default response p Partition number (1-4, default 1): First sector (8192-10485759, default 8192): Using default value 8192 Last sector, +sectors or +size{K,M,G} (8192-10485759, default 10485759): [root@client ~]# partprobe #同步分区表 [root@client ~]# mkfs.xfs /dev/sda1 #格式化文件系统 [root@client ~]# mount /dev/sda1 /mnt/ #挂载 [root@client mnt]# touch file #投入使用 创建的文件保存在服务端的/dev/vdb1 [root@client ~]# blkid #查看投入使用的分区 /dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" /dev/vdb1: UUID="SIaPf4-OdHu-OzAW-NlQG-vZ3D-X8ZO-1FK3Ih" TYPE="LVM2_member" /dev/mapper/vg0-vo: UUID="12294be2-bdad-4817-b162-038e22313d9f" TYPE="ext4" /dev/sda1: UUID="0a645fd3-f948-49a6-896e-c2892d66fe82" TYPE="xfs" [root@client ~]# vim /etc/fstab #分区永久挂载写入配置文件

UUID="0a645fd3-f948-49a6-896e-c2892d66fe82" /mnt xfs defaults,_netdev 0 0 #_netdev告诉系统它不是真正的物理设备,而是网络虚拟设备 #写上UUID以免发生名称偏移。启动时,让网络先激活在读取设备文件

[root@client ~]# poweroff #强制断电 [root@foundation25 ~]# rht-vmctl start desktop Starting desktop. [root@foundation25 ~]# rht-vmctl view desktop

[root@client ~]# vim /etc/fstab #删除挂载信息

[root@client ~]# umount /mnt/ #卸载 [root@client ~]# df [root@client ~]# yum install tree #安装 [root@client ~]# tree /var/lib/iscsi #树形显示 iscsi的内容 [root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.125 -u #退出 重启服务设备又回来了 fdisk -l #查看分区信息 [root@client ~]# systemctl restart iscsi #重启服务 fdisk -l #查看分区信息 /dev/sda1 又回来了 [root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.125 -u #退出

[root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.125 -o delete #删除
[root@client ~]# systemctl restart iscsi #重启服务 [root@client ~]# fdisk -l #退出再删除后,网络设备消失 [root@client ~]# tree /var/lib/iscsi/ #用树形图查看/var/lib/iscsi/无数据 /var/lib/iscsi/ |-- ifaces |-- isns |-- nodes |-- send_targets | -- 172.25.254.125,3260 |-- st_config |-- slp `-- static

服务端不想共享: [root@server ~]# targetcli /> clearconfig confirm=true All configuration cleared #清除共享信息 [root@server ~]# fdisk /dev/vdb Command (m for help): d #删除分区 wq 一般分区采用lvm分区,解决设备不够用问题

[root@server ~]# fdisk /dev/vdb Command (m for help): t Selected partition 1 Hex code (type L to list all codes): Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p Device Boot Start End Blocks Id System /dev/vdb1 2048 2099199 1048576 8e Linux LVM [root@server ~]# partprobe [root@server ~]# cat /proc/partitions major minor #blocks name 253 17 1048576 vdb1 [root@server ~]# pvcreate /dev/vdb1 #/dev/vdb1创建物理卷 [root@server ~]# vgcreate iscsi_vg /dev/vdb1 #为/dev/vdb1创建物理卷组,名字叫iscsi_vg [root@server ~]# vgdisplay #卷组展示,查看有多少物理单元,有255个 Total PE 255 [root@server ~]# lvcreate -l 255 -n iscsi_lv0 iscsi_vg #逻辑卷iscsi_lv0的大小为255个物理扩展单元

[root@server ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert iscsi_lv0 iscsi_vg -wi-a----- 1020.00m [root@server ~]# targetcli /> /backstores/block create westos:storage1 /dev/iscsi_vg/iscsi_lv0 Created block storage object westos:storage1 using /dev/iscsi_vg/iscsi_lv0. /> /iscsi create iqn.2017-12.com.example:storage1 Created target iqn.2017-12.com.example:storage1. Created TPG 1. /> /iscsi/iqn.2017-12.com.example:storage1/tpg1/acls create iqn.2017-12.com.example:key1 Created Node ACL for iqn.2017-12.com.example:key1 /> /iscsi/iqn.2017-12.com.example:storage1/tpg1/luns create /backstores/block/westos:storage1 Created LUN 0. Created LUN 0->0 mapping in node ACL iqn.2017-12.com.example:key1 /> /iscsi/iqn.2017-12.com.example:storage1/tpg1/portals create 172.25.254.125 Using default IP port 3260 Created network portal 172.25.254.125:3260. /> ls o- / ......................................... [...] o- backstores .............................. [...] | o- block .................. [Storage Objects: 1] | | o- westos:storage1 [/dev/iscsi_vg/iscsi_lv0 (1020.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.2017-12.com.example:storage1 .. [TPGs: 1] | o- tpg1 ............... [no-gen-acls, no-auth] | o- acls .......................... [ACLs: 1] | | o- iqn.2017-12.com.example:key1 [Mapped LUNs: 1] | | o- mapped_lun0 [lun0 block/westos:storage1 (rw)] | o- luns .......................... [LUNs: 1] | | o- lun0 [block/westos:storage1 (/dev/iscsi_vg/iscsi_lv0)] | o- portals .................... [Portals: 1] | o- 172.25.254.125: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 客户端: [root@client ~]# tree /var/lib/iscsi/ /var/lib/iscsi/ |-- ifaces |-- isns |-- nodes |-- send_targets | -- 172.25.254.125,3260 |-- st_config |-- slp `-- static

[root@client ~]# vim /etc/iscsi/initiatorname.iscsi [root@client ~]# systemctl restart iscsi [root@client ~]# iscsiadm -m discovery -t st -p 172.25.254.125 [root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.125 -l iscsiadm: initiator reported error (24 - iSCSI login failed due to authorization failure) iscsiadm: Could not log into all portals #当登陆不成功时,解决方法如下: [root@client ~]# tree /var/lib/iscsi/ #查看树形图,登陆信息已被记录,需要删除,在重新生成 /var/lib/iscsi/ |-- ifaces |-- isns |-- nodes | -- iqn.2017-12.com.example:storage1 |-- 172.25.254.125,3260,1 | -- default |-- send_targets |-- 172.25.254.125,3260 | |-- iqn.2017-12.com.example:storage1,172.25.254.125,3260,1,default -> /var/lib/iscsi/nodes/iqn.2017-12.com.example:storage1/172.25.254.125,3260,1 | -- st_config |-- slp-- static

[root@client ~]# rm -fr /var/lib/iscsi/nodes/* [root@client ~]# rm -fr /var/lib/iscsi/send_targets [root@client ~]# tree /var/lib/iscsi//var/lib/iscsi/ |-- ifaces |-- isns |-- nodes |-- slp `-- static

[root@client ~]# systemctl restart iscsid.service #当删除tree中内容时,要重启主目录 [root@client ~]# systemctl restart iscsi #重启子目录 [root@client ~]# iscsiadm -m discovery -t st -p 172.25.254.125 #发现设备 172.25.254.125:3260,1 iqn.2017-12.com.example:storage1 [root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.125 -l #登陆 Login to [iface: default, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.125,3260] successful. #成功

服务端 当分区不够用时,新建分区 [root@server ~]# fdisk /dev/vdb Command (m for help): n Last sector, +sectors or +size{K,M,G} (2099200-20971519, default 20971519): +1G Partition 2 of type Linux and of size 1 GiB is set Command (m for help): t Partition number (1,2, default 2): 2 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): wq [root@server ~]# partprobe [root@server ~]# cat /proc/partitions [root@server ~]# pvcreate /dev/vdb2 [root@server ~]# vgextend iscsi_vg /dev/vdb2 Volume group "iscsi_vg" successfully extended [root@server ~]# lvextend -L 1500M /dev/iscsi_vg/iscsi_lv0 [root@server ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert iscsi_lv0 iscsi_vg -wi-ao---- 1.46g
[root@client ~]# fdisk -l #看出还是原来的分区大小 退出后在登陆才能更新

[root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.125 -u #退出,再登陆,才能更新新的分区大小 Logging out of session [sid: 3, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.125,3260] Logout of [sid: 3, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.125,3260] successful. [root@client ~]# iscsiadm -m discovery -t st -p 172.25.254.125 #发现设备 172.25.254.125:3260,1 iqn.2017-12.com.example:storage1 [root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.125 -l #登陆

[root@client ~]# fdisk -l #可以看出服务端磁盘增大后客户端更新了