How to Connect to Multiple iSCSI Targets Requiring Different Usernames and Passwords
Tuesday, May 15th, 2012Recently I needed to connect a server to multiple iSCSI targets with each target requiring a unique username and password. From what I can tell this isn’t a well documented feature of openiscsi, nor is it handled cleanly in the config file. All that I could find was an email thread that briefly outlined how to get it working.
Here’s how I implemented this on a RHEL 5 host with an equallogic backend, your mileage may vary.
How it works
From what I gather, In order to make this work you first need to add multiple new discovery records. Each time changing the contents of /etc/iscsi/iscsid.conf to reflect your differ username/password combinations. This must be done in a particular way to avoid the default discovery behavior which removes records that are no longer present (to your current user). The command used to perform discovery is as follows, evidently the ‘-o new’ argument prevents removal of old records.
# edit /etc/iscsi/iscsid.conf to change user/pass for current target, then iscsiadm -m discovery -t st -p portal_ip_address -o new --discover
Once discovered, the username and password needs to be set individually for each target.
iscsiadm -m node --targetname TARGET -p PORTAL -o update -n node.session.auth.username -v USERNAME iscsiadm -m node --targetname TARGET -p PORTAL -o update -n node.session.auth.password -v PASSWORD
At this point you should be able to log in/out of that target
iscsiadm -m node --targetname TARGET -p PORTAL --login
Now rince, lather and repeat for the remainder if your target/portal/username/password combinations.
Scripting the process
This script takes an array of target/portal/username/password combinations and loops through them to automate the process. Once complete the script will attempt to log in to all defined targets. This works well for me on RHEL5, your mileage may vary.
#!/bin/bash # iscsi_multi_setup.sh - Set up connections for multiple iSCSI targets having # unique portal/user/password combinations # # 2012 Keith Herron <keith@backdrift.org> TARGET[0]=iqn.2001-05.com.equallogic:0-8a0906-a75e07205-d650005e7f54fb28-lun-0 PORTAL[0]=portal_ip0 USERNAME[0]=username0 PASSWORD[0]=password0 TARGET[1]=iqn.2001-05.com.equallogic:0-8a0906-ab5e07205-43c0005e7f74fb28-lun-1 PORTAL[1]=portal_ip1 USERNAME[1]=username1 PASSWORD[1]=password1 TARGET[2]=iqn.2001-05.com.equallogic:0-8a0906-af4e07205-db40005e7f94fb28-lun-2 PORTAL[2]=portal_ip2 USERNAME[2]=username2 PASSWORD[2]=password2 i=0 while [ "$i" -lt "${#TARGET[@]}" ]; do echo Connecting to: ${TARGET[$i]} # Set discovery user/pass in /etc/iscsi/iscsid.conf and restart the service sed -i "s/^discovery.sendtargets.auth.username.*/discovery.sendtargets.auth.username = ${USERNAME[$i]}/g" /etc/iscsi/iscsid.conf sed -i "s/^discovery.sendtargets.auth.password.*/discovery.sendtargets.auth.password = ${PASSWORD[$i]}/g" /etc/iscsi/iscsid.conf sleep 1 /etc/init.d/iscsid restart sleep 1 # Perform discovery (note -o new argument, this is important) iscsiadm -m discovery -t st -p ${PORTAL[$i]} -o new --discover sleep 1 # Set username/password individually for each target, and login iscsiadm -m node --targetname ${TARGET[$i]} -p ${PORTAL[$i]} -o update -n node.session.auth.username -v ${USERNAME[$i]} iscsiadm -m node --targetname ${TARGET[$i]} -p ${PORTAL[$i]} -o update -n node.session.auth.password -v ${PASSWORD[$i]} iscsiadm -m node --targetname ${TARGET[$i]} -p ${PORTAL[$i]} -l # Log out of target iscsiadm -m node --logoutall all i=$((i+1)) done # Set discovery user/password to nothing in hopes to prevent manual discovery sed -i "s/^discovery.sendtargets.auth.username.*/discovery.sendtargets.auth.username = /g" /etc/iscsi/iscsid.conf sed -i "s/^discovery.sendtargets.auth.password.*/discovery.sendtargets.auth.password = /g" /etc/iscsi/iscsid.conf # Log in to all nodes now echo Logging in to all nodes iscsiadm -m node --login
Hope that helps, please leave a comment if you found this helpful. Or if you were able to adapt it to work on a different distribution.