Files
diskless-workstation/files/dracut/95dot1x-macsec/nvmf-connect.sh
T

50 lines
2.0 KiB
Bash

#!/bin/bash
# nvmf-connect — ExecStart for nvmf-connect.service (initramfs, TEST MODE).
#
# Why a systemd unit and not a dracut hook: this initramfs is systemd-driven and
# does NOT source dracut initqueue/online hooks when the ip=dhcp link comes up
# (verified on the bench — the online hook installs but never fires: no log, no
# connect). dracut's nvmf module also registers no autoconnect, and a job placed
# on the main initqueue is never re-executed once the loop blocks on events. A
# oneshot unit ordered Before=initrd-root-device.target, with its own
# wait-for-network + connect loop, deterministically connects the NVMe-oF root
# namespace so the by-id device appears and systemd-veritysetup can map
# /dev/mapper/root.
#
# rd.dot1x=0 (open-network test) ONLY — the unit's ConditionKernelCommandLine
# already gates this, but re-check so the script is safe to run directly. The
# secure path connects over macsec0 from dot1x-macsec-up.sh.
type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
[ "$(getarg rd.dot1x=)" = "0" ] || exit 0
[ -e /run/dot1x/.nvmf-connected ] && exit 0
_disc=$(getarg rd.nvmf.discover=) # e.g. tcp,192.168.50.10,4420
[ -n "$_disc" ] || { echo "nvmf-connect: rd.nvmf.discover not set" >&2; exit 1; }
_trtype=${_disc%%,*}
_rest=${_disc#*,}
_traddr=${_rest%%,*}
_trsvcid=${_rest##*,}
# nvme_tcp is usually auto-loaded; ensure it so /dev/nvme-fabrics exists.
modprobe nvme_tcp 2>/dev/null
# Networking (ip=dhcp) is brought up in parallel, so poll-and-connect until a
# fabrics namespace appears or we time out. connect-all is idempotent.
_i=0
while [ "$_i" -lt 60 ]; do
nvme connect-all -t "$_trtype" -a "$_traddr" -s "$_trsvcid" 2>/dev/null
for _ns in /dev/nvme[0-9]*n[0-9]*; do
if [ -b "$_ns" ]; then
mkdir -p /run/dot1x
: > /run/dot1x/.nvmf-connected
echo "nvmf-connect: connected ${_trtype}/${_traddr}/${_trsvcid} ($_ns)"
exit 0
fi
done
sleep 1
_i=$((_i + 1))
done
echo "nvmf-connect: timed out connecting ${_trtype}/${_traddr}/${_trsvcid}" >&2
exit 1