CentOS iSCSI服务器搭建------Initiator篇

服务器信息:

[root@initiator ~]# cat /etc/redhat-release 
CentOS release 6.6 (Final)
[root@initiator ~]# uname -a
Linux initiator 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@initiator ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:6A:34:88  
          inet addr:192.168.221.131  Bcast:192.168.221.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe6a:3488/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:49589 errors:0 dropped:0 overruns:0 frame:0
          TX packets:197750 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:36045946 (34.3 MiB)  TX bytes:234344817 (223.4 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:276 (276.0 b)  TX bytes:276 (276.0 b)

  

 iSCSI initiator是能够使用target的客户端,通常是服务器。想要连接到iSCSI target的服务器,必须要安装iSCSI initiator的相关功能后才可以使用。其功能是:1.可以处理本地挂载磁盘上的任意文件系统。2.在使用fdisk命令分区后不需要重启系统。

FBI Warning:永远不要在LUN还挂载在客户端(发起程序)时停止服务。

  要设置iSCSI initiator 必须要安装iscsi-initiator-utils才行。开始上菜。。。

[root@initiator ~]#  yum search all iscsi
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.skyshe.cn
 * extras: mirrors.skyshe.cn
 * updates: mirrors.cqu.edu.cn
================================================================================ Matched: iscsi ================================================================================
iscsi-initiator-utils.x86_64 : iSCSI daemon and utility programs
iscsi-initiator-utils-devel.x86_64 : Development files for iscsi-initiator-utils
gpxe-bootimgs.noarch : Network boot loader images in bootable USB, CD, floppy and GRUB formats
gpxe-roms.noarch : Network boot loader roms in .rom format
gpxe-roms-qemu.noarch : Network boot loader roms supported by QEMU, .rom format
isns-utils.x86_64 : The iSNS daemon and utility programs
scsi-target-utils.x86_64 : The SCSI target daemon and utility programs

1.yum大法安装

[root@initiator ~]# yum install iscsi-initiator-utils iscsi-initiator-utils-devel -y

2.启动服务开机启动

[root@initiator ~]# service iscsid start
正在启动 iscsid:                                          [确定]
[root@initiator ~]# chkconfig iscsi on
[root@initiator ~]# chkconfig iscsid on

3.man大法查看客户端命令iscsiadm,查看示例。

EXAMPLES
       Discover targets at a given IP address:

            iscsiadm --mode discoverydb --type sendtargets --portal 192.168.1.10 --discover

       Login, must use a node record id found by the discovery:

            iscsiadm --mode node --targetname iqn.2001-05.com.doe:test --portal 192.168.1.1:3260 --login

       Logout:

            iscsiadm --mode node --targetname iqn.2001-05.com.doe:test --portal 192.168.1.1:3260 --logout

       List node records:

            iscsiadm --mode node

       Display all data for a given node record:

            iscsiadm --mode node --targetname iqn.2001-05.com.doe:test --portal 192.168.1.1:3260

4.根据示例中我们根据IP找到Target的限定名(iqn)。

[root@initiator ~]# iscsiadm --mode discoverydb --type sendtargets --portal 192.168.221.130 --discover
192.168.221.130:3260,1 iqn.2015-09.com.node:tgt1

  --mode discoverydb:使用检测的方式进行iscsiadmin命令功能

  --type sendtargets :通过iSCSI的协议,检测后面的设备所拥有的target数据

  --portal                 :就是那台iSCSI设备的IP与端口,不写端口的默认是3260

OK。摸摸哒。找到了。

显示出目前系统上面所有的target数据,可能并未登陆:

[root@initiator ~]# iscsiadm -m node
192.168.221.130:3260,1 iqn.2015-09.com.node:tgt1

默认配置都在defaults文件中

[root@initiator ~]# ll -R /var/lib/iscsi/nodes/
/var/lib/iscsi/nodes/:
总用量 4
drw-------. 3 root root 4096 9月  22 23:50 iqn.2015-09.com.node:tgt1

/var/lib/iscsi/nodes/iqn.2015-09.com.node:tgt1:
总用量 4
drw-------. 2 root root 4096 9月  22 23:50 192.168.221.130,3260,1

/var/lib/iscsi/nodes/iqn.2015-09.com.node:tgt1/192.168.221.130,3260,1:
总用量 4
-rw-------. 1 root root 2045 9月  22 23:50 default

6.要登录就用下面的命令来连接一台LUN到我们本地系统中,这会与服务器验证并允许我们登录LUN。

[root@initiator ~]# iscsiadm --mode node --targetname iqn.2015-09.com.node:tgt1 --portal 192.168.221.130:3260 --login
Logging in to [iface: default, target: iqn.2015-09.com.node:tgt1, portal: 192.168.221.130,3260] (multiple)
Login to [iface: default, target: iqn.2015-09.com.node:tgt1, portal: 192.168.221.130,3260] successful.
--targetname:target名字
--login :登陆

warning:
登出使用登录命令并在命令的最后使用logout来替换

[root@initiator ~]# iscsiadm --mode node --targetname iqn.2015-09.com.node:tgt1 --portal 192.168.221.130:3260 --logout
Logging out of session [sid: 1, target: iqn.2015-09.com.node:tgt1, portal: 192.168.221.130,3260]
Logout of [sid: 1, target: iqn.2015-09.com.node:tgt1, portal: 192.168.221.130,3260] successful.

7.登陆后,显示特定target的所有数据

[root@initiator ~]# iscsiadm --mode node --targetname iqn.2015-09.com.node:tgt1 --portal 192.168.221.130:3260
# BEGIN RECORD 6.2.0-873.13.el6
node.name = iqn.2015-09.com.node:tgt1
node.tpgt = 1
node.startup = automatic
node.leading_login = No
iface.hwaddress = 
iface.ipaddress = 
iface.iscsi_ifacename = default
iface.net_ifacename = 
iface.transport_name = tcp
iface.initiatorname = 
iface.state = 
iface.vlan_id = 0
iface.vlan_priority = 0
iface.vlan_state = 
iface.iface_num = 0
iface.mtu = 0
iface.port = 0
iface.bootproto = 
iface.subnet_mask = 
iface.gateway = 
iface.dhcp_alt_client_id_state = 
iface.dhcp_alt_client_id = 
iface.dhcp_dns = 
iface.dhcp_learn_iqn = 
iface.dhcp_req_vendor_id_state = 
iface.dhcp_vendor_id_state = 
iface.dhcp_vendor_id = 
iface.dhcp_slp_da = 
iface.fragmentation = 
iface.gratuitous_arp = 
iface.incoming_forwarding = 
iface.tos_state = 
iface.tos = 0
iface.ttl = 0
........................

8.接着列出使用的磁盘,fdisk会列出所有的登录认证过的磁盘 

[root@initiator ~]# 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: 0x00074c40

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        2040    16384000   83  Linux
/dev/sda2            2040        2611     4586496   82  Linux swap / Solaris

Disk /dev/sdb: 5368 MB, 5368709120 bytes
166 heads, 62 sectors/track, 1018 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


Disk /dev/sdc: 5368 MB, 5368709120 bytes
166 heads, 62 sectors/track, 1018 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


Disk /dev/sdd: 5368 MB, 5368709120 bytes
166 heads, 62 sectors/track, 1018 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


Disk /dev/sde: 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

9.运行fdisk命令来创建一个新的分区,在使用fdisk创建新分区之后,我们无需重启,就像使用我们本地的文件系统一样就行。因为这个将远程共享存储挂载到本地了。

[root@initiator ~]#  fdisk -cu /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x58426cdd.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

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

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-10485759, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): 
Using default value 10485759

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 83

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
166 heads, 62 sectors/track, 1018 cylinders, total 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x58426cdd

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10485759     5241856   83  Linux

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

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

为所欲为的格式化新创建的分区

[root@initiator ~]#  mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310464 blocks
65523 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736

正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

10. 创建一个目录来挂载新创建的分区

[root@initiator ~]# mkdir /mnt/iscsi
[root@initiator ~]# mount /dev/sdb1 /mnt/iscsi/

   [root@initiator ~]# ls -l /mnt/iscsi/
   总用量 16
   drwx------. 2 root root 16384 9月 23 00:38 lost+found

让我们看看挂载点

[root@initiator ~]# df -Th   //-T 输出文件系统类型   -h以宜读的方式显示大小
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda1      ext4    16G  968M   14G   7% /
tmpfs          tmpfs  491M     0  491M   0% /dev/shm
/dev/sdb1      ext4   4.8G   10M  4.6G   1% /mnt/iscsi

要想永久挂载么,当然得东fsab的刀子

[root@initiator ~]# vim /etc/fstab 
[root@initiator ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Tue Sep 22 20:53:15 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=0466fb2b-f1ab-4666-bb8f-2e7d008f5b55 /                       ext4    defaults        1 1
UUID=1766dc2c-509e-4547-b647-ec42e61e9223 swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sdb1		/mnt/iscsi		ext4	defaults,_netdev 0 0

_netdev,说明这是一个网络设备。

检查我们fstab文件是否有错误。

[root@initiator ~]# mount -av    //-a 所有挂载点 -v冗余模式
mount: tmpfs already mounted on /dev/shm
mount: devpts already mounted on /dev/pts
mount: sysfs already mounted on /sys
mount: proc already mounted on /proc
mount: /dev/sdb1 already mounted on /mnt/iscsi
nothing was mounted

beautiful,尽情使用吧。。。。。

 

 

参考资料

 

 

linux中国:https://linux.cn/   我最喜欢的linux中文网站

使用iSCSI Target创建集中式安全存储(一):https://linux.cn/article-4971-1.html

鸟哥的LINUX私房菜--服务器架设篇

 

转载于:https://www.cnblogs.com/XYJK1002/p/4837538.html

你可能感兴趣的:(运维,操作系统,网络)