feat: support duplicate device IDs in dracut
This commit is contained in:
parent
c1f11ce1c3
commit
adedfb01b9
5 changed files with 170 additions and 57 deletions
|
@ -51,7 +51,7 @@ func getBootloader(config *Config) {
|
|||
// This function adds the default kernel arguments we want to the config/cmdline file
|
||||
// This gives us a file we can read all the kernel arguments this system needs
|
||||
// in case of an unknown bootloader
|
||||
func Set_Cmdline(gpu_IDs []string) {
|
||||
func Set_Cmdline(gpu_IDs []string, includeDeviceIdsForVfio bool) {
|
||||
// Get the system info
|
||||
cpuinfo := cpuid.CPU
|
||||
|
||||
|
@ -69,8 +69,10 @@ func Set_Cmdline(gpu_IDs []string) {
|
|||
fileio.AppendContent(" intel_iommu=on", config.Path.CMDLINE)
|
||||
}
|
||||
|
||||
// Add the GPU ids for vfio to the kernel arguments
|
||||
fileio.AppendContent(fmt.Sprintf(" vfio_pci.ids=%s", strings.Join(gpu_IDs, ",")), config.Path.CMDLINE)
|
||||
if includeDeviceIdsForVfio {
|
||||
// Add the GPU ids for vfio to the kernel arguments
|
||||
fileio.AppendContent(fmt.Sprintf(" vfio_pci.ids=%s", strings.Join(gpu_IDs, ",")), config.Path.CMDLINE)
|
||||
}
|
||||
}
|
||||
|
||||
// Set_KernelStub configures systemd-boot using kernelstub.
|
||||
|
|
|
@ -3,16 +3,17 @@ package configs
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/HikariKnight/quickpassthrough/internal/common"
|
||||
"github.com/HikariKnight/quickpassthrough/internal/logger"
|
||||
"github.com/HikariKnight/quickpassthrough/pkg/fileio"
|
||||
)
|
||||
|
||||
// Set_Dracut writes a dracut configuration file for `/etc/dracut.conf.d/`.
|
||||
func Set_Dracut() {
|
||||
config := GetConfig()
|
||||
|
||||
func Set_Dracut(config *Config) {
|
||||
// Set the dracut config file
|
||||
dracutConf := fmt.Sprintf("%s/vfio.conf", config.Path.DRACUT)
|
||||
|
||||
|
@ -38,4 +39,61 @@ func Set_Dracut() {
|
|||
|
||||
// Make a backup of dracutConf if there is one there
|
||||
backupFile(strings.Replace(dracutConf, "config", "", 1))
|
||||
|
||||
if config.HasDuplicateDeviceIds {
|
||||
setDracutEarlyBinds(config)
|
||||
}
|
||||
}
|
||||
|
||||
func setDracutEarlyBinds(config *Config) {
|
||||
err := os.MkdirAll(config.Path.DRACUTMODULE, os.ModePerm)
|
||||
common.ErrorCheck(err, "Error, could not create dracut module config directory")
|
||||
confToSystemPathRe := regexp.MustCompile(`^config`)
|
||||
|
||||
earlyBindScriptConfigPath := path.Join(config.Path.DRACUTMODULE, "early-vfio-bind.sh")
|
||||
earlyBindScriptSysPath := confToSystemPathRe.ReplaceAllString(earlyBindScriptConfigPath, "")
|
||||
config.EarlyBindFilePaths[earlyBindScriptConfigPath] = earlyBindScriptSysPath
|
||||
if exists, _ := fileio.FileExist(earlyBindScriptConfigPath); exists {
|
||||
_ = os.Remove(earlyBindScriptConfigPath)
|
||||
}
|
||||
|
||||
logger.Printf("Writing to early bind script to %s", earlyBindScriptConfigPath)
|
||||
vfioBindScript := fmt.Sprintf(`#!/bin/bash
|
||||
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(vfioBindScript, earlyBindScriptConfigPath)
|
||||
err = os.Chmod(earlyBindScriptConfigPath, 0755)
|
||||
common.ErrorCheck(err, fmt.Sprintf("Error, could not chmod %s", earlyBindScriptConfigPath))
|
||||
|
||||
dracutModuleConfigPath := path.Join(config.Path.DRACUTMODULE, "module-setup.sh")
|
||||
dracutModuleSysPath := confToSystemPathRe.ReplaceAllString(dracutModuleConfigPath, "")
|
||||
config.EarlyBindFilePaths[dracutModuleConfigPath] = dracutModuleSysPath
|
||||
if exists, _ := fileio.FileExist(dracutModuleConfigPath); exists {
|
||||
_ = os.Remove(dracutModuleConfigPath)
|
||||
}
|
||||
|
||||
logger.Printf("Writing to dracut early bind config to %s", dracutModuleConfigPath)
|
||||
dracutConfig := fmt.Sprintf(`#!/bin/bash
|
||||
check() {
|
||||
return 0
|
||||
}
|
||||
|
||||
depends() {
|
||||
return 0
|
||||
}
|
||||
|
||||
install() {
|
||||
inst_hook pre-trigger 90 "$moddir/%s"
|
||||
}`, path.Base(earlyBindScriptSysPath))
|
||||
|
||||
fileio.AppendContent(dracutConfig, dracutModuleConfigPath)
|
||||
err = os.Chmod(dracutModuleConfigPath, 0755)
|
||||
common.ErrorCheck(err, fmt.Sprintf("Error, could not chmod %s", dracutModuleConfigPath))
|
||||
}
|
||||
|
|
|
@ -17,36 +17,41 @@ import (
|
|||
)
|
||||
|
||||
type Path struct {
|
||||
CMDLINE string
|
||||
MODPROBE string
|
||||
INITRAMFS string
|
||||
ETCMODULES string
|
||||
DEFAULT string
|
||||
QEMU string
|
||||
DRACUT string
|
||||
MKINITCPIO string
|
||||
CMDLINE string
|
||||
MODPROBE string
|
||||
INITRAMFS string
|
||||
ETCMODULES string
|
||||
DEFAULT string
|
||||
QEMU string
|
||||
DRACUT string
|
||||
DRACUTMODULE string
|
||||
MKINITCPIO string
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Bootloader string
|
||||
Cpuvendor string
|
||||
Path *Path
|
||||
Gpu_Group string
|
||||
Gpu_IDs []string
|
||||
IsRoot bool
|
||||
Bootloader string
|
||||
Cpuvendor string
|
||||
Path *Path
|
||||
Gpu_Group string
|
||||
Gpu_IDs []string
|
||||
Gpu_Addresses []string
|
||||
EarlyBindFilePaths map[string]string
|
||||
IsRoot bool
|
||||
HasDuplicateDeviceIds bool
|
||||
}
|
||||
|
||||
// GetConfigPaths retrieves the path to all the config files.
|
||||
func GetConfigPaths() *Path {
|
||||
Paths := &Path{
|
||||
CMDLINE: "config/kernel_args",
|
||||
MODPROBE: "config/etc/modprobe.d",
|
||||
INITRAMFS: "config/etc/initramfs-tools",
|
||||
ETCMODULES: "config/etc/modules",
|
||||
DEFAULT: "config/etc/default",
|
||||
QEMU: "config/qemu",
|
||||
DRACUT: "config/etc/dracut.conf.d",
|
||||
MKINITCPIO: "config/etc/mkinitcpio.conf",
|
||||
CMDLINE: "config/kernel_args",
|
||||
MODPROBE: "config/etc/modprobe.d",
|
||||
INITRAMFS: "config/etc/initramfs-tools",
|
||||
ETCMODULES: "config/etc/modules",
|
||||
DEFAULT: "config/etc/default",
|
||||
QEMU: "config/qemu",
|
||||
DRACUT: "config/etc/dracut.conf.d",
|
||||
DRACUTMODULE: "config/usr/lib/dracut/modules.d/90early-vfio-bind",
|
||||
MKINITCPIO: "config/etc/mkinitcpio.conf",
|
||||
}
|
||||
|
||||
return Paths
|
||||
|
@ -55,11 +60,14 @@ func GetConfigPaths() *Path {
|
|||
// GetConfig retrieves all the configs and returns the struct.
|
||||
func GetConfig() *Config {
|
||||
config := &Config{
|
||||
Bootloader: "unknown",
|
||||
Cpuvendor: cpuid.CPU.VendorString,
|
||||
Path: GetConfigPaths(),
|
||||
Gpu_Group: "",
|
||||
Gpu_IDs: []string{},
|
||||
Bootloader: "unknown",
|
||||
Cpuvendor: cpuid.CPU.VendorString,
|
||||
Path: GetConfigPaths(),
|
||||
Gpu_Group: "",
|
||||
Gpu_IDs: []string{},
|
||||
Gpu_Addresses: []string{},
|
||||
EarlyBindFilePaths: map[string]string{},
|
||||
HasDuplicateDeviceIds: false,
|
||||
}
|
||||
|
||||
// Detect the bootloader we are using
|
||||
|
@ -78,6 +86,7 @@ func InitConfigs() {
|
|||
config.Path.INITRAMFS,
|
||||
config.Path.DEFAULT,
|
||||
config.Path.DRACUT,
|
||||
config.Path.DRACUTMODULE,
|
||||
}
|
||||
|
||||
// Remove old config
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue