Fedora 29: 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

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
$ sudo dnf install -y checkpolicy
$ 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

The following error will be occured when tgt starts.

ipc_init(781) failed to open lock file for management IPC

Add SELinux configuration for avoiding this error.

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

require {
        type var_run_t;
        type tgtd_t;
        class dir create;
}

#============= tgtd_t ==============
allow tgtd_t var_run_t:dir create;
EOF
$ checkmodule -M -m -o tgtd-var-run.mod tgtd-var-run.te
$ semodule_package -m tgtd-var-run.mod -o tgtd-var-run.pp
$ sudo semodule -i tgtd-var-run.pp
$ rm -f tgtd-var-run.te tgtd-var-run.mod tgtd-var-run.pp

Run tgtd.

$ sudo systemctl restart tgtd

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/disk bs=1M count=1K

Create iSCSI target (tid 1).

$ sudo tgtadm --lld iscsi --op new --mode target --tid 1 \
       -T iqn.2018-12.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 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.2018-12.com.hiroom2:disk

Connect to iSCSI target.

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