feat: support duplicate device IDs in initramfs-tools and mkinitcpio

This commit is contained in:
jedrw 2025-01-12 13:34:47 +00:00
parent adedfb01b9
commit dfb889122e
7 changed files with 126 additions and 60 deletions

View file

@ -3,18 +3,18 @@ package configs
import (
"fmt"
"os"
"path"
"regexp"
"slices"
"strings"
"github.com/HikariKnight/quickpassthrough/internal/common"
"github.com/HikariKnight/quickpassthrough/internal/logger"
"github.com/HikariKnight/quickpassthrough/pkg/fileio"
)
// Set_Mkinitcpio copies the content of /etc/mkinitcpio.conf to the config folder and does an inline replace/insert on the MODULES=() line
func Set_Mkinitcpio() {
// Get the config struct
config := GetConfig()
func Set_Mkinitcpio(config *Config) {
// Make sure we start from scratch by deleting any old file
if exists, _ := fileio.FileExist(config.Path.MKINITCPIO); exists {
_ = os.Remove(config.Path.MKINITCPIO)
@ -26,6 +26,7 @@ func Set_Mkinitcpio() {
// Make a regex to find the modules line
module_line_re := regexp.MustCompile(`^MODULES=`)
hooks_line_re := regexp.MustCompile(`^HOOKS=`)
modules_re := regexp.MustCompile(`MODULES=\((.*)\)`)
vfio_modules_re := regexp.MustCompile(`(vfio_iommu_type1|vfio_pci|vfio_virqfd|vfio|vendor-reset)`)
@ -67,9 +68,54 @@ func Set_Mkinitcpio() {
// Write the modules line we generated
fileio.AppendContent(fmt.Sprintf("MODULES=(%s)\n", strings.Join(modules, " ")), config.Path.MKINITCPIO)
} else if config.HasDuplicateDeviceIds && hooks_line_re.MatchString(line) {
setMkinitcpioEarlyBinds(config, line)
} else {
// Else just write the line to the config
fileio.AppendContent(fmt.Sprintf("%s\n", line), config.Path.MKINITCPIO)
}
}
}
func setMkinitcpioEarlyBinds(config *Config, hooksLine string) {
err := os.MkdirAll(config.Path.MKINITCPIOHOOKS, os.ModePerm)
common.ErrorCheck(err, "Error, could not create mkinitcpio hook config directory")
confToSystemPathRe := regexp.MustCompile(`^config`)
earlyBindHookConfigPath := path.Join(config.Path.MKINITCPIOHOOKS, "early-vfio-bind")
earlyBindHookSysPath := confToSystemPathRe.ReplaceAllString(earlyBindHookConfigPath, "")
config.EarlyBindFilePaths[earlyBindHookConfigPath] = earlyBindHookSysPath
if exists, _ := fileio.FileExist(earlyBindHookConfigPath); exists {
_ = os.Remove(earlyBindHookConfigPath)
}
logger.Printf("Writing to early bind hook to %s", earlyBindHookConfigPath)
vfioBindHook := fmt.Sprintf(`#!/bin/bash
run_hook() {
DEVS="%s"
for DEV in $DEVS; do
echo "vfio-pci" > /sys/bus/pci/devices/$DEV/driver_override
done
# Load the vfio-pci module
modprobe -i vfio-pci
}`, strings.Join(config.Gpu_Addresses, " "))
fileio.AppendContent(vfioBindHook, earlyBindHookConfigPath)
err = os.Chmod(earlyBindHookConfigPath, 0755)
common.ErrorCheck(err, fmt.Sprintf("Error, could not chmod %s", earlyBindHookConfigPath))
hooksString := strings.Trim(strings.Split(hooksLine, "=")[1], "()")
hooks := strings.Split(hooksString, " ")
customHook := "early-vfio-bind"
if !slices.Contains(hooks, customHook) {
hooks = append(hooks, customHook)
}
// Write to logger
logger.Printf("Replacing line in %s:\n%s\nWith:\nHOOKS=(%s)\n", config.Path.MKINITCPIO, hooksLine, strings.Join(hooks, " "))
// Write the modules line we generated
fileio.AppendContent(fmt.Sprintf("HOOKS=(%s)\n", strings.Join(hooks, " ")), config.Path.MKINITCPIO)
}