iscsi的共享

1、概述

iSCSI技术实现了物理硬盘设备与TCP/TP网络传输协议的相互结合,使得用户可以通过互联网方便的获取到远程机房提供的共享存储资源

2、iscsi的部署

先建立一个分区 实验用

后下载服务

[root@localhost network-scripts]# 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: 0x09ee18d7

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   8e  Linux LVM

Disk /dev/mapper/vg0-vo: 520 MB, 520093696 bytes, 1015808 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

[root@localhost network-scripts]# 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.


Command (m for help): p

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: 0x09ee18d7

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   8e  Linux LVM

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 
First sector (1026048-20971519, default 1026048): 
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-20971519, default 20971519): +1G
Partition 2 of type Linux and of size 1 GiB is set

Command (m for help): p

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: 0x09ee18d7

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   8e  Linux LVM
/dev/vdb2         1026048     3123199     1048576   83  Linux

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost network-scripts]# partprobe 
[root@localhost network-scripts]# yum install targetcli -y
Loaded plugins: langpacks
Repository 'rhel_dvd' is missing name in configuration, using id
Resolving Dependencies
--> Running transaction check
---> Package targetcli.noarch 0:2.1.fb34-1.el7 will be installed
--> Processing Dependency: python-rtslib >= 2.1.fb41 for package: targetcli-2.1.fb34-1.el7.noarch
--> Processing Dependency: python-configshell for package: targetcli-2.1.fb34-1.el7.noarch
--> Running transaction check
---> Package python-configshell.noarch 1:1.1.fb11-3.el7 will be installed
--> Processing Dependency: pyparsing for package: 1:python-configshell-1.1.fb11-3.el7.noarch
--> Processing Dependency: python-urwid for package: 1:python-configshell-1.1.fb11-3.el7.noarch
---> Package python-rtslib.noarch 0:2.1.fb46-1.el7 will be installed
--> Processing Dependency: python-kmod for package: python-rtslib-2.1.fb46-1.el7.noarch
--> Running transaction check
---> Package pyparsing.noarch 0:1.5.6-9.el7 will be installed
---> Package python-kmod.x86_64 0:0.9-4.el7 will be installed
---> Package python-urwid.x86_64 0:1.1.1-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================
 Package                    Arch           Version                   Repository        Size
============================================================================================
Installing:
 targetcli                  noarch         2.1.fb34-1.el7            rhel_dvd          55 k
Installing for dependencies:
 pyparsing                  noarch         1.5.6-9.el7               rhel_dvd          94 k
 python-configshell         noarch         1:1.1.fb11-3.el7          rhel_dvd          64 k
 python-kmod                x86_64         0.9-4.el7                 rhel_dvd          57 k
 python-rtslib              noarch         2.1.fb46-1.el7            rhel_dvd          75 k
 python-urwid               x86_64         1.1.1-3.el7               rhel_dvd         654 k

Transaction Summary
============================================================================================
Install  1 Package (+5 Dependent packages)

Total download size: 998 k
Installed size: 3.9 M
Downloading packages:
(1/6): pyparsing-1.5.6-9.el7.noarch.rpm                              |  94 kB  00:00:00     
(2/6): python-configshell-1.1.fb11-3.el7.noarch.rpm                  |  64 kB  00:00:00     
(3/6): python-kmod-0.9-4.el7.x86_64.rpm                              |  57 kB  00:00:00     
(4/6): python-rtslib-2.1.fb46-1.el7.noarch.rpm                       |  75 kB  00:00:00     
(5/6): python-urwid-1.1.1-3.el7.x86_64.rpm                           | 654 kB  00:00:00     
(6/6): targetcli-2.1.fb34-1.el7.noarch.rpm                           |  55 kB  00:00:00     
--------------------------------------------------------------------------------------------
Total                                                       4.2 MB/s | 998 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python-kmod-0.9-4.el7.x86_64                                             1/6 
  Installing : python-rtslib-2.1.fb46-1.el7.noarch                                      2/6 
  Installing : pyparsing-1.5.6-9.el7.noarch                                             3/6 
  Installing : python-urwid-1.1.1-3.el7.x86_64                                          4/6 
  Installing : 1:python-configshell-1.1.fb11-3.el7.noarch                               5/6 
  Installing : targetcli-2.1.fb34-1.el7.noarch                                          6/6 
  Verifying  : python-rtslib-2.1.fb46-1.el7.noarch                                      1/6 
  Verifying  : python-urwid-1.1.1-3.el7.x86_64                                          2/6 
  Verifying  : targetcli-2.1.fb34-1.el7.noarch                                          3/6 
  Verifying  : 1:python-configshell-1.1.fb11-3.el7.noarch                               4/6 
  Verifying  : pyparsing-1.5.6-9.el7.noarch                                             5/6 
  Verifying  : python-kmod-0.9-4.el7.x86_64                                             6/6 

Installed:
  targetcli.noarch 0:2.1.fb34-1.el7                                                         

Dependency Installed:
  pyparsing.noarch 0:1.5.6-9.el7           python-configshell.noarch 1:1.1.fb11-3.el7       
  python-kmod.x86_64 0:0.9-4.el7           python-rtslib.noarch 0:2.1.fb46-1.el7            
  python-urwid.x86_64 0:1.1.1-3.el7       

Complete!
[root@localhost network-scripts]# systemctl  start target  ## 开启服务

3、使用服务

[root@localhost network-scripts]# 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 westos:storage1 /dev/vdb2      ##添加设备
Created block storage object westos:storage1 using /dev/vdb2.
/> /iscsi create iqn.2018-06.com.example:storage1  ##创建iscsi限定名称 格式为iqn.年-月.域名反写:label标示
Created target iqn.2018-06.com.example:storage1.
Created TPG 1.
/> ls
o- / ................................................................................. [...]
  o- backstores ...................................................................... [...]
  | o- block .......................................................... [Storage Objects: 1]
  | | o- westos:storage1 ....................... [/dev/vdb2 (1.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.example: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.2018-06.com.example:storage1/tpg1/acls create iqn.2018-06.com.example:westoskey
Created Node ACL for iqn.2018-06.com.example:westoskey 创建acl密钥  

/>  /iscsi/iqn.2018-06.com.example:storage1/tpg1/luns create /backstores/block/westos:storage1
Created LUN 0.                    密钥和设备关联  

Created LUN 0->0 mapping in node ACL iqn.2018-06.com.example:westoskey
/> /iscsi/iqn.2018-06.com.example:storage1/tpg1/portals create 172.25.254.66
Using default IP port 3260
Created network portal 172.25.254.66:3260.
/> /iscsi/iqn.2018-06.com.example:storage1/tpg1/portals create 172.25.254.166 开放端口
Using default IP port 3260
Created network portal 172.25.254.166:3260.
/> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json

[root@foundation66 ~]# iscsiadm -m discovery -t st -p 172.25.254.166
172.25.254.66:3260,1 iqn.2018-06.com.example:storage1
172.25.254.166:3260,1 iqn.2018-06.com.example:storage1
  1. 客户端发现设备  
  2.     ####-t 类型 -p IP端口 
[root@foundation66 ~]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.166 -l
Logging in to [iface: default, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.166,3260] (multiple)
iscsiadm: Could not login to [iface: default, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.166,3260].
iscsiadm: initiator reported error (24 - iSCSI login failed due to authorization failure)
iscsiadm: Could not log into all portals

登陆设备失败,需要在配置文件中添加密钥

[root@foundation66 ~]# vim /etc/iscsi/initiatorname.iscsi

iscsi的共享_第1张图片

[root@foundation66 ~]# systemctl restart iscsid.service 
[root@foundation66 ~]# iscsiadm -m discovery -t st -p 172.25.254.166
172.25.254.66:3260,1 iqn.2018-06.com.example:storage1
172.25.254.166:3260,1 iqn.2018-06.com.example:storage1
[root@foundation66 ~]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.166 -l 
Logging in to [iface: default, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.166,3260] (multiple)
Login to [iface: default, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.166,3260] successful.
[root@foundation66 ~]# 

登陆成功

Disk /dev/sdc: 1073 MB, 1073741824 bytes, 2097152 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 /dev/sdc: 1073 MB, 1073741824 bytes, 2097152 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@foundation66]# 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.

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

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

Command (m for help): p

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
Disk label type: dos
Disk identifier: 0x759cd7b5

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            8192     1032191      512000   83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@foundation66]# partprobe                                                    
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
[root@foundation66]# mkfs.xfs /dev/sda1                                            
meta-data=/dev/sda1              isize=256    agcount=8, agsize=16000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=128000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@foundation66]# mount /dev/sda1 /mnt/                                           
[root@foundation66]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3159092   7314808  31% /
devtmpfs          927072       0    927072   0% /dev
tmpfs             942660      80    942580   1% /dev/shm
tmpfs             942660   17028    925632   2% /run
tmpfs             942660       0    942660   0% /sys/fs/cgroup
/dev/sda1         508588   25888    482700   6% /mnt


[root@foundation66 ~]# yum install tree
[root@foundation66 ~]# tree /var/lib/iscsi/                                                      
/var/lib/iscsi/
|-- ifaces
|-- isns
|-- nodes
|   `-- iqn.2018-06.com.example:storage1
|       `-- 172.25.254.166,3260,1
|           `-- default
|-- send_targets
|   `-- 172.25.254.166,3260
|       |-- iqn.2018-06.com.example:storage1,172.25.254.200,3260,1,default -> /var/lib/iscsi/nodes/iqn.2018-06.com.example:storage1/172.25.254.200,3260,1
|       `-- st_config
|-- slp 

可以通过树形结构观察

你可能感兴趣的:(学习日志)