########### iscsi共享设备
redhat7.0
172.25.254.135 172.25.254.235
icsi-server iscsi-client
1.iSCSI概念(iscsi是直接对设备进行共享的服务)
iSCSI(Internet SCSI)支持从客户端(发起端)通过IP向远程服务器上的SCSI存储设备(目标)发送SCSI命令。iSCSI限定名称用于确定发起端和目标,并采用iqn.yyyy-mm.{reverse domain}:label的格式。默认情况下,网络通信是至iSCSI目标上的端口3260/tcp的明文。
iSCSI发起端:需要访问原始SAN存储的客户端。
iSCSI目标:从iSCSI服务器提供的远程硬盘磁盘,或“目标门户”
iSCSI目标门户:通过网络向发起端提供目标的服务器。
IQN:“iSCSI限定名称”。每个发起端和目标需要唯一名称进行标识,最好的做法是使用一个在Internet上可能独一无二的名称。
首先划分出来一块磁盘
Server端:
[root@iscsi-server ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x483acf4d
Device Boot Start End Blocks Id System
/dev/vdb1 2048 10487807 5242880 83 Linux
[root@iscsi-server ~]# yum install -y targetcli 安装软件
[root@iscsi-server ~]# systemctl start target
[root@iscsi-server ~]# systemctl enable target
ln -s ‘/usr/lib/systemd/system/target.service’ ‘/etc/systemd/system/multi-user.target.wants/target.service’
[root@iscsi-server ~]# targetcli ##进入iscsi目标交互式配置模式
Warning: Could not load preferences file /root/.targetcli/prefs.bin.
targetcli shell version 2.1.fb34
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type ‘help’.
/> ls
o- / …………………………………………………………… […]
o- backstores …………………………………………………. […]
| o- block ………………………………………. [Storage Objects: 0]
| o- fileio ……………………………………… [Storage Objects: 0]
| o- pscsi ………………………………………. [Storage Objects: 0]
| o- ramdisk …………………………………….. [Storage Objects: 0]
o- iscsi ……………………………………………….. [Targets: 0]
o- loopback …………………………………………….. [Targets: 0]
/> /backstores/block create westos:storage1 /dev/vdb1 ##和逻辑卷建立联系
Created block storage object westos:storage1 using /dev/vdb1.
/> ls
o- / …………………………………………………………… […]
o- backstores …………………………………………………. […]
| o- block ………………………………………. [Storage Objects: 1]
| | o- westos:storage1 ……….. [/dev/vdb1 (5.0GiB) write-thru deactivated]
| o- fileio ……………………………………… [Storage Objects: 0]
| o- pscsi ………………………………………. [Storage Objects: 0]
| o- ramdisk …………………………………….. [Storage Objects: 0]
o- iscsi ……………………………………………….. [Targets: 0]
o- loopback …………………………………………….. [Targets: 0]
/> /iscsi create iqn.2017-08.com.example:storage1 ##创建iqn服务的名称
Created target iqn.2017-08.com.example:storage1.
Created TPG 1.
/> /iscsi/iqn.2017-08.com.example:storage1/tpg1/luns create /backstores/block/westos:storage1 ##加密
Created LUN 0.
/> /iscsi/iqn.2017-08.com.example:storage1/tpg1/acls create iqn.2017-08.com.example:storage1key ##创建客户端启动服务密钥
Created Node ACL for iqn.2017-08.com.example:storage1key
Created mapped LUN 0.
/> ls
o- / …………………………………………………………… […]
o- backstores …………………………………………………. […]
| o- block ………………………………………. [Storage Objects: 1]
| | o- westos:storage1 …………. [/dev/vdb1 (5.0GiB) 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-08.com.example:storage1 ………………………… [TPGs: 1]
| o- tpg1 ……………………………………. [no-gen-acls, no-auth]
| o- acls ……………………………………………… [ACLs: 1]
| | o- iqn.2017-08.com.example:storage1key ………….. [Mapped LUNs: 1]
| | o- mapped_lun0 ………………. [lun0 block/westos:storage1 (rw)]
| o- luns ……………………………………………… [LUNs: 1]
| | o- lun0 …………………….. [block/westos:storage1 (/dev/vdb1)]
| o- portals ………………………………………… [Portals: 0]
o- loopback …………………………………………….. [Targets: 0]/> iscsi/iqn.2017-08.com.example:storage1/tpg1/portals create 172.25.254.135
Using default IP port 3260 ##创建目标门户
Created network portal 172.25.254.135:3260.
Client端:
[root@iscsi-client ~]# yum install -y iscsi-initiator-utils.x86_64
[root@iscsi-client ~]# vim /etc/iscsi/initiatorname.iscsi ##添加密钥
1 InitiatorName=iqn.2017-08.com.example:storage1key
[root@iscsi-client ~]# systemctl restart iscsi
[root@iscsi-client ~]# iscsiadm -m discovery -t st -p 172.25.254.135
172.25.254.135:3260,1 iqn.2017-08.com.example:storage1
[root@iscsi-client ~]# iscsiadm -mnode -T iqn.2017-08.com.example:storage1 -p 172.25.254.135 -l
Logging in to [iface: default, target: iqn.2017-08.com.example:storage1, portal: 172.25.254.135,3260] (multiple)
Login to [iface: default, target: iqn.2017-08.com.example:storage1, portal: 172.25.254.135,3260] successful.
测试:
[root@iscsi-client ~]# cat /proc/partitions
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 512000 vdb1
252 0 507904 dm-0
8 0 5242880 sda
[root@iscsi-client ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
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): +1G
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): p
Disk /dev/sda: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 4194304 bytes
Disk label type: dos
Disk identifier: 0x245d9e1f
Device Boot Start End Blocks Id System
/dev/sda1 8192 2105343 1048576 83 Linux
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@iscsi-client ~]# partprobe
[root@iscsi-client ~]# mkfs.xfs /dev/sda1
[root@iscsi-client ~]# mount /dev/sda1 /mnt
[root@iscsi-client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3171000 7302900 31% /
devtmpfs 493408 0 493408 0% /dev
tmpfs 508996 12 508984 1% /dev/shm
tmpfs 508996 13424 495572 3% /run
tmpfs 508996 0 508996 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2339 451840 1% /home
/dev/sda1 1038336 33056 1005280 4% /mnt
删除刚才共享的设备:
Client端:
[root@iscsi-client ~]# umount /mnt/
[root@iscsi-client ~]# yum install -y tree.x86_64
[root@iscsi-client ~]# tree -C /var/lib/iscsi/
/var/lib/iscsi/
|– ifaces
|– isns
|– nodes
| -- iqn.2017-08.com.example:storage1
– 172.25.254.135,3260,1
|
| -- default
– 172.25.254.135,3260
|-- send_targets
|
| |– iqn.2017-08.com.example:storage1,172.25.254.135,3260,1,default -> /var/lib/iscsi/nodes/iqn.2017-08.com.example:storage1/172.25.254.135,3260,1
| -- st_config
– static
|-- slp
[root@iscsi-client ~]# iscsiadm -m node -T iqn.2017-08.com.example:storage1 -u
Logging out of session [sid: 1, target: iqn.2017-08.com.example:storage1, portal: 172.25.254.135,3260]
Logout of [sid: 1, target: iqn.2017-08.com.example:storage1, portal: 172.25.254.135,3260] successful.
[root@iscsi-client ~]# iscsiadm -m node -T iqn.2017-08.com.example:storage1 -o delete
Server端:
/> clearconfig confirm=True
All configuration cleared
/> ls
o- / ……………………………………………………….. […]
o- backstores ……………………………………………… […]
| o- block …………………………………… [Storage Objects: 0]
| o- fileio ………………………………….. [Storage Objects: 0]
| o- pscsi …………………………………… [Storage Objects: 0]
| o- ramdisk …………………………………. [Storage Objects: 0]
o- iscsi ……………………………………………. [Targets: 0]
o- loopback …………………………………………. [Targets: 0]
###### iscsi共享lvm可扩展设备
Server端:
[root@iscsi-server ~]# pvcreate /dev/vdb1
WARNING: dos signature detected on /dev/vdb1 at offset 510. Wipe it? [y/n] y
Wiping dos signature on /dev/vdb1.
Physical volume “/dev/vdb1” successfully created
[root@iscsi-server ~]# vgcreate iscsivg /dev/vdb1
Volume group “iscsivg” successfully created
[root@iscsi-server ~]# vgdisplay
— Volume group —
VG Name iscsivg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 5.00 GiB
PE Size 4.00 MiB
Total PE 1279
Alloc PE / Size 0 / 0
Free PE / Size 1279 / 5.00 GiB
VG UUID WE50Bl-qFu9-WYi2-6khh-U6tI-VOZA-mN9XSz
[root@iscsi-server ~]# lvcreate -l 1279 -n iscsilv0 iscsivg
Logical volume “iscsilv0” created
[root@iscsi-server ~]# targetcli
targetcli shell version 2.1.fb34
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type ‘help’.
/> /backstores/block create westos:storage1 /dev/iscsivg/iscsilv0
Created block storage object westos:storage1 using /dev/iscsivg/iscsilv0.
/> /iscsi create iqn.2017-08.com.example:storage1
Created target iqn.2017-08.com.example:storage1.
Created TPG 1.
/> /iscsi/iqn.2017-08.com.example:storage1/tpg1/luns create /backstores/block/westos:storage1
Created LUN 0.
/> /iscsi/iqn.2017-08.com.example:storage1/tpg1/acls create iqn.2017-08.com.example:storage1key
Created Node ACL for iqn.2017-08.com.example:storage1key
Created mapped LUN 0.
/> /iscsi/iqn.2017-08.com.example:storage1/tpg1/portals create 172.25.254.135
Using default IP port 3260
Created network portal 172.25.254.135:3260.
/> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
Client端:
[root@iscsi-client ~]# vim /etc/iscsi/initiatorname.iscsi
1 InitiatorName=iqn.2017-08.com.example:storage1key
[root@iscsi-client ~]# iscsiadm -m discovery -t st -p 172.25.254.135
172.25.254.135:3260,1 iqn.2017-08.com.example:storage1
[root@iscsi-client ~]# iscsiadm -m node -T iqn.2017-08.com.example:storage1 -p 172.25.254.135 -l
Logging in to [iface: default, target: iqn.2017-08.com.example:storage1, portal: 172.25.254.135,3260] (multiple)
Login to [iface: default, target: iqn.2017-08.com.example:storage1, portal: 172.25.254.135,3260] successful.
[root@iscsi-client ~]# cat /proc/partitions
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 512000 vdb1
252 0 507904 dm-0
8 0 5238784 sda
[root@iscsi-client ~]# fdisk -l
~
Device Boot Start End Blocks Id System
/dev/sda1 8192 10477567 5234688 83 Linux
Server端:
[root@iscsi-server ~]# fdisk /dev/vdb
~~~~~
Device Boot Start End Blocks Id System
/dev/vdb1 2048 10487807 5242880 8e Linux LVM
/dev/vdb2 10487808 12584959 1048576 8e Linux LVM
[root@iscsi-server ~]# vgextend iscsivg /dev/vdb2
Physical volume “/dev/vdb2” successfully created
Volume group “iscsivg” successfully extended
[root@iscsi-server ~]# lvextend -L 6000M /dev/iscsivg/iscsilv0
Extending logical volume iscsilv0 to 5.86 GiB
Logical volume iscsilv0 successfully resized
Client端:
[root@iscsi-client ~]# iscsiadm -m node -T iqn.2017-08.com.example:storage1 -u
[root@iscsi-client ~]# systemctl restart iscsid.service
[root@iscsi-client ~]# systemctl restart iscsi
[root@iscsi-client ~]# iscsiadm -m node -T iqn.2017-08.com.example:storage1 -p 172.25.254.135 -l
[root@iscsi-client ~]# cat /proc/partitions
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 512000 vdb1
252 0 507904 dm-0
8 0 6144000 sda
8 1 5234688 sda1
########## tcp_wrapper
[root@foundation35 html]# vim /etc/hosts.allow
sshd:172.25.254. :spawn echo date
from %c to %s >> /var/log/messages
[root@iscsi-server ~]# ssh root@172.25.254.35
root@172.25.254.35’s password:
Last login: Thu Aug 17 13:54:08 2017 from foundation135.ilt.example.com
[root@foundation35 ~]#
[root@foundation35 html]# cat /var/log/messages
Thu Aug 17 13:56:56 CST 2017 from 172.25.254.135 to sshd@172.25.254.35
Aug 17 13:56:59 foundation35 systemd: Created slice user-0.slice.
Aug 17 13:56:59 foundation35 systemd: Starting user-0.slice.
Aug 17 13:56:59 foundation35 systemd-logind: New session 28 of user root.
Aug 17 13:56:59 foundation35 systemd: Started Session 28 of user root.
Aug 17 13:56:59 foundation35 systemd: Starting Session 28 of user root.
[root@foundation35 html]# vim /etc/hosts.allow
sshd:172.25.254. :spawn echo date
from %c to %s >> /dev/pts/0
[root@iscsi-server ~]# ssh root@172.25.254.35
323 root@172.25.254.35’s password:
324 Last login: Thu Aug 17 13:54:08 2017 from foundation135.ilt.example.com
效果是35上开的当前这个shell被占用