Move it into postinst, so it runs before mkinitcpio

This commit is contained in:
Thomas Duckworth
2026-01-24 17:42:31 +11:00
parent c8f8368651
commit aa06e69b61
3 changed files with 41 additions and 42 deletions

View File

@@ -28,8 +28,8 @@ fi
# NOTE: plymouth MUST be after systemd as per the wiki!
cat <<- EOF > mkinitcpio.conf
MODULES=(overlay)
BINARIES=(nvidia nouveau) # Ensure both drivers are pulled into the initrd (see mkosi.finalize.d/20-nvidia-udev-generator.sh.chroot)
FILES=(/usr/lib/udev/rules.d/99-nvidia-module-selection.rules) # Determine which driver is used at boot with udev
BINARIES=(nvidia nouveau) # Ensure both drivers are pulled into the initrd (see mkosi.postinst.chroot)
FILES=(/usr/lib/udev/rules.d/05-nvidia-module-selection.rules) # Determine which driver is used at boot with udev
HOOKS=(base systemd modconf kms keyboard block sd-encrypt filesystems fsck systemd-extension plymouth microcode sd-shutdown)
EOF

View File

@@ -1,40 +0,0 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
# SPDX-FileCopyrightText: 2025 Thomas Duckworth <tduck@filotimoproject.org>
set -e
# Remove the nouveau blacklist so our dynamic selection can actually load it
if [ -f /usr/lib/modprobe.d/nvidia-utils.conf ]; then
sed -i '/blacklist nouveau/d' /usr/lib/modprobe.d/nvidia-utils.conf
fi
# Now, generate a huge udev rule which selects the correct driver for NVIDIA graphics cards.
# This gets included in the initrd - see mkosi.extra/usr/lib/rebuild-efi
PCI_IDS="/usr/share/hwdata/pci.ids"
UDEV_RULE="/usr/lib/udev/rules.d/99-nvidia-module-selection.rules"
echo "# Generated by mkosi.finalize.d/50-nvidia-udev.chroot" > "$UDEV_RULE"
echo "# Static driver overrides for NVIDIA GPUs based on ID ranges" >> "$UDEV_RULE"
gawk '
/^10de/ { in_nvidia=1; next }
/^[0-9a-f]/ { in_nvidia=0 }
in_nvidia && /^\t[0-9a-f]{4}/ {
device_id = $1
dec_id = strtonum("0x" device_id)
# 0x1e00 (7680) is where Turing architecture begins (RTX 20-series / GTX 16-series).
# This is, currently, the cutoff for support in the open kernel modules.
if (dec_id >= 7680) {
driver = "nvidia"
} else {
driver = "nouveau"
}
# Match only Class 03 (Display Controllers) to avoid breaking Audio/USB/Bridges.
# The wildcard .* after 0x03 matches any programming interface/subclass.
printf "ACTION==\"add\", SUBSYSTEM==\"pci\", ATTR{vendor}==\"0x10de\", ATTR{device}==\"0x%s\", ATTR{class}==\"0x03*\", ATTR{driver_override}=\"%s\"\n", device_id, driver
}
' "$PCI_IDS" >> "$UDEV_RULE"

View File

@@ -141,6 +141,45 @@ for theme in $(plymouth-set-default-theme --list); do
fi
done
# Remove the nouveau blacklist so our dynamic selection can actually load it
if [ -f /usr/lib/modprobe.d/nvidia-utils.conf ]; then
sed -i '/blacklist nouveau/d' /usr/lib/modprobe.d/nvidia-utils.conf
fi
# Generate a huge udev rule which selects the correct driver for NVIDIA graphics cards.
# This gets included in the initrd - see mkosi.extra/usr/lib/rebuild-efi
PCI_IDS="/usr/share/hwdata/pci.ids"
UDEV_RULE="/usr/lib/udev/rules.d/05-nvidia-module-selection.rules"
echo "# Generated by mkosi.postinst.chroot" > "$UDEV_RULE"
echo "# Static driver overrides for NVIDIA GPUs based on ID ranges." >> "$UDEV_RULE"
gawk '
BEGIN { IGNORECASE = 1 }
# Vendor line, e.g. "10de NVIDIA Corporation".
/^10de\b/ { in_nvidia = 1; next }
# Any other vendor line (4 hex digits at start) disables in_nvidia.
/^[0-9A-Fa-f]{4}\s/ { in_nvidia = 0; next }
# device lines are indented and begin with a 4-hex-digit device id
in_nvidia && /^\s+[0-9A-Fa-f]{4}\b/ {
device_id = gensub(/^\s+([0-9A-Fa-f]{4}).*/, "\\1", "g")
dec_id = strtonum("0x" device_id)
# 0x1e00 (7680) is where Turing architecture begins (RTX 20-series / GTX 16-series).
# This is, currently, the cutoff for support in the open kernel modules.
if (dec_id >= 7680) {
driver = "nvidia"
} else {
driver = "nouveau"
}
# Match only Class 03 (Display Controllers) to avoid breaking Audio/USB/Bridges.
printf "ACTION==\"add\", SUBSYSTEM==\"pci\", ATTR{vendor}==\"0x10de\", ATTR{device}==\"0x%s\", ATTR{class}==\"0x03*\", ATTR{driver_override}=\"%s\"\n", device_id, driver
}
' "$PCI_IDS" >> "$UDEV_RULE"
cd /tmp
/usr/lib/rebuild-efi
mv -v ./*.efi /