How to load vfio driver at boot before ahci for a sata controller?

Hello,
I’m attaching several devices to vfio at boot with the following method:
modify /etc/modprobe.d/vfio.conf with:
options vfio-pci ids=aaaa:1111, bbbb:2222

modify /etc/mkinitcpio.conf with:
MODULES=(vfio_pci vfio vfio_iommu_type1 vfio_virqfd)

HOOKS=(modconf base udev autodetect block filesystems keyboard)

mkinitcpio -P

Everything is working as expected, except for the sata controller I want to attach to vfio.
Adding vendor and product ids to vfio.conf doesn’t prevent the ahci driver to load.

I found this:

and this:

Anybody knows how to translate this for manjaro mkinitcpio?

Thank you

Kernel Parameter: vfio-pci.ids=aaaa:1111,bbbb:2222

The kernel’s command-line parameters — The Linux Kernel documentation

1 Like

Unfortunately, setting it as a kernel parameter doesn’t change anything (yes, I updated grub), ahci driver is attaching to the sata controller when not in use by vfio.

I am not an expert here… but from what I understand is:

  1. /etc/initramfs-tools/modules is the same as /usr/lib/initcpio/install/

  2. /usr/share/initramfs-tools/scripts/init-top/udev should be the file /usr/lib/initcpio/install/udev (but a different script)

Well in theory it should be enough to add a script here:

 /usr/lib/initcpio/install/load_vfio-pci

With this content:

#!/bin/sh
modprobe vfio-pci ids=8086:1c02

and make it executable:

sudo chmod +x  /usr/lib/initcpio/install/load_vfio-pci

Then add it to the hooks:

HOOKS=(load_vfio-pci modconf base udev autodetect block filesystems keyboard)

Then run sudo mkinitcpio -P -v

1 Like

Thank you again for your inputs,
I tried to follow near all your indications, except for the content of load_vfio-pci file, mkinitcpio wants a build function, so I write it as:

#!/bin/sh

build() {
    modprobe vfio-pci ids=1b4b:9230
}

Not sure if it’s right…

But still that damn ahci driver attaches to the sata controller

Hmm… wrong folder I guess :man_facepalming:
This btrfs for example:

$ cat /usr/lib/initcpio/hooks/btrfs 
#!/usr/bin/ash

run_hook() {
    btrfs device scan
}

# vim: set ft=sh ts=4 sw=4 et:

So must be like that:

#!/usr/bin/ash

run_hook() {
    modprobe vfio-pci ids=1b4b:9230
}

in /usr/lib/initcpio/hooks/vfio-pci_load

1 Like

mmm I don’t think so, because now when building initramfs I have:
==> ERROR: Hook ‘vfio-pci_load’ cannot be found

From the wiki:
Build hooks are found in /usr/lib/initcpio/install/ , custom build hooks can be placed in /etc/initcpio/install/ . These files are sourced by the bash shell during runtime of mkinitcpio and should contain two functions: build and help . The build function describes the modules, files, and binaries which will be added to the image. An API, documented by mkinitcpio(8), serves to facilitate the addition of these items. The help function outputs a description of what the hook accomplishes.

Maybe something like this?
In /usr/lib/initcpio/install/load_vfio-pci

#!/bin/bash

build() {
    add_runscript
}

In /usr/lib/initcpio/hooks/load_vfio-pci

#!/usr/bin/ash

run_hook() {
    modprobe vfio-pci ids=1b4b:9230
}

Nope…I noticed that during boot, it is displayed “running eraly_hook [udev]” and after that “running load_vfio-pci”, so from what I read ahci is already attached.

Maybe in the hook I can try to unbind the controller from ahci…
Something like:

#!/usr/bin/ash

run_hook() {
    echo -n "0000:0a:00.0" > /sys/bus/pci/drivers/ahci/unbind
    modprobe vfio-pci ids=1b4b:9230
}

in /usr/lib/initcpio/hooks/load_vfio-pci
0000:0a:00.0 being the pcie address of the sata controller…

ok, finally this somehow works:
In /usr/lib/initcpio/install/load_vfio-pci

#!/bin/bash

build() {
    add_runscript
}

In /usr/lib/initcpio/hooks/load_vfio-pci

#!/usr/bin/ash

run_hook() {
    echo -n "0000:0a:00.0" > /sys/bus/pci/drivers/ahci/unbind
    echo '1b4b 9230' > /sys/bus/pci/drivers/vfio-pci/new_id
}

In /etc/mkinitcpio.conf

MODULES=(vfio_pci vfio vfio_iommu_type1 vfio_virqfd)

and

HOOKS=(load_vfio-pci base udev autodetect modconf block filesystems keyboard)

Thank you for your inputs, they were really useful!

Still curious if it’s possible to load the hook before “early hooks” (run_earlyhook() maybe?But vfio is still not available in earlyhook…).