Linux搭建ISCSI服务器

目录

1、简介

2、目标规划

3、 坏境准备

4、配置ISCSI服务端

5、ISCSI服务客户端配置 



1、简介

        iSCSI主要是通过TCP/IP的技术,将存储设备端通过iSCSI target功能,组成可以提供磁盘的服务器端,再通过iSCSI initiator(iSCSI初始化用户)功能,成为能够挂载使用iSCSI target的客户端,如此便能够通过iSCSI协议来进行磁盘的应用。

        iSCSI target:存储设备端,存放磁盘的RAID设备,可将Linux主机仿真为iSCSI target,以供其他主机使用;

        iSCSI initiator:安装iSCSI initiator之后才能够使用target提供的磁盘服务,通常为服务器

2、目标规划

Server主机名

服务端ip

iscsi名称

访问权限控制列表

iscsi01.brick.com

192.168.79.131

iqn.2022-11.com.brick.iscsi01:server

iqn.2022-11.com.brick:client01

192.168.79.134iqn.2022-11.com.brick.iscsi02:serveriqn.2022-11.com.brick:client01
双网卡,主要是模拟2个 iSCSI target存储设备端

Client主机名

客户端ip

访问服务端ip

test01.brick.com

192.168.79.132

192.168.79.131192.168.79.134
模拟连接2个 iSCSI targe

3、 坏境准备

        虚拟机添加一块磁盘,划分为3个LV

[root@localhost ~]# lsblk 
NAME            MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda               8:0    0  30G  0 disk 
├─sda1            8:1    0   1G  0 part /boot
└─sda2            8:2    0  29G  0 part 
  ├─centos-root 253:0    0  27G  0 lvm  /
  └─centos-swap 253:1    0   2G  0 lvm  [SWAP]
sdb               8:16   0  10G  0 disk 
[root@localhost ~]# pvcreate /dev/sdb 
  Physical volume "/dev/sdb" successfully created.
[root@localhost ~]# vgcreate /dev/iscsi01 /dev/sdb 
  Volume group "iscsi01" successfully created
[root@localhost ~]# lvcreate -L 2G -n test1 iscsi01
  Logical volume "test1" created.
[root@localhost ~]# lvcreate -L 2G -n test2 iscsi01
  Logical volume "test2" created.
[root@localhost ~]# lvcreate -l 100%FREE -n test3 iscsi01
  Logical volume "test3" created.
[root@localhost ~]# lvs
  LV    VG      Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root  centos  -wi-ao---- 26.99g                                                    
  swap  centos  -wi-ao----  2.00g                                                    
  test1 iscsi01 -wi-a-----  2.00g                                                    
  test2 iscsi01 -wi-a-----  2.00g                                                    
  test3 iscsi01 -wi-a----- <6.00g                                                    
[root@localhost ~]# lsblk 
NAME            MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda               8:0    0  30G  0 disk 
├─sda1            8:1    0   1G  0 part /boot
└─sda2            8:2    0  29G  0 part 
  ├─centos-root 253:0    0  27G  0 lvm  /
  └─centos-swap 253:1    0   2G  0 lvm  [SWAP]
sdb               8:16   0  10G  0 disk 
├─iscsi01-test1 253:2    0   2G  0 lvm  
├─iscsi01-test2 253:3    0   2G  0 lvm  
└─iscsi01-test3 253:4    0   6G  0 lvm  

4、配置ISCSI服务端

[root@localhost ~]# mount /dev/cdrom /mnt/  #挂载镜像
mount: /dev/sr0 写保护,将以只读方式挂载 
[root@localhost ~]# mkdir -p /etc/yum.repos.d/back 
[root@localhost ~]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/back/ #对原有的yum源进行备份
[root@localhost ~]# cd /etc/yum.repos.d/back/
[root@localhost back]# cp CentOS-Base.repo ../
[root@localhost back]# cd ../
[root@localhost yum.repos.d]# vi CentOS-Base.repo #修改yum源
[root@localhost yum.repos.d]# cat CentOS-Base.repo 
[root@test01 mnt]# cat /etc/yum.repos.d/CentOS-Base.repo 
[base]
name=CentOS-Base
baseurl=file:///mnt
gpgcheck=1
gpgkey=file:///RPM-GPG-KEY-CentOS-7



[root@localhost yum.repos.d]# yum clean all
已加载插件:fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
正在清理软件源: base
Cleaning up list of fastest mirrors
Other repos take up 156 M of disk space (use --verbose for details)
[root@localhost yum.repos.d]# yum repolist
已加载插件:fastestmirror
Determining fastest mirrors
base                                                                                                                                | 3.6 kB  00:00:00     
(1/2): base/group_gz                                                                                                                | 153 kB  00:00:00     
(2/2): base/primary_db                                                                                                              | 6.1 MB  00:00:00     
源标识                                                                源名称                                                                         状态
base                                                                  CentOS-7 - Base                                                                10,073
repolist: 10,073
[root@localhost yum.repos.d]# yum install targetcli -y  #安装ISCSI服务端
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 targetcli.noarch.0.2.1.53-1.el7_9 将被 安装
--> 正在处理依赖关系 python-rtslib >= 2.1.fb41,它被软件包 targetcli-2.1.53-1.el7_9.noarch 需要
--> 正在处理依赖关系 python-ethtool,它被软件包 targetcli-2.1.53-1.el7_9.noarch 需要
--> 正在处理依赖关系 python-configshell,它被软件包 targetcli-2.1.53-1.el7_9.noarch 需要
--> 正在检查事务
---> 软件包 python-configshell.noarch.1.1.1.26-1.el7 将被 安装
--> 正在处理依赖关系 python-urwid,它被软件包 1:python-configshell-1.1.26-1.el7.noarch 需要
--> 正在处理依赖关系 python-six,它被软件包 1:python-configshell-1.1.26-1.el7.noarch 需要
--> 正在处理依赖关系 pyparsing,它被软件包 1:python-configshell-1.1.26-1.el7.noarch 需要
---> 软件包 python-ethtool.x86_64.0.0.8-8.el7 将被 安装
--> 正在处理依赖关系 libnl.so.1()(64bit),它被软件包 python-ethtool-0.8-8.el7.x86_64 需要
---> 软件包 python-rtslib.noarch.0.2.1.74-1.el7_9 将被 安装
--> 正在处理依赖关系 python-kmod,它被软件包 python-rtslib-2.1.74-1.el7_9.noarch 需要
--> 正在检查事务
---> 软件包 libnl.x86_64.0.1.1.4-3.el7 将被 安装
---> 软件包 pyparsing.noarch.0.1.5.6-9.el7 将被 安装
---> 软件包 python-kmod.x86_64.0.0.9-4.el7 将被 安装
---> 软件包 python-six.noarch.0.1.9.0-2.el7 将被 安装
---> 软件包 python-urwid.x86_64.0.1.1.1-3.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

===========================================================================================================================================================
 Package                                     架构                            版本                                      源                             大小
===========================================================================================================================================================
正在安装:
 targetcli                                   noarch                          2.1.53-1.el7_9                            base                           75 k
为依赖而安装:
 libnl                                       x86_64                          1.1.4-3.el7                               base                          128 k
 pyparsing                                   noarch                          1.5.6-9.el7                               base                           94 k
 python-configshell                          noarch                          1:1.1.26-1.el7                            base                           68 k
 python-ethtool                              x86_64                          0.8-8.el7                                 base                           34 k
 python-kmod                                 x86_64                          0.9-4.el7                                 base                           57 k
 python-rtslib                               noarch                          2.1.74-1.el7_9                            base                          104 k
 python-six                                  noarch                          1.9.0-2.el7                               base                           29 k
 python-urwid                                x86_64                          1.1.1-3.el7                               base                          654 k

事务概要
===========================================================================================================================================================
安装  1 软件包 (+8 依赖软件包)

总下载量:1.2 M
安装大小:5.2 M
Downloading packages:
-----------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                       5.8 MB/s | 1.2 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 数据库已被非 yum 程序修改。
  正在安装    : python-six-1.9.0-2.el7.noarch                                                                                                          1/9 
  正在安装    : pyparsing-1.5.6-9.el7.noarch                                                                                                           2/9 
  正在安装    : python-kmod-0.9-4.el7.x86_64                                                                                                           3/9 
  正在安装    : python-rtslib-2.1.74-1.el7_9.noarch                                                                                                    4/9 
  正在安装    : libnl-1.1.4-3.el7.x86_64                                                                                                               5/9 
  正在安装    : python-ethtool-0.8-8.el7.x86_64                                                                                                        6/9 
  正在安装    : python-urwid-1.1.1-3.el7.x86_64                                                                                                        7/9 
  正在安装    : 1:python-configshell-1.1.26-1.el7.noarch                                                                                               8/9 
  正在安装    : targetcli-2.1.53-1.el7_9.noarch                                                                                                        9/9 
  验证中      : python-urwid-1.1.1-3.el7.x86_64                                                                                                        1/9 
  验证中      : libnl-1.1.4-3.el7.x86_64                                                                                                               2/9 
  验证中      : python-ethtool-0.8-8.el7.x86_64                                                                                                        3/9 
  验证中      : python-rtslib-2.1.74-1.el7_9.noarch                                                                                                    4/9 
  验证中      : targetcli-2.1.53-1.el7_9.noarch                                                                                                        5/9 
  验证中      : python-kmod-0.9-4.el7.x86_64                                                                                                           6/9 
  验证中      : python-six-1.9.0-2.el7.noarch                                                                                                          7/9 
  验证中      : pyparsing-1.5.6-9.el7.noarch                                                                                                           8/9 
  验证中      : 1:python-configshell-1.1.26-1.el7.noarch                                                                                               9/9 

已安装:
  targetcli.noarch 0:2.1.53-1.el7_9                                                                                                                        

作为依赖被安装:
  libnl.x86_64 0:1.1.4-3.el7       pyparsing.noarch 0:1.5.6-9.el7          python-configshell.noarch 1:1.1.26-1.el7   python-ethtool.x86_64 0:0.8-8.el7  
  python-kmod.x86_64 0:0.9-4.el7   python-rtslib.noarch 0:2.1.74-1.el7_9   python-six.noarch 0:1.9.0-2.el7            python-urwid.x86_64 0:1.1.1-3.el7  

完毕!
[root@localhost yum.repos.d]# systemctl start target #启动
[root@localhost yum.repos.d]# systemctl status target #查询服务状态
● target.service - Restore LIO kernel target configuration
   Loaded: loaded (/usr/lib/systemd/system/target.service; disabled; vendor preset: disabled)
   Active: active (exited) since 日 2022-11-13 00:22:34 CST; 9s ago
  Process: 1388 ExecStart=/usr/bin/targetctl restore (code=exited, status=0/SUCCESS)
 Main PID: 1388 (code=exited, status=0/SUCCESS)

11月 13 00:22:34 localhost.localdomain systemd[1]: Starting Restore LIO kernel target configuration...
11月 13 00:22:34 localhost.localdomain target[1388]: No saved config file at /etc/target/saveconfig.json, ok, exiting
11月 13 00:22:34 localhost.localdomain systemd[1]: Started Restore LIO kernel target configuration.
[root@localhost yum.repos.d]# systemctl enable target #开机自启
Created symlink from /etc/systemd/system/multi-user.target.wants/target.service to /usr/lib/systemd/system/target.service.
[root@localhost yum.repos.d]# systemctl disable firewalld #开机补启动防火墙
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost yum.repos.d]# systemctl stop  firewalld  #关闭防火墙
[root@localhost yum.repos.d]# vi /etc/selinux/config #关闭selinux
[root@localhost yum.repos.d]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 
[root@localhost yum.repos.d]# hostnamectl set-hostname iscsi01.brick.com  #修改主机名
[root@localhost yum.repos.d]# init 6 #重启
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(inter_test_192.168.79.131) at 00:28:25.

Type `help' to learn how to use Xshell prompt.

[root@iscsi01 /]# targetcli
Warning: Could not load preferences file /root/.targetcli/prefs.bin.
targetcli shell version 2.1.53
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- ramdisk .............................................. [Storage Objects: 0]
  o- iscsi .......................................................... [Targets: 0]
  o- loopback ....................................................... [Targets: 0]
/>  

注:targetcli是用于管理iSCSI服务端存储资源的专用配置命令,它提供交互式配置功能,将iSCSI共享资源的配置内容抽象成“目录”的形式,只需将各类配置信息填入到相应的“目录”中即可。
#以下步骤涉及到的目录
 /backstores/block:iSCSI服务端配置共享设备的位置(在这里添加要共享的磁盘设备并重命名)

/> backstores/block create test1 /dev/iscsi01/test1
Created block storage object test1 using /dev/iscsi01/test1.
/> backstores/block create test2 /dev/iscsi01/test2
Created block storage object test2 using /dev/iscsi01/test2.
/> backstores/block create test3 /dev/iscsi01/test3
Created block storage object test3 using /dev/iscsi01/test3.
/> ls
o- / ......................................................................................................................... [...]
  o- backstores .............................................................................................................. [...]
  | o- block .................................................................................................. [Storage Objects: 3]
  | | o- test1 ................................................................ [/dev/iscsi01/test1 (2.0GiB) write-thru deactivated]
  | | | o- alua ................................................................................................... [ALUA Groups: 1]
  | | |   o- default_tg_pt_gp ....................................................................... [ALUA state: Active/optimized]
  | | o- test2 ................................................................ [/dev/iscsi01/test2 (2.0GiB) write-thru deactivated]
  | | | o- alua ................................................................................................... [ALUA Groups: 1]
  | | |   o- default_tg_pt_gp ....................................................................... [ALUA state: Active/optimized]
  | | o- test3 ................................................................ [/dev/iscsi01/test3 (6.0GiB) write-thru deactivated]
  | |   o- alua ................................................................................................... [ALUA Groups: 1]
  | |     o- default_tg_pt_gp ....................................................................... [ALUA state: Active/optimized]
  | o- fileio ................................................................................................. [Storage Objects: 0]
  | o- pscsi .................................................................................................. [Storage Objects: 0]
  | o- ramdisk ................................................................................................ [Storage Objects: 0]
  o- iscsi ............................................................................................................ [Targets: 0]
  o- loopback ......................................................................................................... [Targets: 0]
/> 

注:/iscsi/iqn......xxx/tpg1:通过在/iscsi目录执行create命令生成iSCSI target名称时自动生成的与iSCSI target名称同名的目录的子目录
acls:用于存放能够访问iSCSI服务端共享存储资源的验证信息
luns:用于存放可共享的硬盘设备(/backstores/block只是一个预备资源池,但没声明可共享)
portals:用于存放iscsi服务端的ip地址5

/> #模拟两台ISCSI服务器
/> iscsi/ create iqn.2022-11.com.brick.iscsi01:server
Created target iqn.2022-11.com.brick.iscsi01:server.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.
/> iscsi/ create iqn.2022-11.com.brick.iscsi02:server
Created target iqn.2022-11.com.brick.iscsi02:server.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.
/> #创建ACL控制列表,两台服务的列表名称必须一样,否则客户端无法连接两台服务器
/> iscsi/iqn.2022-11.com.brick.iscsi01:server/tpg1/acls create iqn.2022-11.com.brick:client01
Created Node ACL for iqn.2022-11.com.brick:client01
/> iscsi/iqn.2022-11.com.brick.iscsi01:server/tpg1/luns create /b
/backstores/block/test1  /backstores/block/test2  /backstores/block/test3  /bin/                    /boot/                   
/> iscsi/iqn.2022-11.com.brick.iscsi01:server/tpg1/luns create /backstores/block/test1
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.2022-11.com.brick:client01
/> iscsi/iqn.2022-11.com.brick.iscsi01:server/tpg1/luns create /backstores/block/test2
Created LUN 1.
Created LUN 1->1 mapping in node ACL iqn.2022-11.com.brick:client01
/> iscsi/iqn.2022-11.com.brick.iscsi02:server/tpg1/acls create iqn.2022-11.com.brick:client01
Created Node ACL for iqn.2022-11.com.brick:client01
/> iscsi/iqn.2022-11.com.brick.iscsi02:server/tpg1/luns create /backstores/block/test3
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.2022-11.com.brick:client01
/> #修改portals
/> iscsi/iqn.2022-11.com.brick.iscsi01:server/tpg1/portals/ delete 0.0.0.0 3260
Deleted network portal 0.0.0.0:3260
/> iscsi/iqn.2022-11.com.brick.iscsi02:server/tpg1/portals/ delete 0.0.0.0 3260
Deleted network portal 0.0.0.0:3260
/> iscsi/iqn.2022-11.com.brick.iscsi01:server/tpg1/portals/ create 192.168.79.131 3260
Using default IP port 3260
Created network portal 192.168.79.131:3260.
/> iscsi/iqn.2022-11.com.brick.iscsi02:server/tpg1/portals/ create 192.168.79.134 3260
Using default IP port 3260
Created network portal 192.168.79.134:3260.

/> #最初始化的/> ls
o- / ......................................................................................................................... [...]
  o- backstores .............................................................................................................. [...]
  | o- block .................................................................................................. [Storage Objects: 3]
  | | o- test1 .................................................................. [/dev/iscsi01/test1 (2.0GiB) write-thru activated]
  | | | o- alua ................................................................................................... [ALUA Groups: 1]
  | | |   o- default_tg_pt_gp ....................................................................... [ALUA state: Active/optimized]
  | | o- test2 .................................................................. [/dev/iscsi01/test2 (2.0GiB) write-thru activated]
  | | | o- alua ................................................................................................... [ALUA Groups: 1]
  | | |   o- default_tg_pt_gp ....................................................................... [ALUA state: Active/optimized]
  | | o- test3 .................................................................. [/dev/iscsi01/test3 (6.0GiB) write-thru activated]
  | |   o- alua ................................................................................................... [ALUA Groups: 1]
  | |     o- default_tg_pt_gp ....................................................................... [ALUA state: Active/optimized]
  | o- fileio ................................................................................................. [Storage Objects: 0]
  | o- pscsi .................................................................................................. [Storage Objects: 0]
  | o- ramdisk ................................................................................................ [Storage Objects: 0]
  o- iscsi ............................................................................................................ [Targets: 2]
  | o- iqn.2022-11.com.brick.iscsi01:server .............................................................................. [TPGs: 1]
  | | o- tpg1 ............................................................................................... [no-gen-acls, no-auth]
  | |   o- acls .......................................................................................................... [ACLs: 1]
  | |   | o- iqn.2022-11.com.brick:client01 ....................................................................... [Mapped LUNs: 2]
  | |   |   o- mapped_lun0 ................................................................................. [lun0 block/test1 (rw)]
  | |   |   o- mapped_lun1 ................................................................................. [lun1 block/test2 (rw)]
  | |   o- luns .......................................................................................................... [LUNs: 2]
  | |   | o- lun0 ............................................................ [block/test1 (/dev/iscsi01/test1) (default_tg_pt_gp)]
  | |   | o- lun1 ............................................................ [block/test2 (/dev/iscsi01/test2) (default_tg_pt_gp)]
  | |   o- portals .................................................................................................... [Portals: 1]
  | |     o- 192.168.79.131:3260 .............................................................................................. [OK]
  | o- iqn.2022-11.com.brick.iscsi02:server .............................................................................. [TPGs: 1]
  |   o- tpg1 ............................................................................................... [no-gen-acls, no-auth]
  |     o- acls .......................................................................................................... [ACLs: 1]
  |     | o- iqn.2022-11.com.brick:client01 ....................................................................... [Mapped LUNs: 1]
  |     |   o- mapped_lun0 ................................................................................. [lun0 block/test3 (rw)]
  |     o- luns .......................................................................................................... [LUNs: 1]
  |     | o- lun0 ............................................................ [block/test3 (/dev/iscsi01/test3) (default_tg_pt_gp)]
  |     o- portals .................................................................................................... [Portals: 1]
  |       o- 192.168.79.134:3260 .............................................................................................. [OK]
  o- loopback ......................................................................................................... [Targets: 0]
/> #配置完成
/> saveconfig 
Configuration saved to /etc/target/saveconfig.json
/> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup/.
Configuration saved to /etc/target/saveconfig.json

5、ISCSI服务客户端配置 

[root@localhost ~]# #修改主机名
[root@localhost ~]# hostnamectl set-hostname test01.brick.com
[root@localhost ~]# #关闭防火墙
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# systemctl stop  firewalld
[root@localhost ~]# #关闭Selinux         
[root@localhost ~]# vi /etc/selinux/config 
[root@localhost ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

[root@localhost ~]# init 6
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(test1_192.168.79.132) at 12:56:26.

Type `help' to learn how to use Xshell prompt.

[root@test01 ~]# #挂载yum源
[root@test01 ~]# mount /dev/cdrom /mnt
mount: 在 /dev/sr0 上找不到媒体
[root@test01 ~]# #查看虚拟机是否连接ISO镜像
[root@test01 ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 写保护,将以只读方式挂载
[root@test01 ~]# rm -rf /etc/yum.repos.d/*
[root@test01 ~]# cd /etc/yum.repos.d/
[root@test01 yum.repos.d]# scp root@192.168.79.131:/etc/yum.
yum.conf     yum.repos.d/ 
[root@test01 yum.repos.d]# scp root@192.168.79.131:/etc/yum.repos.d/*.repo ./
The authenticity of host '192.168.79.131 (192.168.79.131)' can't be established.
ECDSA key fingerprint is SHA256:RqX2HnBB90KNzIrKyY7yCTJRMwibwXa8rvZ1BOqizLk.
ECDSA key fingerprint is MD5:12:76:62:cb:44:18:7a:92:fd:45:bc:84:8d:bd:dd:54.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.79.131' (ECDSA) to the list of known hosts.
root@192.168.79.131's password: 
CentOS-Base.repo                                                                                                         100%  537   200.5KB/s   00:00    
[root@test01 yum.repos.d]# cat CentOS-Base.repo 
[root@test01 mnt]# cat /etc/yum.repos.d/CentOS-Base.repo 
[base]
name=CentOS-Base
baseurl=file:///mnt
gpgcheck=1
gpgkey=file:///RPM-GPG-KEY-CentOS-7

[root@test01 yum.repos.d]# #也可以自己创建一个文件,按照格式书写。


[root@test01 mnt]# yum install iscsi-initiator-utils
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 iscsi-initiator-utils.x86_64.0.6.2.0.874-22.el7_9 将被 安装
--> 正在处理依赖关系 iscsi-initiator-utils-iscsiuio >= 6.2.0.874-22.el7_9,它被软件包 iscsi-initiator-utils-6.2.0.874-22.el7_9.x86_64 需要
--> 正在检查事务
---> 软件包 iscsi-initiator-utils-iscsiuio.x86_64.0.6.2.0.874-22.el7_9 将被 安装
--> 解决依赖关系完成

依赖关系解决

===========================================================================================================================================================
 Package                                             架构                        版本                                      源                         大小
===========================================================================================================================================================
正在安装:
 iscsi-initiator-utils                               x86_64                      6.2.0.874-22.el7_9                        base                      423 k
为依赖而安装:
 iscsi-initiator-utils-iscsiuio                      x86_64                      6.2.0.874-22.el7_9                        base                       94 k

事务概要
===========================================================================================================================================================
安装  1 软件包 (+1 依赖软件包)

总下载量:517 k
安装大小:2.5 M
Is this ok [y/d/N]: Y
Downloading packages:
-----------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                        92 MB/s | 517 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : iscsi-initiator-utils-iscsiuio-6.2.0.874-22.el7_9.x86_64                                                                               1/2 
  正在安装    : iscsi-initiator-utils-6.2.0.874-22.el7_9.x86_64                                                                                        2/2 
  验证中      : iscsi-initiator-utils-6.2.0.874-22.el7_9.x86_64                                                                                        1/2 
  验证中      : iscsi-initiator-utils-iscsiuio-6.2.0.874-22.el7_9.x86_64                                                                               2/2 

已安装:
  iscsi-initiator-utils.x86_64 0:6.2.0.874-22.el7_9                                                                                                        

作为依赖被安装:
  iscsi-initiator-utils-iscsiuio.x86_64 0:6.2.0.874-22.el7_9                                                                                               

完毕!
[root@test01 mnt]# 添加acl访问控制列表
[root@test01 mnt]# echo InitiatorName=iqn.2022-11.com.brick:client01 > /etc/iscsi/initiatorname.iscsi 
[root@test01 mnt]# cat /etc/iscsi/initiatorname.iscsi 
InitiatorName=iqn.2022-11.com.brick:client01
[root@test01 mnt]# systemctl restart iscsid
[root@test01 mnt]# systemctl enable  iscsid
Created symlink from /etc/systemd/system/multi-user.target.wants/iscsid.service to /usr/lib/systemd/system/iscsid.service.
[root@test01 mnt]# systemctl status  iscsid
● iscsid.service - Open-iSCSI
   Loaded: loaded (/usr/lib/systemd/system/iscsid.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2022-11-13 13:15:03 CST; 11s ago
     Docs: man:iscsid(8)
           man:iscsiuio(8)
           man:iscsiadm(8)
 Main PID: 1343 (iscsid)
   Status: "Ready to process requests"
   CGroup: /system.slice/iscsid.service
           └─1343 /sbin/iscsid -f

11月 13 13:15:03 test01.brick.com systemd[1]: Starting Open-iSCSI...
11月 13 13:15:03 test01.brick.com systemd[1]: Started Open-iSCSI.

iscsiadm命令详解

iscsiadm是个模式化的工具,其模式可通过-m或--mode选项指定,

常见的模式有discoverydb、node、fw、session、host、iface几个,

如果没有额外指定其它选项,则discoverydb和node会显示其相关的所有记录;

session用于显示所有的活动会话和连接,fw显示所有的启动固件值,host显示所有的iSCSI主机,

iface显示/var/lib/iscsi/ifaces目录中的所有ifaces设定。

-l --login

登入节点(服务器)

-t --type=type

这里可以使用的类型为sendtargets(可简写为st)、slp、fw和 isns,此选项仅用于discovery模式,且目前仅支持st、fw和isns;其中st表示允许每个iSCSItarget发送一个可用target列表给initiator;

-p --portal=ip[:port]

指定target服务的IP和端口

-m --mode 

可用的mode有discovery, node, fw, host iface 和 session

-T --targetname=targetname

用于指定target的名字

-u --logout

登出节点(服务器)

-o --op=OPEARTION

指定针对discoverydb数据库的操作,其仅能为new、delete、update、show和nonpersistent其中之一

[root@test01 ~]# iscsiadm -m discovery -t st -p 192.168.79.131 #查询发现记录
192.168.79.131:3260,1 iqn.2022-11.com.brick.iscsi01:server
192.168.79.134:3260,1 iqn.2022-11.com.brick.iscsi02:server
[root@test01 ~]# iscsiadm -m node show #查看iscsi发现记录,show 可以忽略
192.168.79.131:3260,1 iqn.2022-11.com.brick.iscsi01:server
192.168.79.134:3260,1 iqn.2022-11.com.brick.iscsi02:server
[root@test01 ~]# iscsiadm -m node -o delete -T iqn.2022-11.com.brick.iscsi02:server #删除目标iscsi发现记录
[root@test01 ~]# iscsiadm -m node -o delete #删除所有目标
[root@test01 ~]# iscsiadm -m node -T iqn.2022-11.com.brick.iscsi01:server -p 192.168.79.131  -l #登陆记录
[root@test01 ~]# iscsiadm -m node -L all #登录所有记录
[root@test01 ~]# iscsiadm -m node -u #退出所有登陆
[root@test01 ~]# iscsiadm -m session #查看连接记录
[root@test01 ~]# iscsiadm -m node -o show -T iqn.2022-11.com.brick.iscsi01:server #查看数据结构的树状信息
[root@test01 ~]# iscsiadm -m node –T iqn.2022-11.com.brick.iscsi01:server -p 192.168.79.131 -o update -n node.startup -v automatic  #开机自动登陆或者修改/etc/iscsi/iscsid.conf文件,将:#node.startup = automatic 一行前面的#去掉改成node.startup = automatic

#操作步骤  1发现2登录3开机启动5挂载硬盘6配置开机自动挂载
[root@test01 ~]# iscsiadm -m discovery -t st -p 192.168.79.131 #发现目标
192.168.79.131:3260,1 iqn.2022-11.com.brick.iscsi01:server
192.168.79.134:3260,1 iqn.2022-11.com.brick.iscsi02:server
[root@test01 ~]# iscsiadm -m node -T iqn.2022-11.com.brick.iscsi01:server -p 192.168.79.131  -l #登陆目标
Logging in to [iface: default, target: iqn.2022-11.com.brick.iscsi01:server, portal: 192.168.79.131,3260] (multiple)
Login to [iface: default, target: iqn.2022-11.com.brick.iscsi01:server, portal: 192.168.79.131,3260] successful.
[root@test01 ~]# iscsiadm -m session
tcp: [13] 192.168.79.131:3260,1 iqn.2022-11.com.brick.iscsi01:server (non-flash)
[root@test01 ~]# iscsiadm -m node –T iqn.2022-11.com.brick.iscsi01:server -p 192.168.79.131 -o update -n node.startup -v automatic  #开机自动登陆
[root@test01 ~]# iscsiadm -m session
tcp: [13] 192.168.79.131:3260,1 iqn.2022-11.com.brick.iscsi01:server (non-flash)
[root@test01 ~]# lsblk #查看是否成功,多了 /dev/sdb /dev/sdc
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0    2G  0 disk 
sdc               8:32   0    2G  0 disk 
sr0              11:0    1  9.6G  0 rom
[root@test01 ~]# init 6 #重启确认是否成功
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(test1_192.168.79.132) at 16:16:10.

Type `help' to learn how to use Xshell prompt.
Connecting to 192.168.79.132:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Sun Nov 13 16:21:36 2022 from 192.168.79.1
[root@test01 ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0    2G  0 disk 
sdc               8:32   0    6G  0 disk 
sdd               8:48   0    2G  0 disk 
sr0              11:0    1  9.6G  0 rom

 

6、ISCSI认证

后面有时间更新

  • 1
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: 要在Linux搭建iSCSI服务器,需要进行以下步骤: 1. 安装iSCSI软件包:在Linux系统上安装iSCSI软件包,例如open-iscsiiscsi-target。 2. 配置iSCSI Target:使用iscsi-target软件包创建iSCSI Target,配置iSCSI Target的名称、IP地址、端口等信息。 3. 创建iSCSI LUN:使用iscsi-target软件包创建iSCSI LUN(逻辑单元),并将其映射到物理存储设备上。 4. 配置iSCSI Initiator:在客户端上安装iSCSI Initiator软件包,并配置iSCSI Initiator的名称、IP地址、端口等信息。 5. 连接iSCSI Target:在客户端上使用iSCSI Initiator连接iSCSI Target,并将iSCSI LUN映射到客户端上。 6. 测试iSCSI连接:使用客户端上的工具测试iSCSI连接是否正常,例如使用dd命令在iSCSI LUN上创建文件。 以上是在Linux搭建iSCSI服务器的基本步骤,具体操作可以参考相关文档或教程。 ### 回答2: 先介绍一下iSCSI的基本概念吧:iSCSI是一种存储协议,它是一种基于网络的存储协议,通过网络将远程存储设备映射到本地主机上,使得本地主机可以像使用本地硬盘一样使用远程存储设备。iSCSI协议在网络存储领域应用广泛,特别是在虚拟化环境下,iSCSI存储是实现共享存储的重要方式之一。 搭建iSCSI服务器需要准备两个方面的工作,第一是准备一个可用的Linux服务器,第二是安装和配置iSCSI软件。 一、准备Linux服务器 1. 首先要安装Linux系统,这里我们以CentOS系统为例; 2. 确认服务器硬件配置符合iSCSI的要求; 3. 安装和配置网络环境; 4. 创建一个LUN(Logical Unit Number)存储单元,这是iSCSI存储的最小单位。 二、安装和配置iSCSI软件 1. 安装iSCSI软件,Linux下主流的iSCSI软件有多种,比如open-iscsi、tgt等,这里我们以open-iscsi为例; 2. 配置iSCSI服务器参数,包括iSCSI服务名称、存储密码等; 3. 创建iSCSI Target,这是iSCSI存储的核心部分,它是定义好的LUN逻辑单元映射到物理存储单元的过程; 4. 配置iSCSI Target的参数,包括存储IP地址、端口等; 5. 配置iSCSI Initiator,这是客户端连接iSCSI Target的过程; 6. 测试iSCSI存储的可用性。 综上,搭建iSCSI服务器并不是很复杂,但是需要较多的Linux操作技能。如果您不熟悉Linux操作,建议参考相关的教程或请专业技术人员操作。 ### 回答3: iSCSI是基于以太网的存储网络协议,可以将存储器虚拟化为IP网络资源。Linux搭建iSCSI服务器可以提供高效、稳定、灵活的存储资源服务,并且可以支持多种操作系统的访问,如Windows、Linux、Unix等。 步骤如下: 1. 安装iSCSI-target软件 Linux上的iSCSI-target软件有多种可选,如SCST、tgt、LIO等。在这里以tgt为例,它是Linux下使用最广泛的一个iSCSI-target软件。 在Ubuntu中,可以使用命令sudo apt-get install tgt安装tgt软件包。在CentOS中,可以使用命令yum install scsi-target-utils安装。 2. 配置tgt tgt的配置文件为/etc/tgt/targets.conf,在配置文件中,首先需要定义一个iSCSI Target,如下所示: <target iqn.2019-12.com.example:server.target1> backing-store /dev/sdb # 这里指定了iSCSI Target的存储设备 initiator-address 192.168.0.10 # 指定发起者的IP地址 incominguser user password # 指定iSCSI登录的用户名和密码 </target> 以上配置文件表示定义了一个名称为iqn.2019-12.com.example:server.target1的iSCSI Target,使用/dev/sdb设备作为存储设备,指定发起者IP地址为192.168.0.10。同时定义了一个iSCSI登录的用户名和密码。 3. 启动tgt服务 在tgt服务配置好后,需要使用命令systemctl start tgt启动tgt服务,使之生效。同时可以使用命令systemctl enable tgt设置开机启动服务。 4. 配置发起者 在Windows中,可以使用Microsoft iSCSI Initiator软件来作为iSCSI客户端进行连接。在Linux中,可以使用open-iscsi软件包来配置iSCSI客户端。 在Ubuntu中,可以使用命令sudo apt-get install open-iscsi安装open-iscsi软件包,在CentOS中,可以使用命令yum install iscsi-initiator-utils安装。 在配置iSCSI Initiator时,需要指定以下参数: - 目标iSCSI Target的名称或IP地址 - 登录iSCSI Target时需要用到的用户名和密码 - iSCSI Target的LUN ID(逻辑单元号),表示iSCSI Target中的哪个存储设备 对于open-iscsi,在Ubuntu中配置文件为/etc/iscsi/initiatorname.iscsi,在CentOS中为/etc/iscsi/initiatorname.iscsi,在配置文件中定义以下配置项: InitiatorName=iqn.2020-01.com.example:client # 定义iSCSI Initiator的名称 NodeStartUp=automatic # 设置开机启动 在配置好iSCSI Initiator后,可以使用命令iscsiadm -m discovery -P1来发现iSCSI Target。使用命令iscsiadm -m node -l可以登录iSCSI Target,并使用命令lsblk来查看iSCSI Target中的存储设备。 总结 搭建Linux iSCSI服务器并不复杂,需要操作者掌握基本的Linux系统操作和熟悉iSCSI的相关知识。通过一个简单的例子,我们可以了解到如何安装iSCSI-target软件、配置tgt,并使用iSCSI Initiator来连接iSCSI Target。同时,还需注意一些安全和性能的问题,如防火墙设置、安全登录、并发连接等,以保障服务的可靠性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

multis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值