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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章