Linux-网络共享存储iSCSI配置及使用

iSCSI(Internet Small Computer System Interface)互联网小型计算机系统接口。是一个供硬件设备使用的、可在IP协议上层运行的SCSI指令集,是一种开放的基于IP协议的工业技术标准。iSCSI技术实现了物理硬盘设备与TCP/TP网络传输协议的相互结合,使得用户可以通过互联网方便的获取到远程机房提供的共享存储资源。

测试环境

主机名称        擦作系统        IP地址
Server        RHEL7操作系统    172.25.254.243
Desktop       RHEL7操作系统    172.25.254.143

搭建iSCSI服务端

###安装启动服务###
[root@server ~]# yum install targetcli -y
[root@server ~]# systemctl start target
[root@server ~]# systemctl enable target
ln -s '/usr/lib/systemd/system/target.service' '/etc/systemd/system/multi-user.target.wants/target.service'

###准备共享磁盘###
[root@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

###/dev/vdb磁盘有10G空间,划分出5G用来测试
[root@server ~]# fdisk /dev/vdb
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x8dc33c30.

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 (2048-20971519, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +5G
Partition 1 of type Linux and of size 5 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

###创建iSCSI共享###
##打开targetcli
[root@server ~]# targetcli
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 jinx:storage1 /dev/vdb1
Created block storage object jinx:storage1 using /dev/vdb1.
/> ls
o- / ..................................................................... [...]
  o- backstores .......................................................... [...]
  | o- block .............................................. [Storage Objects: 1]
  | | o- jinx: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
/> /iscsi create iqn.2018-06.com.jinx:storage1
Created target iqn.2018-06.com.jinx:storage1.
Created TPG 1.
/> ls
o- / ..................................................................... [...]
  o- backstores .......................................................... [...]
  | o- block .............................................. [Storage Objects: 1]
  | | o- jinx: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: 1]
  | o- iqn.2018-06.com.jinx:storage1 ................................. [TPGs: 1]
  |   o- tpg1 ........................................... [no-gen-acls, no-auth]
  |     o- acls ...................................................... [ACLs: 0]
  |     o- luns ...................................................... [LUNs: 0]
  |     o- portals ................................................ [Portals: 0]
  o- loopback ..................................................... [Targets: 0]
###iSCSI限定名称用于确定发起端和目标,并采用iqn.yyyy-mm.{reverse domain}:label的格式
###默认情况下,网络通信至iSCSI目标上的端口 3260/tcp的明文端口

##设置iSCSIkey
/> /iscsi/iqn.2018-06.com.jinx:storage1/tpg1/acls create iqn.2018-06.com.jinx:jinxkey
Created Node ACL for iqn.2018-06.com.jinx:jinxkey
##添加存储至iSCSI
/> /iscsi/iqn.2018-06.com.jinx:storage1/tpg1/luns create /backstores/block/jinx:storage1 
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.2018-06.com.jinx:jinxkey
##设置端口
/> /iscsi/iqn.2018-06.com.jinx:storage1/tpg1/portals create 172.25.254.243
Using default IP port 3260
Created network portal 172.25.254.243:3260.
##查看修改内容后退出
/> ls
o- / ..................................................................... [...]
  o- backstores .......................................................... [...]
  | o- block .............................................. [Storage Objects: 1]
  | | o- jinx: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.2018-06.com.jinx:storage1 ................................. [TPGs: 1]
  |   o- tpg1 ........................................... [no-gen-acls, no-auth]
  |     o- acls ...................................................... [ACLs: 1]
  |     | o- iqn.2018-06.com.jinx:jinxkey ..................... [Mapped LUNs: 1]
  |     |   o- mapped_lun0 ..................... [lun0 block/jinx:storage1 (rw)]
  |     o- luns ...................................................... [LUNs: 1]
  |     | o- lun0 ............................ [block/jinx:storage1 (/dev/vdb1)]
  |     o- portals ................................................ [Portals: 1]
  |       o- 172.25.254.243: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@desktop ~]# yum install iscsi-initiator-utils.i686 -y

##配置iSCSIkey
[root@desktop ~]# vim /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2018-06.com.example:jinxkey

##发现iSCSI资源
[root@desktop ~]# iscsiadm -m discovery -t st -p 172.25.254.243
172.25.254.243:3260,1 iqn.2018-06.com.example:storage1

##添加iSCSI存储
[root@desktop ~]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.243 -l
Logging in to [iface: default, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.243,3260] (multiple)
Login to [iface: default, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.243,3260] successful.

##查看存储设备
[root@desktop ~]# fdisk -l
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

##分区后格式化、挂载
[root@desktop ~]# mkfs.ext4 /dev/sda1
[root@desktop ~]# mount /dev/sda1 /mnt
[root@desktop ~]# df -H
Filesystem          Size  Used Avail Use% Mounted on
/dev/vda1            11G  3.4G  7.5G  31% /
devtmpfs            481M     0  481M   0% /dev
tmpfs               497M   82k  497M   1% /dev/shm
tmpfs               497M   14M  484M   3% /run
tmpfs               497M     0  497M   0% /sys/fs/cgroup
/dev/mapper/vg0-vo  496M  2.5M  463M   1% /home
/dev/sda1           5.2G   21M  4.9G   1% /mnt

##开机挂载网络存储
[root@desktop ~]# vim /etc/fstab
/dev/sda1       /mnt    ext4    defaults,_netdev        0 0    ##_netdev,先启动网路设备

##重启客户端,测试

清理实验环境

[root@desktop ~]# umount /mnt		##解除挂载
[root@desktop ~]# df
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3243484   7230416  31% /
devtmpfs              469344       0    469344   0% /dev
tmpfs                 484932      80    484852   1% /dev/shm
tmpfs                 484932   12768    472164   3% /run
tmpfs                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo    483670    2355    451824   1% /home

[root@desktop ~]# vim /etc/fstab	##清除添加的开机自动挂载行

[root@desktop ~]# fdisk -l
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: 0x3df74f0c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            8192    10485759     5238784   83  Linux
[root@desktop ~]# tree /var/lib/iscsi/	##查看iscsi设备文件
/var/lib/iscsi/
├── ifaces
├── isns
├── nodes
│   └── iqn.2018-06.com.example:storage1
│       └── 172.25.254.243,3260,1
│           └── default
├── send_targets
│   └── 172.25.254.243,3260
│       ├── iqn.2018-06.com.example:storage1,172.25.254.243,3260,1,default -> /var/lib/iscsi/nodes/iqn.2018-06.com.example:storage1/172.25.254.243,3260,1
│       └── st_config
├── slp
└── static

10 directories, 2 files

##iscsi设备登出
[root@desktop ~]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.243 -u
Logging out of session [sid: 1, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.243,3260]
Logout of [sid: 1, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.243,3260] successful.

##iscsi设备文件删除
[root@desktop ~]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.243 -o delete
[root@desktop ~]# tree /var/lib/iscsi/	##查看iscsi文件
/var/lib/iscsi/
├── ifaces
├── isns
├── nodes
├── send_targets
│   └── 172.25.254.243,3260
│       └── st_config
├── slp
└── static

7 directories, 1 file


猜你喜欢

转载自blog.csdn.net/xin1889/article/details/80607543