change to use fileio and also finish writing the module manipulation for initramfstools
This commit is contained in:
parent
36b8e44182
commit
3ac5958015
2 changed files with 39 additions and 26 deletions
|
@ -8,9 +8,11 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
|
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
|
||||||
|
"github.com/HikariKnight/quickpassthrough/pkg/fileio"
|
||||||
"github.com/HikariKnight/quickpassthrough/pkg/uname"
|
"github.com/HikariKnight/quickpassthrough/pkg/uname"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Special function to read the header of a file (reads the first N lines)
|
||||||
func readHeader(lines int, fileName string) string {
|
func readHeader(lines int, fileName string) string {
|
||||||
// Open the file
|
// Open the file
|
||||||
f, err := os.Open(fileName)
|
f, err := os.Open(fileName)
|
||||||
|
@ -35,12 +37,14 @@ func readHeader(lines int, fileName string) string {
|
||||||
return fmt.Sprintf("%s\n", strings.Join(header, "\n"))
|
return fmt.Sprintf("%s\n", strings.Join(header, "\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reads the system file and copies over the content while inserting the vfio modules
|
||||||
|
// Takes the config file as argument
|
||||||
func addModules(conffile string) {
|
func addModules(conffile string) {
|
||||||
// Make a regex to get the system path instead of the config path
|
// Make a regex to get the system path instead of the config path
|
||||||
syspath_re := regexp.MustCompile(`^config`)
|
syspath_re := regexp.MustCompile(`^config`)
|
||||||
|
|
||||||
// Make a regex to skip specific modules and comments
|
// Make a regex to skip specific modules and comments
|
||||||
skipmodules_re := regexp.MustCompile(`(^#|vendor-reset)`)
|
skipmodules_re := regexp.MustCompile(`(^#|vendor-reset|vfio|vfio_pci|vfio_iommu_type1|vfio_virqfd)`)
|
||||||
|
|
||||||
// Get the syspath
|
// Get the syspath
|
||||||
syspath := syspath_re.ReplaceAllString(conffile, "")
|
syspath := syspath_re.ReplaceAllString(conffile, "")
|
||||||
|
@ -50,34 +54,42 @@ func addModules(conffile string) {
|
||||||
errorcheck.ErrorCheck(err, fmt.Sprintf("Error opening file for reading %s", syspath))
|
errorcheck.ErrorCheck(err, fmt.Sprintf("Error opening file for reading %s", syspath))
|
||||||
defer sysfile.Close()
|
defer sysfile.Close()
|
||||||
|
|
||||||
// Open config file for writing
|
// Check if user has vendor-reset installed/enabled and make sure that is first
|
||||||
out, err := os.OpenFile(conffile, os.O_APPEND|os.O_WRONLY, os.ModePerm)
|
content := fileio.ReadFile(syspath)
|
||||||
errorcheck.ErrorCheck(err, fmt.Sprintf("Error opening file for writing %s", conffile))
|
if strings.Contains(content, "vendor-reset") {
|
||||||
defer out.Close()
|
fileio.AppendContent("vendor-reset\n", conffile)
|
||||||
|
}
|
||||||
|
|
||||||
// Make a list of modules
|
// Write the vfio modules
|
||||||
//var modules []string
|
fileio.AppendContent(
|
||||||
|
fmt.Sprint(
|
||||||
|
"# Added by quickpassthrough #\n",
|
||||||
|
"vfio\n",
|
||||||
|
"vfio_iommu_type1\n",
|
||||||
|
"vfio_pci\n",
|
||||||
|
),
|
||||||
|
conffile,
|
||||||
|
)
|
||||||
|
|
||||||
// Scan the file line by line
|
// 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("#############################\n", conffile)
|
||||||
|
|
||||||
|
// Scan the system file line by line
|
||||||
scanner := bufio.NewScanner(sysfile)
|
scanner := bufio.NewScanner(sysfile)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
if scanner.Text() == "vendor-reset" {
|
// If this is not a line we skip then
|
||||||
out.WriteString(scanner.Text())
|
if !skipmodules_re.MatchString(scanner.Text()) {
|
||||||
} else if !skipmodules_re.MatchString(scanner.Text()) {
|
// Add the module to our config
|
||||||
writeContent(fmt.Sprintf("%s\n", scanner.Text()), conffile)
|
fileio.AppendContent(fmt.Sprintf("%s\n", scanner.Text()), conffile)
|
||||||
sysinfo := uname.New()
|
|
||||||
writeContent(fmt.Sprintf("%s\n%s\n%s\n%s\n", sysinfo.Nodename, sysinfo.Sysname, sysinfo.Domainname, sysinfo.Machine), conffile)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeContent(content string, fileName string) {
|
|
||||||
// Open the file
|
|
||||||
f, err := os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY, os.ModePerm)
|
|
||||||
errorcheck.ErrorCheck(err, fmt.Sprintf("Error opening %s", fileName))
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
// Make a new scanner
|
|
||||||
_, err = f.WriteString(content)
|
|
||||||
errorcheck.ErrorCheck(err, fmt.Sprintf("Error writing to %s", fileName))
|
|
||||||
}
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
|
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
|
||||||
|
"github.com/HikariKnight/quickpassthrough/pkg/fileio"
|
||||||
"github.com/klauspost/cpuid/v2"
|
"github.com/klauspost/cpuid/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -110,7 +111,7 @@ func InitConfigs() {
|
||||||
// If we now have a config that exists
|
// If we now have a config that exists
|
||||||
if _, err := os.Stat(conffile); !errors.Is(err, os.ErrNotExist) {
|
if _, err := os.Stat(conffile); !errors.Is(err, os.ErrNotExist) {
|
||||||
header := readHeader(4, sysfile)
|
header := readHeader(4, sysfile)
|
||||||
writeContent(header, conffile)
|
fileio.AppendContent(header, conffile)
|
||||||
addModules(conffile)
|
addModules(conffile)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue