iscsi 是直接对设备进行共享的服务

启动iscsi共享设备

在server上

建立一个lvm设备

[root@localhost ~]# 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 0x1d7d0413.


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): +1000M

Partition 1 of type Linux and of size 1000 MiB is set


Command (m for help): p


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: 0x1d7d0413


   Device Boot      Start         End      Blocks   Id  System

/dev/vdb1            2048     2050047     1024000   83  Linux


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/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: 0x1d7d0413


   Device Boot      Start         End      Blocks   Id  System

/dev/vdb1            2048     2050047     1024000   8e  Linux LVM


Command (m for help): wq

The partition table has been altered!


Calling ioctl() to re-read partition table.

Syncing disks.

[root@localhost ~]# partprobe

[root@localhost ~]# pvcreate /dev/vdb1

  Physical volume "/dev/vdb1" successfully created

[root@localhost ~]# vgcreate vg0 /dev/vdb1

  Volume group "vg0" successfully created

[root@localhost ~]# lvcreate -l 249 -n iscsi_storage vg0

  Logical volume "iscsi_storage" created

[root@localhost ~]# lvs

  LV            VG   Attr       LSize   Pool Origin Data%  Move Log Cpy%Sync Convert

  iscsi_storage vg0  -wi-a----- 996.00m                                             

[root@localhost ~]# yum install targetcli -y   安装targetcli服务

[root@localhost ~]# systemctl enable target

[root@localhost ~]# targetcli

/> 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 server1storage /dev/vg0/iscsi_storage 

/> /iscsi create iqn.2017-06.com.example:stragel

/> ls

o- / ..................................................................... [...]

  o- backstores .......................................................... [...]

  | o- block .............................................. [Storage Objects: 1]

  | | o- server1storage  [/dev/vg0/iscsi_storage (996.0MiB) write-thru deactivated]

  | o- fileio ............................................. [Storage Objects: 0]

  | o- pscsi .............................................. [Storage Objects: 0]

  | o- ramdisk ............................................ [Storage Objects: 0]

  o- iscsi ........................................................ [Targets: 1]

  | o- iqn.2017-06.com.example:stragel ............................... [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.2017-06.com.example:stragel/tpg1/luns create /backstores/block/server1storage 

/> /iscsi/iqn.2017-06.com.example:stragel/tpg1/acls create iqn.2017-06.com.example:server1storagekey

/> /iscsi/iqn.2017-06.com.example:stragel/tpg1/portals create 172.25.254.114

/> exit

在desktop中

[root@localhost ~]# vim /etc/iscsi/initiatorname.iscsi 

InitiatorName=iqn.2017-06.com.example:server1storagekey

[root@localhost ~]# iscsiadm -m discovery -t st -p 172.25.254.114

172.25.254.114:3260,1 iqn.2017-06.com.example:stragel

[root@localhost ~]# iscsiadm -m node -T iqn.2017-06.com.example:stragel -p 172.25.254.114 -l

Logging in to [iface: default, target: iqn.2017-06.com.example:stragel, portal: 172.25.254.114,3260] (multiple)

Login to [iface: default, target: iqn.2017-06.com.example:stragel, portal: 172.25.254.114,3260] successful.

[root@localhost ~]# fdisk -l

[root@localhost ~]# fdisk /dev/sda

 n

 p

wq

[root@localhost ~]# partprobe

[root@localhost ~]# mkfs.xfs /dev/sda

mkfs.xfs: /dev/sda appears to contain a partition table (dos).

mkfs.xfs: Use the -f option to force overwrite.

[root@localhost ~]# mkfs.xfs /dev/sda1

[root@localhost ~]# yum install tree.x86_64 x86_64 -y

[root@localhost ~]# tree -C /var/lib/iscsi/

/var/lib/iscsi/

├── ifaces

├── isns

├── nodes

│   └── iqn.2017-06.com.example:stragel

│       └── 172.25.254.114,3260,1

│           └── default

├── send_targets

│   └── 172.25.254.114,3260

│       ├── iqn.2017-06.com.example:stragel,172.25.254.114,3260,1,default -> /var/lib/iscsi/nodes/iqn.2017-06.com.example:stragel/172.25.254.114,3260,1

│       └── st_config

├── slp

└── static


10 directories, 2 files

挂载

UUID=''              /mnt  xfs  defaults,_netdev 0 0

                                        {先开启网络再挂载}

删除

umount /mnt

vim /etc/fstab



[root@localhost ~]# iscsiadm -m node -T iqn.2017-06.com.example:stragel -p 172.25.254.114 -u                     删除并且登出

Logging out of session [sid: 1, target: iqn.2017-06.com.example:stragel, portal: 172.25.254.114,3260]

Logout of [sid: 1, target: iqn.2017-06.com.example:stragel, portal: 172.25.254.114,3260] successful.

[root@localhost ~]# iscsiadm -m node -T iqn.2017-06.com.example:stragel -p 172.25.254.114 -o delete              删除配置文件

服务端删除

[root@localhost ~]# targetcli

targetcli shell version 2.1.fb34

Copyright 2011-2013 by Datera, Inc and others.

For help on commands, type 'help'.


/> clearconfig confirm=True

/> 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]

/>