1、创建20G共享磁盘(此步骤在实际环境中是为server提供一个单独磁盘)
[root@room1pc01 ISO]# qemu-img create -f qcow2 iscsi.img 20G
Formatting 'iscsi.img', fmt=qcow2 size=21474836480 encryption=off cluster_size=65536
2、Server端挂载磁盘并设置分区(但不要格式化)
[root@node1 ~]# lsblk ##挂载磁盘前
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 50G 0 disk
├─vda1 252:1 0 500M 0 part /boot
└─vda2 252:2 0 49.5G 0 part
├─VolGroup-lv_root (dm-0) 253:0 0 47.6G 0 lvm /
└─VolGroup-lv_swap (dm-1) 253:1 0 2G 0 lvm [SWAP]
[root@node1 ~]# lsblk ##挂载磁盘后,发现新磁盘sda
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 50G 0 disk
├─vda1 252:1 0 500M 0 part /boot
└─vda2 252:2 0 49.5G 0 part
├─VolGroup-lv_root (dm-0) 253:0 0 47.6G 0 lvm /
└─VolGroup-lv_swap (dm-1) 253:1 0 2G 0 lvm [SWAP]
sda 8:0 0 20G 0 disk
[root@node1 ~]# parted /dev/sda
GNU Parted 2.1
使用 /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt
警告: The existing disk label on /dev/sda will be destroyed and all data on this disk will be lost. Do you want to continue?
是/Yes/否/No? yes
(parted) mkpart primary 1M -1
(parted) print
Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name 标志
1 1049kB 21.5GB 21.5GB primary
(parted) quit
信息: You may need to update /etc/fstab.
[root@node1 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 50G 0 disk
├─vda1 252:1 0 500M 0 part /boot
└─vda2 252:2 0 49.5G 0 part
├─VolGroup-lv_root (dm-0) 253:0 0 47.6G 0 lvm /
└─VolGroup-lv_swap (dm-1) 253:1 0 2G 0 lvm [SWAP]
sda 8:0 0 20G 0 disk
└─sda1 8:1 0 20G 0 part
3、Server端安装scsi-target-utils
[root@node1 ~]# yum -y install scsi-target-utils
4、修改配置文件,并启动服务
[root@node1 ~]# tail -4 /etc/tgt/targets.conf
<target iqn.2018-04.com.example.node1:server.target1>
backing-store /dev/sda1
initiator-address 192.168.4.102 #权限控制,允许此IP访问。开放访问用ALL。
</target>
[root@node1 ~]# /etc/init.d/tgtd start;chkconfig tgtd on
[root@node1 ~]# tgt-admin -s
Target 1: iqn.2018-04.com.example.node1:server.target1
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: 21473 MB, Block size: 512
Online: Yes
Removable media: No
Prevent removal: No
Readonly: No
Backing store type: rdwr
Backing store path: /dev/sda1
Backing store flags:
Account information:
ACL information:
192.168.4.102
192.168.2.102
5、Client端安装iscsi-initiator-utils
[root@node2 ~]# yum -y install iscsi-initiator-utils
[root@node2 ~]# lsblk ##client未加载共享磁盘前状态
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 50G 0 disk
├─vda1 252:1 0 500M 0 part /boot
└─vda2 252:2 0 49.5G 0 part
├─VolGroup-lv_root (dm-0) 253:0 0 47.6G 0 lvm /
└─VolGroup-lv_swap (dm-1) 253:1 0 2G 0 lvm [SWAP]
6、启动iscsid服务,查看Server共享
[root@node2 ~]# /etc/init.d/iscsid start
[root@node2 ~]# iscsiadm --mode discoverydb --type sendtargets --portal 192.168.4.101 --discover
192.168.4.101:3260,1 iqn.2018-04.com.example.node1:server.target1
7、启动iscsi服务,自动挂载Server共享磁盘并查看挂载后状态
[root@node2 ~]# /etc/init.d/iscsi start;chkconfig iscsi on
正在启动 iscsi: [确定]
[root@node2 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 50G 0 disk
├─vda1 252:1 0 500M 0 part /boot
└─vda2 252:2 0 49.5G 0 part
├─VolGroup-lv_root (dm-0) 253:0 0 47.6G 0 lvm /
└─VolGroup-lv_swap (dm-1) 253:1 0 2G 0 lvm [SWAP]
sda 8:0 0 20G 0 disk
8、分区并格式化新发现的sda磁盘
[root@node2 ~]# parted /dev/sda mklabel gpt
警告: The existing disk label on /dev/sda will be destroyed and all data on this disk will be lost. Do you want to continue?
是/Yes/否/No? yes
信息: You may need to update /etc/fstab.
[root@node2 ~]# parted /dev/sda mkpart priamry 1M 1024
信息: You may need to update /etc/fstab.
[root@node2 ~]# parted /dev/sda print
Model: IET VIRTUAL-DISK (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name 标志
1 1049kB 1024MB 1023MB priamry
[root@node2 ~]# mkfs.ext4 /dev/sda1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
62464 inodes, 249856 blocks
12492 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=255852544
8 block groups
32768 blocks per group, 32768 fragments per group
7808 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
正在写入inode表: 完成
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
32768, 98304, 163840, 229376
正在写入inode表: 完成
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
9、挂载新分区到mysql应用目录,即可使用ISCSI服务提供的存储
[root@node2 ~]# mount /dev/sda1 /var/lib/mysql/
10、把挂载动作写入/etc/fstab
[root@node2 ~]# tail -1 /etc/fstab
/dev/sda1 /var/lib/mysql ext4 defaults 0 0