From: Roger Pau Monne <roger.pau@citrix.com>
To: xen-devel@lists.xen.org
Cc: Ian Jackson <ian.jackson@citrix.com>,
Ian Campbell <ian.campbell@citrix.com>,
Roger Pau Monne <roger.pau@citrix.com>
Subject: [PATCH v2 7/7] hotplug/Linux: add iscsi block hotplug script
Date: Mon, 18 Mar 2013 12:47:11 +0100 [thread overview]
Message-ID: <1363607231-35228-8-git-send-email-roger.pau@citrix.com> (raw)
In-Reply-To: <1363607231-35228-1-git-send-email-roger.pau@citrix.com>
This hotplug script has been tested with IET and NetBSD iSCSI targets,
without authentication.
Authentication parameters, including the password are passed as
parameters to iscsiadm, which is not recommended because other users
of the system can see them. This parameters could also be set by
editing a corresponding file directly, but the location of this file
seems to be different depending on the distribution used.
Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
Cc: Ian Campbell <ian.campbell@citrix.com>
Cc: Ian Jackson <ian.jackson@citrix.com>
---
Changes since v1:
* Add -e to script shebang, and use 'set +e' if we know hotplug
execution might fail.
---
tools/hotplug/Linux/Makefile | 1 +
tools/hotplug/Linux/block-iscsi | 278 +++++++++++++++++++++++++++++++++++++++
2 files changed, 279 insertions(+), 0 deletions(-)
create mode 100644 tools/hotplug/Linux/block-iscsi
diff --git a/tools/hotplug/Linux/Makefile b/tools/hotplug/Linux/Makefile
index 0605559..98c7738 100644
--- a/tools/hotplug/Linux/Makefile
+++ b/tools/hotplug/Linux/Makefile
@@ -21,6 +21,7 @@ XEN_SCRIPTS += blktap
XEN_SCRIPTS += xen-hotplug-cleanup
XEN_SCRIPTS += external-device-migrate
XEN_SCRIPTS += vscsi
+XEN_SCRIPTS += block-iscsi
XEN_SCRIPT_DATA = xen-script-common.sh locking.sh logging.sh
XEN_SCRIPT_DATA += xen-hotplug-common.sh xen-network-common.sh vif-common.sh
XEN_SCRIPT_DATA += block-common.sh
diff --git a/tools/hotplug/Linux/block-iscsi b/tools/hotplug/Linux/block-iscsi
new file mode 100644
index 0000000..badfa52
--- /dev/null
+++ b/tools/hotplug/Linux/block-iscsi
@@ -0,0 +1,278 @@
+#!/bin/sh -e
+#
+# Open-iSCSI Xen block device hotplug script
+#
+# Author Roger Pau Monné <roger.pau@citrix.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published
+# by the Free Software Foundation; version 2.1 only. with the special
+# exception on linking described in file LICENSE.
+#
+# 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 Lesser General Public License for more details.
+
+remove_label()
+{
+ echo $1 | sed "s/^\("$2"\)//"
+}
+
+check_tools()
+{
+ if ! type iscsiadm > /dev/null 2>&1; then
+ echo "Unable to find iscsiadm tool"
+ return 1
+ fi
+ if [ "$multipath" = "y" ] && ! type multipath > /dev/null 2>&1; then
+ echo "Unable to find multipath"
+ return 1
+ fi
+}
+
+# Sets the following global variables based on the params field passed in as
+# a parameter: iqn, portal, auth_method, user, multipath, password
+parse_target()
+{
+ # set multipath default value
+ multipath="n"
+ for param in $(echo "$1" | tr "," "\n")
+ do
+ if [ -n "$password" ]; then
+ password="$password,$param"
+ continue
+ fi
+ case $param in
+ iqn=*)
+ iqn=$(remove_label $param "iqn=")
+ ;;
+ portal=*)
+ portal=$(remove_label $param "portal=")
+ ;;
+ auth_method=*)
+ auth_method=$(remove_label $param "auth_method=")
+ ;;
+ user=*)
+ user=$(remove_label $param "user=")
+ ;;
+ multipath=*)
+ multipath=$(remove_label $param "multipath=")
+ ;;
+ password=*)
+ password=$(remove_label $param "password=")
+ ;;
+ esac
+ done
+ if [ -z "$iqn" ] || [ -z "$portal" ]; then
+ echo "iqn= and portal= are required parameters"
+ return 1
+ fi
+ if [ "$multipath" != "y" ] && [ "$multipath" != "n" ]; then
+ echo "multipath valid values are y and n, $multipath not a valid value"
+ return 1
+ fi
+ return 0
+}
+
+# Outputs the block device major:minor
+device_major_minor()
+{
+ stat -L -c %t:%T "$1"
+}
+
+# Sets $dev to point to the device associated with the value in iqn
+find_device()
+{
+ while [ ! -e /dev/disk/by-path/*"$iqn"-lun-0 ]; do
+ sleep 0.1
+ done
+ set +e
+ sddev=$(readlink -f /dev/disk/by-path/*"$iqn"-lun-0)
+ set -e
+ if [ ! -b "$sddev" ]; then
+ echo "Unable to find attached device path"
+ return 1
+ fi
+ if [ "$multipath" = "y" ]; then
+ mdev=$(multipath -ll "$sddev" | head -1 | awk '{ print $1}')
+ if [ ! -b /dev/mapper/"$mdev" ]; then
+ echo "Unable to find attached device multipath"
+ return 1
+ fi
+ dev="/dev/mapper/$mdev"
+ else
+ dev="$sddev"
+ fi
+ return 0
+}
+
+# Attaches the target $iqn in $portal and sets $dev to point to the
+# multipath device
+attach()
+{
+ set +e
+ iscsiadm -m node --targetname "$iqn" -p "$portal" --login > /dev/null 2>&1
+ rc=$?
+ set -e
+ if [ $rc -ne 0 ]; then
+ echo "Unable to connect to target"
+ return 1
+ fi
+ find_device
+ if [ $? -ne 0 ]; then
+ echo "Unable to find iSCSI device"
+ return 1
+ fi
+ return 0
+}
+
+# Discovers targets in $portal and checks that $iqn is one of those targets
+# Also sets the auth parameters to attach the device
+prepare()
+{
+ # Check if target is already opened
+ set +e
+ iscsiadm -m session 2>&1 | grep -q "$iqn"
+ rc=$?
+ set -e
+ if [ $rc -eq 0 ]; then
+ echo "Device already opened"
+ return 1
+ fi
+ # Discover portal targets
+ set +e
+ iscsiadm -m discovery -t st -p $portal 2>&1 | grep -q "$iqn"
+ rc=$?
+ set -e
+ if [ $rc -ne 0 ]; then
+ echo "No matching target iqn found"
+ return 1
+ fi
+ # Set auth method if necessary
+ if [ -n "$auth_method" ] || [ -n "$user" ] || [ -n "$password" ]; then
+ set +e
+ iscsiadm -m node --targetname "$iqn" -p "$portal" --op=update --name \
+ node.session.auth.authmethod --value="$auth_method" \
+ > /dev/null 2>&1 && \
+ iscsiadm -m node --targetname "$iqn" -p "$portal" --op=update --name \
+ node.session.auth.username --value="$user" \
+ > /dev/null 2>&1 && \
+ iscsiadm -m node --targetname "$iqn" -p "$portal" --op=update --name \
+ node.session.auth.password --value="$password" \
+ > /dev/null 2>&1
+ rc=$?
+ set -e
+ if [ $rc -ne 0 ]; then
+ echo "Unable to set authentication parameters"
+ return 1
+ fi
+ fi
+ return 0
+}
+
+# Attaches the device and writes xenstore backend entries to connect
+# the device
+add()
+{
+ localattach
+ if [ $? -ne 0 ]; then
+ echo "Failed to attach device"
+ return 1
+ fi
+ mm=$(device_major_minor "$dev")
+ xenstore-write "$BACKEND_PATH/physical-device" "$mm"
+ xenstore-write "$BACKEND_PATH/params" "$dev"
+ return 0
+}
+
+# Disconnects the device
+remove()
+{
+ find_device
+ if [ $? -ne 0 ]; then
+ echo "Unable to find device"
+ return 1
+ fi
+ set +e
+ iscsiadm -m node --targetname "$iqn" -p "$portal" --logout > /dev/null 2>&1
+ rc=$?
+ set -e
+ if [ $rc -ne 0 ]; then
+ echo "Unable to disconnect target"
+ return 1
+ fi
+ return 0
+}
+
+# Removes the auth params set by prepare
+unprepare()
+{
+ if [ -n "$auth_method" ] || [ -n "$user" ] || [ -n "$password" ]; then
+ set +e
+ iscsiadm -m node --targetname "$iqn" -p "$portal" --op=delete --name \
+ node.session.auth.authmethod > /dev/null 2>&1
+ iscsiadm -m node --targetname "$iqn" -p "$portal" --op=delete --name \
+ node.session.auth.username > /dev/null 2>&1
+ iscsiadm -m node --targetname "$iqn" -p "$portal" --op=delete --name \
+ node.session.auth.password > /dev/null 2>&1
+ set -e
+ fi
+}
+
+# Attaches the device to the current domain, and writes the resulting
+# block device to xenstore
+localattach()
+{
+ attach
+ if [ $? -ne 0 ]; then
+ echo "Failed to attach device"
+ return 1
+ fi
+ xenstore-write "$HOTPLUG_PATH/pdev" "$dev"
+ return 0
+}
+
+command=$1
+set +e
+target=$(xenstore-read $HOTPLUG_PATH/params)
+set -e
+if [ -z "$target" ]; then
+ echo "No information about the target"
+ exit 1
+fi
+
+check_tools || exit 1
+
+parse_target "$target"
+
+case $command in
+prepare)
+ prepare
+ exit $?
+ ;;
+add)
+ add
+ exit $?
+ ;;
+remove)
+ remove
+ exit $?
+ ;;
+unprepare)
+ unprepare
+ exit $?
+ ;;
+localattach)
+ localattach
+ exit $?
+ ;;
+localdetach)
+ remove
+ exit $?
+ ;;
+version)
+ xenstore-write "$HOTPLUG_PATH/version" "2"
+ exit $?
+ ;;
+esac
--
1.7.7.5 (Apple Git-26)
_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel
next prev parent reply other threads:[~2013-03-18 11:47 UTC|newest]
Thread overview: 74+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-04-17 15:47 [PATCH v3 0/7] libxl: new hotplug calling convention Roger Pau Monne
2013-04-17 15:47 ` [PATCH v3 1/7] libxl: group hotplug related variables Roger Pau Monne
2013-04-17 15:47 ` [PATCH v3 2/7] libxl: add new hotplug interface support to hotplug script callers Roger Pau Monne
2013-03-18 11:47 ` [PATCH v2] libxl: new hotplug calling convention Roger Pau Monne
2013-03-18 11:47 ` [PATCH v2 1/7] libxl: group hotplug related variables Roger Pau Monne
2013-03-18 11:47 ` [PATCH v2 2/7] libxl: add new hotplug interface support to hotplug script callers Roger Pau Monne
2013-04-11 16:25 ` Ian Jackson
2013-04-16 11:30 ` Roger Pau Monné
2013-04-16 12:06 ` Ian Campbell
2013-04-16 14:42 ` Roger Pau Monné
2013-04-16 15:04 ` Ian Jackson
2013-04-11 16:33 ` Ian Jackson
2013-04-12 13:51 ` Roger Pau Monné
2013-04-18 17:23 ` [PATCH v2 2/7] libxl: add new hotplug interface support to hotplug script callers [and 1 more messages] Ian Jackson
2013-04-18 17:40 ` Roger Pau Monné
2013-04-18 17:52 ` Ian Jackson
2013-04-19 8:10 ` Roger Pau Monné
2013-03-18 11:47 ` [PATCH v2 3/7] libxl: add support for hotplug interface v2 in domain creation/destroy Roger Pau Monne
2013-03-18 11:47 ` [PATCH v2 4/7] libxl: chain prepare and attach in libxl_device_disk_add Roger Pau Monne
2013-03-18 11:47 ` [PATCH v2 5/7] libxl: add disk specific remove functions Roger Pau Monne
2013-01-23 17:48 ` [PATCH v1 0/12] libxl: new hotplug calling convention Roger Pau Monne
2013-01-23 17:48 ` [PATCH v1 01/12] libxl: libxl__prepare_ao_device should reset num_exec Roger Pau Monne
2013-01-25 8:57 ` Ian Campbell
2013-01-23 17:48 ` [PATCH v1 02/12] libxl: remove double check in NetBSD hotplug Roger Pau Monne
2013-03-13 14:45 ` Ian Jackson
2013-01-23 17:48 ` [PATCH v1 03/12] libxl: move libxl_device_action to idl Roger Pau Monne
2013-03-13 14:48 ` Ian Jackson
2013-01-23 17:48 ` [PATCH v1 04/12] libxl: pack hotplug related variables Roger Pau Monne
2013-03-13 14:49 ` Ian Jackson
2013-03-14 16:44 ` Roger Pau Monné
2013-01-23 17:48 ` [PATCH v1 05/12] libxl: add new hotplug interface support to hotplug script callers Roger Pau Monne
2013-03-13 14:53 ` Ian Jackson
2013-03-13 16:04 ` Roger Pau Monné
2013-03-13 16:10 ` Ian Jackson
2013-03-14 17:03 ` Roger Pau Monné
2013-01-23 17:48 ` [PATCH v1 06/12] libxl: add support for different hotplug interfaces Roger Pau Monne
2013-03-13 16:17 ` Ian Jackson
2013-03-15 11:03 ` Roger Pau Monné
2013-03-15 11:24 ` Ian Jackson
2013-03-15 11:36 ` Roger Pau Monné
2013-01-23 17:48 ` [PATCH v1 07/12] libxl: add prepare/unprepare operations to the libxl public interface Roger Pau Monne
2013-03-13 16:19 ` Ian Jackson
2013-03-15 11:33 ` Roger Pau Monné
2013-03-15 11:42 ` Roger Pau Monné
2013-03-15 12:20 ` Ian Jackson
2013-03-15 12:39 ` Roger Pau Monné
2013-01-23 17:48 ` [PATCH v1 08/12] libxl: add disk specific remove functions Roger Pau Monne
2013-03-13 16:22 ` Ian Jackson
2013-03-15 11:52 ` Roger Pau Monné
2013-04-11 16:17 ` [PATCH v1 08/12] libxl: add disk specific remove functions [and 1 more messages] Ian Jackson
2013-04-15 7:33 ` Roger Pau Monné
2013-01-23 17:48 ` [PATCH v1 09/12] xl: add support for new hotplug interface to block-attach/detach Roger Pau Monne
2013-03-13 16:22 ` Ian Jackson
2013-03-15 11:54 ` Roger Pau Monné
2013-01-23 17:48 ` [PATCH v1 10/12] libxl: add local attach support for new hotplug scripts Roger Pau Monne
2013-03-13 16:25 ` Ian Jackson
2013-03-15 11:59 ` Roger Pau Monné
2013-01-23 17:48 ` [PATCH v1 11/12] hotplug: document new hotplug interface Roger Pau Monne
2013-03-13 16:29 ` Ian Jackson
2013-03-15 12:29 ` Roger Pau Monné
2013-01-23 17:48 ` [PATCH v1 12/12] hotplug/Linux: add iscsi block hotplug script Roger Pau Monne
2013-03-13 16:31 ` Ian Jackson
2013-03-15 12:08 ` Roger Pau Monné
2013-03-15 12:24 ` Ian Jackson
2013-03-04 11:13 ` [PATCH v1 0/12] libxl: new hotplug calling convention Roger Pau Monné
2013-03-18 11:47 ` [PATCH v2 6/7] libxl: add local attach support for new hotplug scripts Roger Pau Monne
2013-03-18 11:47 ` Roger Pau Monne [this message]
2013-04-17 15:47 ` [PATCH v3 3/7] libxl: add support for hotplug interface v2 in domain creation/destroy Roger Pau Monne
2013-04-18 17:30 ` Ian Jackson
2013-04-17 15:47 ` [PATCH v3 4/7] libxl: chain prepare and attach in libxl_device_disk_add Roger Pau Monne
2013-04-17 15:47 ` [PATCH v3 5/7] libxl: add disk specific remove functions Roger Pau Monne
2013-04-17 15:47 ` [PATCH v3 6/7] libxl: add local attach support for new hotplug scripts Roger Pau Monne
2013-04-17 15:47 ` [PATCH v3 7/7] hotplug/Linux: add iscsi block hotplug script Roger Pau Monne
2013-04-18 16:28 ` [PATCH v3 0/7] libxl: new hotplug calling convention George Dunlap
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1363607231-35228-8-git-send-email-roger.pau@citrix.com \
--to=roger.pau@citrix.com \
--cc=ian.campbell@citrix.com \
--cc=ian.jackson@citrix.com \
--cc=xen-devel@lists.xen.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).