make generating the module list its own function and fix minor things

This commit is contained in:
HikariKnight 2023-04-09 20:40:33 +02:00
parent 8312d8853c
commit e93d215669
5 changed files with 47 additions and 25 deletions

View file

@ -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

View file

@ -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)

View file

@ -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() {

View file

@ -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,
)
}

View file

@ -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
}