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