ISCSI技术实现了物理硬盘设备与TCP/TP网络传输的相互结合,使得客户可以通过互联网方便的获得到远程机房提供的共享存储资源
使用硬盘共享设备
1、服务端设定
(1)下载服务(yum源版本需和下载版本一至,否则服务不可用)
(2)新建一个逻辑卷用于共享
[[email protected] ~]# fdisk -l ##查看现有分区
Disk /dev/sda: 9663 MB, 9663676416 bytes, 18874368 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: 0x00094fb4
Device Boot Start End Blocks Id System
/dev/sda1 2048 411647 204800 83 Linux
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x79ecac8f
Device Boot Start End Blocks Id System
(3)新建一个逻辑卷
[[email protected] ~]# fdisk /dev/sdb
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.
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-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1G
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition ‘Linux’ to ‘Linux LVM’
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x79ecac8f
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 8e Linux LVM
Command (m for help): wq
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.
[[email protected] ~]#
(4)新建物理卷/逻辑卷组以及逻辑卷
[[email protected] ~]# pvcreate /dev/sdb1
Physical volume “/dev/sdb1” successfully created
[[email protected] ~]# vgcreate vg0 /dev/sdb1
Volume group “vg0” successfully created
[[email protected] ~]# vgdisplay
— Volume group —
VG Name vg0
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 496.00 MiB
PE Size 4.00 MiB
Total PE 124
Alloc PE / Size 0 / 0
Free PE / Size 124 / 496.00 MiB
VG UUID qE6TbE-7irb-Cedc-FMg4-u1ui-s9EN-RfXo1I
[[email protected] ~]# lvcreate -l 124 -n lv0 vg0
WARNING: ext4 signature detected on /dev/vg0/lv0 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/vg0/lv0.
Logical volume “lv0” created.
[[email protected] ~]#
2、共享磁盘
在将服务ip添加进去的时候报错报错如图所示
解决方案如下
再次添加访问ip
3、客户端设置
(1)下载客户端ISCSI
[[email protected] ~]# yum install iscsi-initiator-utils -y
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
: manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Package iscsi-initiator-utils-6.2.0.873-32.el7.x86_64 already installed and latest version
Nothing to do
(2)查找共享设备
[[email protected] ~]# iscsiadm -m discovery -t st -p 172.25.254.168 ##查找共享设备
172.25.254.168:3260,1 iqn.2018-11.com.westos:storage1
[[email protected] ~]#
(3)客户端密码认证
[[email protected] ~]# vim /etc/iscsi/initiatorname.iscsi
[[email protected] ~]#
(4)登陆设备
登陆时出错
[[email protected] ~]# iscsiadm -m node -T iqn.2018-11.com.westos:storage1 -p 172.25.254.168 -l
Logging in to [iface: default, target: iqn.2018-11.com.westos:storage1, portal: 172.25.254.168,3260] (multiple)
iscsiadm: Could not login to [iface: default, target: iqn.2018-11.com.westos:storage1, portal: 172.25.254.168,3260].
iscsiadm: initiator reported error (24 - iSCSI login failed due to authorization failure)
iscsiadm: Could not log into all portals
[[email protected] ~]# vim /etc/iscsi/initiatorname.iscsi
[[email protected] ~]#
解决方案
(1查看/etc/iscsi/initiatorname.iscsi加**匙文件是否正确
(2/etc/iscsi/initiatorname.iscsi配置后是否重启服务,经查看未重启服务
(3若(1)(2)确认都无误还是无法登陆,卸载客户端ISCSI重新下载可解决
[[email protected] ~]# iscsiadm -m node -T iqn.2018-11.com.westos:storage1 -p 172.25.254.168 -l ##登陆设备
Logging in to [iface: default, target: iqn.2018-11.com.westos:storage1, portal: 172.25.254.168,3260] (multiple)
Login to [iface: default, target: iqn.2018-11.com.westos:storage1, portal: 172.25.254.133,3260] successful.
(5)查看共享设备
[[email protected] ~]# fdisk -l
Disk /dev/sda: 9663 MB, 9663676416 bytes, 18874368 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: 0x0007d593
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 18874367 9231360 8e Linux LVM
Disk /dev/mapper/rhel-root: 8925 MB, 8925478912 bytes, 17432576 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/mapper/rhel-swap: 524 MB, 524288000 bytes, 1024000 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/sdb: 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 / 33550336 bytes ##此时设备大小为520
(6)验证共享设备
在服务端新建逻辑卷扩展共享逻辑卷,在客户端卸载之前挂载并重新挂载后共享逻辑卷也随之扩大
1>新建逻辑卷
[[email protected] ~]# fdisk /dev/sdb ##新建逻辑卷
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.
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (2099200-41943039, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-41943039, default 41943039): +500M
Partition 2 of type Linux and of size 500 MiB is set
Command (m for help): t
Partition number (1,2, default 2):
Hex code (type L to list all codes): 8e
Changed type of partition ‘Linux’ to ‘Linux LVM’
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x79ecac8f
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 8e Linux LVM
/dev/sdb2 2099200 3123199 512000 8e Linux LVM
Command (m for help): wq
The partition table has been altered!
2>扩展逻辑卷
[[email protected] ~]# partprobe
[[email protected] ~]# pvcreate /dev/sdb2
Physical volume “/dev/sdb2” successfully created
[[email protected] ~]# vgextend vg0 /dev/sdb2
Volume group “vg0” successfully extended
[[email protected] ~]# vgdisplay ##查看逻辑卷组
— Volume group —
VG Name vg0
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 992.00 MiB
PE Size 4.00 MiB
Total PE 248 ##总大小
Alloc PE / Size 124 / 496.00 MiB
Free PE / Size 124 / 496.00 MiB
VG UUID qE6TbE-7irb-Cedc-FMg4-u1ui-s9EN-RfXo1I
[[email protected] ~]# lvextend -l 248 /dev/vg0/lv0 ##扩展逻辑卷
Size of logical volume vg0/lv0 changed from 496.00 MiB (124 extents) to 992.00 MiB (248 extents).
Logical volume lv0 successfully resized.
[[email protected] ~]# lvs ##查看逻辑卷大小
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- 8.31g
swap rhel -wi-ao---- 500.00m
lv0 vg0 -wi-ao---- 992.00m ##992
[[email protected] ~]#
3>在服务端查看逻辑卷
之前大小为520
Disk /dev/sdb: 520 MB, 520093696 bytes, 1015808 sectors ##之前大小为520
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 33550336 bytes
查看服务端逻辑卷
[[email protected] ~]# iscsiadm -m node -T iqn.2018-11.com.westos:storage1 -p 172.25.254.168 -u ##登陆退出
Logging out of session [sid: 29, target: iqn.2018-11.com.westos:storage1, portal: 172.25.254.168,3260]
Logout of [sid: 29, target: iqn.2018-11.com.westos:storage1, portal: 172.25.254.168,3260] successful.
[[email protected] ~]# iscsiadm -m node -T iqn.2018-11.com.westos:storage1 -p 172.25.254.168 -l ##重新登陆
Logging in to [iface: default, target: iqn.2018-11.com.westos:storage1, portal: 172.25.254.168,3260] (multiple)
Login to [iface: default, target: iqn.2018-11.com.westos:storage1, portal: 172.25.254.168,3260] successful.
[[email protected] ~]# fdisk -l ##查看磁盘分区
Disk /dev/sda: 9663 MB, 9663676416 bytes, 18874368 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: 0x0007d593
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 18874367 9231360 8e Linux LVM
Disk /dev/mapper/rhel-root: 8925 MB, 8925478912 bytes, 17432576 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/mapper/rhel-swap: 524 MB, 524288000 bytes, 1024000 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/sdb: 1040 MB, 1040187392 bytes, 2031616 sectors ##此时大小为1040
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 33550336 bytes
(7)此时共享分区可正常使用
[[email protected] ~]# fdisk /dev/sdb ##可以分区
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 0xc892cb8f.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (65528-2031615, default 65528):
Using default value 65528
Last sector, +sectors or +size{K,M,G} (65528-2031615, default 2031615): +500M
Partition 1 of type Linux and of size 512 MiB is set
Command (m for help): p
Disk /dev/sdb: 1040 MB, 1040187392 bytes, 2031616 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 33550336 bytes
Disk label type: dos
Disk identifier: 0xc892cb8f
Device Boot Start End Blocks Id System
/dev/sdb1 65528 1113975 524224 83 Linux
Command (m for help): wq
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] ~]# mkfs.xfs /dev/sdb1 ##新建的分区可正常格式化
meta-data=/dev/sdb1 isize=256 agcount=4, agsize=32764 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=131056, 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
[[email protected] ~]# mount /dev/sdb1 /mnt ##新建的分区可以正常挂载并使用
[[email protected] ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/rhel-root 8706048 3006180 5699868 35% /
devtmpfs 492916 0 492916 0% /dev
tmpfs 508456 84 508372 1% /dev/shm
tmpfs 508456 7148 501308 2% /run
tmpfs 508456 0 508456 0% /sys/fs/cgroup
/dev/sda1 201388 145484 55904 73% /boot
tmpfs 101692 16 101676 1% /run/user/42
/dev/sr1 3947824 3947824 0 100% /var/www/html/rhtl7.2
tmpfs 101692 0 101692 0% /run/user/0
/dev/sdb1 520812 26368 494444 6% /mnt
[[email protected] ~]#
(8)撤回共享分区
分区永久挂载文件/etc/fstab
[[email protected] ~]# vim /etc/fstab
[[email protected] ~]#
/dev/sda1 /mnt xfs defaults,_netdev 0 0
撤回共享分区
(1)客户端
1》撤销登陆
[[email protected] ~]# iscsiadm -m node -T iqn.2018-11.com.westos:storage1 -p 172.25.254.168 -u
Logging out of session [sid: 30, target: iqn.2018-11.com.westos:storage1, portal: 172.25.254.168,3260]
Logout of [sid: 30, target: iqn.2018-11.com.westos:storage1, portal: 172.25.254.168,3260] successful.
[[email protected] ~]#
2》查看都有哪些共享设备
下载一个树服务以供查看
[[email protected] ~]# 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
Dependencies Resolved
查看
此时重启服务和后fdisk -l 发现/dev/sdb仍存在。是因为登陆数据未清除
3>删除数据
删除数据时若报错iscsiadm: Could not execute operation on all records: session exists可重新登陆后退出登陆再删除数据
[[email protected] ~]# iscsiadm -m node -T iqn.2018-11.com.westos:storage1 -p 172.25.254.168 -o delete ##删除数据
[[email protected] ~]# fdisk -l
Disk /dev/sda: 9663 MB, 9663676416 bytes, 18874368 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: 0x0007d593
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 18874367 9231360 8e Linux LVM
Disk /dev/mapper/rhel-root: 8925 MB, 8925478912 bytes, 17432576 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/mapper/rhel-swap: 524 MB, 524288000 bytes, 1024000 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
[[email protected] ~]#
[[email protected] ~]# tree /var/lib/iscsi/
4》服务端取消共享