Linux下iscsi的配置与使用

来源:互联网 发布:淘宝个人网店怎么注册 编辑:程序博客网 时间:2024/06/05 00:36

iscsi 配置

SCSI目标配置

安装iSCSI目标软件包:

# yum install -y targetcli

启动服务:

# systemctl enable target; systemctl start target

进入iSCSI目标交互式配置模式:

# targetcli

www.westos.org

8Demo

/> 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 server0.disk1 /dev/iSCSI_vg/disk1_lv

Created block storage object server0.disk1 using /dev/iSCSI_vg/disk1_lv.

/> /iscsi create iqn.2014-12.com.example:server0

Created target iqn.2014-12.com.example:server0.

Created TPG 1.

www.westos.org

/> /iscsi/iqn.2014-12.com.example:server0/tpg1/acls create iqn.2014-12.com.example:desktop0

Created Node ACL for iqn.2014-12.com.example:desktop0

/> /iscsi/iqn.2014-12.com.example:server0/tpg1/luns create /backstores/block/server0.disk1

Created LUN 0.

Created LUN 0->0 mapping in node ACL iqn.2014-12.com.example:desktop0

/> /iscsi/iqn.2014-12.com.example:server0/tpg1/portals create 172.25.0.11

Using default IP port 3260

Created network portal 172.25.0.11:3260.

/> exit

1访问iSCSI存储

安装iSCSI发起端软件包:

# yum install -y iscsi-initiator-utils

/etc/iscsi/initiatorname.iscsi中设置发起端的IQN:

InitiatorName=iqn.2014-12.com.example:desktop0

查找iSCSI服务器所提供的iSCSI目标(目标门户)

# iscsiadm -m discovery -t st -p 172.25.0.11

 

登录服务器上的一个或多个iscsi目标

# iscsiadm -m node -T iqn.2024-12.com.example:server0 -p

172.25.0.11 -l

2

此时,可以使用iSCSI磁盘,就好像它是本地连接硬盘驱动器。

可以挂载现有文件系统。如果磁盘未格式化,可以通过fdisk进行分区,例如,通过文件系统格式化分区或作为LVM物理卷。

/etc/fstab中永久挂载文件系统

1). 使用blkid确定文件系统UUID并使用UUID挂载,而不是/dev/sd*设备名称。(每次

引导时显示的设备名称都不同,具体取决于iSCSI设备通过网络进行响应的顺序。

如果按设备名称挂载,这会导致使用错误的设备。)

2). 在/etc/fstab中使用_netdev作为挂载选项。(这将确保客户端不会尝试挂载文件系

,直至启用联网。否则,在引导时系统将出错。)

3). 确保iscsi服务在引导时启动。

12中断使用iSCSI目标

确保没有使用目标所提供的任何设备。

确保从/etc/fstab等位置中删除使用目标的所有永久挂载。

登出iSCSI目标,以暂时断开连接。

# iscsiadm -m node -T iqn.2010-09.com.example:rdisks.demo -p 192.168.0.254 -u

删除iSCSI目标的本地记录,以永久断开连接。

# iscsiadm -m node -T iqn.2010-09.com.example:rdisks.demo -p 192.168.0.254 -o delete

操作实例

服务端

[root@server etc]# yum install targetcli -y

Loaded plugins: langpacks

Resolving Dependencies

--> Running transaction check

---> Package targetcli.noarch 0:2.1.fb34-1.el7 will be installed

--> Processing Dependency: python-rtslib >= 2.1.fb41 for package: targetcli-2.1.fb34-1.el7.noarch

--> Processing Dependency: python-configshell for package: targetcli-2.1.fb34-1.el7.noarch

--> Running transaction check

---> Package python-configshell.noarch 1:1.1.fb11-3.el7 will be installed

--> Processing Dependency: pyparsing for package: 1:python-configshell-1.1.fb11-3.el7.noarch

--> Processing Dependency: python-urwid for package: 1:python-configshell-1.1.fb11-3.el7.noarch

---> Package python-rtslib.noarch 0:2.1.fb46-1.el7 will be installed

--> Processing Dependency: python-kmod for package: python-rtslib-2.1.fb46-1.el7.noarch

--> Running transaction check

---> Package pyparsing.noarch 0:1.5.6-9.el7 will be installed

---> Package python-kmod.x86_64 0:0.9-4.el7 will be installed

---> Package python-urwid.x86_64 0:1.1.1-3.el7 will be installed

--> Finished Dependency Resolution

 

Dependencies Resolved

 

================================================================

 Package             Arch    Version            Repository

                                                           Size

================================================================

Installing:

 targetcli           noarch  2.1.fb34-1.el7     rhel_dvd   55 k

Installing for dependencies:

 pyparsing           noarch  1.5.6-9.el7        rhel_dvd   94 k

 python-configshell  noarch  1:1.1.fb11-3.el7   rhel_dvd   64 k

 python-kmod         x86_64  0.9-4.el7          rhel_dvd   57 k

 python-rtslib       noarch  2.1.fb46-1.el7     rhel_dvd   75 k

 python-urwid        x86_64  1.1.1-3.el7        rhel_dvd  654 k

 

Transaction Summary

================================================================

Install  1 Package (+5 Dependent packages)

 

Total download size: 998 k

Installed size: 3.9 M

Downloading packages:

(1/6): python-configshell-1.1.fb11-3.el7.n |  64 kB   00:00     

(2/6): pyparsing-1.5.6-9.el7.noarch.rpm    |  94 kB   00:00     

(3/6): python-kmod-0.9-4.el7.x86_64.rpm    |  57 kB   00:00     

(4/6): python-rtslib-2.1.fb46-1.el7.noarch |  75 kB   00:00     

(5/6): targetcli-2.1.fb34-1.el7.noarch.rpm |  55 kB   00:00     

(6/6): python-urwid-1.1.1-3.el7.x86_64.rpm | 654 kB   00:00     

----------------------------------------------------------------

Total                              5.5 MB/s | 998 kB  00:00     

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

  Installing : python-kmod-0.9-4.el7.x86_64                 1/6

  Installing : python-rtslib-2.1.fb46-1.el7.noarch          2/6

  Installing : pyparsing-1.5.6-9.el7.noarch                 3/6

  Installing : python-urwid-1.1.1-3.el7.x86_64              4/6

  Installing : 1:python-configshell-1.1.fb11-3.el7.noarch   5/6

  Installing : targetcli-2.1.fb34-1.el7.noarch              6/6

  Verifying  : python-rtslib-2.1.fb46-1.el7.noarch          1/6

  Verifying  : python-urwid-1.1.1-3.el7.x86_64              2/6

  Verifying  : targetcli-2.1.fb34-1.el7.noarch              3/6

  Verifying  : 1:python-configshell-1.1.fb11-3.el7.noarch   4/6

  Verifying  : pyparsing-1.5.6-9.el7.noarch                 5/6

  Verifying  : python-kmod-0.9-4.el7.x86_64                 6/6

 

Installed:

  targetcli.noarch 0:2.1.fb34-1.el7                             

 

Dependency Installed:

  pyparsing.noarch 0:1.5.6-9.el7                                

  python-configshell.noarch 1:1.1.fb11-3.el7                    

  python-kmod.x86_64 0:0.9-4.el7                                

  python-rtslib.noarch 0:2.1.fb46-1.el7                         

  python-urwid.x86_64 0:1.1.1-3.el7                             

 

Complete!

[root@server etc]# 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 0xf14beb9f.

 

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 (2048-20971519, default 2048):

Using default value 2048

Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +500M

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

 

Command (m for help): wq

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

[root@server etc]# partprobe

[root@server etc]# targetcli

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

Created block storage object westos:storage1 using /dev/vdb1.

/> iscsi create iqn.2017-05.com.example:strage1.

Command not found iscsi

/> /iscsi create iqn.2017-05.com.example:strage1.

Created target iqn.2017-05.com.example:strage1..

Created TPG 1.

/> /iscsi/iqn.2017-05.com.example:key1

No such path /iscsi/iqn.2017-05.com.example:key1

/> /iscsi/iqn.2017-05.com.example:strage1/tpg1/acls creat iqn.2017-05.com.example:key1

No such path /iscsi/iqn.2017-05.com.example:strage1

/> /iscsi iqn.2017-05.com.example:strage1/tpg1/acls creat iqn.2017-05.com.example:key1

Command not found iqn

/> /iscsi/iqn.2017-05.com.example:strage1/tpg1/acls creat iqn.2017-05.com.example:key1

/> /iscsi/iqn.2017-05.com.example:strage1/tpg1/acls creat iqn.20/> /iscsi/iqn.2017-05.com.example:strage1./tpg1/acls creae iqn.2017-05.com.example:key1

Command not found creae

/> /iscsi/iqn.2017-05.com.example:strage1./tpg1/acls create iqn.2017-05.com.example:key1

Created Node ACL for iqn.2017-05.com.example:key1

/> /iscsi/iqn.2017-05.com.example:strage1./tpg1/luns create /backstores/block/westos:storage1

Created LUN 0.

Created LUN 0->0 mapping in node ACL iqn.2017-05.com.example:key1

/> /iscsi/iqn.2017-05.com.example:strage1./tpg1/portals

/iscsi/iqn.20.../tpg1/portals> create 172.25.37.11

Using default IP port 3260

Created network portal 172.25.37.11:3260.

/iscsi/iqn.20.../tpg1/portals>

/iscsi/iqn.20.../tpg1/portals> exit

Global pref auto_save_on_exit=true

Last 10 configs saved in /etc/target/backup.

Configuration saved to /etc/target/saveconfig.json

客户端

[root@desktop37 ~]# yum search iscsi

Loaded plugins: langpacks

=========================== N/S matched: iscsi ============================

iscsi-initiator-utils.i686 : iSCSI daemon and utility programs

iscsi-initiator-utils.x86_64 : iSCSI daemon and utility programs

iscsi-initiator-utils-iscsiuio.x86_64 : Userspace configuration daemon

                                      : required for some iSCSI hardware

libiscsi.i686 : iSCSI client library

libiscsi.x86_64 : iSCSI client library

 

  Name and summary matches only, use "search all" for everything.

[root@desktop37 ~]# yum install iscsi-initiator-utils.x86_64

Loaded plugins: langpacks

Package iscsi-initiator-utils-6.2.0.873-21.el7.x86_64 already installed and latest version

Nothing to do

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

InitiatorName=iqn.2017-05.com.example:key1   ##新建的钥匙

[root@desktop37 ~]# systemctl restart iscsi

[root@desktop37 ~]# iscsiadm -m discovery -t st -p 172.25.37.11

172.25.37.11:3260,1 iqn.2017-05.com.example:strage1.

[root@desktop37 ~]# iscsiadm -m node -T iqn.2017-05.com.example:strage1. -p 172.25.37.11 -l

Logging in to [iface: default, target: iqn.2017-05.com.example:strage1., portal: 172.25.37.11,3260] (multiple)

Login to [iface: default, target: iqn.2017-05.com.example:strage1., portal: 172.25.37.11,3260] successful.

[root@desktop37 ~]# 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 /dev/sda: 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 / 4194304 bytes

 

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

 

Command (m for help):

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 (8192-1023999, default 8192):

Using default value 8192

Last sector, +sectors or +size{K,M,G} (8192-1023999, default 1023999): +100M

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

 

Command (m for help): wq

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

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

meta-data=/dev/sda1              isize=256    agcount=4, agsize=6400 blks

         =                       sectsz=512   attr=2, projid32bit=1

         =                       crc=0

data     =                       bsize=4096   blocks=25600, 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@desktop37 ~]# df

Filesystem     1K-blocks    Used Available Use% Mounted on

/dev/vda1       10473900 3174412   7299488  31% /

devtmpfs          927072       0    927072   0% /dev

tmpfs             942660      80    942580   1% /dev/shm

tmpfs             942660   17024    925636   2% /run

tmpfs             942660       0    942660   0% /sys/fs/cgroup

[root@desktop37 ~]# mount /dev/sda1 /mnt

[root@desktop37 ~]# touch file

(2)卸载服务

客户端

[root@desktop37 ~]# iscsiadm -m node -T iqn.2017-05.com.example:strage1. -p 172.25.37.11 -u

Logging out of session [sid: 1, target: iqn.2017-05.com.example:strage1., portal: 172.25.37.11,3260]

Logout of [sid: 1, target: iqn.2017-05.com.example:strage1., portal: 172.25.37.11,3260] successful.

[root@desktop37 ~]# iscsiadm -m node -T iqn.2017-05.com.example:strage1. -p 172.25.37.11 -o delete

[root@desktop37 ~]# 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

服务端

[root@server etc]# targetcli

targetcli shell version 2.1.fb34

Copyright 2011-2013 by Datera, Inc and others.

For help on commands, type 'help'.

/> clearconfig confirm=True

All configuration cleared

/> exit

Global pref auto_save_on_exit=true

Last 10 configs saved in /etc/target/backup.

Configuration saved to /etc/target/saveconfig.json

[root@server etc]# targetcli

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]

/>

 

0 0
原创粉丝点击