Skip to content

Instantly share code, notes, and snippets.

@kylechase
Forked from albertomolina/iSCSI_cheatsheet.md
Created December 19, 2023 19:10
Show Gist options
  • Save kylechase/73ae5366063bf61634d7d4b131980cc3 to your computer and use it in GitHub Desktop.
Save kylechase/73ae5366063bf61634d7d4b131980cc3 to your computer and use it in GitHub Desktop.
iSCSI cheatsheet

iSCSI server with tgt

apt install tgt

Manual configuration of targets

Create a new target named "target1":

tgtadm --lld iscsi --op new --mode target --tid 1 -T iqn.2020-01.es.tinaja:target1

Delete a specific target:

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

Add a logical unit to the target:

tgtadm --lld iscsi --op new --mode logicalunit --tid 1 --lun 1 -b /dev/vdb

Delete a logical unit from the target:

tgtadm --lld iscsi --op delete --mode logicalunit --tid 1 --lun 1

View the targets defined:

tgtadm --lld iscsi --op show --mode target

Enable the target with tid 1 in all the interfaces:

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

Or we can just use tgt-admin :)

tgt-admin -h

Automatic creation of targets

Include logical units in /etc/tgt/targets.conf or /etc/tgt/conf.d/*.conf as:

include /dev/vdb

iSCSI Initiator

Install open-iscsi on the linux client:

apt install open-iscsi

The installation will set up automatically the initiator's name and it'll be stored in /etc/iscsi/initiatorname.iscsi:

## DO NOT EDIT OR REMOVE THIS FILE!
## If you remove this file, the iSCSI daemon will not start.
## If you change the InitiatorName, existing access control lists
## may reject this initiator.  The InitiatorName must be unique
## for each iSCSI initiator.  Do NOT duplicate iSCSI InitiatorNames.
InitiatorName=iqn.1993-08.org.debian:01:0000000001

We need to discover the targets configured on the iSCSI server (the following command will show a list of all the IQNs of the targets):

iscsiadm --mode discovery --type sendtargets --portal 10.0.0.1[:3260]

And then, we can manually login to a specific target:

iscsiadm --mode node -T iqn.2020-01.es.tinaja:target1 --portal 10.0.0.1 --login

After login to the target a new block device will be available at the initiator system.

If the target is defined with a username and password, they must be defined before login:

iscsiadm --mode node -T iqn.2020-01.es.tinaja:target1 --portal 10.0.0.1 -o update \
-n node.session.auth.username -v username
iscsiadm --mode node -T iqn.2020-01.es.tinaja:target1 --portal 10.0.0.1 -o update \
-n node.session.auth.password -v password

It's also possible to login to all the available targets with -L:

iscsiadm --mode node --portal 10.0.0.1 -L

To display all the current sessions:

iscsiadm -m session

When needed, we can logout from a specific target:

iscsiadm -m node -T iqn.2020-01.es.tinaja:target1 -p 10.0.0.1 -u

Or logout from all the targets:

iscsiadm -m node -p 10.0.0.1 --logout

open-iscsi provides a daemon that takes care of the sessions and will login to the targets after the next reboot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment