Linux中iscsi

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 
Linux中iscsi
Linux中iscsi
[root@server ~]# yum install targetcli
Linux中iscsi
[root@server ~]# systemctl start target
Linux中iscsi
[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 #所写内容保存在此文件

Linux中iscsi

Linux中iscsi
[root@server ~]# firewall-cmd --permanent --add-port=3260/tcp #开启端口
success
[root@server ~]# firewall-cmd --reload 
success
Linux中iscsi
[root@server ~]# cat /etc/target/saveconfig.json #targetcli中所写内容
Linux中iscsi
客户端:
[root@client ~]# yum search iscsi
iscsi-initiator-utils.x86_64 
Linux中iscsi
[root@client ~]# yum install iscsi-initiator-utils.x86_64
Linux中iscsi
[root@client ~]# systemctl start iscsi
Linux中iscsi
[root@client ~]# vim /etc/iscsi/initiatorname.iscsi #key文件
InitiatorName=iqn.2017-12.com.example:westoskey
Linux中iscsi
Linux中iscsi
[root@client ~]# systemctl restart iscsi #重启
Linux中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 #发现设备
Linux中iscsi
[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:登陆
Linux中iscsi
[root@client ~]# fdisk -l

Disk /dev/sda: 5368 MB, 5368709120 bytes, 10485760 sectors #共享的磁盘/dev/sda
Linux中iscsi
Linux中iscsi
[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): 
Linux中iscsi
[root@client ~]# partprobe #同步分区表
Linux中iscsi
[root@client ~]# mkfs.xfs /dev/sda1 #格式化文件系统
Linux中iscsi
[root@client ~]# mount /dev/sda1 /mnt/ #挂载
Linux中iscsi
[root@client mnt]# touch file #投入使用 创建的文件保存在服务端的/dev/vdb1
Linux中iscsi
[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以免发生名称偏移。启动时,让网络先激活在读取设备文件
Linux中iscsi
Linux中iscsi

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

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

[root@client ~]# umount /mnt/ #卸载
Linux中iscsi
[root@client ~]# df
Linux中iscsi
[root@client ~]# yum install tree #安装
Linux中iscsi
[root@client ~]# tree /var/lib/iscsi #树形显示 iscsi的内容
Linux中iscsi
[root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.125 -u #退出 重启服务设备又回来了
Linux中iscsi
fdisk -l #查看分区信息
Linux中iscsi
Linux中iscsi
[root@client ~]# systemctl restart iscsi #重启服务
Linux中iscsi
fdisk -l #查看分区信息
/dev/sda1 又回来了
Linux中iscsi
Linux中iscsi
[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 #删除 
Linux中iscsi
[root@client ~]# systemctl restart iscsi #重启服务
Linux中iscsi
[root@client ~]# fdisk -l #退出再删除后,网络设备消失
Linux中iscsi
Linux中iscsi
[root@client ~]# tree /var/lib/iscsi/ #用树形图查看/var/lib/iscsi/无数据
/var/lib/iscsi/
|-- ifaces
|-- isns
|-- nodes
|-- send_targets
-- 172.25.254.125,3260<br/>|-- st_config
|-- slp
`-- static
Linux中iscsi

服务端不想共享:
[root@server ~]# targetcli 
/> clearconfig confirm=true
All configuration cleared #清除共享信息
Linux中iscsi
[root@server ~]# fdisk /dev/vdb
Command (m for help): d #删除分区
wq
Linux中iscsi
Linux中iscsi
Linux中iscsi
一般分区采用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
Linux中iscsi
Linux中iscsi
Linux中iscsi
Linux中iscsi
[root@server ~]# pvcreate /dev/vdb1 #/dev/vdb1创建物理卷
Linux中iscsi
[root@server ~]# vgcreate iscsi_vg /dev/vdb1 #为/dev/vdb1创建物理卷组,名字叫iscsi_vg
Linux中iscsi
[root@server ~]# vgdisplay #卷组展示,查看有多少物理单元,有255个
Total PE 255
Linux中iscsi
[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 
Linux中iscsi
[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
Linux中iscsi
Linux中iscsi
客户端:
[root@client ~]# tree /var/lib/iscsi/
/var/lib/iscsi/
|-- ifaces
|-- isns
|-- nodes
|-- send_targets
-- 172.25.254.125,3260<br/>|-- st_config
|-- slp
`-- static

Linux中iscsi
[root@client ~]# vim /etc/iscsi/initiatorname.iscsi 
Linux中iscsi
Linux中iscsi
[root@client ~]# systemctl restart iscsi
Linux中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<br/>|-- 172.25.254.125,3260,1
-- default<br/>|-- send_targets<br/>|-- 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<br/>|-- slp<br/>-- 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. #成功
Linux中iscsi

服务端
当分区不够用时,新建分区
[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
Linux中iscsi
Linux中iscsi
[root@server ~]# partprobe 
[root@server ~]# cat /proc/partitions 
Linux中iscsi
[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 
Linux中iscsi
[root@server ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
iscsi_lv0 iscsi_vg -wi-ao---- 1.46g 
Linux中iscsi
[root@client ~]# fdisk -l #看出还是原来的分区大小
退出后在登陆才能更新
Linux中iscsi
Linux中iscsi

[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.
Linux中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 #登陆
Linux中iscsi

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







本文转自Uniqueh51CTO博客,原文链接: http://blog.51cto.com/13363488/2050057,如需转载请自行联系原作者


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值