新秀篇 ##Linux中ISCSI网路磁盘共享##

ISCSI技术介绍:

iSCSI技术实现了物理硬盘设备与TCP/TP网络传输协议的相互结合,使得用户可以通过互联网方便的获取到远程机房提供的共享存储资源
1.ISCSI网络磁盘共享:
【在服务端server】:新建磁盘,划分分区

[root@server ~]# yum install targetcli -y             ##安装targetcli服务
Loaded plugins: langpacks
rhel_dvd                                                 | 4.1 kB     00:00     
Resolving Dependencies
[root@server ~]# fdisk /dev/vdb                  ##新建磁盘
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xe43428f2.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-20971519, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +2G             ##建立一个2G的磁盘
Partition 1 of type Linux and of size 2 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@server ~]# partprobe                   ##手动更新分区表
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
[root@server ~]# systemctl start target                ##开启target服务
[root@server ~]# systemctl enable target               ##开机自动开启
ln -s '/usr/lib/systemd/system/target.service' '/etc/systemd/system/multi-user.target.wants/target.service'
[root@server ~]# targetcli                    ##进入iscsi目标交互式配置模式
Warning: Could not load preferences file /root/.targetcli/prefs.bin.
targetcli shell version 2.1.fb34
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.
/> /backstores/block create westos:storage1 /dev/vdb1          ##和逻辑卷建立联系,且给要共享的逻辑卷起名,提供硬盘创建储存设备
Created block storage object westos:storage1 using /dev/vdb1.
/> /iscsi create iqn.2018-06.com.example:storage1    ##创建iqn名称(每个发起端和目标需要以名称进行标识) iqn域名反写:随机串
Created target iqn.2018-06.com.example:storage1.
Created TPG 1.
/> ls                         ##查看iscsi1对象
o- / ..................................................................... [...]
  o- backstores .......................................................... [...]
  | o- block .............................................. [Storage Objects: 1]
  | | o- westos:storage1 ........... [/dev/vdb1 (2.0GiB) write-thru deactivated]
  | o- fileio ............................................. [Storage Objects: 0]
  | o- pscsi .............................................. [Storage Objects: 0]
  | o- ramdisk ............................................ [Storage Objects: 0]
  o- iscsi ........................................................ [Targets: 1]
  | o- iqn.2018-06.com.example:storage1 .............................. [TPGs: 1]
  |   o- tpg1 ........................................... [no-gen-acls, no-auth]
  |     o- acls ...................................................... [ACLs: 0]
  |     o- luns ...................................................... [LUNs: 0]
  |     o- portals ................................................ [Portals: 0]
  o- loopback ..................................................... [Targets: 0]
/> /iscsi/iqn.2018-06.com.example:storage1/tpg1/acls create iqn.2018-06.com.example:westoskey       ##创建客户端连接服务端密钥
Created Node ACL for iqn.2018-06.com.example:westoskey
/> /iscsi/iqn.2018-06.com.example:storage1/tpg1/luns create /backstores/block/westos:storage1       ##关联设备
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.2018-06.com.example:westoskey
/> /iscsi/iqn.2018-06.com.example:storage1/tpg1/portals create 172.25.254.220      ##启动监听程序
Using default IP port 3260
Created network portal 172.25.254.220:3260.
/> exit                      ##退出
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json

【在客户端desktop】:安装服务,进行测试

[root@client ~]# yum install iscsi-initiator-utils.x86_64 -y            ##安装服务包
[root@client ~]# vim /etc/iscsi/initiatorname.iscsi                     ##编辑key配置文件
编辑:InitiatorName=iqn.2018-06.com.example:westoskey
[root@client ~]# systemctl restart iscsi                                                
[root@client ~]# systemctl restart iscsid.service                       ##开启服务
[root@client ~]# iscsiadm -m discovery -t st -p 172.25.254.220          ##发现服务(-m 指定服务器,-t 发现网络设备类型,-p 主机IP)
172.25.254.220:3260,1 iqn.2018-06.com.example:storage1
[root@client ~]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.220 -l           ##登录服务(-T 指定域名 -l 指定lun的ID号 )
Logging in to [iface: default, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.220,3260] (multiple)
Login to [iface: default, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.220,3260] successful.
[root@client ~]# fdisk -l                       ##查看磁盘
Disk /dev/sda: 2147 MB, 2147483648 bytes, 4194304 sectors                        ##有刚才共享的2G磁盘
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 4194304 bytes

2.共享磁盘的使用与自动挂载:
【在客户端desktop】:

[root@client ~]# fdisk /dev/sda        ##新建磁盘
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x829ead8c.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (8192-4194303, default 8192): 
Using default value 8192
Last sector, +sectors or +size{K,M,G} (8192-4194303, default 4194303): +1000M
Partition 1 of type Linux and of size 1000 MiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@client ~]# mkfs.xfs  /dev/sda1               ##格式化新建磁盘
meta-data=/dev/sda1              isize=256    agcount=8, agsize=32000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=256000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@client ~]# vim /etc/fstab            ##编辑配置文件
写入:/dev/sda1     /mnt          xfs   defaults,_netdev   0  0 
[root@client ~]# mount  /dev/sda1   /mnt        ##手动挂载
[root@client ~]# df
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3192672   7281228  31% /
devtmpfs              469344       0    469344   0% /dev
tmpfs                 484932     124    484808   1% /dev/shm
tmpfs                 484932   12808    472124   3% /run
tmpfs                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo    483670    2356    451823   1% /home
/dev/sda1            1020588   33056    987532   4% /mnt              ##挂载成功
[root@client ~]# umount  /mnt
[root@client ~]# df
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3192672   7281228  31% /
devtmpfs              469344       0    469344   0% /dev
tmpfs                 484932     124    484808   1% /dev/shm
tmpfs                 484932   12808    472124   3% /run
tmpfs                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo    483670    2356    451823   1% /home
[root@client ~]# reboot                ##重启虚拟机
Connection to 172.25.254.120 closed by remote host.
Connection to 172.25.254.120 closed.
[kiosk@foundation20 Desktop]$ ssh root@172.25.254.120
root@172.25.254.120's password: 
Last login: Sun Jun  3 01:47:08 2018
[root@client ~]# df                         ##查看挂载
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3191960   7281940  31% /
devtmpfs              469344       0    469344   0% /dev
tmpfs                 484932      84    484848   1% /dev/shm
tmpfs                 484932   12792    472140   3% /run
tmpfs                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo    483670    2356    451823   1% /home
/dev/sda1            1020588   33056    987532   4% /mnt                     ##自动挂载成功

3.删除共享磁盘:
【在客户端desktop】:

[root@client ~]# yum install tree -y                   ##安装数结构显示服务
Loaded plugins: langpacks
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution
[root@client ~]# tree /var/lib/iscsi/                ##数结构显示iscsi主文件
/var/lib/iscsi/
|-- ifaces
|-- isns
|-- nodes
|   `-- iqn.2018-06.com.example:storage1
|       `-- 172.25.254.220,3260,1
|           `-- default
|-- send_targets
|   `-- 172.25.254.220,3260
|       |-- iqn.2018-06.com.example:storage1,172.25.254.220,3260,1,default -> /var/lib/iscsi/nodes/iqn.2018-06.com.example:storage1/172.25.254.220,3260,1
|       `-- st_config
|-- slp
`-- static

10 directories, 2 files
[root@client ~]# fdisk -l              ##查看共享磁盘
 Device Boot      Start         End      Blocks   Id  System
/dev/sda1            8192     2056191     1024000   83  Linux
[root@client ~]# df             ##查看挂载
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3193200   7280700  31% /
devtmpfs              469344       0    469344   0% /dev
tmpfs                 484932      84    484848   1% /dev/shm
tmpfs                 484932   12792    472140   3% /run
tmpfs                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo    483670    2356    451823   1% /home
/dev/sda1            1020588   33056    987532   4% /mnt
[root@client ~]# umount /mnt                      ##先进行卸载
[root@client ~]# df               ##查看
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3193200   7280700  31% /
devtmpfs              469344       0    469344   0% /dev
tmpfs                 484932      84    484848   1% /dev/shm
tmpfs                 484932   12788    472144   3% /run
tmpfs                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo    483670    2356    451823   1% /home        ##卸载成功
[root@client ~]# vim /etc/fstab               ##还原配置文件
[root@client ~]# fdisk -l                ##查看共享磁盘
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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 label type: dos
Disk identifier: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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 label type: dos
Disk identifier: 0x09ee18d7

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   8e  Linux LVM

Disk /dev/mapper/vg0-vo: 520 MB, 520093696 bytes, 1015808 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 /dev/sda: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 4194304 bytes
Disk label type: dos
Disk identifier: 0x829ead8c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            8192     2056191     1024000   83  Linux
[root@client ~]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.220 -u                  ##登出iscsi服务,暂时断开连接
Logging out of session [sid: 1, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.220,3260]
Logout of [sid: 1, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.220,3260] successful.
[root@client ~]# fdisk -l                 ##共享磁盘消失

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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 label type: dos
Disk identifier: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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 label type: dos
Disk identifier: 0x09ee18d7

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   8e  Linux LVM

Disk /dev/mapper/vg0-vo: 520 MB, 520093696 bytes, 1015808 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

[root@client ~]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.220 -o delete               ##删除iscsi目标的本地记录,以永久断开连接
[root@client ~]# tree /var/lib/iscsi/               ##树结构显示
/var/lib/iscsi/
|-- ifaces
|-- isns
|-- nodes
|-- send_targets
|   `-- 172.25.254.220,3260
|       `-- st_config
|-- slp
`-- static

7 directories, 1 file                           ###删除成功

你可能感兴趣的:(新秀篇 ##Linux中ISCSI网路磁盘共享##)