Harald Hoyer 3610a1
From a187bc3ca5bb6c8d66e5942eaf33da7a9610c7c2 Mon Sep 17 00:00:00 2001
Harald Hoyer 3610a1
From: Harald Hoyer <harald@hoyer.xyz>
Harald Hoyer 3610a1
Date: Tue, 19 Dec 2017 14:39:07 +0100
Harald Hoyer 3610a1
Subject: [PATCH] Merge pull request #337 from danimo/lunmask
Harald Hoyer 3610a1
Harald Hoyer 3610a1
95lunmask: Add module to handle LUN masking
Harald Hoyer 3610a1
---
Harald Hoyer 3610a1
 modules.d/95lunmask/fc_transport_scan_lun.sh  | 26 ++++++++++
Harald Hoyer 3610a1
 modules.d/95lunmask/module-setup.sh           | 70 +++++++++++++++++++++++++++
Harald Hoyer 3610a1
 modules.d/95lunmask/parse-lunmask.sh          | 40 +++++++++++++++
Harald Hoyer 3610a1
 modules.d/95lunmask/sas_transport_scan_lun.sh | 26 ++++++++++
Harald Hoyer 3610a1
 4 files changed, 162 insertions(+)
Harald Hoyer 3610a1
Harald Hoyer 3610a1
diff --git a/modules.d/95lunmask/fc_transport_scan_lun.sh b/modules.d/95lunmask/fc_transport_scan_lun.sh
Harald Hoyer 3610a1
new file mode 100755
Harald Hoyer 3610a1
index 00000000..d9f84a38
Harald Hoyer 3610a1
--- /dev/null
Harald Hoyer 3610a1
+++ b/modules.d/95lunmask/fc_transport_scan_lun.sh
Harald Hoyer 3610a1
@@ -0,0 +1,26 @@
Harald Hoyer 3610a1
+#!/bin/bash
Harald Hoyer 3610a1
+#
Harald Hoyer 3610a1
+# fc_transport_lun_scan
Harald Hoyer 3610a1
+#
Harald Hoyer 3610a1
+# Selectively enable individual LUNs behind an FC remote port
Harald Hoyer 3610a1
+#
Harald Hoyer 3610a1
+# ACTION=="add", SUBSYSTEM=="fc_transport", ATTR{port_name}=="wwpn", \
Harald Hoyer 3610a1
+#    PROGRAM="fc_transport_lun_scan lun"
Harald Hoyer 3610a1
+#
Harald Hoyer 3610a1
+
Harald Hoyer 3610a1
+[ -z $DEVPATH ] && exit 1
Harald Hoyer 3610a1
+
Harald Hoyer 3610a1
+if [ -n "$1" ] ; then
Harald Hoyer 3610a1
+    LUN=$1
Harald Hoyer 3610a1
+else
Harald Hoyer 3610a1
+    LUN=-
Harald Hoyer 3610a1
+fi
Harald Hoyer 3610a1
+ID=${DEVPATH##*/rport-}
Harald Hoyer 3610a1
+HOST=${ID%%:*}
Harald Hoyer 3610a1
+CHANNEL=${ID%%-*}
Harald Hoyer 3610a1
+CHANNEL=${CHANNEL#*:}
Harald Hoyer 3610a1
+if [ -f /sys$DEVPATH/scsi_target_id ] ; then
Harald Hoyer 3610a1
+    TARGET=$(cat /sys$DEVPATH/scsi_target_id)
Harald Hoyer 3610a1
+fi
Harald Hoyer 3610a1
+[ -z "$TARGET" ] && exit 1
Harald Hoyer 3610a1
+echo $CHANNEL $TARGET $LUN > /sys/class/scsi_host/host$HOST/scan
Harald Hoyer 3610a1
diff --git a/modules.d/95lunmask/module-setup.sh b/modules.d/95lunmask/module-setup.sh
Harald Hoyer 3610a1
new file mode 100755
Harald Hoyer 3610a1
index 00000000..6afaed26
Harald Hoyer 3610a1
--- /dev/null
Harald Hoyer 3610a1
+++ b/modules.d/95lunmask/module-setup.sh
Harald Hoyer 3610a1
@@ -0,0 +1,70 @@
Harald Hoyer 3610a1
+#!/bin/bash
Harald Hoyer 3610a1
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
Harald Hoyer 3610a1
+# ex: ts=8 sw=4 sts=4 et filetype=sh
Harald Hoyer 3610a1
+
Harald Hoyer 3610a1
+# called by dracut
Harald Hoyer 3610a1
+cmdline() {
Harald Hoyer 3610a1
+    get_lunmask() {
Harald Hoyer 3610a1
+        local _dev=$1
Harald Hoyer 3610a1
+        local _devpath=$(cd -P /sys/dev/block/$_dev ; echo $PWD)
Harald Hoyer 3610a1
+        local _sdev _lun _rport _end_device _classdev _wwpn _sas_address
Harald Hoyer 3610a1
+
Harald Hoyer 3610a1
+        [ "${_devpath#*/sd}" == "$_devpath" ] && return 1
Harald Hoyer 3610a1
+        _sdev="${_devpath%%/block/*}"
Harald Hoyer 3610a1
+        _lun="${_sdev##*:}"
Harald Hoyer 3610a1
+        # Check for FibreChannel
Harald Hoyer 3610a1
+        _rport="${_devpath##*/rport-}"
Harald Hoyer 3610a1
+        if [ "$_rport" != "$_devpath" ] ; then
Harald Hoyer 3610a1
+            _rport="${_rport%%/*}"
Harald Hoyer 3610a1
+            _classdev="/sys/class/fc_remote_ports/rport-${_rport}"
Harald Hoyer 3610a1
+            [ -d "$_classdev" ] || return 1
Harald Hoyer 3610a1
+            _wwpn=$(cat ${_classdev}/port_name)
Harald Hoyer 3610a1
+            echo "rd.lunmask=fc,${_wwpn},${_lun}"
Harald Hoyer 3610a1
+            return 0
Harald Hoyer 3610a1
+        fi
Harald Hoyer 3610a1
+        # Check for SAS
Harald Hoyer 3610a1
+        _end_device="${_devpath##*/end_device-}"
Harald Hoyer 3610a1
+        if [ "$_end_device" != "$_devpath" ] ; then
Harald Hoyer 3610a1
+            _end_device="${_end_device%%/*}"
Harald Hoyer 3610a1
+            _classdev="/sys/class/sas_device/end_device-${_end_device}"
Harald Hoyer 3610a1
+            [ -e "$_classdev" ] || return 1
Harald Hoyer 3610a1
+            _sas_address=$(cat ${_classdev}/sas_address)
Harald Hoyer 3610a1
+            echo "rd.lunmask=sas,${_sas_address},${_lun}"
Harald Hoyer 3610a1
+            return 0
Harald Hoyer 3610a1
+        fi
Harald Hoyer 3610a1
+        return 1
Harald Hoyer 3610a1
+    }
Harald Hoyer 3610a1
+    [[ $hostonly ]] || [[ $mount_needs ]] && {
Harald Hoyer 3610a1
+        for_each_host_dev_and_slaves_all get_lunmask
Harald Hoyer 3610a1
+    } | sort | uniq
Harald Hoyer 3610a1
+}
Harald Hoyer 3610a1
+
Harald Hoyer 3610a1
+# called by dracut
Harald Hoyer 3610a1
+check() {
Harald Hoyer 3610a1
+    [[ $hostonly ]] || [[ $mount_needs ]] && {
Harald Hoyer 3610a1
+        [ -w /sys/module/scsi_mod/parameters/scan ] || return 255
Harald Hoyer 3610a1
+        scan_type=$(cat /sys/module/scsi_mod/parameters/scan)
Harald Hoyer 3610a1
+        [ "$scan_type" = "manual" ] && return 0
Harald Hoyer 3610a1
+        return 255
Harald Hoyer 3610a1
+    }
Harald Hoyer 3610a1
+    return 0
Harald Hoyer 3610a1
+}
Harald Hoyer 3610a1
+
Harald Hoyer 3610a1
+# called by dracut
Harald Hoyer 3610a1
+depends() {
Harald Hoyer 3610a1
+    return 0
Harald Hoyer 3610a1
+}
Harald Hoyer 3610a1
+
Harald Hoyer 3610a1
+# called by dracut
Harald Hoyer 3610a1
+install() {
Harald Hoyer 3610a1
+    inst_script "$moddir/fc_transport_scan_lun.sh" /usr/lib/udev/fc_transport_scan_lun.sh
Harald Hoyer 3610a1
+    inst_script "$moddir/sas_transport_scan_lun.sh" /usr/lib/udev/sas_transport_scan_lun.sh
Harald Hoyer 3610a1
+    inst_hook cmdline 30 "$moddir/parse-lunmask.sh"
Harald Hoyer 3610a1
+    if [[ $hostonly_cmdline == "yes" ]] ; then
Harald Hoyer 3610a1
+        local _lunmask
Harald Hoyer 3610a1
+
Harald Hoyer 3610a1
+        for _lunmask in $(cmdline) ; do
Harald Hoyer 3610a1
+            printf "%s\n" "$_lunmask" >> "${initdir}/etc/cmdline.d/95lunmask.conf"
Harald Hoyer 3610a1
+        done
Harald Hoyer 3610a1
+    fi
Harald Hoyer 3610a1
+}
Harald Hoyer 3610a1
diff --git a/modules.d/95lunmask/parse-lunmask.sh b/modules.d/95lunmask/parse-lunmask.sh
Harald Hoyer 3610a1
new file mode 100755
Harald Hoyer 3610a1
index 00000000..480f6147
Harald Hoyer 3610a1
--- /dev/null
Harald Hoyer 3610a1
+++ b/modules.d/95lunmask/parse-lunmask.sh
Harald Hoyer 3610a1
@@ -0,0 +1,40 @@
Harald Hoyer 3610a1
+#!/bin/sh
Harald Hoyer 3610a1
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
Harald Hoyer 3610a1
+# ex: ts=8 sw=4 sts=4 et filetype=sh
Harald Hoyer 3610a1
+
Harald Hoyer 3610a1
+create_udev_rule() {
Harald Hoyer 3610a1
+    local transport=$1
Harald Hoyer 3610a1
+    local tgtid=$2
Harald Hoyer 3610a1
+    local lun=$3
Harald Hoyer 3610a1
+    local _rule=/etc/udev/rules.d/51-${transport}-lunmask-${tgtid}.rules
Harald Hoyer 3610a1
+    local _cu_type _dev_type
Harald Hoyer 3610a1
+
Harald Hoyer 3610a1
+    [ -e ${_rule} ] && return 0
Harald Hoyer 3610a1
+
Harald Hoyer 3610a1
+    if [ ! -f "$_rule" ] ; then
Harald Hoyer 3610a1
+        if [ "$transport" = "fc" ] ; then
Harald Hoyer 3610a1
+            cat > $_rule <
Harald Hoyer 3610a1
+ACTION=="add", SUBSYSTEM=="fc_remote_ports", ATTR{port_name}=="$tgtid", PROGRAM="fc_transport_scan_lun.sh $lun"
Harald Hoyer 3610a1
+EOF
Harald Hoyer 3610a1
+        elif [ "$transport" = "sas" ] ; then
Harald Hoyer 3610a1
+            cat > $_rule <
Harald Hoyer 3610a1
+ACTION=="add", SUBSYSTEM=="sas_device", ATTR{sas_address}=="$tgtid", PROGRAM="sas_transport_scan_lun.sh $lun"
Harald Hoyer 3610a1
+EOF
Harald Hoyer 3610a1
+        fi
Harald Hoyer 3610a1
+    fi
Harald Hoyer 3610a1
+}
Harald Hoyer 3610a1
+
Harald Hoyer 3610a1
+for lunmask_arg in $(getargs rd.lunmask); do
Harald Hoyer 3610a1
+    (
Harald Hoyer 3610a1
+        local OLDIFS="$IFS"
Harald Hoyer 3610a1
+        local IFS=","
Harald Hoyer 3610a1
+        set $lunmask_arg
Harald Hoyer 3610a1
+        IFS="$OLDIFS"
Harald Hoyer 3610a1
+        if [ -d /sys/module/scsi_mod ] ; then
Harald Hoyer 3610a1
+            printf "manual" > /sys/module/scsi_mod/parameters/scan
Harald Hoyer 3610a1
+        elif [ ! -f /etc/modprobe.d/95lunmask.conf ] ; then
Harald Hoyer 3610a1
+            echo "options scsi_mod scan=manual" > /etc/modprobe.d/95lunmask.conf
Harald Hoyer 3610a1
+        fi
Harald Hoyer 3610a1
+        create_udev_rule $1 $2 $3
Harald Hoyer 3610a1
+    )
Harald Hoyer 3610a1
+done
Harald Hoyer 3610a1
diff --git a/modules.d/95lunmask/sas_transport_scan_lun.sh b/modules.d/95lunmask/sas_transport_scan_lun.sh
Harald Hoyer 3610a1
new file mode 100755
Harald Hoyer 3610a1
index 00000000..f7702f8b
Harald Hoyer 3610a1
--- /dev/null
Harald Hoyer 3610a1
+++ b/modules.d/95lunmask/sas_transport_scan_lun.sh
Harald Hoyer 3610a1
@@ -0,0 +1,26 @@
Harald Hoyer 3610a1
+#!/bin/bash
Harald Hoyer 3610a1
+#
Harald Hoyer 3610a1
+# sas_transport_lun_scan
Harald Hoyer 3610a1
+#
Harald Hoyer 3610a1
+# Selectively enable individual LUNs behind a SAS end device
Harald Hoyer 3610a1
+#
Harald Hoyer 3610a1
+# ACTION=="add", SUBSYSTEM=="sas_transport", ATTR{sas_address}=="sas_addr", \
Harald Hoyer 3610a1
+#    PROGRAM="sas_transport_lun_scan lun"
Harald Hoyer 3610a1
+#
Harald Hoyer 3610a1
+
Harald Hoyer 3610a1
+[ -z $DEVPATH ] && exit 1
Harald Hoyer 3610a1
+
Harald Hoyer 3610a1
+if [ -n "$1" ] ; then
Harald Hoyer 3610a1
+    LUN=$1
Harald Hoyer 3610a1
+else
Harald Hoyer 3610a1
+    LUN=-
Harald Hoyer 3610a1
+fi
Harald Hoyer 3610a1
+ID=${DEVPATH##*/end_device-}
Harald Hoyer 3610a1
+HOST=${ID%%:*}
Harald Hoyer 3610a1
+CHANNEL=${ID%%-*}
Harald Hoyer 3610a1
+CHANNEL=${CHANNEL#*:}
Harald Hoyer 3610a1
+if [ -f /sys$DEVPATH/scsi_target_id ] ; then
Harald Hoyer 3610a1
+    TARGET=$(cat /sys$DEVPATH/scsi_target_id)
Harald Hoyer 3610a1
+fi
Harald Hoyer 3610a1
+[ -z "$TARGET" ] && exit 1
Harald Hoyer 3610a1
+echo 0 $TARGET $LUN > /sys/class/scsi_host/host$HOST/scan
Harald Hoyer 3610a1