iSCSI Divertimento

iSCSI (Intenet Small Computer Storage Interface) implements SCSI protocol over a TCP/IP network. SCSI was a standard interface to transfer data in a computer bus. So iSCSI is a way to Store and Retrieve data from a remote disk as if it were a local Disk. It can be uses as a kind of Storage Area Network.

It is rather ease to install both the server (called iSCSI Target) and the clients (known as iSCSI initiators). As in title this is just some kind of divertimento. I’m quite experienced with Openstack thus I want to emulate some kind of simplification of the use of iSCSI using LVM as Openstack Cinder Volume does.

So, my first step is using a disk with LVM and create a volume group as explained in the first post I wrote related to LVM:

pvcreate /dev/vdb
vgcreate krtvolumes /dev/vdb

Install the Target

sudo apt install tgt

Great… the target is working now. That was easy (of course we might want to add further configurations, but not now).

Install the Initiators

sudo apt install open-iscsi

That’s the way to install the initiator software, that is, the software for the clients.

Adding volumes to our target

I’ve written a simple script which creates LVM device, adds the device as a new target and exports it. This script must be run as root.

# Creates a new Logical Volume with lvcreate.
# The name is krtvol and a generated ID.
volumeGroup=krtvolumes
volId=krtvol-$(uuidgen)
iqn=iqn.2020-01.eu.jicg
size=${1:-1}G

lvcreate -L ${size} -n ${volumeGroup}/${volId}

# lastId is a file containing a number as Last Target ID 
thisId=$(($(cat lastId) + 1))

# Creates a new target using the LV previously created
tgtadm --lld iscsi --mode target --op new --tid ${thisId} -T ${iqn}:${volId}

# Gets the device file name from the volId (changing every - for --)
deviceFile=/dev/mapper/${volumeGroup}-$(echo ${volId} | sed 's/-/--/g')

# Adds the new target so it can be found
tgtadm --lld iscsi --mode logicalunit --op new \
  --tid ${thisId} --lun 1 -b ${deviceFile}

# formats it using ext4
mkfs -t ext4 ${deviceFile}

# Sets the new last targret Id in the "counter" file.
echo -n $thisId > lastId

# echoes the name of the target
echo ${iqn}:${volId}

Using that simple script we can add a new volume, formatted with ext4 to our scsi target.

In my example the IP address of my iSCSI target is 192.168.192.10 and using that small script, I got a volume which ID is: 70f370fc-5954-4d2e-a3ff-fccfb57caf2

Setting up volumes in our Initiator

I know that mi iSCSI IP is 192.168.192.10 — So from my initiator node I can query the tgtd server this way:

sudo iscsiadm -m discovery -t st -p 192.168.192.10

When we run that command, it is created an structure tree accesible only by root. We can see that structure with the tree command so we can get a grasp of it: sudo tree /etc/iscsi/nodes/

Later, we’ll make some changes in the default file belonging to one of the targets we’ve discovered.

I see that the VolumeID was “70f370fc-5954-4d2e-a3ff-fccfb57caf25” . So, from the Initiator (client) I can type the following command:

iscsiadm --mode node --targetname iqm.2020-1.eu.jicg:krtvol-70f370fc-5954-4d2e-a3ff-fccfb57caf25 \
-p 192.168.192.10 --login

I’ll be able to see this kind of logs in /etc/log/syslog:

Great, now I have a new disk /dev/sda there and I’m able to use it:

sudo mount /dev/sda /mnt

If I try to find the openend connections in the initiator I woud find one with my Target node:

Connecting automatilly at startup

We can connect automatically a disk at startup. Basically we’d need to add a new line to /etc/fstab. A good idea would be using it’s block id:

sudo blkid /dev/sda

So I edit /etc/fstab to add a new line:

UUID="b3e931d4-a923-4e3d-8c4e-bbd5f5c0a390" /mnt ext4   _netdev,rw        0 0

And finally, I set the connection automatillay in the file describing the initiator, so iscsiadm connects to it when the daemon starts:

sudo sed -i 's|manual|automatic|g' /etc/iscsi/nodes/iqm.2020-1.eu.jicg\:krtvol-70f370fc-5954-4d2e-a3ff-fccfb57caf25/192.168.192.10\,3260\,1/default

This “weird” directory structure is shown previously in this article.

So, whenever we restart open-iscsi service an initiator would be started to this exported volume. And as it is in our /etc/fstab file, it would be automatically mount in /mnt.

Unconnecting the Initiator

Of course the remote disk should be disconnected with care. We need to umount the disk and “logout” the initiator:

sudo umount /mnt
sudo iscsiadm --mode node --targetname iqm.2020-1.eu.jicg:krtvol-70f370fc-5954-4d2e-a3ff-fccfb57caf25 -p 192.168.192.10 --logout

Deleting a target

The way to delete the target in our SCSI targets server is using this command:

sudo tgtadm --lld iscsi --op delete --mode target --tid 1

Once done this, we can delete de Logical Volumes we’ve created in order to clean everything.

We can use the command lvs to see the volumes we’ve created, and the command to remove one of the volumes (the one we’ve been using through this example) is:

sudo lvremove krtvolumes/krtvol-70f370fc-5954-4d2e-a3ff-fccfb57caf25