Iscsiadm

From LQWiki
Jump to navigation Jump to search

iscsiadm is a command that allows you to use an iscsi lun as a block device. To use it you need an iscsi server to connect to. This article demonstrates how you attach an iscsi target (a LUN on a server) on your own, connected computer using iscsiadm. In our example, localhost is the iscsi server.

Show your block devices

iscsiadm is a console program, so open a console and enter:

tweedleburg:~ # hwinfo --block --short
disk:                                                           
  /dev/sda             ST31000528AS                             
partition:                                                      
  /dev/sda1            Partition                                
cdrom:                                                          
  /dev/sr0             HL-DT-ST DVD+-RW GH50N

Verify the iscsi service is reachable

tweedleburg:~ # nmap localhost -p 3260

Starting Nmap 5.00 ( http://nmap.org ) at 2010-07-08 14:24 CEST
Interesting ports on localhost (127.0.0.1):                    
PORT     STATE SERVICE                                         
3260/tcp open  iscsi                                           

Nmap done: 1 IP address (1 host up) scanned in 0.14 seconds

Invoke iscsiadm

Ok, let's call this command and have it explain itself:

tweedleburg:~ # iscsiadm                                   
iscsiadm -m discovery [ -hV ] [ -d debug_level ] [-P printlevel] [ -t type -p ip:port -I ifaceN ... [ -l ] ] | [ -p ip:port ] [ -o operation ] [ -n name ] [ -v value ]                                                                         
iscsiadm -m node [ -hV ] [ -d debug_level ] [ -P printlevel ] [ -L all,manual,automatic ] [ -U all,manual,automatic ] [ -S ] [ [ -T targetname -p ip:port -I ifaceN ] [ -l | -u | -R | -s] ] [ [ -o  operation  ] [ -n name ] [ -v value ] ]    
iscsiadm -m session [ -hV ] [ -d debug_level ] [ -P  printlevel] [ -r sessionid | sysfsdir [ -R | -u | -s ] [ -o operation ] [ -n name ] [ -v value ] ]         
iscsiadm -m iface [ -hV ] [ -d debug_level ] [ -P printlevel ] [ -I ifacename ] [ [ -o  operation  ] [ -n name ] [ -v value ] ]                                 
iscsiadm -m fw [ -l ]                                                           
iscsiadm -k priority    

Now we want to discover our iscsi LUN.

tweedleburg:~ # iscsiadm -m discovery -t st -p 127.0.0.1
127.0.0.1:3260,1 iqn.2001-04.com.example:storage.disk2.sys1.xyz
tweedleburg:~ # iscsiadm -m node
127.0.0.1:3260,1 iqn.2001-04.com.example:storage.disk2.sys1.xyz
tweedleburg:~ # iscsiadm -m node -p 127.0.0.1
node.name = iqn.2001-04.com.example:storage.disk2.sys1.xyz
[...]

Now, for a last time, have a look at the short list of block devices:

tweedleburg:~ # hwinfo --block --short
disk:                                                           
  /dev/sda             ST31000528AS                                                      
partition:                                                      
  /dev/sda1            Partition                                                               
cdrom:                                                          
  /dev/sr0             HL-DT-ST DVD+-RW GH50N                   

Attach the LUN, in iscsi language, log in to it:

tweedleburg:~ # iscsiadm -m node -T iqn.2001-04.com.example:storage.disk2.sys1.xyz --login
Logging in to [iface: default, target: iqn.2001-04.com.example:storage.disk2.sys1.xyz, portal: 127.0.0.1,3260]
Login to [iface: default, target: iqn.2001-04.com.example:storage.disk2.sys1.xyz, portal: 127.0.0.1,3260]: successful

Verify you see an additional block device:

tweedleburg:~ # hwinfo --block --short
disk:
  /dev/sda             ST31000528AS
  /dev/sdx             IET VIRTUAL-DISK
partition:
  /dev/sda1            Partition
cdrom:
  /dev/sr0             HL-DT-ST DVD+-RW GH50N
tweedleburg:~ #

You can now format and mount your block device just as any block device.

Warning
The following will format your new iscsi disk:
tweedleburg:~ # mkfs.ext3 /dev/sdx
mke2fs 1.41.9 (22-Aug-2009)
/dev/sdx is entire device, not just one partition!
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
62592 inodes, 250000 blocks
12500 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=260046848
8 block groups
32768 blocks per group, 32768 fragments per group
7824 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376

Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
tweedleburg:~ # mount /dev/sdx /mnt
tweedleburg:~ # ls /mnt
lost+found

Problems

iscsiadm is seen as tedious. The reason is that people tend to forget what they do not understand, a common error is to forget "sendtargets" (st) as in this example:

tweedleburg:~ # iscsiadm -m discovery                                           
tweedleburg:~ # iscsiadm -m discovery -p 127.0.0.1
iscsiadm: discovery record [127.0.0.1,3260] not found!
tweedleburg:~ # iscsiadm -m node -p 127.0.0.1
iscsiadm: no records found!

See also