以下为在deiban上配置iSCSI,什么是iSCSI,具体参见百科。通俗来讲,就是在一个大硬盘上,划一小块给其它的电脑使用。我们现在就在debian划一块硬盘出来。

服务器端的iSCSI称作iSCSI Target,客户机上的iSCSI叫iSCSI Initiator,我们先来创建iSCSI Target。

安装系统:debian 11 bullseye

一,安装iSCSI Target

apt-get install tgt

二,配置iSCSI Target.

1,创建一个磁盘

root@dlp:~# mkdir /var/lib/iscsi_disks
root@dlp:~# dd if=/dev/zero of=/var/lib/iscsi_disks/disk01.img count=0 bs=1 seek=2G

一个2G的磁盘已建好

root@debian:~# ls -la /var/lib/iscsi_disks
total 8
drwxr-xr-x  2 root root       4096 Sep 19 20:26 .
drwxr-xr-x 26 root root       4096 Sep 19 20:25 ..
-rw-r--r--  1 root root 2147483648 Sep 19 20:26 disk01.img

2,编辑Target配置

nano /etc/tgt/conf.d/target01.conf

创建一个新的Target

# create new
# 命名规则:[ iqn.(年份/如2021)-(月份)/如09.(颠倒的域名):(自定义名字) ]
# naming rule : [ iqn.(year)-(month).(reverse of domain name):(any name you like) ]
<target iqn.2021-09.lan.debian:target01>
    # 生成iSCSI target的硬盘,上面of后面生成的
    backing-store /var/lib/iscsi_disks/disk01.img
    # 同意客户端连接这个Target
    # initiator-name iqn.2021-09.lan.debian:192.168.0.3
    # 设置用户名和密码authentication info ( set any name you like for "username", "password" )
    # incominguser username password
</target>

3,重启一下

systemctl restart tgt

4,查看Target状态

root@dlp:~# tgtadm --mode target --op show

显示如下

root@debian:~# tgtadm --mode target --op show
Target 1: iqn.2021-09.lan.debian:target01
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
    LUN information:
        LUN: 0
            Type: controller
            SCSI ID: IET     00010000
            SCSI SN: beaf10
            Size: 0 MB, Block size: 1
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            SWP: No
            Thin-provisioning: No
            Backing store type: null
            Backing store path: None
            Backing store flags:
        LUN: 1
            Type: disk
            SCSI ID: IET     00010001
            SCSI SN: beaf11
            Size: 2147 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            SWP: No
            Thin-provisioning: No
            Backing store type: rdwr
            Backing store path: /var/lib/iscsi_disks/disk01.img
            Backing store flags:
    Account information:
        username
    ACL information:
        ALL
        iqn.2021-09.lan.debian:192.168.0.3

2021-9-25更新

在debian 10下面,重启时发现一个问题,每当重启debian之后,发现tgt运行之后不挂载生成的img文件,要重启一次才生效,解决方法如下:

打开/etc/rc.local文件,在exit 0上面一行,加上命令

systectl restart tgt

这样,在每次开机后,都会重启一次tgt服务。

root@debian:~# cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

ipset restore < /etc/ipset.conf
iptables-restore < /etc/iptables.conf
systemctl restart tgt

exit 0

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注