You are on page 1of 9

Linux tgtadm: Setup iSCSI Target ( SAN )

1 of 9

http://www.cyberciti.biz/tips/howto-setup-linux-iscsi-target-sanwith-tgt.html

About
Forum
Howtos & FAQs
Low graphics
Shell Scripts
RSS/Feed

nixcraft - insight into linux admin work

Linux tgtadm: Setup iSCSI Target ( SAN )


by nixCraft on November 11, 2008 26 comments Last updated May 25, 2012
Linux target framework (tgt) aims to simplify various SCSI target driver (iSCSI, Fibre Channel, SRP, etc) creation and maintenance. The key goals
are the clean integration into the scsi-mid layer and implementing a great portion of tgt in user space.
The developer of IET is also helping to develop Linux SCSI target framework (stgt) which looks like it might lead to an iSCSI target implementation
with an upstream kernel component. iSCSI Target can be useful:
a] To setup stateless server / client (used in diskless setups).
b] Share disks and tape drives with remote client over LAN, Wan or the Internet.
c] Setup SAN - Storage array.
d] To setup loadbalanced webcluser using cluster aware Linux file system etc.
In this tutorial you will learn how to have a fully functional Linux iSCSI SAN using tgt framework.

iSCSI target (server)


Storage resource located on an iSCSI server known as a "target". An iSCSI target usually represents nothing but hard disk storage. As with initiators, software to provide
an iSCSI target is available for most mainstream operating systems.

iSCSI initiator (client)


An initiator functions as an iSCSI client. An initiator typically serves the same purpose to a computer as a SCSI bus adapter would, except that instead of physically
cabling SCSI devices (like hard drives and tape changers), an iSCSI initiator sends SCSI commands over an IP network.

Debian / Ubuntu Linux Install tgt


Type the following command to install Linux target framework user-space tools:
$ sudo apt-get install tgt

CentOS / RHEL / Red Hat Linux Install tgt


RHEL 5.2 and older version do not have tgt tools. However, RHEL 5.3 (preview version) comes with tgt tools.

tgtadm - Linux SCSI Target Administration Utility


tgtadm is used to monitor and modify everything about Linux SCSI target software: targets, volumes, etc. This tool allows a system to serve block-level SCSI storage to
other systems that have a SCSI initiator. This capability is being initially deployed as a Linux iSCSI target, serving storage over a network to any iSCSI initiator.

Start tgtd
To start the tgtd, enter:
# /usr/sbin/tgtd

Under RHEL 5.3 to start the tgtd service, enter:


# /etc/init.d/tgtd start

Define an iscsi target name


The following example creates a target with id 1 (the iqn is 19 iqn.2001-04.com.example:storage.disk2.amiens.sys1.xyz) and adds a 20 logical unit (backed by /dev/hdc1)
with lun 1.
# tgtadm --lld iscsi --op new --mode target --tid 1 -T iqn.2001-04.com.example:storage.disk2.amiens.sys1.xyz

To view the current configuration, enter:


# tgtadm --lld iscsi --op show --mode target

Sample output:

05/29/2013 5:53 PM

Linux tgtadm: Setup iSCSI Target ( SAN )

2 of 9

http://www.cyberciti.biz/tips/howto-setup-linux-iscsi-target-sanwith-tgt.html

Target 1: iqn.2001-04.com.example:storage.disk1.amiens.sys1.xyz
System information:
Driver: iscsi
Status: running
I_T nexus information:
LUN information:
LUN: 0
Type: controller
SCSI ID: deadbeaf1:0
SCSI SN: beaf10
Size: 0
Online: No
Poweron/Reset: Yes
Removable media: No
Backing store: No backing store
Account information:
ACL information:

Add a logical unit to the target (/dev/sdb1):


# tgtadm --lld iscsi --op new --mode logicalunit --tid 1 --lun 1 -b /dev/sdb1

A note about home computer / test system


Most production boxes will only use iSCSI root with real iSCSI devices, but for testing purposes it can be quite useful to set up an iSCSI target on your image server. This
is useful for testing and learning iSCSI target and iSCSI initiator at home, simply use filesystem for testing purpose. Use dd command to create diskbased filesystem:
# dd if=/dev/zero of=/fs.iscsi.disk bs=1M count=512

Add /fs.iscsi.disk as a logical unit to the target:


# tgtadm --lld iscsi --op new --mode logicalunit --tid 1 --lun 1 -b /fs.iscsi.disk

Now, you should able to view details:


# tgtadm --lld iscsi --op show --mode target

Sample output:
Target 1: iqn.2001-04.com.example:storage.disk1.amiens.sys1.xyz
System information:
Driver: iscsi
Status: running
I_T nexus information:
LUN information:
LUN: 0
Type: controller
SCSI ID: deadbeaf1:0
SCSI SN: beaf10
Size: 0
Online: No
Poweron/Reset: Yes
Removable media: No
Backing store: No backing store
LUN: 1
Type: disk
SCSI ID: deadbeaf1:1
SCSI SN: beaf11
Size: 512M
Online: Yes
Poweron/Reset: Yes
Removable media: No
Backing store: /fs.iscsi.disk
Account information:
ACL information:

A Note About Selinux


Renout Gerrits adds - On RHEL and friends you will run into problems if selinux is enabled if using files. While adding a logicalunit to a target you will get the error:
'tgtadm: invalid request'. A bit of a misguiding error. fixed by setting the correct context:
# semanage fcontext -a -t tgtd_var_lib_t /fs.iscsi.disk
# restorecon -Rv /fs.iscsi.disk

Accept iSCSI Target


To enable the target to accept any initiators, enter:
# tgtadm --lld iscsi --op bind --mode target --tid 1 -I ALL

This should open network port # 3260:


# netstat -tulpn | grep 3260

Sample output:
tcp
tcp6

0
0

0 0.0.0.0:3260
0 :::3260

0.0.0.0:*
:::*

LISTEN
LISTEN

27328/tgtd
27328/tgtd

And you are done. Your system is configured as iSCSI Target. Remote client computer can access this computers hard disk over network. Your can use cluster aware
filesystem to setup real shared storage for small business. Open TCP port 3260 in your firewall, if required.

How do I access iSCSI Target (server) via iSCSI initiator (client)?


See detailed os specific iSCSI initiator instuctions:

05/29/2013 5:53 PM

Linux tgtadm: Setup iSCSI Target ( SAN )

3 of 9

1.
2.
3.
4.

http://www.cyberciti.biz/tips/howto-setup-linux-iscsi-target-sanwith-tgt.html

RHEL 4 or RHEL 5 Linux iSCSI initiator tutorial.


Debian Linux iSCSI initiator tutorial.
FreeBSD iSCSI initiator tutorial.
Windows iSCSI initiator tutorial.

Following is a quick way to access iSCSI target, under RHEL 5. Let us say your server iSCSI Target IP is 192.168.1.2. Type the following command to discover targets at
a given IP address such as 192.168.1.2 (use 127.0.0.1 if you are testing it from same computer):
# iscsiadm --mode discovery --type sendtargets --portal 192.168.1.2

OR
# iscsiadm --mode discovery --type sendtargets --portal 127.0.0.1

Sample output:
127.0.0.1:3260,1 iqn.2001-04.com.example:storage.disk1.amiens.sys1.xyz

Login to the iscsi target session:


# iscsiadm --mode node --targetname iqn.2001-04.com.example:storage.disk1.amiens.sys1.xyz --portal 192.168.1.2:3260 --login

OR
# iscsiadm --mode node --targetname iqn.2001-04.com.example:storage.disk1.amiens.sys1.xyz --portal 127.0.0.1:3260 --login

Verify that login was successful:


# tail -f /var/log/messages

Sample output:
Nov
Nov
Nov
Nov
Nov
Nov
Nov
Nov
Nov
Nov

11
11
11
11
11
11
11
11
11
11

07:34:04
07:34:04
07:34:04
07:34:04
07:34:04
07:34:04
07:34:04
07:34:04
07:34:04
07:34:04

vivek-desktop
vivek-desktop
vivek-desktop
vivek-desktop
vivek-desktop
vivek-desktop
vivek-desktop
vivek-desktop
vivek-desktop
vivek-desktop

kernel:
kernel:
kernel:
kernel:
kernel:
kernel:
kernel:
kernel:
kernel:
kernel:

[
[
[
[
[
[
[
[
[
[

9039.562312]
9039.572268]
9039.572374]
9039.572530]
9039.572715]
9039.572790]
9039.572940]
9039.572946]
9039.573492]
9039.573593]

scsi 6:0:0:1: Direct-Access


IET
VIRTUAL-DISK
0001 PQ: 0 ANSI: 5
sd 6:0:0:1: [sdc] 1048576 512-byte hardware sectors (537 MB)
sd 6:0:0:1: [sdc] Write Protect is off
sd 6:0:0:1: [sdc] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
sd 6:0:0:1: [sdc] 1048576 512-byte hardware sectors (537 MB)
sd 6:0:0:1: [sdc] Write Protect is off
sd 6:0:0:1: [sdc] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
sdc: unknown partition table
sd 6:0:0:1: [sdc] Attached SCSI disk
sd 6:0:0:1: Attached scsi generic sg4 type 0

sdc is new scsi disk. You may need to restart iSCSI to probe partition and check disks:
# service iscsi restart
# partprobe
# fdisk -l

You can now create parition and mount file system using usual fdisk and mkfs.ext3 commands:
# fdisk /dev/sdc
# mkfs.ext3 /dev/sdc1mkdir /iscsi
# mkdir /iscsi
# mount /dev/sdc1 /iscsi
# df -H

Sample output:
Filesystem
/dev/sda2
tmpfs
varrun
varlock
udev
tmpfs
/dev/sda1
/dev/sda5
/dev/sdb2
/dev/sdc1

Size
99G
1.1G
1.1G
1.1G
1.1G
1.1G
105G
294G
247G
520M

Used
30G
0
361k
0
2.9M
312k
32G
275G
119G
11M

Avail Use% Mounted on


64G 32% /
1.1G
0% /lib/init/rw
1.1G
1% /var/run
1.1G
0% /var/lock
1.1G
1% /dev
1.1G
1% /dev/shm
73G 31% /media/sda1
20G 94% /share
116G 51% /disk1p2
483M
3% /iscsi

References:
man page tgtadm
tgt project
iSCSI article from the wikipedia
Tweet

0
Like

You should follow me on twitter here or grab rss feed to keep track of new changes.

This blog post is 4 of 4 in the "Linux iSCSI Initiator (SCSI protocol over TCP/IP networks) Tutorial" series. Keep reading the rest of the series:
Table of Contents:

05/29/2013 5:53 PM

Linux tgtadm: Setup iSCSI Target ( SAN )

4 of 9

1.
2.
3.
4.

http://www.cyberciti.biz/tips/howto-setup-linux-iscsi-target-sanwith-tgt.html

CentOS / Red Hat Linux: Install and manage iSCSI Volume


CentOS 4 / Redhat Enterprise Linux 4 iSCSI Installation and Configuration ( iscsi initiators )
How To: Windows Server / XP / 2000 / 2003 ISCSI Initiator Configuration
Linux tgtadm: Setup iSCSI Target ( SAN )

{ 26 comments read them below or add one }


1 Britto November 11, 2008 at 11:21 am
You guys can try with OPENFILER Opensource SAN Implementation
Reply
2 vivek November 11, 2008 at 2:01 pm
Britto,
Good suggestion. OPENFILER is really good for SAN.
Reply
3 Tapas Mallick November 13, 2008 at 10:50 am
Nice tutorial. Please post topics on Cluster aware file system.
Reply
4 Matt Willsher December 5, 2008 at 1:57 pm
Great how-to, thanks.
tgt is available on Centos 5.2. (details on the introduction to RHEL 5 is at http://rhn.redhat.com/errata/RHEA-2007-0713.html)
To install:

# yum install scsi-target-utils

Works as detailed above.


Reply
5 xiaolong July 16, 2009 at 9:35 am
hi,guys,i have a question,
when i setup a iscsi target then export an iscsi device,
then i discovery the target device on two diffenence initoator ,and mount the device
the in node a i create a file in the device which was iscsi device ,and then i think i will the file in the node b the iscsi device ,but i cannt see the file .i get lost.
Reply
6 chris October 24, 2009 at 5:19 am
xiaolong
iscsi cannot do file sharing. because, unlike nfs, there is no locking.
just like fc san.
you can make a lun into a zone visible to more than 1 server, you can even access the lun from them. but you do not do that because it is just a raw access. not
locking.
Reply
7 ftaurino November 7, 2009 at 11:05 am
hi,
iscsi can export devices, but not filesystems. if you want more than one server access the same filesystem, you have to go on gfs2 or ocfs2. its not so difficult to
setup ;-)
and what about iet vs tgt? whats the best iscsi target for linux?
many thanks,
ftaurino
Reply
8 sayantan February 23, 2010 at 6:22 am
amiens.sys1.xyz
is what?

05/29/2013 5:53 PM

Linux tgtadm: Setup iSCSI Target ( SAN )

5 of 9

http://www.cyberciti.biz/tips/howto-setup-linux-iscsi-target-sanwith-tgt.html

Reply
9 Vincent April 14, 2010 at 4:44 pm
How do you make it persistent after reboot? I am working on ubuntu in my case.
Reply
10 Ade December 23, 2010 at 5:27 pm
I also would like to know the answer to Vincents question how to make it survive a reboot
Great post thanks!
Reply
11 Ade December 23, 2010 at 6:11 pm
Vincent
For making the setup persistent over reboot type this command:
# tgt-admin dump > /etc/tgt/targets.conf
# chkconfig tgtd on
Reply
12 John December 30, 2010 at 9:51 pm
How would you make a boot LUN if all you can set it is LUN 1? Or is there anyway to make it bootable? The only OS Ive been able to boot from that LUN is
SLES 11 SP1. Tried RHEL 5 and Win2k8 and both could not locate the LUN.
Reply
13 Travis Hershberger July 8, 2011 at 6:51 pm
Thanks for a good tutorial. First one I found that was actually helpful in getting everything setup, bookmarked.
Reply
14 Benjamin July 15, 2011 at 11:39 pm
@John
If you are booting Win2k8 from SAN per se, the boot partition has to have a LUN # that is neither 0 or 1 and is not more than 3 digits (ex. LUN 100, 101).
Reply
15 sid August 5, 2011 at 2:25 am
Im on CentOS 5.4 and when i run this command getting below error:
tgtadm lld iscsi op new mode target tid 1 -T iqn.2001-04.com.example:storage.disk2.amiens.sys1.xyz
tgtadm: unrecognized option iscsi
try tgtadm help
can you please help? Thanks
Reply
16 Travis Hershberger August 5, 2011 at 12:09 pm
At first glance it sounds like something isnt correct with the iscsi utilities, Id try reinstalling them.
yum reinstall iscsi-initiator-utils scsi-target-utils
Reply
17 Ethan_ February 21, 2013 at 1:56 am
you should add the sudo command as
sudo tgtadm lld iscsi op new mode target tid 1 -T iqn.2001-04.com.example:storage.disk2.amiens.sys1.xyz
Reply
18 sid August 7, 2011 at 12:50 pm
Thanks for the quick response. I was missing iscsi-initiator-utils. After installing it tgtadm lld iscsi op show mode target started showing results. But is not
persistent after reboot, I did try steps from above comment(# tgt-admin dump > /etc/tgt/targets.conf and # chkconfig tgtd on) and it still doesnt work. Im using
CentOS 5.4. Thanks again for the wonderful post.

05/29/2013 5:53 PM

Linux tgtadm: Setup iSCSI Target ( SAN )

6 of 9

http://www.cyberciti.biz/tips/howto-setup-linux-iscsi-target-sanwith-tgt.html

Reply
19 sid August 7, 2011 at 1:49 pm
Never mind. I re-did the configuration from scratch and now it is persistent after reboots. Thanks again
Reply
20 rex October 20, 2011 at 5:08 pm
is it possible to do high available cluster environment using this technology??
Reply
21 Travis Hershberger October 20, 2011 at 5:50 pm
It is so long as you remember to use a locking filesystem properly. Something like drbl. Cant say Ive ever set one up so thats all for now.
Reply
22 Soumitra Pal December 4, 2011 at 8:01 am
Its really nice tutorial. I am doing a LAB setup. Where I am using Open Filer for storage. In 2 node RHEL5 systems I need to share the same LUN. using
iscsiadm mode discovery type sendtargets portal 192.168.1.2 I am able to discover the LUN in both of the systems.
Reply
23 Cman February 17, 2012 at 4:06 pm
I have implement one iscsi target . Now wont to implement two iscsi targets with load balancing. is it posible to do so?..
Reply
24 jaja-we February 26, 2012 at 9:19 am
is it is
you nee to use mdadm or LVM mirroring , discover the 2 devices then set up a single device to mount , if you are using a high storage system with ISCSI
capabilites then you can use multipath to bind the 2 devices.
before that you need to make sure the initiator have 2 different IPs (of the same storage ) to talk with.
Reply
25 tmar March 29, 2012 at 11:18 am
Hello,
Is it possible to connect 2 initiators on the same target (initiators and target on centos 6)?, each initiator accessing different block device of the target?
I have tried to configured the /etc/targets.conf using initiator-address ,and also using incominguser password, but it seems these are global variable and can not be
defined per lun. Any ideas?
Reply
26 omkar lagu June 18, 2012 at 5:50 pm
I have a rhel6.1 setup and the below command is not working.. is it a BUG.
/usr/sbin/tgtadm lld iscsi op new mode logicalunit tid 1 lun 1 -b /mnt1/lun10
return code 22
==============================================
tgtadm: invalid request
==============================================
Reply
Leave a Comment

05/29/2013 5:53 PM

Linux tgtadm: Setup iSCSI Target ( SAN )

7 of 9

http://www.cyberciti.biz/tips/howto-setup-linux-iscsi-target-sanwith-tgt.html

Name *
E-mail *
Website

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
Security Question:
What is 3 + 2 ?
Solve the simple math so we know that you are a human and not a bot.

Tagged as: bus adapter, iscsi target, linux setup iSCSI Target, san storage, scsi, scsi initiator, server storage, software targets, storage array, target server, target software, tgtadm command, tgtd command

Previous post: Ubuntu Linux: Watch and Play BBC / YouTube Videos With Totem Movie Player
Next post: Install Linux On Intel Xeon 7400 Dunnington

GET FREE TIPS & NEWS


Make the most of Linux Sysadmin work!

Youtube | Twitter | Google +

nixCraft
Like

33,836 people like nixCraft.

Facebook social plugin

Related Posts

05/29/2013 5:53 PM

Linux tgtadm: Setup iSCSI Target ( SAN )

8 of 9

http://www.cyberciti.biz/tips/howto-setup-linux-iscsi-target-sanwith-tgt.html

CentOS 4 / Redhat Enterprise Linux 4 iSCSI Installation and Configuration ( iscsi initiators )

Linux: Boot a 2TB+ partition or Larger Array Using Grub

Linux Fibre Channel over Ethernet implementation code released

Red Hat / CentOS Linux 4: Setup Device Mapper Multipathing

Linux: Should You Use Twice the Amount of Ram as Swap Space?

Parallel NFS: Read / Write Hundreds of Gigabytes Per Second

Linux Creating a Partition Size Larger Than 2TB

How To Avoid Sudden Outburst Of Backup Shell Script / Program Disk I/O

How do I find out Linux Disk utilization?

05/29/2013 5:53 PM

Linux tgtadm: Setup iSCSI Target ( SAN )

9 of 9

http://www.cyberciti.biz/tips/howto-setup-linux-iscsi-target-sanwith-tgt.html

Seagate Barracuda: 1.5TB Hard Drive Launched

2004-2013 nixCraft. All rights reserved. Cannot be reproduced without written permission.
Privacy Policy | Terms of Service | Questions or Comments | Copyright Info | Sitemap

05/29/2013 5:53 PM

You might also like