ISCSI设备共享


#1).安装建立
服务端:
[[email protected] ~]# yum install targetcli.noarch

[[email protected] ~]# fdisk /dev/vdb               #在本机分出一块设备用于共享

[[email protected] ~]# systemctl start target

[[email protected] ~]# targetcli

ISCSI设备共享

/> /backstores/block create westos:storage1 /dev/vdb1                        #添加要共享的设备

Created block storage object westos:storage1 using /dev/vdb1.                   

ISCSI设备共享

/> /iscsi create iqn.2017-12.com.example:storage1             #给设备命名,格式为:iqn.年-月.域名反写
Created target iqn.2017-12.com.example:storage1.                       

Created TPG 1.                                           

ISCSI设备共享

/> iscsi/iqn.2017-12.com.example:storage1/tpg1/acls create iqn.2017-12.com.example:westoskey    #给设备加密,可用明文或者加密字符

Created Node ACL for iqn.2017-12.com.example:westoskey                       

ISCSI设备共享

/> 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:westoskey               

ISCSI设备共享

/> iscsi/iqn.2017-12.com.example:storage1/tpg1/portals create 172.25.254.126            #开启端口
Using default IP port 3260                                   

Created network portal 172.25.254.126:3260.                          

ISCSI设备共享

/> ls

ISCSI设备共享

在本机上开启3260端口

ISCSI设备共享


#2).挂载使用
客户端:
[[email protected] ~]# yum install iscsi-initiator-utils.x86_64
[[email protected] ~]# systemctl start iscsi
[[email protected] ~]# vim /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2017-12.com.example:westoskey            #要共享的设备

ISCSI设备共享

[[email protected] ~]# iscsiadm -m discovery -t st -p 172.25.254.126    #发现设备
172.25.254.126:3260,1 iqn.2017-12.com.example:storage1
ISCSI设备共享
[[email protected] ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.126 -l    #登陆设备
Logging in to [iface: default, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.126,3260] (multiple)

Login to [iface: default, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.126,3260] successful.

登陆是可能会遇到报错,这时可依次重启iscsid和iscsi服务,iscsid是iscsi的上层服务。

ISCSI设备共享


[[email protected] ~]# fdisk -l
ISCSI设备共享


[[email protected] ~]# fdisk /dev/sda         #分区

[[email protected] ~]# partprobe
[[email protected] ~]# mkfs.xfs /dev/sda1    #制作文件系统
ISCSI设备共享


[[email protected] ~]# mount /dev/sda1 /mnt    #挂载使用

[[email protected] mnt]# df
ISCSI设备共享
[[email protected] ~]# cd /mnt
[[email protected] mnt]# touch file
[[email protected] mnt]# ls
fISCSI设备共享

[[email protected] ~]# blkid            #服务端没有文件系统
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/vdb1: PTTYPE="dos"
ISCSI设备共享


#3).开机自动挂载

[[email protected] mnt]# blkid

/dev/sda1: UUID="2305391d-c87a-4e16-aca9-b5a88daa491e" TYPE="xfs"     

#查看设备的uuid,通过网络共享,设备名在不同主机上可能会发生漂移,但uuid是唯一的。因此通过uuid识别设备是比较可靠的方式


[[email protected] ~]# vim /etc/fstab
UUID="2305391d-c87a-4e16-aca9-b5a88daa491e"  /mnt   xfs  defaults,_netdev 0 0

#系统启动会先读取fstab内容挂载设备,然后才开启网络。所以要用参数defaults,_netdev告诉系统,这块设备是网络设备。防止系统因找不到此设备而无法正常启动。


#4).卸载
客户端:
[[email protected] mnt]# umount /mnt
[[email protected] mnt]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.126 -u    #退出登陆
Logging out of session [sid: 1, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.126,3260]
Logout of [sid: 1, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.126,3260] successful.
[[email protected] mnt]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.126 -o delete    #清除缓存信息

ISCSI设备共享


服务端:
[[email protected] ~]# targetcli           #执行clearconfig confirm=true命令即可
ISCSI设备共享ISCSI设备共享

#5).建立逻辑卷设备共享

当我们真正在企业作共享服务时,由于数据的不断增多,一般会建立lvm可扩展的设备来提供共享服务。与一般的设备共享操作类似,这里只需建立一个lvm设备并把它共享出去即可。


服务端:
[[email protected] ~]# pvcreate /dev/vdb1
  Physical volume "/dev/vdb1" successfully created
[[email protected] ~]# vgcreate iscsi_vg0 /dev/vdb1
  Volume group "iscsi_vg0" successfully created
[[email protected] ~]# lvcreate -L 1020M -n iscsi_lv0 iscsi_vg0
  Logical volume "iscsi_lv0" created                #创建逻辑卷

ISCSI设备共享


[[email protected] ~]# targetcli               #共享逻辑卷
targetcli shell version 2.1.fb34
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.

ISCSI设备共享


客户端:
[[email protected] ~]# iscsiadm -m discovery -t st -p 172.25.254.126
172.25.254.126:3260,1 iqn.2017-12.com.example:storage1
[[email protected] ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.126 -l
Logging in to [iface: default, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.126,3260] (multiple)

Login to [iface: default, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.126,3260] successful.

ISCSI设备共享


[[email protected] ~]# fdisk -l

Disk /dev/sda: 1069 MB, 1069547520 bytes, 2088960 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

ISCSI设备共享


逻辑卷空间不足后:
服务端:
[[email protected] ~]# lvextend -L 2G /dev/iscsi_vg0/iscsi_lv0
  Extending logical volume iscsi_lv0 to 2.00 GiB

  Logical volume iscsi_lv0 successfully resized                #扩张逻辑卷

ISCSI设备共享

客户端:
[[email protected] ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.126 -u
Logging out of session [sid: 3, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.126,3260]
Logout of [sid: 3, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.126,3260] successful.
[[email protected] ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.126 -l
Logging in to [iface: default, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.126,3260] (multiple)

Login to [iface: default, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.126,3260] successful.

ISCSI设备共享


[[email protected] ~]# fdisk -l

Disk /dev/sda: 2147 MB, 2147483648 bytes, 4194304 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            #重新加载
ISCSI设备共享

若vg中的剩余空间也不足了,可参考lvm的管理。再分一块设备,制成pv加入要扩展共享lv所在的vg组里,在扩展lv即可

[[email protected] ~]# pvcreate /dev/vdb3
  Physical volume "/dev/vdb2" successfully created
[[email protected] ~]# vgextend iscsi_vg0 /dev/vdb3
  Volume group "iscsi_vg0" successfully extended