Saturday, 18 June 2011

Configure a system as an iSCSI initiator that persistently mounts an iSCSI target

The first time I tried to do this I used FreeNAS, if anything because my girlfriend thinks that Beastie is cute, but there is no need to use it in order to set up a iSCSI target, Red Hat can do it as well. Thus, even though, it is not part of the objectives the first part of this post will be to set up a iSCSI target. Once the target has been set up, I'll show you how to mount it persistently, which is the objective.

Setting up a iSCSI target, Red Hat style:

First you need to install the necessary packages:
yum install scsi-target-utils -y
Start the target daemon and set it to start on boot:
service tgtd start; chkconfig tgtd on
You can now add a target, using the tgtadm command. Oddly enough, the help command is actually extremely helpful, in that it essentially gives you examples rather than a list of what each flag does. Not that there is anything wrong with a list of what each flag does, but sometimes more examples would not go amiss, anyway, create the target with:
tgtadm --lld iscsi --mode target --op new --tid=1 --targetname
You then need to add a LUN to this target and tell it what storage it should use:
 tgtadm --lld iscsi --mode logicalunit --op new --tid=1 --lun=1 --backing-store=/dev/sdb
All that remains is to enable the target to accept initiators:
tgtadm --lld iscsi --mode target --op bind --tid=1 --initiator-address=ALL
You can check port 3260 with (thanks to this daniel miessler for this post):
lsof -i:3260
If you don't see any output then something has gone wrong. You can list the targets with this command: 
tgtadm --lld iscsi --op show --mode target
Make sure that you open port 3260 on your firewall and save it:
iptables -I INPUT -p tcp --dport 3260 -j ACCEPT; service iptables save
That should be it, you now have an iSCSI target ready to be mounted. Note that this will allow anybody to mount your target, so it is really only good as a internal test.

Configure a system as an iSCSI initiator that persistently mounts an iSCSI target

This is the actual objective. You need to install the relevant packages first:
yum groupinstall 'iSCSI Storage Client'
Strangely, this group seems to consist solely of iscsi-initiator-utils. Anyway, start the iscsi service and set it to start on boot:
 /etc/init.d/iscsi start; chkconfig iscsi on
You can now look for targets with this, where is the ip address of the server hosting the iSCSI targets:
iscsiadm -m discovery -t sendtargets -p
Restart the iscsi service:
service iscsi restart
Check that you see the new device with:
fdisk -l
You can now use the new disk to create partition(s) and file systems for those partition(s) and mount it (them).

A note of caution, as this is a network device, you need to make sure that you specify it in the fstab options, otherwise your system will refuse to boot up, something like this is needed
UUID=54e1bd41-68d0-4804-94f8-1b255e53a88d /iscsi ext4 _netdev 0 0


  1. If your /dev/sdb is physical disk, better to use "direct-store" instead of "backing-store".

  2. @Anonymous
    I'm using a virtual machine so I guess it really doesn't matter, but thanks for pointing this out, I was not aware of it.

    backing-store - defines a virtual device on the target.

    direct-store - defines a direct mapped device with the same properties as the physical device (such as VENDOR_ID, SERIAL_NUM, etc.)

  3. I don't want to post this on too many sites, so I will stop here. I run "iscsiadm -m discovery -t sendtargets -p" and "service iscsi restart". The result is an sg device such as /dev/sg2. It is not what I need, which is /dev/sdb or similar. Any idea? Thanks.

  4. @Sam
    The drive names are due to naming conventions, see this Naming Conventions

    What are you trying to do that requires an "sd" drive?