quickpassthrough/internal/configs/configs.go

178 lines
3.9 KiB
Go
Raw Normal View History

2023-04-07 16:50:53 -04:00
package configs
import (
"fmt"
"os"
"regexp"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
2023-04-10 07:10:19 -04:00
"github.com/HikariKnight/quickpassthrough/internal/logger"
"github.com/HikariKnight/quickpassthrough/pkg/fileio"
"github.com/HikariKnight/quickpassthrough/pkg/uname"
"github.com/klauspost/cpuid/v2"
2023-04-07 16:50:53 -04:00
)
type Path struct {
CMDLINE string
2023-04-07 16:50:53 -04:00
MODPROBE string
INITRAMFS string
ETCMODULES string
DEFAULT string
QUICKEMU string
DRACUT string
MKINITCPIO string
}
type Config struct {
2023-04-09 11:59:10 -04:00
Bootloader string
Cpuvendor string
Path *Path
}
2023-04-07 16:50:53 -04:00
func GetConfigPaths() *Path {
Paths := &Path{
CMDLINE: "config/kernel_args",
2023-04-07 16:50:53 -04:00
MODPROBE: "config/etc/modprobe.d",
INITRAMFS: "config/etc/initramfs-tools",
ETCMODULES: "config/etc/modules",
DEFAULT: "config/etc/default",
QUICKEMU: "config/quickemu",
DRACUT: "config/etc/dracut.conf.d",
MKINITCPIO: "config/etc/mkinitcpio.conf",
}
return Paths
}
func GetConfig() *Config {
2023-04-09 11:59:10 -04:00
config := &Config{
Bootloader: "unknown",
Cpuvendor: cpuid.CPU.VendorString,
Path: GetConfigPaths(),
}
// Detect the bootloader we are using
getBootloader(config)
return config
}
2023-04-07 16:50:53 -04:00
func InitConfigs() {
config := GetConfig()
2023-04-07 16:50:53 -04:00
2023-04-08 21:21:25 -04:00
// Add all directories we need into a stringlist
2023-04-07 16:50:53 -04:00
dirs := []string{
2023-04-09 11:59:10 -04:00
config.Path.MODPROBE,
config.Path.INITRAMFS,
config.Path.DEFAULT,
config.Path.DRACUT,
2023-04-07 16:50:53 -04:00
}
// Remove old config
os.RemoveAll("config")
// Make the config folder
os.Mkdir("config", os.ModePerm)
2023-04-07 16:50:53 -04:00
// Make a regex to get the system path instead of the config path
syspath_re := regexp.MustCompile(`^config`)
// For each directory
for _, confpath := range dirs {
// Get the system path
syspath := syspath_re.ReplaceAllString(confpath, "")
// If the path exists
if fileio.FileExist(syspath) {
2023-04-10 07:10:19 -04:00
// Write to log
2023-04-10 08:26:50 -04:00
logger.Printf(
"%s found on the system\n"+
"Creating %s",
syspath,
confpath,
)
2023-04-10 07:10:19 -04:00
2023-04-07 16:50:53 -04:00
// Create the directories for our configs
err := os.MkdirAll(confpath, os.ModePerm)
errorcheck.ErrorCheck(err)
}
}
2023-04-08 21:21:25 -04:00
// Add all files we need to a stringlist
2023-04-07 16:50:53 -04:00
files := []string{
2023-04-09 11:59:10 -04:00
config.Path.ETCMODULES,
config.Path.MKINITCPIO,
fmt.Sprintf("%s/modules", config.Path.INITRAMFS),
fmt.Sprintf("%s/grub", config.Path.DEFAULT),
2023-04-07 16:50:53 -04:00
}
for _, conffile := range files {
// Get the system file path
sysfile := syspath_re.ReplaceAllString(conffile, "")
// If the file exists
if fileio.FileExist(sysfile) {
2023-04-10 07:10:19 -04:00
// Write to log
2023-04-10 08:26:50 -04:00
logger.Printf(
"%s found on the system\n"+
"Creating %s",
sysfile,
conffile,
)
2023-04-10 07:10:19 -04:00
2023-04-07 16:50:53 -04:00
// Create the directories for our configs
file, err := os.Create(conffile)
errorcheck.ErrorCheck(err)
2023-04-08 21:21:25 -04:00
// Close the file so we can edit it
file.Close()
}
// If we now have a config that exists
if fileio.FileExist(conffile) {
2023-04-10 08:26:50 -04:00
// Write to logger
logger.Printf("Getting the header (if it is there) from %s", conffile)
switch conffile {
2023-04-09 11:59:10 -04:00
case config.Path.ETCMODULES:
// Read the header
header := initramfs_readHeader(4, sysfile)
fileio.AppendContent(header, conffile)
// Add the modules to the config file
initramfs_addModules(conffile)
2023-04-09 11:59:10 -04:00
case fmt.Sprintf("%s/modules", config.Path.INITRAMFS):
// Read the header
header := initramfs_readHeader(11, sysfile)
fileio.AppendContent(header, conffile)
// Add the modules to the config file
initramfs_addModules(conffile)
}
2023-04-07 16:50:53 -04:00
}
}
}
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) {
2023-04-10 08:26:50 -04:00
// Write to the debug log
logger.Printf("Linux kernel version %s detected!\nIncluding vfio_virqfd module")
// 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
2023-04-07 16:50:53 -04:00
}