tgtadm配置
环境:
centos 6.9
iSCSI Target:192.168.56.150 + scsi-target-utils
iSCSI Initiator:
192.168.56.151 + iscsi-initiator-utils
一.服务器端配置
1. 准备要共享出去的设备,我们这里将/dev/sdb共享出去
[root@tgtamd-server ~]# fdisk -l
Disk /dev/sda: 37.6 GB, 37580963840 bytes
255 heads, 63 sectors/track, 4568 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0009da7b
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 587 4194304 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 587 4569 31992832 8e Linux LVM
Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/vgroot-lvroot: 32.8 GB, 32757514240 bytes
255 heads, 63 sectors/track, 3982 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
2.安装SCSI服务端
[root@localhost ~]# yum install -y scsi-target-utils
3.启动服务并查看
[root@tgtamd-server ios]# service tgtd start
Starting SCSI target daemon: [ OK ]
[root@tgtamd-server ios]# chkconfig tgtd on
[root@tgtamd-server ios]# netstat -tnlp | grep :3260
tcp 0 0 0.0.0.0:3260 0.0.0.0:* LISTEN 2418/tgtd
tcp 0 0 :::3260 :::* LISTEN 2418/tgtd
4.新建一个target并查看
[root@tgtamd-server ios]# tgtadm -L iscsi -o new -m target -t 1 -T iqn.hxl.com.test
[root@tgtamd-server ios]# tgtadm -L iscsi -o show -m target
Target 1: iqn.hxl.com.test
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
Backing store type: null
Backing store path: None
Backing store flags:
Account information:
ACL information:
然后必须向target中添加lun并关联存储设备才能被客户端访问使用;
5.创建一个lun
[root@tgtamd-server ios]# tgtadm -L iscsi -o new -m logicalunit -t 1 -l 1 -b /dev/sdb
[root@tgtamd-server ios]# tgtadm -L iscsi -o show -m target
Target 1: iqn.hxl.com.test
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
Backing store type: null
Backing store path: None
Backing store flags:
LUN: 1
Type: disk
SCSI ID: IET 00010001
SCSI SN: beaf11
Size: 4295 MB, Block size: 512
Online: Yes
Removable media: No
Prevent removal: No
Readonly: No
Backing store type: rdwr
Backing store path: /dev/sdb
Backing store flags:
Account information:
ACL information:
lun0是一个controller,而lun1则是一个磁盘;
6.开放给192.168.56.0/24网段内的主机访问
[root@tgtamd-server ios]# tgtadm -L iscsi -o bind -m target -t 1 -I 192.168.56.0/24
[root@tgtamd-server ios]# tgtadm -L iscsi -o show -m target
Target 1: iqn.hxl.com.test
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
Backing store type: null
Backing store path: None
Backing store flags:
LUN: 1
Type: disk
SCSI ID: IET 00010001
SCSI SN: beaf11
Size: 4295 MB, Block size: 512
Online: Yes
Removable media: No
Prevent removal: No
Readonly: No
Backing store type: rdwr
Backing store path: /dev/sdb
Backing store flags:
Account information:
ACL information:
192.168.56.0/24
7.写到配置文件,使重启后生效
将设置保存到配置文件 tgt-admin --dump > /etc/tgt/targets.conf
查看配置 tgt-admin -dump
二.客户端配置
1.安装SCSI客户端
[root@node2 ~]# yum install -y iscsi-initiator-utils
2.初始化,为每个initiator命名并放在/etc/iscsi/下
2.1可以自动生成
[root@tgtamd-client mnt]# echo "InitiatorName=`iscsi-iname -p iqn.hxl.com.test`" > /etc/iscsi/initiatorname.iscsi
[root@tgtamd-client mnt]# cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.hxl.com.test:686d0a1af5b
2.2或者为了方便记忆自定义别名
[root@tgtamd-client mnt]# echo "InitiatorAlias=initiator1" >> /etc/iscsi/initiatorname.iscsi
[root@tgtamd-client mnt]# cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.hxl.com.test:686d0a1af5b
InitiatorAlias=initiator1
3.启动服务
[root@tgtamd-client mnt]# service iscsi start
[root@tgtamd-client mnt]# service iscsid start
[root@tgtamd-client mnt]# chkconfig iscsi on
[root@tgtamd-client mnt]# chkconfig iscsid on
4.查找Target
关闭防火墙(服务端和客户端都需要关闭)
[root@tgtamd-client mnt]# service iptables stop
[root@tgtamd-client mnt]# chkconfig iptables off
[root@tgtamd-client mnt]# iscsiadm -m discovery -t st -p 192.168.56.150
192.168.56.150:3260,1 iqn.hxl.com.test
5.此时只是能找到,但还不能使用,所以,接下来要登录并使用;
5.1 先查看此时客户端的分区情况
[root@tgtamd-client mnt]# fdisk -l
Disk /dev/loop0: 3972 MB, 3972005888 bytes
64 heads, 32 sectors/track, 3788 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0dba0805
Device Boot Start End Blocks Id System
/dev/loop0p1 * 1 3788 3878912 17 Hidden HPFS/NTFS
Disk /dev/sda: 37.6 GB, 37580963840 bytes
255 heads, 63 sectors/track, 4568 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0004fba2
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 587 4194304 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 587 4569 31992832 8e Linux LVM
Disk /dev/mapper/vgroot-lvroot: 32.8 GB, 32757514240 bytes
255 heads, 63 sectors/track, 3982 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
5.2 登录服务器端使用其共享的磁盘(分区)
[root@tgtamd-client mnt]# iscsiadm -m node -T iqn.hxl.com.test -p 192.168.56.150 -l
Logging in to [iface: default, target: iqn.hxl.com.test, portal: 192.168.56.150,3260] (multiple)
Login to [iface: default, target: iqn.hxl.com.test, portal: 192.168.56.150,3260] successful.
5.3 此时的分区情况
[root@tgtamd-client mnt]# fdisk -l
Disk /dev/loop0: 3972 MB, 3972005888 bytes
64 heads, 32 sectors/track, 3788 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0dba0805
Device Boot Start End Blocks Id System
/dev/loop0p1 * 1 3788 3878912 17 Hidden HPFS/NTFS
Disk /dev/sda: 37.6 GB, 37580963840 bytes
255 heads, 63 sectors/track, 4568 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0004fba2
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 587 4194304 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 587 4569 31992832 8e Linux LVM
Disk /dev/mapper/vgroot-lvroot: 32.8 GB, 32757514240 bytes
255 heads, 63 sectors/track, 3982 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/sdb: 4294 MB, 4294967296 bytes
133 heads, 62 sectors/track, 1017 cylinders
Units = cylinders of 8246 * 512 = 4221952 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
6. 然后将/dev/sdb格式化
[root@node2 ~]# mke2fs -t ext4 /dev/sdb
挂载
[root@tgtamd-client /]#cd /
[root@tgtamd-client /]# mkdir media
[root@tgtamd-client /]# mount /dev/sdb /media/
[root@tgtamd-client /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vgroot-lvroot
30G 7.1G 22G 25% /
tmpfs 2.9G 300K 2.9G 1% /dev/shm
/dev/sda1 477M 41M 411M 10% /boot
/soft/ios/CentOS-6.9-x86_64-bin-DVD1.iso
3.7G 3.7G 0 100% /mnt
/dev/sdb 3.9G 8.0M 3.7G 1% /media
[root@tgtamd-client media]# echo aa>a.txt
[root@tgtamd-client media]# more a.txt
aa
7.登出节点
iscsiadm -m node -T iqn.2015-11.com.a.test -p 192.168.85.128 -u
[root@tgtamd-client media]# iscsiadm -m node -T iqn.hxl.com.test -p 192.168.56.150 -u
Logging out of session [sid: 1, target: iqn.hxl.com.test, portal: 192.168.56.150,3260]
Logout of [sid: 1, target: iqn.hxl.com.test, portal: 192.168.56.150,3260] successful.
这个时候无法访问挂载的目录
[root@tgtamd-client media]# ls
ls: reading directory .: Input/output error
将挂载点umount
[root@tgtamd-client /]# umount /media
重新登录
[root@tgtamd-client /]# iscsiadm -m node -T iqn.hxl.com.test -p 192.168.56.150 -l
Logging in to [iface: default, target: iqn.hxl.com.test, portal: 192.168.56.150,3260] (multiple)
Login to [iface: default, target: iqn.hxl.com.test, portal: 192.168.56.150,3260] successful.
重新挂载
[root@tgtamd-client /]# mount /dev/sdb /media/
之前的文件是可以查看得到的
[root@tgtamd-client media]# ls
a.txt lost+found
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?