File nvgpu-power.sh of Package nv-dgpu-pwr
#!/bin/bash
# 61-nvgpu-power – early-boot helper for discrete GPUs (dracut)
# shellcheck shell=bash
# dracut helpers: getarg(), warn(), etc.
. /lib/dracut-lib.sh
set -ux
warn "nvgpu-power: start"
# ── 0) Fast-bail conditions ────────────────────────────────────────────────
# If resuming from hibernate or the nvidia/nouveau drivers are blacklisted, do nothing.
[ -n "${resume-}" ] && exit 0
case "$(cat /proc/cmdline)" in
*modprobe.blacklist=nouveau*|*modprobe.blacklist=*nouveau*|*modprobe.blacklist=nvidia*|*modprobe.blacklist=*nvidia*) exit 0 ;;
esac
# ── 1) Inputs / defaults (override via kernel cmdline if needed) ───────────
# BUS_PCI is the bridge we "remove" before rescanning the bus.
BUS_PCI_DEFAULT="0000:00:1d.0"
TARGET_PCI_DEFAULT="0000:3c:00.0" # device we want to appear
# Allow overrides:
# rd.nvgpu_pci=<bridge BDF> e.g. rd.nvgpu_pci=0000:00:1d.0
# rd.nvgpu_target=<device BDF> e.g. rd.nvgpu_target=0000:3c:00.0
BUS_PCI="${BUS_PCI_DEFAULT}"
arg=$(getarg rd.nvgpu_pci=) && BUS_PCI="${arg#*=}"
TARGET_PCI="${TARGET_PCI_DEFAULT}"
arg=$(getarg rd.nvgpu_target=) && TARGET_PCI="${arg#*=}"
BUS_PATH="/sys/bus/pci/devices/${BUS_PCI}"
TARGET_PATH="/sys/bus/pci/devices/${TARGET_PCI}"
# ── 2) Remove→rescan loop: up to 10 iterations or until TARGET appears ─────
max_iters=10
for ((i=1; i<=max_iters; i++)); do
# If the target device is present already, we’re done.
if [ -e "${TARGET_PATH}" ]; then
warn "nvgpu-power: target ${TARGET_PCI} present after ${i}-1 cycles"
exit 0
fi
# If the bridge exists, try removing it to trigger a clean rescan.
if [ -e "${BUS_PATH}/remove" ]; then
echo 1 > "${BUS_PATH}/remove" || warn "nvgpu-power: failed to remove ${BUS_PCI}"
else
warn "nvgpu-power: ${BUS_PATH}/remove not present (iter ${i})"
fi
sleep 2
# Rescan the PCI bus and give the kernel a moment.
echo 1 > /sys/bus/pci/rescan
sleep 2
done
# Final check after the last rescan
if [ -e "${TARGET_PATH}" ]; then
warn "nvgpu-power: target ${TARGET_PCI} present after ${max_iters} cycles"
exit 0
fi
warn "nvgpu-power: target ${TARGET_PCI} did not appear after ${max_iters} cycles"
exit 0