本文的初衷仅供自己做备忘笔记, 内容大多从网上搜集和整理, 并非都是自己原创.
参考的来源我会在后面注明, 对于可能遗漏的来源, 还请相关原创作者提醒, 非常感谢.
参考来源
https://blog.csdn.net/nnuljl/article/details/38237437
https://www.ubuntubar.com/y2019/403.html
https://help.ubuntu.com/lts/serverguide/iscsi-initiator.html
一开始准备用samba做文件共享, 但后来配置过程中出现了一系列的问题(权限方面), 最终放弃了samba, 选择了iSCSI.
环境
Ubuntu 18.04 LTS (服务端和客户端都是)
服务端
安装tgt
$ sudo apt install tgt
检查tgt服务的运行状态
$ sudo service tgt status
● tgt.service - (i)SCSI target daemon
Loaded: loaded (/lib/systemd/system/tgt.service; enabled; vendor preset: enab
Active: active (running) since Fri 2020-03-20 18:27:46 UTC; 51s ago
Docs: man:tgtd(8)
Main PID: 9517 (tgtd)
Status: "Starting event loop..."
Tasks: 1
CGroup: /system.slice/tgt.service
└─9517 /usr/sbin/tgtd -f
......
tgtadm --help可以查看tgt的命令参数
创建target
$ sudo tgtadm --lld iscsi --mode target --op new --tid 1 --targetname sss-storage-iscsi-1
- --lld iscsi 固定参数驱动iscsi
- --mode target 模式target
- --op new 操作new新创建
- --tid 1 target id号
- --targetname ...target名称
查看Target记录
$ sudo tgt-admin --show
Target 1: sss-storage-iscsi-1
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:
Account information:
ACL information:
注意最下面的ACL information, 下面没有任何内容, 应为还没配置允许访问的规则. 下面要进行配置.
绑定initiator name(允许访问的地址)
sudo tgtadm --lld iscsi --mode target --op bind --tid 1 -I ALL
或者
sudo tgtadm --lld iscsi --mode target --op bind --tid 1 -I 10.0.0.6
这里-I ALL表示所有地址均可访问, -I 10.0.0.6表示只有10.0.0.6可以访问. 想添加多个, 可以修改访问地址多执行几次.
再查看target记录
$ sudo tgt-admin --show
Target 1: sss-storage-iscsi-1
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:
Account information:
ACL information:
ALL
为Target新增逻辑单元
帮助文档中的命令规范为:
--lld <driver> --mode logicalunit --op new --tid <id> --lun <lun>
--backing-store <path> --bstype <type> --bsopts <bs options> --bsoflags <options>
add a new logical unit with <lun> to the specific
target with <id>. The logical unit is offered
to the initiators. <path> must be block device files
<span style="color:#ff0000;">(including LVM and RAID devices)</span> or<span style="color:#ff0000;"> regular files</span>.
bstype option is optional.
bsopts are specific to the bstype.
bsoflags supported options are sync and direct
(sync:direct for both).
--lld <driver> --mode logicalunit --op delete --tid <id> --lun <lun>
delete the specific logical unit with <lun> that
the target with <id> has.
实际运行
$ sudo tgtadm --lld iscsi --mode logicalunit --op new --tid 1 --lun 1 -b /dev/hdd_vgs/hdd_lv1
tid可以理解为target的id, lun可以理解成表示逻辑单元, 一个target下可以有多个逻辑单元.
再查看target记录
$ sudo tgtadm --lld iscsi --mode target --op show
Target 1: sss-storage-iscsi-1
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: 1000195 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: /dev/hdd_vgs/hdd_lv1
Backing store flags:
Account information:
ACL information:
ALL
删除target逻辑单元
帮助文档中的命令规范为:
--lld <driver> --op delete --mode logicalunit --tid <id> --lun <lun>
Delete specific logical unit with <lun> that the target with <id>
has.
实际运行
sudo tgtadm --lld iscsi --op delete --mode logicalunit --tid 1 --lun 1
再查看target
$ sudo tgtadm --lld iscsi --mode target --op show
Target 1: sss-storage-iscsi-1
System information:
Driver: iscsi
State: ready
I_T nexus information:
I_T nexus: 1
Initiator: iqn.1993-08.org.debian:01:802652a3a2f8 alias: sss-OptiPlex-3070
Connection: 0
IP Address: 10.0.0.6
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:
Account information:
ACL information:
ALL
删除target
帮助文档中的命令规范为:
--lld <driver> --op delete --mode target --tid <id>
Delete specific target with <id>. The target must have no active
I_T nexus.
实际运行
sudo tgtadm --lld iscsi --op delete --mode target --tid 1
再查看记录
$ sudo tgt-admin show
No action specified.
target端(客户端)
安装open-iscsi
sudo apt install open-iscsi
这里Ubuntu 18.04 LTS自带了open-iscsi, 没有手动安装
发现并找出共享目标
安装完成后,对我们的iSCSI目标服务器运行目标发现以找出共享目标。
$ sudo iscsiadm -m discovery -t st -p 10.0.0.7
10.0.0.7:3260,1 sss-storage-iscsi-1
显示发现了10.0.0.7上的iscsi target, 接下来需要登录才能使用
连接target(登录)
sudo iscsiadm -m node --login
或
$ sudo iscsiadm --mode node --targetname sss-storage-iscsi-3070 --portal 10.0.0.7:3260 --login
Logging in to [iface: default, target: sss-storage-iscsi-3070, portal: 10.0.0.7,3260] (multiple)
Login to [iface: default, target: sss-storage-iscsi-3070, portal: 10.0.0.7,3260] successful.
注:target name为sss-storage-iscsi-3070,3260为iSCSI服务默认端口
这里可以运行如下命令, 检查新磁盘
dmesg | grep sd
[ 4.322384] sd 2:0:0:0: Attached scsi generic sg1 type 0
[ 4.322797] sd 2:0:0:0: [sda] 41943040 512-byte logical blocks: (21.4 GB/20.0 GiB)
[ 4.322843] sd 2:0:0:0: [sda] Write Protect is off
[ 4.322846] sd 2:0:0:0: [sda] Mode Sense: 03 00 00 00
[ 4.322896] sd 2:0:0:0: [sda] Cache data unavailable
[ 4.322899] sd 2:0:0:0: [sda] Assuming drive cache: write through
[ 4.323230] sd 2:0:0:0: [sda] Cache data unavailable
[ 4.323233] sd 2:0:0:0: [sda] Assuming drive cache: write through
[ 4.325312] sda: sda1 sda2 < sda5 >
[ 4.325729] sd 2:0:0:0: [sda] Cache data unavailable
[ 4.325732] sd 2:0:0:0: [sda] Assuming drive cache: write through
[ 4.325735] sd 2:0:0:0: [sda] Attached SCSI disk
[ 2486.941805] sd 4:0:0:3: Attached scsi generic sg3 type 0
[ 2486.952093] sd 4:0:0:3: [sdb] 1126400000 512-byte logical blocks: (576 GB/537 GiB)
[ 2486.954195] sd 4:0:0:3: [sdb] Write Protect is off
[ 2486.954200] sd 4:0:0:3: [sdb] Mode Sense: 8f 00 00 08
[ 2486.954692] sd 4:0:0:3: [sdb] Write cache: disabled, read cache: enabled, doesn't
support DPO or FUA
[ 2486.960577] sdb: sdb1
[ 2486.964862] sd 4:0:0:3: [sdb] Attached SCSI disk
输出中的sdb就是新的iscsi磁盘(这里省略了一堆其他输出).
断开Target(登出)
$ sudo iscsiadm -m node -p 192.168.1.120 --logout
Logging out of session [sid: 1, target: iqn.2014-0728.com:longtang, portal: 192.168.1.120,3260]
Logout of [sid: 1, target: iqn.2014-0728.com:longtang, portal: 192.168.1.120,3260] successful.
删除指定Target
用logout只是暂时登出,发现的target信息会保存在数据库中,下次重启iscsi服务时(service iscsi restart),仍会找回该Target卷。
如果想从数据库中删除该Target,需用以下命令:
查询数据库中Target内容:
$ sudo iscsiadm -m node
10.0.0.7:3260,1 sss-storage-iscsi-3070
删除指定的Target:
$ sudo iscsiadm --mode node -o delete --targetname sss-storage-iscsi-3070 -p 10.0.0.7:3260
磁盘分区 & 挂载
创建一个主分区(具体解释看fdisk)
sudo fdisk /dev/sdb
n
p
enter
w
格式化成ext4
sudo mkfs.ext4 /dev/sdb1
挂载
sudo mount /dev/sdb1 /srv
配置开机自动挂载
$ sudo vim /etc/fstab
# 添加
/dev/sdb1 /srv ext4 defaults,auto,_netdev 0 0
配置重启自动连接
编辑配置文件/etc/iscsi/iscsid.conf,修改node.startup = automatic。若只通过IP验证,没有启用CHAP则不需要修改CHAP项。
然后重启服务/etc/init.d/open-iscsi restart。