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