Introduction
If you have a NAS in your network, it can be convinient to setup iSCSI on the NAS and export logical drives to your Debian server. This HOWTO has been tested on Debian Wheezy.
Package that need to be installed
apt-get install open-iscsi
On the target (NAS)
In this example we use XEN on our Debian server and we use iSCSI for the different drive partitions on the virtual machines (DomU's).
Create one target in the NAS for every virtual machine and one LUN on the target for every partition the virtual machine will use e g disk and swap.
On the initiator (Debian server running XEN)
Configure udev rules
UPDATED: 2016-06-26 (fixed typo in script below)
Run the following as root:
echo 'KERNEL=="sd*", ACTION=="add", PROGRAM="/etc/udev/scripts/iscsidev.sh %p",SYMLINK+="iscsi/%c{1}/lun%c{2}"' > /etc/udev/rules.d/55-openiscsi.rules
cat > /etc/udev/scripts/iscsidev.sh << EOF
#!/bin/sh
# FILE: /etc/udev/scripts/iscsidev.sh
BUS="\`echo "\$1" | sed 's/\//\n/g' | egrep '^[0-9]+:'\`"
HOST=\${BUS%%:*}
[ -e /sys/class/iscsi_host ] || exit 1
file="/sys/class/iscsi_host/host\${HOST}/device/session*/iscsi_session*/session*/targetname"
target_name=\$(cat \${file})
# This is not an open-scsi drive
if [ -z "\${target_name}" ]; then
exit 1
fi
# Check if QNAP drive
check_qnap_target_name=\${target_name%%:*}
if [ \$check_qnap_target_name = "iqn.2004-04.com.qnap" ]; then
target_name=\`echo "\${target_name%.*}"\`
fi
lun="\`echo \$BUS | awk -F: '{print \$NF}'\`"
echo "\${target_name##*.} \$lun"
EOF
chmod 755 /etc/udev/scripts/iscsidev.sh
Commands to use
To find targets from the NAS on the initiator (Debian server)
iscsiadm --mode discovery --type sendtargets --portal <TARGET_IP>
It is useful to use this command after a new target has been added on the NAS to get the target name.
Manually connect to a target
iscsiadm --mode node --targetname <TARGET> --portal <TARGET_IP>:3260 --login
Example
iscsiadm --mode node --targetname iqn.2004-04.com.qnap:ts-412:iscsi.dhcpserver.d8a009 --portal 192.168.1.1:3260 --login
Manually disconnect from a target
iscsiadm --mode node --targetname <TARGET> --portal <TARGET_IP>:3260 --logout
Tell open iscsi to automatically connect to a target during boot
iscsiadm --mode node --targetname <TARGET> --portal <TARGET_IP> --op update --name node.startup --value automatic
IMPORTANT NOTE
Your automatic settings will be reverted to manual everytime you perform a scan usingiscsiadm --mode discovery --type sendtargets
It might be a good idea to create a script where you configure your targets startup to be automatic. This script can be executed manually when needed.
/usr/local/bin/update-iscsi-targets.sh
1 2 3 4 5 6 7 8 |
|
The result
Your imported devices should now be available in /dev/iscsi/<TARGET_NAME>/<LUN>
Example
/dev/iscsi/dhcpserver/lun0
You can use the dives directly in your cfg file for your DomU or setup encryption with cryptsetup.