Fedora 25: Install scsi-target-utils for iSCSI target

This article will describe installing scsi-target-utils which provides iSCSI target.

1 Install scsi-target-utils

Install scsi-target-utils package.

$ sudo dnf install -y scsi-target-utils
$ sudo firewall-cmd --add-service=iscsi-target --permanent
$ sudo firewall-cmd --reload
$ sudo systemctl enable tgtd
$ sudo systemctl restart tgtd

Add SELinux configuration for creating Logical unit to /var/lib/iscsi.

$ cat <<EOF > tgtd-var-lib.te
module tgtd-var-lib 1.0;

require {
        type iscsi_var_lib_t;
        type tgtd_t;
        class file { open read write getattr };
        class dir { search };
}

#============= tgtd_t ==============
allow tgtd_t iscsi_var_lib_t:file { open read write getattr };
allow tgtd_t iscsi_var_lib_t:dir { search };
EOF
$ checkmodule -M -m -o tgtd-var-lib.mod tgtd-var-lib.te
$ semodule_package -m tgtd-var-lib.mod -o tgtd-var-lib.pp
$ sudo semodule -i tgtd-var-lib.pp
$ rm -f tgtd-var-lib.te tgtd-var-lib.mod tgtd-var-lib.pp

2 Create iSCSI target

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

$ sudo dd if=/dev/zero of=/var/lib/iscsi/debian-9 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:debian-9

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/debian-9

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/debian-9.conf

3 Connect to iSCSI target with iscsi-initiator-utils 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 iscsi-initiator-utils package.

$ sudo dnf install -y iscsi-initiator-utils

Get iSCSI target list.

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

Connect to iSCSI target.

$ sudo iscsiadm -m node \
       --targetname iqn.2017-07.com.hiroom2:debian-9 -p localhost -l
Logging in to [iface: default, target:
iqn.2017-07.com.hiroom2:debian-9, portal: ::1,3260] (multiple)
Login to [iface: default, target:
iqn.2017-07.com.hiroom2:debian-9, 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