Ubuntu 17.04: Install tgt for iSCSI target

This article will describe installing tgt which provides iSCSI target.

1 Install tgt

Install tgt package.

$ sudo apt install -y tgt

2 Create iSCSI target

This article uses a file as logical unit. You can use block device as logical unit.

$ sudo mkdir /var/lib/iscsi
$ sudo dd if=/dev/zero of=/var/lib/iscsi/disk bs=1M count=1K

Create iSCSI target (tid 1).

$ sudo tgtadm --lld iscsi --op new --mode target --tid 1 \
       -T iqn.2017-07.com.hiroom2:disk

Add logical Unit (lun 1) to iSCSI target (Target ID 1).

$ sudo tgtadm --lld iscsi --op new --mode logicalunit --tid 1 --lun 1 \
       -b /var/lib/iscsi/disk

Publish iSCSI target (tid 1) to all IP address. You can specify 192.168.11.1 and 192.168.11.0/24 in addition to ALL.

$ sudo tgtadm --lld iscsi --op bind --mode target --tid 1 -I ALL

Save configuration for iSCSI target. If you do not save configuration, configuration will be removed after restarting tgtd.

$ sudo tgtadm --dump | sudo tee /etc/tgt/conf.d/disk.conf

3 Connect to iSCSI target with open-iscsi which is iSCSI initiator

iSCSI initiator runs on server which is installed iSCSI target.

The partition is the following before connecting to iSCSI target.

$ cat /proc/partitions
major minor  #blocks  name

 254        0   10485760 vda
 254        1     248832 vda1
 254        2          1 vda2
 254        5   10233856 vda5
  11        0    1048575 sr0
 253        0    9183232 dm-0
 253        1    1048576 dm-1

Install open-iscsi package.

$ sudo apt install -y open-iscsi

Get iSCSI target list.

$ sudo iscsiadm -m discovery -t st -p localhost
[::1]:3260,1 iqn.2017-07.com.hiroom2:disk

Connect to iSCSI target.

$ sudo iscsiadm -m node \
       --targetname iqn.2017-07.com.hiroom2:disk -p localhost -l
Logging in to [iface: default, target:
iqn.2017-07.com.hiroom2:disk, portal: ::1,3260] (multiple)
Login to [iface: default, target:
iqn.2017-07.com.hiroom2:disk, portal: ::1,3260]
successful.

The partition is the following after connecting to iSCSI target. The partition sda is appended.

$ cat /proc/partitions
major minor  #blocks  name

 254        0   10485760 vda
 254        1     248832 vda1
 254        2          1 vda2
 254        5   10233856 vda5
  11        0    1048575 sr0
 253        0    9183232 dm-0
 253        1    1048576 dm-1
   8        0    1048576 sda