ISCSI

Setting up the Target (iSCSI Server)

Install the iSCSI Target server

  apt-get install iscsitarget

Open /etc/default/iscsitarget and set ISCSITARGET_ENABLE to true

  vi /etc/default/iscsitarget
  ISCSITARGET_ENABLE=true

We can use unused logical volumes, image files, hard drives (e.g. /dev/sdb), hard drive partitions (e.g. /dev/sdb1) or RAID devices (e.g. /dev/md0) for the storage. In this example we will create a logical volume of 20GB named storage_lun1 in the volume group vg0:

  # lvcreate -L20G -n storage_lun1 vg0  

If you want to use an image file, you can create it as follows. The following command creates a image file /storage/lun1.img with a size of 20GB.

  mkdir /storage
  dd if=/dev/zero of=/storage/lun1.img bs=1024k count=20000

Now, edit the ietd.conf file. This is the place, where you configure your iSCSI targets and daemon defaults. Comment out everything in that file. At the end we add the following stanza:

  # vi /etc/iet/ietd.conf
  [...]
  Target iqn.2001-04.com.example:storage.lun1
        IncomingUser someuser secret
        OutgoingUser
        Lun 0 Path=/dev/vg0/storage_lun1,Type=fileio
        Alias LUN1
        #MaxConnections  6

The target name must be a globally unique name, the iSCSI standard defines the "iSCSI Qualified Name" as follows: iqn.yyyy-mm.<reversed domain name>[:identifier] where yyyy-mm is the date at which the domain is valid; the identifier is freely selectable. The IncomingUser line contains a username and a password so that only the initiators (clients) that provide this username and password can log in and use the storage device; if you don't need authentication, don't specify a username and password in the IncomingUser line. In the Lun line, we must specify the full path to the storage device (e.g. /dev/vg0/storage_lun1, /storage/lun1.img, /dev/sdb, etc.).

Now we tell the target that we want to allow connections to the device iqn.2001-04.com.example:storage.lun1 from the IP address 192.168.0.100 (server1.example.com) (comment out the ALL ALL line because that would allow all initiators to connect to all targets)...

  # vi /etc/iet/initiators.allow
  [...]
  iqn.2001-04.com.example:storage.lun1 192.168.0.100
  #ALL ALL

Note: In the older ubuntu systems, the ietd.conf and initiators.allow files are in /etc directory instead of /etc/iet directory.

Now, start the target daemon

  /etc/init.d/iscsitarget start

Setting up the Initiator (iSCSI Storage client)

Install the client fileset

  # apt-get install open-iscsi

Edit the /etc/iscsi/iscsid.conf and set node.startup to automatic

  # vi /etc/iscsi/iscsid.conf
  [...]
   node.startup = automatic
  [...]

Restart the Initiator

   # /etc/init.d/open-iscsi restart

Now, check what storage devices (LUNS) are available in the target

  # iscsiadm -m discovery -t st -p 192.168.123.10
  192.168.123.10:3260,1 iqn.2010-04.net.sys-admin:storage.lun0

  # iscsiadm -m node
  192.168.123.10:3260,1 iqn.2010-04.net.sys-admin:storage.lun0

The settings for the storage device iqn.2010-04.net.sys-admin:storage.lun0 on 192.168.123.10:3260,1 are stored in the file /etc/iscsi/nodes/iqn.2010-04.net.sys-admin:storage.lun0/192.168.123.10,3260,1/default. We need to set the username and password for the target in that file; instead of editing that file manually, we can use the iscsiadm command to do this for us:

  # iscsiadm -m node --targetname "iqn.2010-04.net.sys-admin:storage.lun0" --portal
  "192.168.123.10:3260" --op=update --name node.session.auth.authmethod --value=CHAP
  # iscsiadm -m node --targetname "iqn.2010-04.net.sys-admin:storage.lun0" --portal
  "192.168.123.10:3260" --op=update --name node.session.auth.username --value=someuser
  # iscsiadm -m node --targetname "iqn.2010-04.net.sys-admin:storage.lun0" --portal
  "192.168.123.10:3260" --op=update --name node.session.auth.password --value=secret

Now, we can login, either by running

  # iscsiadm -m node --targetname "iqn.2010-04.net.sys-admin:storage.lun0" --portal 
  "192.168.123.10:3260" --login
  Logging in to [iface: default, target: iqn.2010-04.net.sys-admin:storage.lun0, portal:
  192.168.123.10,3260]
  Login to [iface: default, target: iqn.2010-04.net.sys-admin:storage.lun0, portal:
  192.168.123.10,3260]: successful

... or by restarting the initiator:

  # /etc/init.d/open-iscsi restart

Now, the fdisk -l output will show the new LUN
If you want to log out (disconnect), you can run

   # iscsiadm -m node --targetname "iqn.2001-04.com.example:storage.lun1" --portal 
  "192.168.0.101:3260" --logout

http://ubuntuforums.org/showthread.php?t=1877348