IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    在 CentOS8 搭建 iSCSI 服务器

    Teacher Du发表于 2024-11-01 03:51:26
    love 0

    在前文中,我们讲解了 iSCSI 相关概念。本文将展示如何在 CentOS8 上配置 iSCSI 存储服务器,如遇任何问题,欢迎在评论区与杜老师交流,让我们开始吧!

    服务安装

    使用 dnf 命令安装 targetcli:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    [root@localhost ~]# dnf -y install targetcli
    上次元数据过期检查:1:49:08 前,执行于 2021年01月24日 星期日 01时34分43秒。
    依赖关系解决。
    ================================================================================
    软件包 架构 版本 仓库 大小
    ================================================================================
    安装:
    targetcli noarch 2.1.53-1.el8 appstream 80 k
    安装依赖关系:
    python3-configshell noarch 1:1.1.28-1.el8 baseos 72 k
    python3-kmod x86_64 0.9-20.el8 baseos 90 k
    python3-pyparsing noarch 2.1.10-7.el8 baseos 142 k
    python3-rtslib noarch 2.1.73-2.el8 baseos 102 k
    python3-urwid x86_64 1.3.1-4.el8 baseos 783 k
    target-restore noarch 2.1.73-2.el8 baseos 24 k

    事务概要
    ================================================================================
    安装 7 软件包

    总下载:1.3 M
    安装大小:4.7 M
    下载软件包:
    (1/7): targetcli-2.1.53-1.el8.noarch.rpm 68 kB/s | 80 kB 00:01
    (2/7): python3-pyparsing-2.1.10-7.el8.noarch.rp 538 kB/s | 142 kB 00:00
    (3/7): python3-configshell-1.1.28-1.el8.noarch. 50 kB/s | 72 kB 00:01
    (4/7): python3-kmod-0.9-20.el8.x86_64.rpm 62 kB/s | 90 kB 00:01
    (5/7): python3-rtslib-2.1.73-2.el8.noarch.rpm 1.1 MB/s | 102 kB 00:00
    (6/7): target-restore-2.1.73-2.el8.noarch.rpm 319 kB/s | 24 kB 00:00
    (7/7): python3-urwid-1.3.1-4.el8.x86_64.rpm 3.7 MB/s | 783 kB 00:00
    --------------------------------------------------------------------------------
    总计 449 kB/s | 1.3 MB 00:02
    运行事务检查
    事务检查成功。
    运行事务测试
    事务测试成功。
    运行事务
    准备中 : 1/1
    安装 : python3-urwid-1.3.1-4.el8.x86_64 1/7
    安装 : python3-pyparsing-2.1.10-7.el8.noarch 2/7
    安装 : python3-configshell-1:1.1.28-1.el8.noarch 3/7
    安装 : python3-kmod-0.9-20.el8.x86_64 4/7
    安装 : python3-rtslib-2.1.73-2.el8.noarch 5/7
    安装 : target-restore-2.1.73-2.el8.noarch 6/7
    运行脚本: target-restore-2.1.73-2.el8.noarch 6/7
    安装 : targetcli-2.1.53-1.el8.noarch 7/7
    运行脚本: targetcli-2.1.53-1.el8.noarch 7/7
    验证 : targetcli-2.1.53-1.el8.noarch 1/7
    验证 : python3-configshell-1:1.1.28-1.el8.noarch 2/7
    验证 : python3-kmod-0.9-20.el8.x86_64 3/7
    验证 : python3-pyparsing-2.1.10-7.el8.noarch 4/7
    验证 : python3-rtslib-2.1.73-2.el8.noarch 5/7
    验证 : python3-urwid-1.3.1-4.el8.x86_64 6/7
    验证 : target-restore-2.1.73-2.el8.noarch 7/7
    Installed products updated.

    已安装:
    python3-configshell-1:1.1.28-1.el8.noarch python3-kmod-0.9-20.el8.x86_64
    python3-pyparsing-2.1.10-7.el8.noarch python3-rtslib-2.1.73-2.el8.noarch
    python3-urwid-1.3.1-4.el8.x86_64 target-restore-2.1.73-2.el8.noarch
    targetcli-2.1.53-1.el8.noarch

    完毕!

    输入 targetcli 命令后会提示配置文件不存在,使用 exit 会自动生成配置文件:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@localhost ~]# 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'.

    /> exit
    Global pref auto_save_on_exit=true
    Configuration saved to /etc/target/saveconfig.json

    服务启动

    启动服务:

    1
    [root@localhost ~]# systemctl start target

    设置开机启动:

    1
    2
    [root@localhost ~]# systemctl enable target
    Created symlink /etc/systemd/system/multi-user.target.wants/target.service → /usr/lib/systemd/system/target.service.

    创建存储

    这里我们以/dusays/目录为例,使用命令 /backstores/fileio create dusays /dusays/dusays.img 1G 创建名为 dusays 的 1G 大小存储文件,创建后通过 ls 查看执行结果:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    [root@localhost ~]# mkdir /dusays
    [root@localhost ~]# targetcli
    targetcli shell version 2.1.53
    Copyright 2011-2013 by Datera, Inc and others.
    For help on commands, type 'help'.

    /> backstores/fileio/ create www /dusays/www.img 1G
    Created fileio www with size 1073741824
    /> ls
    o- / ..................................................................... [...]
    o- backstores .......................................................... [...]
    | o- block .............................................. [Storage Objects: 0]
    | o- fileio ............................................. [Storage Objects: 1]
    | | o- www ................. [/dusays/www.img (1.0GiB) write-back deactivated]
    | | o- alua ............................................... [ALUA Groups: 1]
    | | o- default_tg_pt_gp ................... [ALUA state: Active/optimized]
    | o- pscsi .............................................. [Storage Objects: 0]
    | o- ramdisk ............................................ [Storage Objects: 0]
    o- iscsi ........................................................ [Targets: 0]
    o- loopback ..................................................... [Targets: 0]

    接下来我们创建名为 iqn.2021-01.com.dusays:www 的 iSCSI 文件,一般命名规则为 iqn.年份-月份.网址倒叙:存储文件名称,完整命令为 /iscsi create iqn.2021-01.com.dusays:www,创建后通过 ls 查看执行结果:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    /> iscsi/ create iqn.2021-01.com.dusays:www
    Created target iqn.2021-01.com.dusays:www.
    Created TPG 1.
    Global pref auto_add_default_portal=true
    Created default portal listening on all IPs (0.0.0.0), port 3260.
    /> ls
    o- / ..................................................................... [...]
    o- backstores .......................................................... [...]
    | o- block .............................................. [Storage Objects: 0]
    | o- fileio ............................................. [Storage Objects: 1]
    | | o- www ................. [/dusays/www.img (1.0GiB) write-back deactivated]
    | | o- alua ............................................... [ALUA Groups: 1]
    | | o- default_tg_pt_gp ................... [ALUA state: Active/optimized]
    | o- pscsi .............................................. [Storage Objects: 0]
    | o- ramdisk ............................................ [Storage Objects: 0]
    o- iscsi ........................................................ [Targets: 1]
    | o- iqn.2021-01.com.dusays:www .................................... [TPGs: 1]
    | o- tpg1 ........................................... [no-gen-acls, no-auth]
    | o- acls ...................................................... [ACLs: 0]
    | o- luns ...................................................... [LUNs: 0]
    | o- portals ................................................ [Portals: 1]
    | o- 0.0.0.0:3260 ................................................. [OK]
    o- loopback ..................................................... [Targets: 0]

    创建 LUN 号

    从先前创建的 www 存储中创建 LUN 号,命令为 iscsi/iqn.2021-01.com.dusays:www/tpg1/luns/ create /dusays/www.img,创建后通过 ls 查看执行结果:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    /> iscsi/iqn.2021-01.com.dusays:www/tpg1/luns/ create /dusays/www.img
    Created storage object dusays-www.img.
    Created LUN 0.
    /> ls
    o- / ..................................................................... [...]
    o- backstores .......................................................... [...]
    | o- block .............................................. [Storage Objects: 0]
    | o- fileio ............................................. [Storage Objects: 2]
    | | o- dusays-www.img ........ [/dusays/www.img (1.0GiB) write-thru activated]
    | | | o- alua ............................................... [ALUA Groups: 1]
    | | | o- default_tg_pt_gp ................... [ALUA state: Active/optimized]
    | | o- www ................. [/dusays/www.img (1.0GiB) write-back deactivated]
    | | o- alua ............................................... [ALUA Groups: 1]
    | | o- default_tg_pt_gp ................... [ALUA state: Active/optimized]
    | o- pscsi .............................................. [Storage Objects: 0]
    | o- ramdisk ............................................ [Storage Objects: 0]
    o- iscsi ........................................................ [Targets: 1]
    | o- iqn.2021-01.com.dusays:www .................................... [TPGs: 1]
    | o- tpg1 ........................................... [no-gen-acls, no-auth]
    | o- acls ...................................................... [ACLs: 0]
    | o- luns ...................................................... [LUNs: 1]
    | | o- lun0 . [fileio/dusays-www.img (/dusays/www.img) (default_tg_pt_gp)]
    | o- portals ................................................ [Portals: 1]
    | o- 0.0.0.0:3260 ................................................. [OK]
    o- loopback ..................................................... [Targets: 0]

    为启动器 iqn.2021-01.com.dusays:www 创建 ACL,以便 iSCSI 客户端可以访问此目标中的 LUN,命令为 iscsi/iqn.2021-01.com.dusays:www/tpg1/acls create iqn.2021-01.com.dusays:init 通过 ls 查看执行结果,通过 saveconfig 保存所有的配置,并使用 exit 退出:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    /> iscsi/iqn.2021-01.com.dusays:www/tpg1/acls create iqn.2021-01.com.dusays:init
    Created Node ACL for iqn.2021-01.com.dusays:init
    Created mapped LUN 0.
    /> ls
    o- / ..................................................................... [...]
    o- backstores .......................................................... [...]
    | o- block .............................................. [Storage Objects: 0]
    | o- fileio ............................................. [Storage Objects: 2]
    | | o- dusays-www.img ........ [/dusays/www.img (1.0GiB) write-thru activated]
    | | | o- alua ............................................... [ALUA Groups: 1]
    | | | o- default_tg_pt_gp ................... [ALUA state: Active/optimized]
    | | o- www ................. [/dusays/www.img (1.0GiB) write-back deactivated]
    | | o- alua ............................................... [ALUA Groups: 1]
    | | o- default_tg_pt_gp ................... [ALUA state: Active/optimized]
    | o- pscsi .............................................. [Storage Objects: 0]
    | o- ramdisk ............................................ [Storage Objects: 0]
    o- iscsi ........................................................ [Targets: 1]
    | o- iqn.2021-01.com.dusays:www .................................... [TPGs: 1]
    | o- tpg1 ........................................... [no-gen-acls, no-auth]
    | o- acls ...................................................... [ACLs: 1]
    | | o- iqn.2021-01.com.dusays:init ...................... [Mapped LUNs: 1]
    | | o- mapped_lun0 ................... [lun0 fileio/dusays-www.img (rw)]
    | o- luns ...................................................... [LUNs: 1]
    | | o- lun0 . [fileio/dusays-www.img (/dusays/www.img) (default_tg_pt_gp)]
    | o- portals ................................................ [Portals: 1]
    | o- 0.0.0.0:3260 ................................................. [OK]
    o- loopback ..................................................... [Targets: 0]
    /> saveconfig
    Last 10 configs saved in /etc/target/backup/.
    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


沪ICP备19023445号-2号
友情链接