在Ubuntu 12.04上配置iSCSI Target服务

By , March 25, 2013 2:29 pm

今天自己按照网上搜来的教程自己在Ubuntu 12.04上配置了iSCSI Target服务,在这里简单地做个纪录。操作系统是全新安装的Ubuntu 12.04,配置一块500 GB的SATA笔记本硬盘。安装操作系统时将硬盘划分为两个分区,其中一个50 GB的分区(/dev/sda1)挂载为操作系统的根分区;另外一个450 GB的分区(/dev/sda2)作为提供存储服务的空间,暂时不挂载。

安装完操作系统之后,安装必要的软件:

$ sudo apt-get install chkconfig lvm2 iscsitarget iscsitarget-dkms

创建物理卷/dev/sda2:

$ sudo pvcreate /dev/sda2

在物理卷/dev/sda2上创建一个volume group:

$ sudo vgcreate -s 32M vg_target00 /dev/sda2

看一下刚才创建的volume group:

$ sudo vgdisplay

--- Volume group ---
VG Name vg_target00
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 419.19 GiB
PE Size 32.00 MiB
Total PE 13414
Alloc PE / Size 0 / 0
Free PE / Size 13414 / 419.19 GiB
VG UUID vZplQx-DRjE-Kd4k-tr4e-RV6m-nIr2-k7SEvt

在如上volume group中创建一个逻辑卷lv_target00:

$ sudo lvcreate -L 50G -n lv_target00 vg_target00

看一下刚才创建的逻辑卷:

$ sudo lvdisplay

--- Logical volume ---
LV Name /dev/vg_target00/lv_target00
VG Name vg_target00
LV UUID gD9RO2-xAqr-3u5c-mM1H-O3l8-E3ra-XRT9QT
LV Write Access read/write
LV Status available
# open 0
LV Size 50.00 GiB
Current LE 1600
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0

修改文件/etc/default/iscsitarget,启用iSCSI Target服务:

ISCSITARGET_ENABLE=true

修改文件/etc/iet/ietd.conf,配置iSCSI Target服务的参数(这里假定使用iSCSI Target服务的机器IP为192.168.100.104):

Target iqn.2013-03.world.server:target0
Lun 0 Path=/dev/mapper/vg_target00-lv_target00,Type=blockio
initiator-address 192.168.100.104
incominguser test_user test_pass

重启一下iscsitarget服务:

$ sudo service iscsitarget restart

将iscsitarget服务设置为开机后自动启动:

$ sudo chkconfig iscsitarget on

看一下刚刚启动的iscsitarget服务:

$ sudo ietadm --op show --tid=1

Wthreads=8
Type=0
QueuedCommands=32
NOPInterval=0
NOPTimeout=0

到这里为止,iSCSI Target服务就已经配置完毕了。接下来我们在需要挂载iSCSI Target的机器(也称为iSCSI Initiator)上做一些配置:

$ sudo apt-get install open-iscsi

修改配置文件/etc/iscsi/iscsid.conf:

# line 39: uncomment
node.session.auth.authmethod = CHAP

# line 43,44: uncomment and set username and password which set on iSCSI Target
node.session.auth.username = test_user
node.session.auth.password = test_pass

使用下面这个命令发现iSCSI Target服务(这里假定提供iSCSI Target服务的机器IP为192.168.100.5):

$ sudo iscsiadm -m discovery -t sendtargets -p 192.168.100.5 
192.168.100.5:3260,1 iqn.2013-03.world.server:target0

使用下面这个命令确认iSCSI Target服务的具体信息:

$ sudo iscsiadm -m node -o show

使用下面这个命令登陆到iSCSI Target服务:

$ sudo iscsiadm -m node --login

使用下面这个命令确认登录session的情况:

$ sudo iscsiadm -m session -o show

这个时候你应该能够通过/proc/partitions看到多出来一个分区(sdb):

$ more /proc/partitions

major minor  #blocks  name

  11        0    1048575 sr0
   8        0    8388608 sda
   8        1     248832 sda1
   8        2          1 sda2
   8        5    8136704 sda5
 252        0    7581696 dm-0
 252        1     520192 dm-1
   8       16   52428800 sdb

也可以使用fdisk命令查看分区情况,应该可以看到/dev/sdb的相关信息:

$ sudo fdisk -l

Disk /dev/sdb: 53.7 GB, 53687091200 bytes
64 heads, 32 sectors/track, 51200 cylinders, total 104857600 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 identifier: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table

这个时候就可以正常地格式化、挂载、和使用/dev/sdb了。

如果不打算继续使用这个分区了,可以从iSCSI Target退出服务:

$ sudo iscsiadm -m node --logout

7 Responses to “在Ubuntu 12.04上配置iSCSI Target服务”

  1. 你好,您的这个Blog内容很详细,很有操作性。我能否转载一下,注明作者和出处。我的Blog地址:blog.csdn.net/lynn_kong
    期待您的回答!

  2. 我再补充一点吧:
    在使用iSCSI时,可以设置自动登陆目标器
    1. 对于已经存在于数据库中的node,使用iscsiadm更新纪录项:
    # iscsiadm -m node -p 192.168.122.102 –op update -n node.conn[0].startup -v automatic
    2. 对于还没有添加的节点,可以编辑配置文件/etc/iscsi/iscsid.conf,在其中增加一项:
    node.conn[0].startup = automatic
    这样以后通过discovery新添加的节点node.conn[0].startup都会被设置为automatic

    设置自动登陆目标器后,每次使用init脚本启动iscsi服务时都会自动登入目标器

  3. qyjohn says:

    谢谢补充。

  4. [...] 测试环境包含三台服务器。其中一台服务器配置iSCSI TGT山寨一个IP SAN服务,将一块本地硬盘通过iSCSI协议暴露给所有的服务器。三台服务器均通过openi-scsi发现山寨IP SAN所提供的iSCSI存储。具体的配置方法可以蚕茧《在Ubuntu 12.04上配置iSCSI Target服务》一文。 [...]

  5. [...] 测试环境包含三台服务器。其中一台服务器配置iSCSI TGT山寨一个IP SAN服务,将一块本地硬盘通过iSCSI协议暴露给所有的服务器。三台服务器均通过openi-scsi发现山寨IP SAN所提供的iSCSI存储。具体的配置方法可以蚕茧《在Ubuntu 12.04上配置iSCSI Target服务》一文。 [...]

  6. [...] 测试环境包含三台服务器。其中一台服务器配置iSCSI TGT山寨一个IP SAN服务,将一块本地硬盘通过iSCSI协议暴露给所有的服务器。三台服务器均通过openi-scsi发现山寨IP SAN所提供的iSCSI存储。具体的配置方法可以参见《在Ubuntu 12.04上配置iSCSI Target服务》一文。 [...]

  7. steven wu says:

    In the iscsi target machine, with vgcreate and lvcreate commands, created /dev/vg-one/lv-one as the iscsi storage.

    1) in the file /etc/iet/ietd.conf, for the Target and Lun lines, specify them as below:

    Target iqn.2013-12.local.iSCSIHost:vg-one.lv-one
    Lun 0 Path=/dev/vg-one/lv-one,Type=blockio

    is this ok? the Target needs to be “iqn.2013-12.local.iSCSIHost:vg-one.lv-one” or “iqn.2013-12.local.iSCSIHost:lv-one”? the Type needs to be fileio or blockio? for the Target, is it ok to remove the “.local” from “iqn.2013-12.local.iSCSIHost:vg-one.lv-one”?

    If the Target and Lun specified in the /etc/iet/ietd.conf are incorrect, how to specify them correctly?

    Note: the target machine host specified in /etc/hosts is “192.168.1.1 iSCSIHost.localhost iSCSIHost”

    2) in the iscsi initiator machine, follow all the steps upto “more /proc/partitions”, in more that one day testing, only once can see the “sdb”. reboot the iscsi target machine and iscsi initiator machine for more than 15 times and follow the steps for more than 15 times, but can never replicate this, i.e. can’t see the “sdb” after run “more /proc/partitions”.

    very confused about this. it is much appreciated if you can help with this.

    Note: the iscsi target machine is a VM created within this iscsi initiator machine. from the information got from Internet, it looks ok to create VM within iscsi initiator machine as target machine.

    thanks for the help !

Leave a Reply

Panorama Theme by Themocracy