How to Connect to iSCSI Volume from Ubuntu 20.04

In the previous article, I have written a short tutorial on how to create an iSCSI Target on TrueNAS. Now in this post, I will guide you on how to connect to the iSCSI target from an Ubuntu machine. In this case, the TrueNAS acts as a Target server, and the Ubuntu is the Initiator (client). When we connect to a remote iSCSI target, we can then use the iSCSI disk as extra storage. We can format it and mount it as if it’s a local drive.

Steps to Connect to iSCSI Volume from Ubuntu 20.04

So basically, this tutorial also works on other Ubuntu versions as well as it’s derivatives.

Step 1. Install open-iscsi

In order to communicate and connect to iSCSI volume, we need to install open-iscsi package. Make sure you have this package on your system. If you don’t have it, you can install using command below

sudo apt update
sudo apt install open-iscsi

Step 2. Configure initiatorname.conf

We need to edit the initiatorname.conf file located in /etc/iscsi directory.

sudo nano /etc/iscsi/initiatorname.conf

Now enter the initiator name of the iSCSI target into this file. For example, in my case, it would be:

Now save and close the file.

Step 3. Configure iscsid.conf

Now we need to edit the default configuration file

sudo nano /etc/iscsi/iscsid.conf

In this config file, we need to edit some sections to match our iSCSI target server. If your iSCSI target discovery authentication mode set to None, you can leave this config untouched. It will work out of the box. If your iSCSI target server use CHAP, then you need to edit the following

Find and uncomment the following line

node.session.auth.authmethod = CHAP
node.session.auth.username = username
node.session.auth.password = password

Enter the CHAP credentials, username, and password you created on the iSCSI target server. Close and save the file. Now restart the service

sudo systemctl restart iscsid open-iscsi

Step 3. Connect to the iSCSI Target

Discover the iSCSI Target

First, we will need to discover the iSCSI target. On the Terminal, use this command to discover the target

sudo iscsiadm -m discovery -t sendtargets -p 192.168.100.100

Change the IP address with your actual iSCSI Target server IP.

Note down the output as we will need to use it for the next step below. This is important if we have multiple targets in the server like above.

Connect to the iSCSI Target Volume

Now we can login to the target with this command.

sudo iscsiadm --mode node --targetname iqn.2005-10.org.freenas.ctl:iscsi01 --portal 192.168.100.100 --login

Output:

dhani@dhani-Ubuntu:~$ sudo iscsiadm --mode node --targetname iqn.2005-10.org.freenas.ctl:iscsi01 --portal 192.168.100.100 --login
Logging in to iface: default, target: iqn.2005-10.org.freenas.ctl:iscsi01, portal: 192.168.100.100,3260
Login to [iface: default, target: iqn.2005-10.org.freenas.ctl:iscsi01, portal: 192.168.100.100,3260] successful.

The command above will only connect to the iscsi01 target only. If you have multiple targets on the server, and you want to login into them all, you can use this command

sudo iscsiadm -m node -l

To log out from all the targets, use this

sudo iscsiadm -m node -u

Done.

Now let’s check our block disk using fdisk command

sudo fdisk -l

As you can see my iSCSI disk is recognized as /dev/sdb.

Step 4. Format the iSCSI Disk

So now we’ve connected to the iSCSI disk. We can then format or create a partition on the iSCSI Disk, just like a local disk. In the following example, I will create a new partition and format the disk to ext4 file system.

sudo fdisk /dev/sdb

Now type n to create a new partition.

Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):

Type p to create primary partition and then type 1 to create one partition. You can change this to two for example, if you want to create two partitions.

Select (default p): p
Partition number (1-4, default 1): 1
First sector (256-13107203, default 256):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (256-13107203, default 13107203):

Created a new partition 1 of type ‘Linux’ and of size 50 GiB.

Type w to write the change. Next, let’s check using fdisk

sudo fdisk -l

As you can see, now I have a new partition called /dev/sdb1.

Format the partition to ext4

Next, I will format this partition to ext4 file system.

sudo mkfs.ext4 /dev/sdb1
dhani@dhani-Ubuntu:~/Downloads$ sudo sudo mkfs.ext4 /dev/sdb1
mke2fs 1.45.5 (07-Jan-2020)
Discarding device blocks: done
Creating filesystem with 13106948 4k blocks and 3276800 inodes
Filesystem UUID: aa38ff4c-9d84-49c2-a1d9-9c368319f95f
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424
Allocating group tables: done
Writing inode tables: done
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done

Mount the partition

sudo mkdir /mnt/iscsi_disk
mount /dev/sdb1 /mnt/iscsi-disk

Be the first to comment

Leave a Reply