open-iscsi: add package open-iscsi

iscsi initiator utilities

Signed-off-by: Lucian Cristian <lucian.cristian@gmail.com>
This commit is contained in:
Lucian Cristian
2020-04-12 03:06:23 +03:00
parent a4bb706918
commit a636c85eda
15 changed files with 1667 additions and 0 deletions
+75
View File
@@ -0,0 +1,75 @@
#!/bin/sh
#
# /sbin/iscsi-gen-initiatorname
#
# Generate a default iSCSI Initiatorname for SUSE installations.
#
# Copyright (c) 2011 Hannes Reinecke, SUSE Labs
# This script is licensed under the GPL.
#
if [ "$1" ] ; then
if [ "$1" = "-f" ] ; then
FORCE=1
else
echo "Invalid option $1"
echo "Usage: $0 [-f]"
exit 1
fi
fi
if [ -d /sys/firmware/ibft/initiator ] ; then
read iSCSI_INITIATOR_NAME < /sys/firmware/ibft/initiator/initiator-name
fi
if [ -f /etc/iscsi/initiatorname.iscsi -a -z "$FORCE" ] ; then
if [ "$iSCSI_INITIATOR_NAME" ] ; then
eval $(cat /etc/iscsi/initiatorname.iscsi | sed -e '/^#/d')
if [ "$iSCSI_INITIATOR_NAME" != "$InitiatorName" ] ; then
echo "iSCSI Initiatorname from iBFT is different from the current setting."
echo "Please call '/sbin/iscsi-gen-initiatorname -f' to update the iSCSI Initiatorname."
exit 1
fi
fi
fi
if [ "$iSCSI_INITIATOR_NAME" ] ; then
cat << EOF >> /etc/iscsi/initiatorname.iscsi
##
## /etc/iscsi/iscsi.initiatorname
##
## iSCSI Initiatorname taken from iBFT BIOS tables.
##
## DO NOT EDIT OR REMOVE THIS FILE!
## If you remove this file, the iSCSI daemon will not start.
## Any change here will not be reflected to the iBFT BIOS tables.
## If a different initiatorname is required please change the
## initiatorname in the BIOS setup and call
## /sbin/iscsi-gen-initiatorname -f
## to recreate an updated version of this file.
##
InitiatorName=$iSCSI_INITIATOR_NAME
EOF
fi
if [ ! -f /etc/iscsi/initiatorname.iscsi ] ; then
cat << EOF >> /etc/iscsi/initiatorname.iscsi
##
## /etc/iscsi/iscsi.initiatorname
##
## Default iSCSI Initiatorname.
##
## 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.
EOF
YEAR=$(date +"%Y")
MONTH=$(date +"%m")
ISSUEDATE="$YEAR$MONTH"
INAME=$(iscsi-iname -p iqn.$ISSUEDATE.local.openwrt:01)
printf "InitiatorName=$INAME\n" >>/etc/iscsi/initiatorname.iscsi
chmod 0600 /etc/iscsi/initiatorname.iscsi
fi
+195
View File
@@ -0,0 +1,195 @@
#!/bin/sh
#
# Copyright (C) Voltaire Ltd. 2006. ALL RIGHTS RESERVED.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Author: Dan Bar Dov <danb@voltaire.com>
# iscsi_discovery:
# * does a send-targets discovery to the given IP
# * set the transport type to the preferred transport (or tcp is -t flag is not used)
# * tries to login
# * if succeeds,
# o logout,
# o mark record autmatic (unless -m flag is used)
# * else
# o reset transport type to TCP
# o try to login
# o if succeeded
# + logout
# + mark record automatic (unless -m flag is used)
#
usage()
{
echo "Usage: $0 <IP> [-p <port>] [-d] [-t <tcp|iser> [-f]] [-m] [-l]"
echo "Options:"
echo "-p set the port number (default is 3260)."
echo "-d print debugging information"
echo "-t set transport (default is tcp)."
echo "-f force specific transport -disable the fallback to tcp (default is fallback enabled)."
echo " force the transport specified by the argument of the -t flag."
echo "-m manual startup - will set manual startup (default is automatic startup)."
echo "-l login to the new discovered nodes (default is false)."
}
dbg()
{
$debug && echo $@
}
initialize()
{
trap "exit" 2
debug=false
force="0"
log_out="1"
startup_manual="0"
#set default transport to tcp
transport=tcp
#set default port to 3260
port=3260;
}
parse_cmdline()
{
if [ $# -lt 1 ]; then
usage
exit 1
fi
# check if the IP address is valid
ip=`echo $1 | awk -F'.' '$1 != "" && $1 <=255 && $2 != "" && $2 <= 255 && $3 != "" && $3 <= 255 && $4 != "" && $4 <= 255 {print $0}'`
if [ -z "$ip" ]; then
echo "$1 is not a vaild IP address!"
exit 1
fi
shift
while getopts "dfmlt:p:" options; do
case $options in
d ) debug=true;;
f ) force="1";;
t ) transport=$OPTARG;;
p ) port=$OPTARG;;
m ) startup_manual="1";;
l ) log_out=0;;
\? ) usage
exit 1;;
* ) usage
exit 1;;
esac
done
}
discover()
{
# If open-iscsi is already logged in to the portal, exit
if [ $(iscsiadm -m session | grep -c ${ip}:${port}) -ne 0 ]; then
echo "Please logout from all targets on ${ip}:${port} before trying to run discovery on that portal"
exit 2
fi
connected=0
discovered=0
dbg "starting discovery to $ip"
disc="$(iscsiadm -m discovery --type sendtargets --portal ${ip}:${port})"
echo "${disc}" | while read portal target
do
portal=${portal%,*}
select_transport
done
discovered=$(echo "${disc}" | wc -l)
if [ ${discovered} = 0 ]; then
echo "failed to discover targets at ${ip}"
exit 2
else
echo "discovered ${discovered} targets at ${ip}"
fi
}
try_login()
{
if [ "$startup_manual" != "1" ]; then
iscsiadm -m node --targetname ${target} --portal ${portal} --op update -n node.conn[0].startup -v automatic
fi
iscsiadm -m node --targetname ${target} --portal ${portal} --login >/dev/null 2>&1
ret=$?
if [ ${ret} = 0 ]; then
echo "Set target ${target} to automatic login over ${transport} to portal ${portal}"
((connected++))
if [ "$log_out" = "1" ]; then
iscsiadm -m node --targetname ${target} --portal ${portal} --logout
fi
else
echo "Cannot login over ${transport} to portal ${portal}"
iscsiadm -m node --targetname ${target} --portal ${portal} --op update -n node.conn[0].startup -v manual
fi
return ${ret}
}
set_transport()
{
transport=$1
case "$transport" in
iser)
# iSER does not use digest
iscsiadm -m node --targetname ${target} --portal ${portal} \
--op update -n node.conn[0].iscsi.HeaderDigest -v None
iscsiadm -m node --targetname ${target} --portal ${portal} \
--op update -n node.conn[0].iscsi.DataDigest -v None
;;
cxgb3i)
# cxgb3i supports <= 16K packet (BHS + AHS + pdu payload + digests)
iscsiadm -m node --targetname ${target} --portal ${portal} \
--op update -n node.conn[0].iscsi.MaxRecvDataSegmentLength \
-v 8192
;;
esac
transport_name=`iscsiadm -m node -p ${portal} -T ${target} |awk '/transport_name/ {print $1}'`
iscsiadm -m node --targetname ${target} --portal ${portal} \
--op update -n ${transport_name} -v ${transport}
}
select_transport()
{
set_transport $transport
dbg "Testing $transport-login to target ${target} portal ${portal}"
try_login;
if [ $? != 0 -a "$force" = "0" ]; then
set_transport tcp
dbg "starting to test tcp-login to target ${target} portal ${portal}"
try_login;
fi
}
check_iscsid()
{
#check if iscsid is running
pidof iscsid &>/dev/null
ret=$?
if [ $ret -ne 0 ]; then
echo "iscsid is not running"
echo "Exiting..."
exit 1
fi
}
check_iscsid
initialize
parse_cmdline "$@"
discover
+12
View File
@@ -0,0 +1,12 @@
#!/bin/sh
#
# iscsi_fw_login -- login to iscsi firmware targets, if any
#
# This script is called when udev discovers a new iscsi
# firmware target
#
ARGS="-m fw -l"
ISCSIADM="iscsiadm"
$ISCSIADM $ARGS
+378
View File
@@ -0,0 +1,378 @@
#!/bin/sh
#
# iscsi_offload
#
# Configure iSCSI offload engines for use with open-iscsi
# Usage:
# iscsi_offload [-d | -f | -i <ipaddr> | -t ] <nic>
#
# Copyright (c) 2011 Hannes Reinecke, SUSE Labs
# This script is licensed under the GPL.
#
# The script creates an open-iscsi interface definition
# in the style <nic>-<module>, where <nic> matches the
# network interface passed on the commandline.
# If '-t' (test mode) is passed as an option, the script
# will not create nor modify any setting but just print
# the currently active ones.
#
# Currently the script works with Broadcom (bnx2i) and
# Chelsio T3 (cxgbi) iSCSI offload engines.
# Should work with Chelsio T4, but has not been tested.
# ServerEngines (be2iscsi) and QLogic (qla4xxx) can only
# be configured via BIOS, open-iscsi support is still in
# development.
#
#
# Return codes:
# 0: Success
# 1: Invalid command line parameter
# 2: iSCSI offloading not supported
# 3: Error during module loading
# 4: Cannot configure interface via iscsiadm, use BIOS setup
# 5: internal error running iscsiadm
#
# Output:
# <mac> [none|dhcp|ip <ipaddr>|ibft]
# where
# <mac>: MAC Address of the iSCSI offload engine
# none: No IP configuration set for the iSCSI offload engine
# dhcp: iSCSI offload engine configured for DHCP
# ip: iSCSI offload engine configured with static IP address <ipaddr>
# ibft: iSCSI offload engine configured from iBFT values
#
#
# Figure out the MAC address of the iSCSI offload engine
# corresponding to a NIC from a given PCI device.
# bnx2 is using one PCI device per port for both network and iSCSI offloading
# cxgb3 is using one PCI device for everything.
#
iscsi_macaddress_from_pcidevice()
{
local path=$1
local if=$2
local h
local host
for h in $path/host* ; do
if [ -d "$h" ] ; then
host=${h##*/}
read netdev < /sys/class/iscsi_host/$host/netdev
if [ "$netdev" = "$IFNAME" ] ; then
read mac < /sys/class/iscsi_host/$host/hwaddress
if [ "$mac" != "00:00:00:00:00:00" ] ; then
echo "$mac"
fi
break;
fi
fi
done
}
#
# Figure out the MAC address of the iSCSI offload engine
# corresponding to a NIC from a given PCI function.
# It is assumed that the MAC address of the iSCSI offload
# engine is equal of the MAC address of the NIC plus one.
# Suitable for be2iscsi and qla4xxx
#
iscsi_macaddress_from_pcifn()
{
local path=$1
local if=$2
local h
local host
local ifmac
ifmac=$(ip addr show dev $if | sed -n 's/ *link\/ether \(.*\) brd.*/\1/p')
m5=$(( 0x${ifmac##*:} ))
m5=$(( $m5 + 1 ))
ifmac=$(printf "%s:%02x" ${ifmac%:*} $m5)
for host in /sys/class/iscsi_host/host* ; do
if [ -L "$host" ] ; then
read mac < $host/hwaddress
if [ "$mac" = "$ifmac" ] ; then
echo "$mac"
break;
fi
fi
done
}
update_iface_setting() {
local iface="$1"
local name="$2"
local value="$3"
iface_value=$(iscsiadm -m iface -I $iface | sed -n "s/$name = \(.*\)/\1/p")
if [ "$iface_value" = "<empty>" ] ; then
iface_value=
fi
if [ "$iface_value" != "$value" ] ; then
if ! iscsiadm -m iface -I $iface -o update -n "$name" -v "$value" ; then
return 1
fi
fi
return 0
}
while getopts di:t options ; do
case $options in
d ) mode=dhcp;;
i ) mode=static
optaddr=$OPTARG
;;
f ) mode=firmware;;
t ) dry_run=1;;
?) printf "Usage: %s [-d|-t|-i ipaddr|-f] ifname\n" $0
exit 1;;
esac
done
shift $(($OPTIND - 1))
IFNAME=$1
ibft_mode="none"
if [ -z "$IFNAME" ] ; then
echo "No interface specified"
exit 1
fi
if [ "$dry_run" ] ; then
if [ "$mode" = "dhcp" ] ; then
echo "'-t' specified, ignoring '-d'"
mode=
elif [ "$mode" = "static" ] ; then
echo "'-t' specified, ignoring '-s'"
mode=
fi
fi
if [ ! -L /sys/class/net/$IFNAME ] ; then
echo "Interface $IFNAME not found"
exit 1
fi
if [ "$optaddr" ] && ! ip route get $optaddr ; then
echo "Invalid IP address $optaddr"
exit 1
fi
if [ "$dry_run" ] ; then
mode=
fi
ifpath=$(cd -P /sys/class/net/$IFNAME; echo $PWD)
pcipath=$(cd -P $ifpath/device; echo $PWD)
if [ -d $pcipath ] ; then
drvlink=$(readlink $pcipath/driver)
driver=${drvlink##*/}
fi
if [ -z "$driver" ] ; then
echo "No driver found for interface $IFNAME"
exit 1
fi
case "$driver" in
bnx2*)
mod=bnx2i
;;
cxgb*)
mod=cxgb3i
;;
be2*)
mod=be2iscsi
;;
qla*)
mod=qla4xxx
;;
esac
if [ -z "$mod" ] ; then
echo "iSCSI offloading not supported on interface $IFNAME"
exit 2
fi
# Check if the required modules are already loaded
loaded=$(sed -n "/^$mod/p" /proc/modules)
if [ -z "$loaded" ] ; then
modprobe $mod
fi
loaded=$(sed -n "/^$mod/p" /proc/modules)
if [ -z "$loaded" ] ; then
echo "Loading of $mod.ko failed, please check dmesg"
exit 3
fi
# Get the correct MAC address for the various devices
if [ "$mod" = "bnx2i" ] ; then
mac=$(iscsi_macaddress_from_pcidevice $pcipath $IFNAME)
elif [ "$mod" = "cxgb3i" ] ; then
mac=$(iscsi_macaddress_from_pcidevice $pcipath $IFNAME)
elif [ "$mod" = "be2iscsi" ] ; then
mac=$(iscsi_macaddress_from_pcifn $pcipath $IFNAME)
elif [ "$mod" = "qla4xxx" ] ; then
mac=$(iscsi_macaddress_from_pcifn $pcipath $IFNAME)
fi
if [ -z "$mac" ] ; then
echo "iSCSI offloading not supported on interface $IFNAME"
exit 2
fi
gen_iface="$mod.$mac"
ioe_iface="${IFNAME}-${mod}"
# Get existing settings
if iscsiadm -m iface -I $ioe_iface > /dev/null 2>&1 ; then
ioe_mac=$(iscsiadm -m iface -I $ioe_iface 2> /dev/null| sed -n "s/iface\.hwaddress = \(.*\)/\1/p")
ioe_mod=$(iscsiadm -m iface -I $ioe_iface 2> /dev/null| sed -n "s/iface\.transport_name = \(.*\)/\1/p")
ipaddr=$(iscsiadm -m iface -I $ioe_iface 2> /dev/null| sed -n "s/iface\.ipaddress = \(.*\)/\1/p")
if [ "$ipaddr" == "<empty>" ] ; then
ipaddr=
fi
elif [ "$mod" = "be2iscsi" ] ; then
ioe_mac=$mac
ioe_mod=$mod
else
# Create new interface
iscsiadm -m iface -I $ioe_iface --op=new 2> /dev/null
ioe_mac=
ioe_mod=
ipaddr=
fi
if [ -z "$dry_run" ] ; then
if [ "$ioe_mac" != "$mac" ] ; then
if [ -n "$ioe_mac" ] ; then
echo "Warning: Updating MAC address on iface $ioe_iface"
fi
update_iface_setting $ioe_iface iface.hwaddress "$mac"
fi
if [ "$ioe_mod" != "$mod" ] ; then
if [ -n "$ioe_mod" ] ; then
echo "Warning: Update transport on iface $ioe_iface"
fi
update_iface_setting $ioe_iface iface.transport_name "$mod"
fi
elif [ -z "$ipaddr" ] ; then
ipaddr=$(iscsiadm -m iface -I $gen_iface 2> /dev/null| sed -n "s/iface\.ipaddress = \(.*\)/\1/p")
if [ "$ipaddr" = "<empty>" ] ; then
ipaddr=
fi
elif [ "$ioe_mod" != "$mod" ] ; then
echo "Warning: Transport mismatch on iface $ioe_iface: $ioe_mod should be $mod"
fi
# Check iBFT setting
for d in /sys/firmware/* ; do
[ -d $d ] || continue
[ -d $d/ethernet0 ] || continue
iboot_dir=$d
done
if [ -n "$iboot_dir" ] && [ -d "$iboot_dir" ] ; then
for if in ${iboot_dir}/ethernet* ; do
read ibft_mac < $if/mac
[ "$ibft_mac" = "$mac" ] || continue
ibft_origin=0
[ -f ${if}/origin ] && read ibft_origin < $if/origin
if [ "$ibft_origin" -eq 1 ] ; then
ibft_mode="static"
elif [ "$ibft_origin" -eq 3 ] ; then
ibft_mode="dhcp"
fi
[ -f $if/dhcp ] && read ibft_dhcp < $if/dhcp
if [ -n "$ibft_dhcp" -a "$ibft_mode" != "dhcp" ] ; then
ibft_mode=dhcp
fi
if [ "$ibft_mode" = "dhcp" ] ; then
ibft_ipaddr="0.0.0.0"
ibft_gateway=
ibft_mask=
break
fi
[ -f $if/ip-addr ] && read ibft_ipaddr < $if/ip-addr
[ -f $if/gateway ] && read ibft_gateway < $if/gateway
[ -f $if/subnet-mask ] && read ibft_mask < $if/subnet-mask
break
done
fi
if [ -z "$optaddr" ] && [ "$ibft_ipaddr" ] ; then
optaddr=$ibft_ipaddr
fi
# Check if the interface needs to be configured
if [ -z "$mode" ] ; then
if [ "$ibft_mode" != "none" ] ; then
echo "$mac ibft"
mode="ibft"
elif [ -z "$ipaddr" ] ; then
echo "$mac none"
mode="none"
elif [ "$ipaddr" = "0.0.0.0" ] ; then
echo "$mac dhcp"
ipaddr=
mode="dhcp"
else
echo "$mac ip $ipaddr"
mode="static"
fi
[ "$dry_run" ] && exit 0
elif [ "$mode" = "dhcp" ] ; then
if [ "$ipaddr" = "0.0.0.0" ] ; then
echo "$mac dhcp"
exit 0
fi
optaddr="0.0.0.0"
elif [ "$mode" = "static" ] && [ "$ipaddr" = "$optaddr" ] ; then
echo "$mac ip $ipaddr"
exit 0
fi
if [ "$mod" = "be2iscsi" ] ; then
exit 4
fi
if ! update_iface_setting $ioe_iface iface.ipaddress "$optaddr" ; then
echo "Failed to set IP address: $?"
exit 1
fi
if ! update_iface_setting $gen_iface iface.ipaddress "$optaddr" ; then
echo "Failed to set IP address for generic interface: $?"
exit 1
fi
if ! update_iface_setting $ioe_iface iface.gateway "$ibft_gateway" ; then
echo "Failed to set gateway address: $?"
exit 1
fi
if ! update_iface_setting $gen_iface iface.gateway "$ibft_gateway" ; then
echo "Failed to set gateway address for generic interface: $?"
exit 1
fi
if ! update_iface_setting $ioe_iface iface.subnet_mask "$ibft_mask" ; then
echo "Failed to set subnet mask: $?"
exit 1
fi
if ! update_iface_setting $gen_iface iface.subnet_mask "$ibft_mask" ; then
echo "Failed to set subnet mask for generic interface: $?"
exit 1
fi
if [ "$mod" = "qla4xxx" ] ; then
iscsiadm -m iface -H $mac -o applyall
fi
ip link set dev $IFNAME up
exit 0
+93
View File
@@ -0,0 +1,93 @@
#!/bin/sh /etc/rc.common
START=50
STOP=50
#USE_PROCD=1
DAEMON=/sbin/iscsid
ADM=/sbin/iscsiadm
PIDFILE=/var/run/iscsid.pid
log()
{
logger -t iscsid -s -p daemon.info "$@"
}
#start_service() {
# procd_open_instance
# procd_set_param command $DAEMON -f -d 8 -p $PIDFILE
# procd_set_param respawn
# procd_close_instance
#}
prepare() {
[ ! -f /etc/iscsi/initiatorname.iscsi ] && logger "Generate initator name ..." \
&& /usr/sbin/iscsi-gen-initiatorname
mkdir -p /var/run/lock/iscsi
}
unmount() {
TARGETS="$(iscsiadm -m session -P 3 | grep disk | awk '{print $4}')"
for VAL in $TARGETS; do
MOUNTED="$(mount | grep $VAL | awk '{print $1}')"
for PART in $MOUNTED; do
log "Umount $PART"
$(umount $PART)
done
done
RETVAL=$?
}
start() {
pgrep -o iscsid && return 1
prepare
$DAEMON -p $PIDFILE
starttargets
}
starttargets() {
log "Setting up iSCSI targets"
$ADM -m node -l
}
stoptargets() {
unmount
if [ $RETVAL -ne 0 ]; then
log "Could not umount all partitions, verify file lock!"
return $RETVAL
fi
log "Disconnecting iSCSI targets"
sync
$ADM -m node -u
}
stop() {
pgrep -o iscsid || return 1
stoptargets
#needs more checks
if [ $RETVAL -ne 0 ]; then
log "Could not stop all targets, try again later"
return $RETVAL
fi
log "Stopping iSCSI initiator service"
$ADM -k 0 2
rm -f $PIDFILE
}
restart() {
stop
if [ $RETVAL -ne 0 ]; then
log "Stopping iSCSI initiator service failed, not starting"
return $RETVAL
fi
start
}
restarttargets() {
stoptargets
if [ $RETVAL -ne 0 ]; then
log "Could not stop all targets, try again later"
return $RETVAL
fi
starttargets
}