1bf31c
From 0954a69e884e99af5082f93e2de30fa8dfdba83e Mon Sep 17 00:00:00 2001
1bf31c
From: Hannes Reinecke <hare@suse.com>
1bf31c
Date: Mon, 25 Nov 2019 11:33:42 +0100
1bf31c
Subject: [PATCH] 95nvmf: add module for NVMe-oF
1bf31c
1bf31c
Add a module for booting from NVMe-oF devices.
1bf31c
1bf31c
Signed-off-by: Hannes Reinecke <hare@suse.de>
1bf31c
(cherry picked from commit 2f03d69f9dfdc0c905c4d9cb69ee93a5256123cb)
1bf31c
1bf31c
Resolves: #1731601
1bf31c
---
1bf31c
 dracut.spec                                     |  1 +
1bf31c
 modules.d/95nvmf/module-setup.sh                | 88 +++++++++++++++++++++++
1bf31c
 modules.d/95nvmf/parse-nvmf-boot-connections.sh | 95 +++++++++++++++++++++++++
1bf31c
 3 files changed, 184 insertions(+)
1bf31c
1bf31c
diff --git a/dracut.spec b/dracut.spec
1bf31c
index a8076c18..3db5c0cf 100644
1bf31c
--- a/dracut.spec
1bf31c
+++ b/dracut.spec
1bf31c
@@ -370,6 +370,7 @@ install -m 0755 51-dracut-rescue-postinst.sh $RPM_BUILD_ROOT%{_sysconfdir}/kerne
1bf31c
 %{dracutlibdir}/modules.d/95debug
1bf31c
 %{dracutlibdir}/modules.d/95fstab-sys
1bf31c
 %{dracutlibdir}/modules.d/95lunmask
1bf31c
+%{dracutlibdir}/modules.d/95nvmf
1bf31c
 %{dracutlibdir}/modules.d/95resume
1bf31c
 %{dracutlibdir}/modules.d/95rootfs-block
1bf31c
 %{dracutlibdir}/modules.d/95terminfo
1bf31c
diff --git a/modules.d/95nvmf/module-setup.sh b/modules.d/95nvmf/module-setup.sh
1bf31c
new file mode 100755
1bf31c
index 00000000..db43ec01
1bf31c
--- /dev/null
1bf31c
+++ b/modules.d/95nvmf/module-setup.sh
1bf31c
@@ -0,0 +1,88 @@
1bf31c
+#!/bin/bash
1bf31c
+
1bf31c
+# called by dracut
1bf31c
+check() {
1bf31c
+    require_binaries nvme || return 1
1bf31c
+    [ -f /etc/nvme/hostnqn ] || return 255
1bf31c
+    [ -f /etc/nvme/hostid ] || return 255
1bf31c
+
1bf31c
+    is_nvme_fc() {
1bf31c
+        local _dev=$1
1bf31c
+        local traddr
1bf31c
+
1bf31c
+        [[ -L "/sys/dev/block/$_dev" ]] || return 0
1bf31c
+        cd -P "/sys/dev/block/$_dev" || return 0
1bf31c
+        if [ -f partition ] ; then
1bf31c
+            cd ..
1bf31c
+        fi
1bf31c
+        for d in device/nvme* ; do
1bf31c
+            [ -L "$d" ] || continue
1bf31c
+            if readlink "$d" | grep -q nvme-fabrics ; then
1bf31c
+                traddr=$(cat "$d"/address)
1bf31c
+		break
1bf31c
+	    fi
1bf31c
+	done
1bf31c
+        [[ "${traddr#traddr=nn-}" != "$traddr" ]]
1bf31c
+    }
1bf31c
+
1bf31c
+    [[ $hostonly ]] || [[ $mount_needs ]] && {
1bf31c
+        pushd . >/dev/null
1bf31c
+        for_each_host_dev_and_slaves is_nvme_fc
1bf31c
+        local _is_nvme_fc=$?
1bf31c
+        popd >/dev/null
1bf31c
+        [[ $_is_nvme_fc == 0 ]] || return 255
1bf31c
+        if [ ! -f /sys/class/fc/fc_udev_device/nvme_discovery ] ; then
1bf31c
+            if [ ! -f /etc/nvme/discovery.conf ] ; then
1bf31c
+                echo "No discovery arguments present"
1bf31c
+                return 255
1bf31c
+            fi
1bf31c
+        fi
1bf31c
+    }
1bf31c
+    return 0
1bf31c
+}
1bf31c
+
1bf31c
+# called by dracut
1bf31c
+depends() {
1bf31c
+    echo bash rootfs-block
1bf31c
+    return 0
1bf31c
+}
1bf31c
+
1bf31c
+# called by dracut
1bf31c
+installkernel() {
1bf31c
+    instmods nvme_fc lpfc qla2xxx
1bf31c
+}
1bf31c
+
1bf31c
+# called by dracut
1bf31c
+cmdline() {
1bf31c
+    local _hostnqn
1bf31c
+    local _hostid
1bf31c
+    if [ -f /etc/nvme/hostnqn ] ; then
1bf31c
+        _hostnqn=$(cat /etc/nvme/hostnqn)
1bf31c
+        echo -n " nvmf.hostnqn=${_hostnqn}"
1bf31c
+    fi
1bf31c
+    if [ -f /etc/nvme/hostid ] ; then
1bf31c
+        _hostid=$(cat /etc/nvme/hostid)
1bf31c
+        echo -n " nvmf.hostid=${_hostid}"
1bf31c
+    fi
1bf31c
+    echo ""
1bf31c
+}
1bf31c
+
1bf31c
+# called by dracut
1bf31c
+install() {
1bf31c
+    if [[ $hostonly_cmdline == "yes" ]]; then
1bf31c
+	local _nvmf_args=$(cmdline)
1bf31c
+	[[ "$_nvmf_args" ]] && printf "%s" "$_nvmf_args" >> "${initdir}/etc/cmdline.d/95nvmf-args.conf"
1bf31c
+    fi
1bf31c
+    inst_simple "/etc/nvme/hostnqn"
1bf31c
+    inst_simple "/etc/nvme/hostid"
1bf31c
+
1bf31c
+    inst_multiple nvme
1bf31c
+    inst_multiple -o \
1bf31c
+        "$systemdsystemunitdir/nvm*-connect@.service" \
1bf31c
+        "$systemdsystemunitdir/nvm*-connect.target"
1bf31c
+    inst_hook cmdline 99 "$moddir/parse-nvmf-boot-connections.sh"
1bf31c
+    inst_simple "/etc/nvme/discovery.conf"
1bf31c
+    inst_rules /usr/lib/udev/rules.d/70-nvm*-autoconnect.rules
1bf31c
+    inst_rules /usr/lib/udev/rules.d/71-nvmf-iopolicy-netapp.rules
1bf31c
+    dracut_need_initqueue
1bf31c
+}
1bf31c
diff --git a/modules.d/95nvmf/parse-nvmf-boot-connections.sh b/modules.d/95nvmf/parse-nvmf-boot-connections.sh
1bf31c
new file mode 100755
1bf31c
index 00000000..0d16b871
1bf31c
--- /dev/null
1bf31c
+++ b/modules.d/95nvmf/parse-nvmf-boot-connections.sh
1bf31c
@@ -0,0 +1,95 @@
1bf31c
+#!/bin/sh
1bf31c
+#
1bf31c
+# Supported formats:
1bf31c
+# nvmf.hostnqn=<hostnqn>
1bf31c
+# nvmf.hostid=<hostid>
1bf31c
+# nvmf.discover=<transport>:<traddr>:<host-traddr>:<trsvcid>
1bf31c
+#
1bf31c
+# Examples:
1bf31c
+# nvmf.hostnqn=nqn.2014-08.org.nvmexpress:uuid:37303738-3034-584d-5137-333230423843
1bf31c
+# nvmf.discover=rdma:192.168.1.3::4420
1bf31c
+# nvmf.discover=fc:auto
1bf31c
+#
1bf31c
+# Note: FC does autodiscovery, so typically there is no need to
1bf31c
+# specify any discover parameters for FC.
1bf31c
+#
1bf31c
+
1bf31c
+parse_nvmf_discover() {
1bf31c
+    OLDIFS="$IFS"
1bf31c
+    IFS=:
1bf31c
+    trtype="none"
1bf31c
+    traddr="none"
1bf31c
+    hosttraddr="none"
1bf31c
+    trsvcid=4420
1bf31c
+
1bf31c
+    set $1
1bf31c
+    IFS="$OLDIFS"
1bf31c
+
1bf31c
+    case $# in
1bf31c
+        2)
1bf31c
+            trtype=$1
1bf31c
+            traddr=$2
1bf31c
+            ;;
1bf31c
+        3)
1bf31c
+            trtype=$1
1bf31c
+            traddr=$2
1bf31c
+            hosttraddr=$3
1bf31c
+            ;;
1bf31c
+        4)
1bf31c
+            trtype=$1
1bf31c
+            traddr=$2
1bf31c
+            hosttraddr=$3
1bf31c
+            trsvcid=$4
1bf31c
+            ;;
1bf31c
+        *)
1bf31c
+            warn "Invalid arguments for nvmf.discover=$1"
1bf31c
+            return 1
1bf31c
+            ;;
1bf31c
+    esac
1bf31c
+    if [ -z "$traddr" ] ; then
1bf31c
+        warn "traddr is mandatory for $trtype"
1bf31c
+        return 1;
1bf31c
+    fi
1bf31c
+    [ -z "$hosttraddr" ] && hosttraddr="none"
1bf31c
+    [ -z "$trsvcid" ] && trsvcid="none"
1bf31c
+    if [ "$trtype" = "fc" ] ; then
1bf31c
+        if [ -z "$hosttraddr" ] ; then
1bf31c
+            warn "host traddr is mandatory for fc"
1bf31c
+            return 1
1bf31c
+        fi
1bf31c
+    elif [ "$trtype" != "rdma" ] && [ "$trtype" != "tcp" ] ; then
1bf31c
+        warn "unsupported transport $trtype"
1bf31c
+        return 1
1bf31c
+    elif [ -z "$trsvcid" ] ; then
1bf31c
+        trsvcid=4420
1bf31c
+    fi
1bf31c
+    echo "--transport=$trtype --traddr=$traddr --host-traddr=$hosttraddr --trsvcid=$trsvcid" >> /etc/nvme/discovery.conf
1bf31c
+}
1bf31c
+
1bf31c
+if ! getargbool 0 rd.nonvmf ; then
1bf31c
+	info "rd.nonvmf=0: skipping nvmf"
1bf31c
+	return 0
1bf31c
+fi
1bf31c
+
1bf31c
+nvmf_hostnqn=$(getarg nvmf.hostnqn=)
1bf31c
+if [ -n "$nvmf_hostnqn" ] ; then
1bf31c
+    echo "$nvmf_hostnqn" > /etc/nvme/hostnqn
1bf31c
+fi
1bf31c
+nvmf_hostid=$(getarg nvmf.hostid=)
1bf31c
+if [ -n "$nvmf_hostid" ] ; then
1bf31c
+    echo "$nvmf_hostid" > /etc/nvme/hostid
1bf31c
+fi
1bf31c
+
1bf31c
+for d in $(getargs nvmf.discover=); do
1bf31c
+    parse_nvmf_discover "$d"
1bf31c
+done
1bf31c
+
1bf31c
+# Host NQN and host id are mandatory for NVMe-oF
1bf31c
+[ -f "/etc/nvme/hostnqn" ] || exit 0
1bf31c
+[ -f "/etc/nvme/hostid" ] || exit 0
1bf31c
+
1bf31c
+if [ -f "/etc/nvme/discovery.conf" ] ; then
1bf31c
+    /sbin/initqueue --onetime --unique --name nvme-discover /usr/sbin/nvme connect-all
1bf31c
+else
1bf31c
+    /sbin/initqueue --finished --unique --name nvme-fc-autoconnect echo 1 > /sys/class/fc/fc_udev_device/nvme_discovery
1bf31c
+fi
1bf31c