quickpassthrough/internal/configs/config_modprobe.go

67 lines
2 KiB
Go
Raw Normal View History

package configs
2023-04-09 12:16:21 -04:00
2023-04-09 13:05:49 -04:00
import (
"fmt"
"os"
2023-04-09 13:05:49 -04:00
"strings"
2023-04-09 12:16:21 -04:00
2023-04-10 08:26:50 -04:00
"github.com/HikariKnight/quickpassthrough/internal/logger"
2023-04-09 13:05:49 -04:00
"github.com/HikariKnight/quickpassthrough/pkg/fileio"
)
2023-04-10 08:36:57 -04:00
// This function generates a modprobe file for /etc/modprobe.d/
2023-04-09 13:05:49 -04:00
func Set_Modprobe(gpu_IDs []string) {
// Get the config
config := GetConfig()
// Read our current kernel arguments
kernel_args := fileio.ReadFile(config.Path.CMDLINE)
var vfio_pci_options []string
vfio_pci_options = append(vfio_pci_options, strings.Join(gpu_IDs, ","))
if strings.Contains(kernel_args, "vfio_pci.disable_vga=1") {
2023-04-10 08:26:50 -04:00
// Write to logger
logger.Printf("User has disabled vfio video output on host, adding disable_vga=1 to the optional hardcoded vfio_pci options")
2023-04-09 13:05:49 -04:00
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)
}
2023-04-10 08:26:50 -04:00
content := fmt.Sprint(
"## This is an autogenerated file that stubs your graphic card for use with vfio\n",
"## This file should be placed inside /etc/modprobe.d/\n",
"# Uncomment the line below to \"hardcode\" your graphic card to be bound to the vfio-pci driver.\n",
"# In most cases this should not be neccessary, it will also prevent you from turning off vfio in the bootloader.\n",
fmt.Sprintf(
"#options vfio_pci ids=%s\n",
strings.Join(vfio_pci_options, " "),
),
"\n",
"# Make sure vfio_pci is loaded before these modules: nvidia, nouveau, amdgpu and radeon\n",
"softdep nvidia pre: vfio vfio_pci\n",
"softdep nouveau pre: vfio vfio_pci\n",
"softdep amdgpu pre: vfio vfio_pci\n",
"softdep radeon pre: vfio vfio_pci\n",
)
// Write to logger
logger.Printf("Writing %s:\n%s", conffile, content)
2023-04-09 13:05:49 -04:00
// Write the vfio.conf file to our modprobe config
fileio.AppendContent(
2023-04-10 08:26:50 -04:00
content,
conffile,
2023-04-09 13:05:49 -04:00
)
2023-04-10 12:33:48 -04:00
// Make a backup of dracutConf if there is one there
backupFile(strings.Replace(conffile, "config", "", 1))
2023-04-09 12:16:21 -04:00
}