[root@shenzhen ~]# targetcli
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'.
/> ls
o- / ..................................................................... [...]
o- backstores .......................................................... [...]
| o- block .............................................. [Storage Objects: 0]
| o- fileio ............................................. [Storage Objects: 0]
| o- pscsi .............................................. [Storage Objects: 0]
| o- ramdisk ............................................ [Storage Objects: 0]
o- iscsi ........................................................ [Targets: 0]
o- loopback ..................................................... [Targets: 0]
/> /backstores/block(存储数据的目录名) create westos:storage1(别名) /dev/iscsi_vg0/iscsi_lv0 (存储数据的设备)
------->在目录下创建一个iscsi设备
Created block storage object westos:storage1 using /dev/iscsi_vg0/iscsi_lv0.
<7>指定iscsi限定名称
iqn 全局唯一名称的命名方式
iqn.2019-03.com.westos:storage1
iqn开头.年-月.域名的反写:设备名
/> iscsi/ create iqn.2019-03.com.westos:storage1------>指定iscsi限定名称
Created target iqn.2019-03.com.westos:storage1.
Created TPG 1.
<8>指定存储数据的路径
/> iscsi/iqn.2019-03.com.westos:storage1/tpg1/luns create /backstores/block/westos:storage1
------>在iqn上存储数据实际上是存储在/backstores/block/westos:storage1文件里,而他又会将数据保存在LVM设备上,所以这一步是指定存储数据的路径
Created LUN 0.
<9>安全认证,生成锁
安全认证 一个密钥,别人访问之前要输入密钥
/> iscsi/iqn.2019-03.com.westos:storage1/tpg1/acls create iqn.2019-03.com.westos:storage1key
---->安全认证,生成锁
Created Node ACL for iqn.2019-02.com.westos:storage1key
Created mapped LUN 0.
<10>指定服务端的ip,exit 退出
设备在那台主机ip写哪
/> iscsi/iqn.2019-03.com.westos:storage1/tpg1/portals create 172.25.254.127
----->指定服务端的ip
Using default IP port 3260
Created network portal 172.25.254.127:3260.
<1>查看客户端原来存在的设备
[root@localhost ~]# fdisk -l
<2>安装软件
[root@localhost ~]# yum install iscsi-initiator-utils.x86_64 -y
[root@localhost ~]# iscsiadm -m discovery -t st -p 172.25.254.127
iscsiadm: cannot make connection to 172.25.254.127: No route to host
iscsiadm: cannot make connection to 172.25.254.127: No route to host
iscsiadm: cannot make connection to 172.25.254.127: No route to host
^Ciscsiadm: caught SIGINT, exiting...
这里报错是火墙开着服务端没有添加端口的原因,如果服务端关闭火墙后者添加端口则可以添加
iscsiadm -m动作 discovery 寻找 -t st(磁盘或者设备) -p 172.25.254.127服务端的ip
no route to host 可能是因为火墙没关或没加端口
<4>服务端添加端口
[root@shenzhen ~]# firewall-cmd --permanent --add-port=3260/tcp #添加端口
success
[root@shenzhen ~]# firewall-cmd --reload ##重新加载火墙策略
success
[root@shenzhen ~]# firewall-cmd --list-all
public (default, active)
interfaces: eth0
sources:
services: dhcpv6-client http mountd nfs rpc-bind ssh
ports: 3260/tcp 8080/tcp
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
<5>客户端添加iscsi设备
[root@localhost ~]# iscsiadm -m discovery -t st -p 172.25.254.127
172.25.254.127:3260,1 iqn.2019-02.com.westos:storage1
iscsiadm -m node指定节点 -T 指定设备 iqn.2019-02.com.westos:storage
[root@localhost ~]# iscsiadm -m node -T iqn.2019-02.com.westos:storage1 -p 172.25.254.127 -l
Logging in to [iface: default, target: iqn.2019-02.com.westos:storage1, portal: 172.25.254.127,3260] (multiple)
iscsiadm: Could not login to [iface: default, target: iqn.2019-02.com.westos:storage1, portal: 172.25.254.127,3260].
iscsiadm: initiator reported error (24 - iSCSI login failed due to authorization failure)
iscsiadm: Could not log into all portals
<7>编辑配置文件,将服务端设置的iscsi设备和钥匙写如文件中
[root@localhost ~]# vim /etc/iscsi/initiatorname.iscsi
[root@localhost ~]# systemctl restart iscsid
InitiatorName=iqn.2019-03.com.westos:storage1key
<8>再次登录登录成功
[root@localhost ~]# iscsiadm -m node -T iqn.2019-02.com.westos:storage1 -p 172.25.254.127 -l
Logging in to [iface: default, target: iqn.2019-02.com.westos:storage1, portal: 172.25.254.127,3260] (multiple)
Login to [iface: default, target: iqn.2019-02.com.westos:storage1, portal: 172.25.254.127,3260] successful.
<9>将服务端的设备添加称自己的分区并格式化
[root@localhost ~]# fdisk -l
客户端此时查看存在的设备,多出了一个设备,这是从服务端通不过来的iscsi设备
[root@localhost ~]# fdisk /dev/sda
[root@localhost ~]# mkfs.xfs /dev/sda1
meta-data=/dev/sda1 isize=256 agcount=8, agsize=32512 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=260096, 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
<10> 挂载共享存储设备,建立文件
[root@localhost ~]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/vda1 10473900 3153436 7320464 31% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 80 942568 1% /dev/shm
tmpfs 942648 17016 925632 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
[root@localhost ~]# mount /dev/sda1 /mnt
[root@localhost ~]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/vda1 10473900 3153376 7320524 31% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 80 942568 1% /dev/shm
tmpfs 942648 17016 925632 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
/dev/sda1 1036972 33056 1003916 4% /mnt
[root@localhost ~]# touch /mnt/file
<1>新建分区,修改分区的标签,同步分区表
[root@shenzhen ~]# fdisk /dev/vdb
[root@shenzhen ~]# partprobe
device-mapper: reload ioctl on iscsi_vg0-iscsi_lv0p1 failed: 无效的参数
device-mapper: remove ioctl on iscsi_vg0-iscsi_lv0p1 failed: 没有那个设备或地址
这里partprobe报错是因为客户端没有卸载设备
<2>扩展LVM
[root@shenzhen ~]# pvcreate /dev/vdb2
Physical volume "/dev/vdb2" successfully created
[root@shenzhen ~]# vgextend iscsi_vg0 /dev/vdb2
Volume group "iscsi_vg0" successfully extended
[root@shenzhen ~]# vgdisplay iscsi_vg0
--- Volume group ---
VG Name iscsi_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 1.99 GiB
PE Size 4.00 MiB
Total PE 510
Alloc PE / Size 255 / 1020.00 MiB
Free PE / Size 255 / 1020.00 MiB
VG UUID y6Piym-8klf-VbxO-rO4Y-2LQA-4C7f-0zY5Af
[root@shenzhen ~]# lvextend -l 510 /dev/iscsi_vg0/iscsi_lv0
Extending logical volume iscsi_lv0 to 1.99 GiB
Logical volume iscsi_lv0 successfully resized
[root@localhost ~]# yum install tree
[root@localhost ~]# tree /var/lib/iscsi/
/var/lib/iscsi/
|-- ifaces
|-- isns
|-- nodes
| `-- iqn.2019-02.com.westos:storage1
| `-- 172.25.254.127,3260,1
| `-- default
|-- send_targets
| `-- 172.25.254.127,3260
| |-- iqn.2019-02.com.westos:storage1,172.25.254.127,3260,1,default ->
/var/lib/iscsi/nodes/iqn.2019-02.com.westos:storage1/172.25.254.127,3260,1
| `-- st_config
|-- slp
`-- static
10 directories, 2 files
[root@localhost ~]# umount /mnt
[root@localhost ~]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/vda1 10473900 3155204 7318696 31% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 80 942568 1% /dev/shm
tmpfs 942648 17016 925632 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
[root@localhost ~]# iscsiadm -m node -T iqn.2019-02.com.westos:storage1 -p 172.25.254.127 -u
Logging out of session [sid: 2, target: iqn.2019-02.com.westos:storage1, portal: 172.25.254.127,3260]
Logout of [sid: 2, target: iqn.2019-02.com.westos:storage1, portal: 172.25.254.127,3260] successful.
[root@localhost ~]# iscsiadm -m node -T iqn.2019-02.com.westos:storage1 -p 172.25.254.127 -l
Logging in to [iface: default, target: iqn.2019-02.com.westos:storage1, portal: 172.25.254.127,3260] (multiple)
Login to [iface: default, target: iqn.2019-02.com.westos:storage1, portal: 172.25.254.127,3260] successful.
网络设备要自动激活需要手动添加参数,再/etc/fstab/defaults,_netdev
<1>编辑/etc/fstab 文件
[root@localhost ~]# vim /etc/fstab
/dev/sda1 /mnt xfs defaults 0 0
<2>df查看已挂载的设备
[root@localhost ~]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/vda1 10473900 3155356 7318544 31% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 80 942568 1% /dev/shm
tmpfs 942648 17016 925632 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
<3>激活设备,再次查看已挂载
*第一次关闭时需要手动强行关闭,开机的时候也会出现问题,设备挂载问题
因为系统中设备挂载的读取比网络服务优先
*解决办法:
可以强制关机在此开机时进入挽救模式
chroot /mnt/sysimage/挂载获得shell
vim /etc/fstab
将defaults改成defaults,_netdev------->表示先激活网络服务再读取设备
关闭selinux,加速开机启动
两次exit退出
开机后查看到自动挂载
1)临时删除
<1>登出iscsi共享设备,同时自动卸载
[root@localhost ~]# iscsiadm -m node -T iqn.2019-02.com.westos:storage1 -p 172.25.254.127 -u
Logging out of session [sid: 3, target: iqn.2019-02.com.westos:storage1, portal: 172.25.254.127,3260]
Logout of [sid: 3, target: iqn.2019-02.com.westos:storage1, portal: 172.25.254.127,3260] successful.
<2>查看系统中真实存在的设备,没有iscsi设备的信息
[root@localhost ~]# fdisk -l
<3>重启服务后再次查看系统中存在的设备,iscsi设备有自动挂载
[root@localhost ~]# systemctl restart iscsi
[root@localhost ~]# fdisk -l
2) 永久删除
<1>先登出设备,否则删除的时候会报错
[root@localhost ~]# iscsiadm -m node -T iqn.2019-02.com.westos:storage1 -p 172.25.254.127 -u
<2>查看/var/lib/iscsi文件的树结构,仍然存在已经登出的iscsi设备的信息
[root@localhost ~]# tree /var/lib/iscsi/
<3>永久删除设备,/var/lib/iscsi文件的树结构中没有iscsi设备的信息
[root@localhost ~]# iscsiadm -m node -T iqn.2019-02.com.westos:storage1 -p 172.25.254.127 -o delete
[root@localhost ~]# tree /var/lib/iscsi/
<4>重启服务,/var/lib/iscsi/文件中没有已删除设备的信息,fdisk -l系统中存在的设备也没有iscsi设备的信息
[root@localhost ~]# systemctl restart iscsi
[root@localhost ~]# tree /var/lib/iscsi/
[root@localhost ~]# fdisk -l