diff --git a/internal/configs/config_bootloaders.go b/internal/configs/config_bootloaders.go index 5f038ff..60bbf1d 100644 --- a/internal/configs/config_bootloaders.go +++ b/internal/configs/config_bootloaders.go @@ -1,6 +1,9 @@ package configs import ( + "fmt" + "strings" + "github.com/HikariKnight/quickpassthrough/pkg/command" "github.com/HikariKnight/quickpassthrough/pkg/fileio" "github.com/klauspost/cpuid/v2" @@ -34,7 +37,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() { +func Set_Cmdline(gpu_IDs []string) { // Get the system info cpuinfo := cpuid.CPU @@ -52,6 +55,9 @@ func set_Cmdline() { 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 the config folder for dracut exists in our configs if fileio.FileExist(config.Path.DRACUT) { // Add an extra kernel argument needed for dracut users diff --git a/internal/configs/config_dracut.go b/internal/configs/config_dracut.go index fce039f..1c145d2 100644 --- a/internal/configs/config_dracut.go +++ b/internal/configs/config_dracut.go @@ -2,6 +2,7 @@ package configs import ( "fmt" + "strings" "github.com/HikariKnight/quickpassthrough/pkg/fileio" ) @@ -10,7 +11,7 @@ func Set_Dracut() { config := GetConfig() // Write the dracut config file - fileio.AppendContent("add_drivers+=\" vfio_pci vfio vfio_iommu_type1 vfio_virqfd \"\n", fmt.Sprintf("%s/vfio.conf", config.Path.DRACUT)) + fileio.AppendContent(fmt.Sprintf("add_drivers+=\" %s \"\n", strings.Join(vfio_modules(), " ")), fmt.Sprintf("%s/vfio.conf", config.Path.DRACUT)) // Add to our kernel arguments file that vfio_pci should load early (dracut does this using kernel arguments) fileio.AppendContent(" rd.driver.pre=vfio_pci", config.Path.CMDLINE) diff --git a/internal/configs/config_initramfstools.go b/internal/configs/config_initramfstools.go index 4d63521..f198959 100644 --- a/internal/configs/config_initramfstools.go +++ b/internal/configs/config_initramfstools.go @@ -9,7 +9,6 @@ import ( "github.com/HikariKnight/ls-iommu/pkg/errorcheck" "github.com/HikariKnight/quickpassthrough/pkg/fileio" - "github.com/HikariKnight/quickpassthrough/pkg/uname" ) // Special function to read the header of a file (reads the first N lines) @@ -64,25 +63,15 @@ func initramfs_addModules(conffile string) { fileio.AppendContent( fmt.Sprint( "# Added by quickpassthrough #\n", - "vfio\n", - "vfio_iommu_type1\n", - "vfio_pci\n", + fmt.Sprintf( + "%s\n", + strings.Join(vfio_modules(), "\n"), + ), + "#############################\n", ), conffile, ) - // If we are on a kernel older than 6.2 - sysinfo := uname.New() - kernel_re := regexp.MustCompile(`^(6\.1|6\.0|[1-5]\.)`) - if kernel_re.MatchString(sysinfo.Kernel) { - // Include the vfio_virqfd module - // NOTE: this driver was merged into the vfio module in 6.2 - fileio.AppendContent("vfio_virqfd\n", conffile) - } - - // Write the footer - fileio.AppendContent("#############################", conffile) - // Scan the system file line by line scanner := bufio.NewScanner(sysfile) for scanner.Scan() { diff --git a/internal/configs/config_modprobe.go b/internal/configs/config_modprobe.go index 76c0063..c3095b4 100644 --- a/internal/configs/config_modprobe.go +++ b/internal/configs/config_modprobe.go @@ -2,6 +2,7 @@ package configs import ( "fmt" + "os" "strings" "github.com/HikariKnight/quickpassthrough/pkg/fileio" @@ -21,6 +22,15 @@ func Set_Modprobe(gpu_IDs []string) { vfio_pci_options = append(vfio_pci_options, "disable_vga=1") } + // Put our config file path into a string + conffile := fmt.Sprintf("%s/vfio.conf", config.Path.MODPROBE) + + // If the file exists + if fileio.FileExist(conffile) { + // Delete the old file + os.Remove(conffile) + } + // Write the vfio.conf file to our modprobe config fileio.AppendContent( fmt.Sprint( @@ -39,9 +49,6 @@ func Set_Modprobe(gpu_IDs []string) { "softdep amdgpu pre: vfio vfio_pci\n", "softdep radeon pre: vfio vfio_pci\n", ), - fmt.Sprintf( - "%s/vfio.conf", - config.Path.MODPROBE, - ), + conffile, ) } diff --git a/internal/configs/configs.go b/internal/configs/configs.go index 7a76a15..15d43dd 100644 --- a/internal/configs/configs.go +++ b/internal/configs/configs.go @@ -7,6 +7,7 @@ import ( "github.com/HikariKnight/ls-iommu/pkg/errorcheck" "github.com/HikariKnight/quickpassthrough/pkg/fileio" + "github.com/HikariKnight/quickpassthrough/pkg/uname" "github.com/klauspost/cpuid/v2" ) @@ -129,7 +130,25 @@ func InitConfigs() { } } } - - // Generate the kernel arguments - set_Cmdline() +} + +func vfio_modules() []string { + // Make the list of modules + modules := []string{ + "vfio_pci", + "vfio", + "vfio_iommu_type1", + } + + // If we are on a kernel older than 6.2 + sysinfo := uname.New() + kernel_re := regexp.MustCompile(`^(6\.1|6\.0|[1-5]\.)`) + if kernel_re.MatchString(sysinfo.Kernel) { + // Include the vfio_virqfd module + // NOTE: this driver was merged into the vfio module in 6.2 + modules = append(modules, "vfio_virqfd") + } + + // Return the modules + return modules }