当前位置 : 主页 > 手机开发 > ROM >

iscsi共享存储

来源:互联网 收集:自由互联 发布时间:2021-06-10
1.配置target # ll /mnt/cdrom/Packages/ | grep target -r--r--r-- 2 root root 180992 May 12 2016 scsi-target-utils-1.0.24-18.el6.x86_64.rpm # yum install scsi-target-utils # rpm -ql scsi-target-utils | less /etc/rc.d/init.d/ tgtd /etc/sysco

1.配置target

# ll /mnt/cdrom/Packages/ | grep target

-r--r--r-- 2 root root   180992 May 12  2016 scsi-target-utils-1.0.24-18.el6.x86_64.rpm

# yum install scsi-target-utils

# rpm -ql scsi-target-utils | less

/etc/rc.d/init.d/tgtd
/etc/sysconfig/tgtd
/etc/tgt/targets.conf  (配置文件)
/usr/sbin/tgt-admin
/usr/sbin/tgt-setup-lun
/usr/sbin/tgtadm   (target的管理工具)
/usr/sbin/tgtd
/usr/sbin/tgtimg
/usr/share/doc/scsi-target-utils-1.0.24
/usr/share/doc/scsi-target-utils-1.0.24/README
/usr/share/doc/scsi-target-utils-1.0.24/README.iscsi
/usr/share/doc/scsi-target-utils-1.0.24/README.iser
/usr/share/doc/scsi-target-utils-1.0.24/README.lu_configuration
/usr/share/doc/scsi-target-utils-1.0.24/README.mmc
/usr/share/man/man5/targets.conf.5.gz
/usr/share/man/man8/tgt-admin.8.gz
/usr/share/man/man8/tgt-setup-lun.8.gz
/usr/share/man/man8/tgtadm.8.gz

# man tgtadm

initator访问target需要认证:

1.地址

2.chap

tgtadm的使用:

--lld <driver> --op new --mode target --tid <id> --targetname <name>     (声明target)

  Add a new target with <id> and <name>.

--lld <driver> --op show --mode target       (查看target)

  Show all the targets.

--lld <driver> --op new --mode logicalunit --tid <id> --lun <lun> --backing-store <path>     (声明逻辑单元lun)

  Add a new logical unit with <lun> to specific target with <id>. The logical unit is offered to the

  initiators. <path> must be block device files (including LVM and RAID devices) or regular files.
  lun0 is reserved for a special device automatically created.

--lld <driver> --op bind --mode target --tid <id> --initiator-address <address>       (访问控制)

  Add the address to the access lists of the target with <id>. Initiators with the address can access
  to the target. ‘ALL‘ is a special address to allow all initiators to access to a target.

 

iqn格式的名称:iqn.日期(年-月).域名(反写).string:substring

# service tgtd start

# tgtadm --lld iscsi --op new --mode target --tid 1 --targetname iqn.2019-10.cn.itecs.target:disk   (声明target)

# tgtadm --lld iscsi --op show --mode target

Target 1: iqn.2019-10.cn.itecs.target:disk
    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:

 

把原有硬盘的一个分区作为一个逻辑单元lun

# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x00025156

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1301    10240000   83  Linux
/dev/sda3            1301        1366      524288   82  Linux swap / Solaris

 # fdisk /dev/sda

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partitions system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n  
Command action
   e   extended
   p   primary partition (1-4)
p
Selected partition 4
First cylinder (1366-2610, default 1366): <回车>
Using default value 1366
Last cylinder, +cylinders or +size{K,M,G} (1366-2610, default 2610): +5G  

Command (m for help): p

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x00025156

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1301    10240000   83  Linux
/dev/sda3            1301        1366      524288   82  Linux swap / Solaris
/dev/sda4            1366        2019     5247505+  83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

在Linux中,某些命令名称和rpm包的名称是不一样的,可以通过以下命令查看某些命令属于哪个rpm包

# yum provides */partprobe   (查找命令partprobe属于哪个包)

parted-2.1-29.el6.i686 : The GNU disk partition manipulation program
Repo        : base
Matched from:
Filename    : /sbin/partprobe

parted-2.1-29.el6.x86_64 : The GNU disk partition manipulation program
Repo        : base
Matched from:
Filename    : /sbin/partprobe

# yum install parted

# partprobe /dev/sda

Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.

# tgtadm --lld iscsi --op new --mode logicalunit --tid 1 --lun 1 --backing-store /dev/sda4   (声明逻辑单元lun)

# tgtadm --lld iscsi --op show --mode target

Target 1: iqn.2019-10.cn.itecs.target:disk
    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: 5373 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /dev/sda4
            Backing store flags: 
    Account information:
    ACL information:

 

# tgtadm --lld iscsi --op bind --mode target --tid 1 --initiator-address 192.168.80.0/24(添加访问控制)

# tgtadm --lld iscsi --op show --mode target

Target 1: iqn.2019-10.cn.itecs.target:disk
    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: 5373 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /dev/sda4
            Backing store flags: 
    Account information:
    ACL information:
        192.168.80.0/24

要把上面的配置信息写到一个文件中,否则机器重启这些配置信息就没有了。

# vim /etc/tgt/targets.conf

<target iqn.2019-10.cn.itecs.target:disk>
    backing-store /dev/sda4
    initiator-address 192.168.80.0/24
</target>

 至此,target配置完成。

 

 2.在另一台服务器上配置initiator1

# ll /mnt/cdrom/Packages/*initiator*

-r--r--r-- 2 root root 748696 Mar 23 2017 /mnt/cdrom/Packages/iscsi-initiator-utils-6.2.0.873-26.el6.x86_64.rpm

# rpm -qa | grep iscsi-initiator-utils

# yum install iscsi-initiator-utils

# rpm -ql iscsi-initiator-utils

/etc/NetworkManager
/etc/NetworkManager/dispatcher.d
/etc/NetworkManager/dispatcher.d/04-iscsi
/etc/iscsi
/etc/iscsi/iscsid.conf
/etc/logrotate.d/iscsiuiolog
/etc/rc.d/init.d/iscsi
/etc/rc.d/init.d/iscsid
/sbin/brcm_iscsiuio
/sbin/iscsi-iname
/sbin/iscsiadm   (管理工具)
/sbin/iscsid
/sbin/iscsistart
/sbin/iscsiuio
/usr/lib64/libiscsi.so.0
/usr/lib64/python2.6/site-packages/libiscsimodule.so
/usr/share/doc/iscsi-initiator-utils-6.2.0.873
/usr/share/doc/iscsi-initiator-utils-6.2.0.873/README
/usr/share/man/man8/iscsi-iname.8.gz
/usr/share/man/man8/iscsiadm.8.gz
/usr/share/man/man8/iscsid.8.gz
/usr/share/man/man8/iscsistart.8.gz
/usr/share/man/man8/iscsiuio.8.gz
/var/lib/iscsi
/var/lib/iscsi/ifaces
/var/lib/iscsi/isns
/var/lib/iscsi/nodes
/var/lib/iscsi/send_targets
/var/lib/iscsi/slp
/var/lib/iscsi/static
/var/lock/iscsi

# man iscsiadm

  iscsiadm --mode discoverydb --type sendtargets --portal 192.168.80.134 --discover 或 iscsiadm --mode discovery --type sendtargets --portal 192.168.80.134

  iscsiadm --mode node --targetname iqn.2019-10.cn.itecs.target:disk --portal 192.168.80.134:3260 --login

  iscsiadm --mode node --targetname iqn.2019-10.cn.itecs.target:disk --portal 192.168.80.134:3260 --logout

 

# iscsiadm --mode discovery --type sendtargets --portal 192.168.80.134 或 iscsiadm -m discovery -t sendtargets -p 192.168.80.134

Starting iscsid:                                           [  OK  ]
192.168.80.134:3260,1 iqn.2019-10.cn.itecs.target:disk

默认端口3260。discovery之前会自动启动iscsid服务,有时候service iscsid start启动没反应,可以通过这种方式启动服务。

# vim /etc/iscsi/initiatorname.iscsi(给initiator指定一个名字)

InitiatorName=iqn.2019-10.cn.itecs.server1:initiator

 # service iscsi restart

iscsi服务启动成功后,initiator就自动登录进了target,也可以手动登录进去。

# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x000e7987

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        2576    20480000   83  Linux
/dev/sda3            2576        2641      524288   82  Linux swap / Solaris

Disk /dev/sdb: 5373 MB, 5373445632 bytes
166 heads, 62 sectors/track, 1019 cylinders
Units = cylinders of 10292 * 512 = 5269504 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

发现多出了一块硬盘。此时在target上查看:

[[email protected] ~]# tgtadm --lld iscsi --op show --mode target
Target 1: iqn.2019-10.cn.itecs.target:disk
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
        I_T nexus: 1
            Initiator: iqn.2019-10.cn.itecs.server1:initiator
            Connection: 0
                IP Address: 192.168.80.100
    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: 5373 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /dev/sda4
            Backing store flags: 
    Account information:
    ACL information:
        192.168.80.0/24

接下来进行分区和格式化:

[[email protected] ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x395d904f.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content wont be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. Its strongly recommended to
         switch off the mode (command c) and change display units to
         sectors (command u).

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1019, default 1): <回车>
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1019, default 1019): <回车>
Using default value 1019

Command (m for help): p

Disk /dev/sdb: 5373 MB, 5373445632 bytes
166 heads, 62 sectors/track, 1019 cylinders
Units = cylinders of 10292 * 512 = 5269504 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x395d904f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1019     5243743   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

# mkfs -t ext4 /dev/sdb1

# mkdir /mnt/1

# mount /dev/sdb1 /mnt/1

# cd /mnt/1

# touch f{1,2}

至此,第一个initiator配置完成。依葫芦画瓢,配置第二个initiator。

网友评论